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  
19  import org.apache.commons.lang3.StringUtils;
20  import org.htmlunit.WebDriverTestCase;
21  import org.htmlunit.junit.BrowserRunner;
22  import org.junit.Before;
23  import org.junit.Test;
24  import org.junit.runner.RunWith;
25  import org.openqa.selenium.By;
26  import org.openqa.selenium.WebDriver;
27  import org.openqa.selenium.WebElement;
28  
29  /**
30   * Tests for compatibility with <a href="http://mochikit.com">MochiKit</a>.
31   *
32   * @author Marc Guillemot
33   * @author Frank Danek
34   * @author Ronald Brill#
35   */
36  @RunWith(BrowserRunner.class)
37  public abstract class MochiKitTest extends WebDriverTestCase {
38  
39      /**
40       * @return the src folder containing mochikit sources
41       */
42      public abstract String srcFolder();
43  
44      /**
45       * @throws Exception if the test fails
46       */
47      @Test
48      public void async() throws Exception {
49          doTest("Async");
50      }
51  
52      /**
53       * @throws Exception if the test fails
54       */
55      @Test
56      public void base() throws Exception {
57          doTest("Base");
58      }
59  
60      /**
61       * @throws Exception if the test fails
62       */
63      @Test
64      public void color() throws Exception {
65          doTest("Color");
66      }
67  
68      /**
69       * @throws Exception if the test fails
70       */
71      @Test
72      public void dateTime() throws Exception {
73          doTest("DateTime");
74      }
75  
76      /**
77       * @throws Exception if the test fails
78       */
79      @Test
80      public void dom() throws Exception {
81          doTest("DOM");
82      }
83  
84      /**
85       * @throws Exception if the test fails
86       */
87      @Test
88      public void domSafari() throws Exception {
89          doTest("DOM-Safari");
90      }
91  
92      // /**
93      //  * @throws Exception if the test fails
94      //  */
95      // have to investigate why this fails in HtmlUnit
96      // @Test
97      // public void dragAndDrop() throws Exception {
98      //     doTest("DragAndDrop");
99      // }
100 
101     /**
102      * @throws Exception if the test fails
103      */
104     @Test
105     public void format() throws Exception {
106         doTest("Format");
107     }
108 
109     /**
110      * @throws Exception if the test fails
111      */
112     @Test
113     public void iter() throws Exception {
114         doTest("Iter");
115     }
116 
117     /**
118      * @throws Exception if the test fails
119      */
120     @Test
121     public void logging() throws Exception {
122         doTest("Logging");
123     }
124 
125     /**
126      * @throws Exception if the test fails
127      */
128     @Test
129     public void mochiKit() throws Exception {
130         doTest("MochiKit");
131     }
132 
133     /**
134      * @throws Exception if the test fails
135      */
136     @Test
137     public void selector() throws Exception {
138         doTest("Selector");
139     }
140 
141     /**
142      * @throws Exception if the test fails
143      */
144     @Test
145     public void signal() throws Exception {
146         doTest("Signal");
147     }
148 
149     /**
150      * @throws Exception if the test fails
151      */
152     @Test
153     public void style() throws Exception {
154         doTest("Style");
155     }
156 
157     /**
158      * @throws Exception if the test fails
159      */
160     @Test
161     public void visual() throws Exception {
162         doTest("Visual");
163     }
164 
165     private void doTest(final String testName) throws Exception {
166         final String url = URL_FIRST + "tests/test_MochiKit-" + testName + ".html";
167         assertNotNull(url);
168 
169         final WebDriver driver = getWebDriver();
170         driver.get(url);
171 
172         // make single test results visible
173         driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
174         driver.findElement(By.linkText("Toggle passed tests")).click();
175         driver.findElement(By.linkText("Toggle failed tests")).click();
176         driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(0));
177 
178         String expected = loadExpectation(testName);
179         expected = expected.trim();
180         expected = StringUtils.replace(expected, "\r\n", "\n");
181         final WebElement div = driver.findElement(By.xpath("//div[@class = 'tests_report']"));
182 
183         assertNotNull(div);
184         String actual = div.getText().trim();
185         actual = StringUtils.replace(actual, "\n\n", "\n");
186         assertEquals(expected.trim(), actual);
187     }
188 
189     private String loadExpectation(final String testName) throws Exception {
190         final String resourcePrefix = "/libraries/MochiKit/" + srcFolder() + "/test-" + testName;
191         return loadExpectation(resourcePrefix, ".expected.txt");
192     }
193 
194     /**
195      * Performs pre-test initialization.
196      * @throws Exception if an error occurs
197      */
198     @Before
199     public void setUp() throws Exception {
200         startWebServer("src/test/resources/libraries/MochiKit/" + srcFolder(), null, null);
201     }
202 }