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.NotYetImplemented;
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.StaleElementReferenceException;
36  import org.openqa.selenium.WebDriver;
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     @NotYetImplemented
157     public void editor() throws Exception {
158         doTest("editor.html", Arrays.asList(getExpectedAlerts()));
159     }
160 
161     /**
162      * @throws Exception if an error occurs
163      */
164     @Test
165     @NotYetImplemented
166     public void yuiLoaderRollup() throws Exception {
167         doTest("yuiloader_rollup.html");
168     }
169 
170     /**
171      * @throws Exception if an error occurs
172      */
173     @Test
174     @Alerts("test_page_modules")
175     public void yuiLoaderConfig() throws Exception {
176         // The "test_page_modules" test fails in FF, too, so it's OK.
177         doTest("yuiloader_config.html", Arrays.asList(getExpectedAlerts()));
178     }
179 
180     /**
181      * @throws Exception if an error occurs
182      */
183     @Test
184     public void yuiLoader() throws Exception {
185         doTest("yuiloader.html");
186     }
187 
188     /**
189      * @throws Exception if an error occurs
190      */
191     @Test
192     @Alerts("testConstructor")
193     public void module() throws Exception {
194         doTest("module.html", Arrays.asList(getExpectedAlerts()));
195     }
196 
197     /**
198      * @throws Exception if an error occurs
199      */
200     @Test
201     public void imageLoader() throws Exception {
202         doTest("imageloader.html");
203     }
204 
205     /**
206      * @throws Exception if an error occurs
207      */
208     @Test
209     public void element() throws Exception {
210         doTest("element.html");
211     }
212 
213     private void doTest(final String fileName, final String buttonToClick) throws Exception {
214         doTest(fileName, Collections.<String>emptyList(), buttonToClick, 0);
215     }
216 
217     private void doTest(final String fileName) throws Exception {
218         doTest(fileName, Collections.<String>emptyList(), null, 0);
219     }
220 
221     private void doTest(final String fileName, final List<String> knownFailingTests) throws Exception {
222         doTest(fileName, knownFailingTests, null, 0);
223     }
224 
225     private void doTest(final String fileName, final List<String> knownFailingTests,
226             final String buttonToPush, final long timeToWait) throws Exception {
227 
228         // final URL url = getClass().getClassLoader().getResource("tests/" + fileName);
229         final String url = URL_FIRST + "tests/" + fileName;
230         assertNotNull(url);
231 
232         final WebDriver driver = getWebDriver();
233         driver.get(url);
234 
235         if (buttonToPush != null) {
236             driver.findElement(By.id(buttonToPush)).click();
237         }
238 
239         driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
240         final WebElement logDiv = driver.findElement(By.className("yui-log-bd"));
241         final WebElement lastMessage = logDiv.findElement(
242             By.xpath("pre[last() and contains(string(.), 'Testing completed')]"));
243 
244         LOG.info(lastMessage.getText());
245 
246         driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(0));
247         final List<WebElement> tests = driver.findElements(By.xpath("//p[span[@class='pass' or @class='fail']]"));
248         if (tests.isEmpty()) {
249             fail("No tests were executed!");
250         }
251 
252         for (final WebElement pre : tests) {
253             final String[] parts;
254             try {
255                 parts = pre.getText().split(" ");
256             }
257             catch (final StaleElementReferenceException e) {
258                 continue; // happens for FF17 on imageLoader test
259             }
260             final String result = parts[0];
261             final String testName = parts[1].substring(0, parts[1].length() - 1);
262             if ("pass".equalsIgnoreCase(result)) {
263                 assertTrue("Test case '" + testName + "' is in the known failing list, but passes!", !knownFailingTests
264                                 .contains(testName));
265             }
266             else {
267                 assertTrue("Test case '" + testName + "' is not in the known failing list, but fails!",
268                                 knownFailingTests.contains(testName));
269             }
270         }
271     }
272 
273     /**
274      * Performs pre-test initialization.
275      * @throws Exception if an error occurs
276      */
277     @Before
278     public void setUp() throws Exception {
279         startWebServer("src/test/resources/libraries/yui/2.3.0", null, null);
280     }
281 }