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.ArrayList;
19  import java.util.Arrays;
20  import java.util.List;
21  
22  import org.eclipse.jetty.server.Server;
23  import org.htmlunit.WebDriverTestCase;
24  import org.htmlunit.WebServerTestCase;
25  import org.htmlunit.junit.annotation.Alerts;
26  import org.junit.jupiter.api.AfterAll;
27  import org.junit.jupiter.api.BeforeAll;
28  import org.junit.jupiter.api.Test;
29  import org.openqa.selenium.By;
30  import org.openqa.selenium.WebDriver;
31  import org.openqa.selenium.WebElement;
32  
33  /**
34   * Tests for compatibility with version 1.2.1 of the <a href="http://mootools.net/">MooTools JavaScript library</a>.
35   *
36   * @author Daniel Gredler
37   * @author Marc Guillemot
38   * @author Frank Danek
39   * @author Ronald Brill
40   */
41  public class MooTools121Test extends WebDriverTestCase {
42  
43      private static Server SERVER_;
44  
45      /**
46       * @throws Exception if an error occurs
47       */
48      @BeforeAll
49      public static void startSesrver() throws Exception {
50          SERVER_ = WebServerTestCase.createWebServer("src/test/resources/libraries/mootools/1.2.1", null);
51      }
52  
53      /**
54       * @throws Exception if an error occurs
55       */
56      @AfterAll
57      public static void stopServer() throws Exception {
58          if (SERVER_ != null) {
59              SERVER_.stop();
60              SERVER_.destroy();
61              SERVER_ = null;
62          }
63      }
64  
65      /**
66       * @throws Exception if an error occurs
67       */
68      @Alerts({"364", "1", "0",
69               "should return the function bound to an object with multiple arguments"})
70      @Test
71      public void mooTools() throws Exception {
72          final WebDriver driver = getWebDriver();
73          driver.get(URL_FIRST + "Specs/index.html");
74  
75          driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(60));
76          driver.findElement(By.xpath("id('progress')[text() = '100']"));
77          // usually this need 40s but sometimes our build machine is slower
78          // this is not a performance test, we only like to ensure that all
79          // functionality is running
80  
81          final List<WebElement> failed = driver.findElements(By.xpath("//li[@class = 'exception']/h4"));
82          final List<String> failures = new ArrayList<>();
83          for (final WebElement elt : failed) {
84              failures.add(elt.getText());
85          }
86  
87          // final File tmpFile = File.createTempFile("htmlunit", "mootools.html");
88          // System.out.println(tmpFile.getAbsolutePath());
89          // FileUtils.writeStringToFile(tmpFile, driver.getPageSource());
90  
91          assertEquals(Arrays.copyOfRange(getExpectedAlerts(), 3, getExpectedAlerts().length), failures);
92  
93          assertEquals(getExpectedAlerts()[0], driver.findElement(By.id("total_examples")).getText());
94          assertEquals(getExpectedAlerts()[1], driver.findElement(By.id("total_failures")).getText());
95          assertEquals(getExpectedAlerts()[2], driver.findElement(By.id("total_errors")).getText());
96      }
97  }