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