View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.example.demo.qunit;
21  
22  
23  import org.junit.jupiter.api.Assertions;
24  import org.junit.jupiter.params.ParameterizedTest;
25  import org.junit.jupiter.params.provider.Arguments;
26  import org.junit.jupiter.params.provider.MethodSource;
27  import org.openqa.selenium.By;
28  import org.openqa.selenium.WebDriver;
29  import org.openqa.selenium.WebElement;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  import java.io.IOException;
34  import java.io.UnsupportedEncodingException;
35  import java.lang.invoke.MethodHandles;
36  import java.net.MalformedURLException;
37  import java.nio.file.Files;
38  import java.nio.file.Path;
39  import java.nio.file.Paths;
40  import java.time.LocalTime;
41  import java.util.LinkedList;
42  import java.util.List;
43  import java.util.stream.Collectors;
44  import java.util.stream.Stream;
45  
46  class AccessAllPagesTest extends SeleniumBase {
47  
48    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
49  
50    /**
51     * Verify qunit test for "no exception".
52     */
53    @ParameterizedTest
54    @MethodSource("verifyNoException404TestProvider")
55    void verifyNoExceptionTest(Browser browser, String serverUrl)
56        throws MalformedURLException, UnsupportedEncodingException {
57      final String path = "error/exception.xhtml";
58      LOG.info("browser: " + browser + " - url: " + serverUrl + "/" + path);
59  
60      if (isIgnored(serverUrl, path)) {
61        logIgnoreMessage(serverUrl, path);
62      } else {
63        setupWebDriver(browser, serverUrl, path, true);
64  
65        final WebDriver webDriver = getWebDriver(browser);
66        waitForQUnitBanner(webDriver);
67        WebElement qunitTests = webDriver.findElement(By.id("qunit-tests"));
68  
69        List<WebElement> results = qunitTests.findElements(By.xpath("li"));
70        boolean testException = false;
71        for (final WebElement result : results) {
72          if ("has no exception".equals(result.findElement(By.className("test-name")).getText())) {
73            Assertions.assertEquals(result.getAttribute("class"), "fail");
74            testException = true;
75          }
76        }
77        Assertions.assertTrue(testException, "Could not verify 'has no exception' test.");
78      }
79    }
80  
81    /**
82     * Verify qunit test for "no 404".
83     */
84    @ParameterizedTest
85    @MethodSource("verifyNoException404TestProvider")
86    void verifyNo404Test(Browser browser, String serverUrl) throws MalformedURLException, UnsupportedEncodingException {
87      final String path = "error/404.xhtml";
88      LOG.info("browser: " + browser + " - url: " + serverUrl + "/" + path);
89  
90      if (isIgnored(serverUrl, path)) {
91        logIgnoreMessage(serverUrl, path);
92      } else {
93        setupWebDriver(browser, serverUrl, path, true);
94  
95        final WebDriver webDriver = getWebDriver(browser);
96        waitForQUnitBanner(webDriver);
97        WebElement qunitTests = webDriver.findElement(By.id("qunit-tests"));
98  
99        List<WebElement> results = qunitTests.findElements(By.xpath("li"));
100       boolean test404 = false;
101       for (final WebElement result : results) {
102         if ("has no 404".equals(result.findElement(By.className("test-name")).getText())) {
103           Assertions.assertEquals(result.getAttribute("class"), "fail");
104           test404 = true;
105         }
106       }
107       Assertions.assertTrue(test404, "Could not verify 'has no 404' test.");
108     }
109   }
110 
111   private static Stream<Arguments> verifyNoException404TestProvider() throws IOException {
112     List<Arguments> arguments = new LinkedList<>();
113 
114     for (Browser browser : Browser.values()) {
115       for (String serverUrl : getServerUrls()) {
116         arguments.add(Arguments.of(browser, serverUrl));
117       }
118     }
119 
120     return arguments.stream();
121   }
122 
123   @ParameterizedTest
124   @MethodSource("accessAllPagesProvider")
125   void testAccessAllPages(Browser browser, String serverUrl, String path, LocalTime startTime, int testSize, int testNo)
126       throws MalformedURLException, UnsupportedEncodingException {
127 
128     double percent = 100 * (double) testNo / testSize;
129     final String timeLeft = getTimeLeft(startTime, testSize, testNo);
130 
131     LOG.info("(" + String.format("%.2f", percent) + " % complete" + " | time left: " + timeLeft + ")"
132         + " browser: " + browser + " - url: " + serverUrl + "/" + path);
133 
134     if (isIgnored(serverUrl, path)) {
135       logIgnoreMessage(serverUrl, path);
136     } else {
137       setupWebDriver(browser, serverUrl, path, true);
138       parseQUnitResults(browser, serverUrl, path);
139     }
140   }
141 
142   private static Stream<Arguments> accessAllPagesProvider() throws IOException {
143     final List<String> paths = Files.walk(Paths.get("src/main/webapp/content/"))
144         .filter(Files::isRegularFile)
145         .map(Path::toString)
146         .filter(s -> s.endsWith(".xhtml"))
147         .filter(s -> !s.contains("/x-"))
148         .map(s -> s.substring("src/main/webapp/".length()))
149         .sorted()
150         .collect(Collectors.toList());
151 
152     for (String standardTestPath : getStandardTestPaths()) {
153       paths.remove(standardTestPath); // already tested by standard test
154     }
155 
156     return getArguments(paths);
157   }
158 }