1
2
3
4
5
6
7
8
9
10
11
12
13
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
38
39
40
41
42
43
44 public abstract class PrototypeTestBase extends WebDriverTestCase {
45
46 private static final Log LOG = LogFactory.getLog(PrototypeTestBase.class);
47
48 protected static Server SERVER_;
49
50
51
52
53
54 protected abstract String getVersion();
55
56
57
58
59
60
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
74
75
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
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
109 }
110
111
112 expected = expected.replaceAll("Info:.*", "Info: -- skipped for comparison --");
113 actual = actual.replaceAll("Info:.*", "Info: -- skipped for comparison --");
114
115
116 expected = expected.replaceAll("\r\n", "\n");
117 actual = actual.replaceAll("\r\n", "\n");
118
119
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
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
144
145 protected String getBaseUrl() {
146 return URL_FIRST.toExternalForm();
147 }
148 }