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