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 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   * Tests for <a href="https://htmx.org/">htmx</a>.
34   *
35   * @author Ronald Brill
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         // if the elem is not available or stale we return an empty string
110         // this will force a second try
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 }