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