View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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.time.Duration;
18  import java.util.Arrays;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.htmlunit.WebDriverTestCase;
25  import org.htmlunit.junit.annotation.Alerts;
26  import org.htmlunit.junit.annotation.HtmlUnitNYI;
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.BeforeAll;
29  import org.junit.jupiter.api.Disabled;
30  import org.junit.jupiter.api.Test;
31  import org.openqa.selenium.By;
32  import org.openqa.selenium.WebDriver;
33  import org.openqa.selenium.WebDriverException;
34  import org.openqa.selenium.WebElement;
35  
36  /**
37   * Tests for compatibility with the <a href="http://developer.yahoo.com/yui/">YUI JavaScript library</a>.
38   *
39   * @author Rob Di Marco
40   * @author Daniel Gredler
41   * @author Marc Guillemot
42   * @author Frank Danek
43   * @author Ronald Brill
44   */
45  public class YuiTest extends WebDriverTestCase {
46      private static final Log LOG = LogFactory.getLog(YuiTest.class);
47  
48      /**
49       * @throws Exception if an error occurs
50       */
51      @BeforeAll
52      public static void startServer() throws Exception {
53          startWebServer("src/test/resources/libraries/yui/2.3.0", null);
54      }
55  
56      /**
57       * @throws Exception if an error occurs
58       */
59      @Test
60      public void logger() throws Exception {
61          doTest("logger.html");
62      }
63  
64      /**
65       * @throws Exception if an error occurs
66       */
67      @Test
68      public void animation() throws Exception {
69          doTest("animation.html");
70      }
71  
72      /**
73       * @throws Exception if an error occurs
74       */
75      @Test
76      public void tabView() throws Exception {
77          doTest("tabview.html");
78      }
79  
80      /**
81       * @throws Exception if an error occurs
82       */
83      @Test
84      public void dateMath() throws Exception {
85          doTest("datemath.html", "btnRun");
86      }
87  
88      /**
89       * @throws Exception if an error occurs
90       */
91      @Test
92      public void calendar() throws Exception {
93          doTest("calendar.html", "btnRun");
94      }
95  
96      /**
97       * @throws Exception if an error occurs
98       */
99      @Test
100     public void colorPicker() throws Exception {
101         doTest("colorpicker.html");
102     }
103 
104     /**
105      * @throws Exception if an error occurs
106      */
107     @Test
108     @Disabled
109     public void config() throws Exception {
110         // Test currently commented out as there are problems with the YUI test.
111         // A bug has been filed against YUI regarding the problems with the test.
112         Assertions.fail("YUI test has a bug that causes this to fail.");
113         //doTest("config.html");
114     }
115 
116     /**
117      * @throws Exception if an error occurs
118      */
119     @Test
120     public void dataSource() throws Exception {
121         doTest("datasource.html", "btnRun");
122     }
123 
124     /**
125      * @throws Exception if an error occurs
126      */
127     @Test
128     public void dom() throws Exception {
129         doTest("dom.html", Arrays.asList(getExpectedAlerts()));
130     }
131 
132     /**
133      * @throws Exception if an error occurs
134      */
135     @Test
136     public void dragDrop() throws Exception {
137         doTest("dragdrop.html", Arrays.asList(getExpectedAlerts()));
138     }
139 
140     /**
141      * @throws Exception if an error occurs
142      */
143     @Test
144     public void dataTable() throws Exception {
145         doTest("datatable.html", "btnRun");
146     }
147 
148     /**
149      * @throws Exception if an error occurs
150      */
151     @Test
152     @Alerts(CHROME = {"test_blank_image", "test_insertimage", "test_image_props",
153                       "test_close_window", "test_regex",
154                       "test_createlink", "test_selected_element", "test_dom_path"},
155             EDGE = {"test_blank_image", "test_insertimage", "test_image_props",
156                     "test_close_window", "test_regex",
157                     "test_createlink", "test_selected_element", "test_dom_path"},
158             FF = "test_createlink",
159             FF_ESR = "test_createlink")
160     @HtmlUnitNYI(CHROME = {"test_blank_image", "test_insertimage", "test_image_props",
161                            "test_close_window", "test_bold", "test_selected_element",
162                            "test_dom_path", "test_createlink"},
163             EDGE = {"test_blank_image", "test_insertimage", "test_image_props",
164                     "test_close_window", "test_bold", "test_selected_element",
165                     "test_dom_path", "test_createlink"},
166             FF = {"test_blank_image", "test_insertimage", "test_image_props",
167                   "test_bold", "test_createlink", "test_hidden_elements"},
168             FF_ESR = {"test_blank_image", "test_insertimage", "test_image_props",
169                       "test_bold", "test_createlink", "test_hidden_elements"})
170     public void editor() throws Exception {
171         doTest("editor.html", Arrays.asList(getExpectedAlerts()));
172     }
173 
174     /**
175      * @throws Exception if an error occurs
176      */
177     @Test
178     @Alerts("")
179     @HtmlUnitNYI(CHROME = "org.htmlunit.ScriptException: TypeError: Cannot call method \"appendChild\" of null",
180             EDGE = "org.htmlunit.ScriptException: TypeError: Cannot call method \"appendChild\" of null",
181             FF = "org.htmlunit.ScriptException: TypeError: Cannot call method \"appendChild\" of null",
182             FF_ESR = "org.htmlunit.ScriptException: TypeError: Cannot call method \"appendChild\" of null")
183     public void yuiLoaderRollup() throws Exception {
184         doTest("yuiloader_rollup.html");
185     }
186 
187     /**
188      * @throws Exception if an error occurs
189      */
190     @Test
191     @Alerts("test_page_modules")
192     public void yuiLoaderConfig() throws Exception {
193         // The "test_page_modules" test fails in FF, too, so it's OK.
194         doTest("yuiloader_config.html", Arrays.asList(getExpectedAlerts()));
195     }
196 
197     /**
198      * @throws Exception if an error occurs
199      */
200     @Test
201     public void yuiLoader() throws Exception {
202         doTest("yuiloader.html");
203     }
204 
205     /**
206      * @throws Exception if an error occurs
207      */
208     @Test
209     @Alerts("testConstructor")
210     public void module() throws Exception {
211         doTest("module.html", Arrays.asList(getExpectedAlerts()));
212     }
213 
214     /**
215      * @throws Exception if an error occurs
216      */
217     @Test
218     public void imageLoader() throws Exception {
219         doTest("imageloader.html");
220     }
221 
222     /**
223      * @throws Exception if an error occurs
224      */
225     @Test
226     public void element() throws Exception {
227         doTest("element.html");
228     }
229 
230     private void doTest(final String fileName, final String buttonToClick) throws Exception {
231         doTest(fileName, Collections.emptyList(), buttonToClick, 0);
232     }
233 
234     private void doTest(final String fileName) throws Exception {
235         doTest(fileName, Collections.emptyList(), null, 0);
236     }
237 
238     private void doTest(final String fileName, final List<String> knownFailingTests) throws Exception {
239         doTest(fileName, knownFailingTests, null, 0);
240     }
241 
242     private void doTest(final String fileName, final List<String> knownFailingTests,
243             final String buttonToPush, final long timeToWait) throws Exception {
244 
245         // final URL url = getClass().getClassLoader().getResource("tests/" + fileName);
246         final String url = URL_FIRST + "tests/" + fileName;
247         assertNotNull(url);
248 
249         final WebDriver driver = getWebDriver();
250         try {
251             driver.get(url);
252         }
253         catch (final WebDriverException e) {
254             assertTrue(e.getMessage(), e.getMessage().startsWith(getExpectedAlerts()[0]));
255             return;
256         }
257 
258         if (buttonToPush != null) {
259             driver.findElement(By.id(buttonToPush)).click();
260         }
261 
262         driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
263         final WebElement logDiv = driver.findElement(By.className("yui-log-bd"));
264         final WebElement lastMessage = logDiv.findElement(
265             By.xpath("pre[last() and contains(string(.), 'Testing completed')]"));
266 
267         LOG.info(lastMessage.getText());
268 
269         driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(0));
270         final List<WebElement> tests = driver.findElements(By.xpath("//p[span[@class='pass' or @class='fail']]"));
271         if (tests.isEmpty()) {
272             Assertions.fail("No tests were executed!");
273         }
274 
275         for (final WebElement pre : tests) {
276             final String[] parts = pre.getText().split(" ");
277             final String result = parts[0];
278             final String testName = parts[1].substring(0, parts[1].length() - 1);
279             if ("pass".equalsIgnoreCase(result)) {
280                 assertTrue("Test case '" + testName + "' is in the known failing list, but passes!", !knownFailingTests
281                                 .contains(testName));
282             }
283             else {
284                 assertTrue("Test case '" + testName + "' is not in the known failing list, but fails!",
285                                 knownFailingTests.contains(testName));
286             }
287         }
288     }
289 }