1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.libraries;
16
17 import java.util.List;
18
19 import org.htmlunit.Page;
20 import org.htmlunit.WebClient;
21 import org.htmlunit.WebDriverTestCase;
22 import org.htmlunit.html.HtmlPage;
23 import org.htmlunit.junit.BrowserRunner;
24 import org.junit.runner.RunWith;
25 import org.openqa.selenium.By;
26 import org.openqa.selenium.NoSuchElementException;
27 import org.openqa.selenium.StaleElementReferenceException;
28 import org.openqa.selenium.WebDriver;
29 import org.openqa.selenium.WebElement;
30 import org.openqa.selenium.htmlunit.HtmlUnitDriver;
31
32
33
34
35
36
37 @RunWith(BrowserRunner.class)
38 public abstract class HtmxTest extends WebDriverTestCase {
39
40 private static final boolean BUG_HUNTING = false;
41
42 private static final int RETRIES = 2;
43 private static final long RUN_TIME = 42 * DEFAULT_WAIT_TIME.toMillis();
44
45 protected void htmx(final String subDir) throws Exception {
46 startWebServer("src/test/resources/libraries/htmx/" + subDir, null, null);
47
48 try {
49 final String url = URL_FIRST + "test/index.html";
50 final WebDriver webDriver = getWebDriver();
51
52 if (webDriver instanceof HtmlUnitDriver) {
53 setupWebClient(((HtmlUnitDriver) webDriver).getWebClient());
54 }
55
56 int tries = 0;
57 String lastStats = "";
58 webDriver.get(url);
59 long endTime = System.currentTimeMillis() + RUN_TIME;
60
61 while (true) {
62 lastStats = getResultElementText(webDriver);
63 if (lastStats.startsWith(getExpectedAlerts()[0])) {
64 break;
65 }
66 Thread.sleep(100);
67
68 if (System.currentTimeMillis() > endTime) {
69 tries++;
70
71 if (tries < RETRIES) {
72 lastStats = "";
73 webDriver.get(url);
74 endTime = System.currentTimeMillis() + RUN_TIME;
75 }
76 else {
77 lastStats = "HtmxTest runs too long (longer than " + RUN_TIME / 1000 + "s) - "
78 + getResultElementText(webDriver);
79 break;
80 }
81 }
82 }
83
84 if (BUG_HUNTING && getWebDriver() instanceof HtmlUnitDriver) {
85 final WebClient webClient = ((HtmlUnitDriver) getWebDriver()).getWebClient();
86
87 final Page page = webClient.getCurrentWindow().getEnclosedPage();
88 System.out.println(((HtmlPage) page).asNormalizedText());
89 }
90
91 assertTrue(lastStats + "\n\n" + getErrors(webDriver), lastStats.startsWith(getExpectedAlerts()[0]));
92 }
93 catch (final Exception e) {
94 if (BUG_HUNTING && getWebDriver() instanceof HtmlUnitDriver) {
95 e.printStackTrace();
96 Throwable t = e;
97 while ((t = t.getCause()) != null) {
98 t.printStackTrace();
99 }
100 }
101 throw e;
102 }
103 }
104
105 protected void setupWebClient(final WebClient webClient) {
106 }
107
108 private static String getResultElementText(final WebDriver webdriver) {
109
110
111 try {
112 final WebElement elem = webdriver.findElement(By.cssSelector("#mocha-stats"));
113 try {
114 String result = elem.getText();
115 result = result.replaceAll("\\s", "");
116 return result;
117 }
118 catch (final StaleElementReferenceException e) {
119 return "";
120 }
121 }
122 catch (final NoSuchElementException e) {
123 return "";
124 }
125 }
126
127 private static String getErrors(final WebDriver webdriver) {
128 final StringBuilder result = new StringBuilder();
129
130 try {
131 final List<WebElement> elements = webdriver.findElements(By.tagName("li"));
132 for (final WebElement elem : elements) {
133 final String cssClass = elem.getAttribute("class");
134 if (cssClass != null && cssClass.contains(" fail")) {
135 result.append(elem.getText())
136 .append("\n-------------------------\n\n");
137 }
138 }
139 return result.toString();
140 }
141 catch (final Exception e) {
142 return e.getMessage();
143 }
144 }
145
146 }