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