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.htmlunit.junit.annotation.HtmlUnitNYI;
20  import org.junit.jupiter.api.Test;
21  import org.openqa.selenium.By;
22  import org.openqa.selenium.WebDriver;
23  
24  /**
25   * Tests for {@link PerformanceTiming}.
26   *
27   * @author Ronald Brill
28   */
29  public class PerformanceTimingTest extends WebDriverTestCase {
30      private static final long NOE = System.currentTimeMillis();
31  
32      /**
33       * @throws Exception if the test fails
34       */
35      @Test
36      @Alerts("[object PerformanceTiming]")
37      public void available() throws Exception {
38          final String html = DOCTYPE_HTML
39                  + "<html>\n"
40                  + "<head>\n"
41                  + "<script>\n"
42                  + LOG_TITLE_FUNCTION
43                  + "  function test() {\n"
44                  + "    var performanceTiming = performance.timing;\n"
45                  + "    log(performanceTiming);\n"
46                  + "  }\n"
47                  + "  test();\n"
48                  + "</script>\n"
49                  + "</head>\n"
50                  + "<body></body>\n"
51                  + "</html>";
52  
53          loadPageVerifyTitle2(html);
54      }
55  
56      /**
57       * @throws Exception if the test fails
58       */
59      @Test
60      @Alerts("true")
61      public void navigationStart() 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                  + "    var performanceTiming = performance.timing;\n"
69                  + "    log(performanceTiming.navigationStart > " + NOE + ");\n"
70                  + "  }\n"
71                  + "  test();\n"
72                  + "</script>\n"
73                  + "</head>\n"
74                  + "<body></body>\n"
75                  + "</html>";
76  
77          loadPageVerifyTitle2(html);
78      }
79  
80      /**
81       * @throws Exception if the test fails
82       */
83      @Test
84      @Alerts("0")
85      public void secureConnectionStart() throws Exception {
86          final String html = DOCTYPE_HTML
87                  + "<html>\n"
88                  + "<head>\n"
89                  + "<script>\n"
90                  + LOG_TITLE_FUNCTION
91                  + "  function test() {\n"
92                  + "    var performanceTiming = performance.timing;\n"
93                  + "    log(performanceTiming.secureConnectionStart);\n"
94                  + "  }\n"
95                  + "  test();\n"
96                  + "</script>\n"
97                  + "</head>\n"
98                  + "<body></body>\n"
99                  + "</html>";
100 
101         loadPageVerifyTitle2(html);
102     }
103 
104     /**
105      * @throws Exception if the test fails
106      */
107     @Test
108     @Alerts({"0", "0"})
109     public void unloadEvent() throws Exception {
110         final String html = DOCTYPE_HTML
111                 + "<html>\n"
112                 + "<head>\n"
113                 + "<script>\n"
114                 + LOG_TITLE_FUNCTION
115                 + "  function test() {\n"
116                 + "    var performanceTiming = performance.timing;\n"
117                 + "    log(performanceTiming.unloadEventStart);\n"
118                 + "    log(performanceTiming.unloadEventEnd);\n"
119                 + "  }\n"
120                 + "  test();\n"
121                 + "</script>\n"
122                 + "</head>\n"
123                 + "<body></body>\n"
124                 + "</html>";
125 
126         loadPageVerifyTitle2(html);
127     }
128 
129     /**
130      * @throws Exception if the test fails
131      */
132     @Test
133     @Alerts({"0", "0"})
134     public void redirect() throws Exception {
135         final String html = DOCTYPE_HTML
136                 + "<html>\n"
137                 + "<head>\n"
138                 + "<script>\n"
139                 + LOG_TITLE_FUNCTION
140                 + "  function test() {\n"
141                 + "    var performanceTiming = performance.timing;\n"
142                 + "    log(performanceTiming.redirectStart);\n"
143                 + "    log(performanceTiming.redirectEnd);\n"
144                 + "  }\n"
145                 + "  test();\n"
146                 + "</script>\n"
147                 + "</head>\n"
148                 + "<body></body>\n"
149                 + "</html>";
150 
151         loadPageVerifyTitle2(html);
152     }
153 
154     /**
155      * @throws Exception if the test fails
156      */
157     @Test
158     @Alerts({"true", "true"})
159     public void domainLookup() throws Exception {
160         final String html = DOCTYPE_HTML
161                 + "<html>\n"
162                 + "<head>\n"
163                 + "<script>\n"
164                 + LOG_TITLE_FUNCTION
165                 + "  function test() {\n"
166                 + "    var performanceTiming = performance.timing;\n"
167                 + "    var start = performanceTiming.domainLookupStart;\n"
168                 + "    log(start > " + NOE + ");\n"
169                 + "    log(performanceTiming.domainLookupEnd >= start);\n"
170                 + "  }\n"
171                 + "  test();\n"
172                 + "</script>\n"
173                 + "</head>\n"
174                 + "<body></body>\n"
175                 + "</html>";
176 
177         loadPageVerifyTitle2(html);
178     }
179 
180     /**
181      * @throws Exception if the test fails
182      */
183     @Test
184     @Alerts({"true", "true"})
185     public void response() throws Exception {
186         final String html = DOCTYPE_HTML
187                 + "<html>\n"
188                 + "<head>\n"
189                 + "<script>\n"
190                 + LOG_TITLE_FUNCTION
191                 + "  function test() {\n"
192                 + "    var performanceTiming = performance.timing;\n"
193                 + "    var start = performanceTiming.responseStart;\n"
194                 + "    log(start > " + NOE + ");\n"
195                 + "    log(performanceTiming.responseEnd >= start);\n"
196                 + "  }\n"
197                 + "  test();\n"
198                 + "</script>\n"
199                 + "</head>\n"
200                 + "<body></body>\n"
201                 + "</html>";
202 
203         loadPageVerifyTitle2(html);
204     }
205 
206     /**
207      * @throws Exception if the test fails
208      */
209     @Test
210     @Alerts({"true", "true"})
211     public void loadEvent() throws Exception {
212         final String html = DOCTYPE_HTML
213                 + "<html>\n"
214                 + "<head>\n"
215                 + "<script>\n"
216                 + LOG_TITLE_FUNCTION
217                 + "  function test() {\n"
218                 + "    var performanceTiming = performance.timing;\n"
219                 + "    var start = performanceTiming.loadEventStart;\n"
220                 + "    log(start > " + NOE + ");\n"
221                 + "    log(performanceTiming.loadEventEnd >= start);\n"
222                 + "  }\n"
223                 + "</script>\n"
224                 + "</head>\n"
225                 + "<body>\n"
226                 + "  <button id='clickMe' onClick='test()'>do it</button>\n"
227                 + "</body>\n"
228                 + "</html>";
229 
230         final WebDriver driver = loadPage2(html);
231         driver.findElement(By.id("clickMe")).click();
232         verifyTitle2(driver, getExpectedAlerts());
233     }
234 
235     /**
236      * @throws Exception if the test fails
237      */
238     @Test
239     @Alerts({"true", "true"})
240     public void connect() throws Exception {
241         final String html = DOCTYPE_HTML
242                 + "<html>\n"
243                 + "<head>\n"
244                 + "<script>\n"
245                 + LOG_TITLE_FUNCTION
246                 + "  function test() {\n"
247                 + "    var performanceTiming = performance.timing;\n"
248                 + "    var start = performanceTiming.connectStart;\n"
249                 + "    log(start > " + NOE + ");\n"
250                 + "    log(performanceTiming.connectEnd >= start);\n"
251                 + "  }\n"
252                 + "  test();\n"
253                 + "</script>\n"
254                 + "</head>\n"
255                 + "<body></body>\n"
256                 + "</html>";
257 
258         loadPageVerifyTitle2(html);
259     }
260 
261     /**
262      * @throws Exception if the test fails
263      */
264     @Test
265     @Alerts("true")
266     public void fetchStart() throws Exception {
267         final String html = DOCTYPE_HTML
268                 + "<html>\n"
269                 + "<head>\n"
270                 + "<script>\n"
271                 + LOG_TITLE_FUNCTION
272                 + "  function test() {\n"
273                 + "    var performanceTiming = performance.timing;\n"
274                 + "    log(performanceTiming.fetchStart > " + NOE + ");\n"
275                 + "  }\n"
276                 + "  test();\n"
277                 + "</script>\n"
278                 + "</head>\n"
279                 + "<body></body>\n"
280                 + "</html>";
281 
282         loadPageVerifyTitle2(html);
283     }
284 
285     /**
286      * @throws Exception if the test fails
287      */
288     @Test
289     @Alerts({"true", "true"})
290     public void domContentLoadedEvent() throws Exception {
291         final String html = DOCTYPE_HTML
292                 + "<html>\n"
293                 + "<head>\n"
294                 + "<script>\n"
295                 + LOG_TITLE_FUNCTION
296                 + "  function test() {\n"
297                 + "    var performanceTiming = performance.timing;\n"
298                 + "    var start = performanceTiming.domContentLoadedEventStart;\n"
299                 + "    log(start > " + NOE + ");\n"
300                 + "    log(performanceTiming.domContentLoadedEventEnd >= start);\n"
301                 + "  }\n"
302                 + "</script>\n"
303                 + "</head>\n"
304                 + "<body>\n"
305                 + "  <button id='clickMe' onClick='test()'>do it</button>\n"
306                 + "</body>\n"
307                 + "</html>";
308 
309         final WebDriver driver = loadPage2(html);
310         driver.findElement(By.id("clickMe")).click();
311         verifyTitle2(driver, getExpectedAlerts());
312     }
313 
314     /**
315      * @throws Exception if the test fails
316      */
317     @Test
318     @Alerts({"true", "true", "true", "true", "true"})
319     public void dom() throws Exception {
320         final String html = DOCTYPE_HTML
321                 + "<html>\n"
322                 + "<head>\n"
323                 + "<script>\n"
324                 + LOG_TITLE_FUNCTION
325                 + "  function test() {\n"
326                 + "    var performanceTiming = performance.timing;\n"
327                 + "    log(performanceTiming.domLoading > " + NOE + ");\n"
328                 + "    log(performanceTiming.domInteractive > " + NOE + ");\n"
329                 + "    log(performanceTiming.domContentLoadedEventStart > " + NOE + ");\n"
330                 + "    log(performanceTiming.domContentLoadedEventEnd > " + NOE + ");\n"
331                 + "    log(performanceTiming.domComplete > " + NOE + ");\n"
332                 + "  }\n"
333                 + "</script>\n"
334                 + "</head>\n"
335                 + "<body>\n"
336                 + "  <button id='clickMe' onClick='test()'>do it</button>\n"
337                 + "</body>\n"
338                 + "</html>";
339 
340         final WebDriver driver = loadPage2(html);
341         driver.findElement(By.id("clickMe")).click();
342         verifyTitle2(driver, getExpectedAlerts());
343     }
344 
345     /**
346      * @throws Exception if the test fails
347      */
348     @Test
349     @Alerts("[object Object]")
350     @HtmlUnitNYI(CHROME = {},
351             EDGE = {},
352             FF = {},
353             FF_ESR = {})
354     public void toJSON() throws Exception {
355         final String html = DOCTYPE_HTML
356                 + "<html>\n"
357                 + "<head>\n"
358                 + "<script>\n"
359                 + LOG_TITLE_FUNCTION
360                 + "  function test() {\n"
361                 + "    var performanceTiming = performance.timing;\n"
362                 + "    log(performanceTiming.toJSON());\n"
363                 + "  }\n"
364                 + "</script>\n"
365                 + "</head>\n"
366                 + "<body>\n"
367                 + "  <button id='clickMe' onClick='test()'>do it</button>\n"
368                 + "</body>\n"
369                 + "</html>";
370 
371         final WebDriver driver = loadPage2(html);
372         driver.findElement(By.id("clickMe")).click();
373         verifyTitle2(driver, getExpectedAlerts());
374     }
375 }