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.javascript.host.performance;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  import org.openqa.selenium.WebDriver;
21  
22  /**
23   * Tests for {@link Performance}.
24   *
25   * @author Ronald Brill
26   */
27  public class PerformanceNavigationTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      public void toJSON() throws Exception {
34          final String html = DOCTYPE_HTML
35                  + "<html>\n"
36                  + "<head>\n"
37                  + "<script>\n"
38                  + "  function test() {\n"
39                  + "    alert(JSON.stringify(performance.navigation.toJSON()));\n"
40                  + "  }\n"
41                  + "  test();\n"
42                  + "</script>\n"
43                  + "</head>\n"
44                  + "<body></body>\n"
45                  + "</html>";
46  
47          final WebDriver driver = loadPage2(html);
48          final String json = getCollectedAlerts(driver, 1).get(0);
49          assertTrue(json, json.contains("\"type\":0"));
50          assertTrue(json, json.contains("\"redirectCount\":0"));
51      }
52  
53      /**
54       * @throws Exception if the test fails
55       */
56      @Test
57      @Alerts("0")
58      public void redirectCount() throws Exception {
59          final String html = DOCTYPE_HTML
60                  + "<html>\n"
61                  + "<head>\n"
62                  + "<script>\n"
63                  + LOG_TITLE_FUNCTION
64                  + "  function test() {\n"
65                  + "    log(performance.navigation.redirectCount);\n"
66                  + "  }\n"
67                  + "  test();\n"
68                  + "</script>\n"
69                  + "</head>\n"
70                  + "<body></body>\n"
71                  + "</html>";
72  
73          loadPageVerifyTitle2(html);
74      }
75  
76      /**
77       * @throws Exception if the test fails
78       */
79      @Test
80      @Alerts("0")
81      public void type() throws Exception {
82          final String html = DOCTYPE_HTML
83                  + "<html>\n"
84                  + "<head>\n"
85                  + "<script>\n"
86                  + LOG_TITLE_FUNCTION
87                  + "  function test() {\n"
88                  + "    log(performance.navigation.type);\n"
89                  + "  }\n"
90                  + "  test();\n"
91                  + "</script>\n"
92                  + "</head>\n"
93                  + "<body></body>\n"
94                  + "</html>";
95  
96          loadPageVerifyTitle2(html);
97      }
98  
99      /**
100      * @throws Exception if the test fails
101      */
102     @Test
103     @Alerts({"number", "function"})
104     public void methods() throws Exception {
105         final String html = DOCTYPE_HTML
106                 + "<html>\n"
107                 + "<body>\n"
108                 + "<script>\n"
109                 + LOG_TITLE_FUNCTION
110                 + "  log(typeof performance.navigation.redirectCount);\n"
111                 + "  log(typeof performance.navigation.toJSON);\n"
112                 + "</script>\n"
113                 + "</body></html>";
114 
115         loadPageVerifyTitle2(html);
116     }
117 }