View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.libraries;
16  
17  import static java.nio.charset.StandardCharsets.UTF_8;
18  import static org.junit.Assert.fail;
19  
20  import java.io.File;
21  import java.util.List;
22  
23  import org.apache.commons.io.FileUtils;
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.eclipse.jetty.server.Server;
28  import org.htmlunit.WebDriverTestCase;
29  import org.junit.AfterClass;
30  import org.openqa.selenium.By;
31  import org.openqa.selenium.NoSuchElementException;
32  import org.openqa.selenium.WebDriver;
33  import org.openqa.selenium.WebElement;
34  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
35  
36  /**
37   * Base class for tests for compatibility with <a href="http://prototype.conio.net/">Prototype</a>.
38   *
39   * @author Daniel Gredler
40   * @author Ahmed Ashour
41   * @author Marc Guillemot
42   * @author Ronald Brill
43   */
44  public abstract class PrototypeTestBase extends WebDriverTestCase {
45  
46      private static final Log LOG = LogFactory.getLog(PrototypeTestBase.class);
47      /** The server. */
48      protected static Server SERVER_;
49  
50      /**
51       * Gets the prototype tested version.
52       * @return the version
53       */
54      protected abstract String getVersion();
55  
56      /**
57       * Helper, because the element was different for the
58       * different versions.
59       * @param driver the WebDriver
60       * @return the WebElement
61       */
62      protected boolean testFinished(final WebDriver driver) {
63          final List<WebElement> status = driver.findElements(By.cssSelector("div.logsummary"));
64          for (final WebElement webElement : status) {
65              if (!webElement.getText().contains("errors")) {
66                  return false;
67              }
68          }
69          return true;
70      }
71  
72      /**
73       * Runs the specified test.
74       * @param filename the test file to run
75       * @throws Exception if the test fails
76       */
77      protected void test(final String filename) throws Exception {
78          final WebDriver driver = getWebDriver();
79          if (!(driver instanceof HtmlUnitDriver)) {
80              resizeIfNeeded(driver);
81          }
82          driver.get(getBaseUrl() + filename);
83  
84          // wait
85          final long runTime = 60 * DEFAULT_WAIT_TIME.toMillis();
86          final long endTime = System.currentTimeMillis() + runTime;
87  
88          while (!testFinished(driver)) {
89              Thread.sleep(100);
90  
91              if (System.currentTimeMillis() > endTime) {
92                  fail("Test '" + filename + "' runs too long (longer than " + runTime / 1000 + "s)");
93              }
94          }
95  
96          final String expFileName = StringUtils.substringBeforeLast(filename, ".");
97          final String resourcePrefix = "/libraries/prototype/" + getVersion() + "/expected." + expFileName;
98          String expected = loadExpectation(resourcePrefix, ".txt");
99  
100         WebElement testlog = driver.findElement(By.id("testlog"));
101         String actual = testlog.getText();
102 
103         try {
104             testlog = driver.findElement(By.id("testlog_2"));
105             actual = actual + "\n" + testlog.getText();
106         }
107         catch (final NoSuchElementException e) {
108             // ignore
109         }
110 
111         // ignore Info lines
112         expected = expected.replaceAll("Info:.*", "Info: -- skipped for comparison --");
113         actual = actual.replaceAll("Info:.*", "Info: -- skipped for comparison --");
114 
115         // normalize line break
116         expected = expected.replaceAll("\r\n", "\n");
117         actual = actual.replaceAll("\r\n", "\n");
118 
119         // dump the result page if not ok
120         if (!expected.equals(actual)) {
121             final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
122             final File f = new File(tmpDir, "prototype" + getVersion() + "_result_" + filename);
123             FileUtils.writeStringToFile(f, driver.getPageSource(), UTF_8);
124             LOG.info("Test result for " + filename + " written to: " + f.getAbsolutePath());
125         }
126 
127         assertEquals(expected, actual);
128     }
129 
130     /**
131      * @throws Exception if an error occurs
132      */
133     @AfterClass
134     public static void stopServer() throws Exception {
135         if (SERVER_ != null) {
136             SERVER_.stop();
137             SERVER_.destroy();
138             SERVER_ = null;
139         }
140     }
141 
142     /**
143      * @return the resource base URL
144      */
145     protected String getBaseUrl() {
146         return URL_FIRST.toExternalForm();
147     }
148 }