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