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;
16  
17  import org.htmlunit.History;
18  import org.htmlunit.TopLevelWindow;
19  import org.htmlunit.WebClient;
20  import org.htmlunit.WebServerTestCase;
21  import org.htmlunit.html.HtmlPage;
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests for {@link History}.
27   *
28   * @author Daniel Gredler
29   * @author Ronald Brill
30   */
31  public class HistoryTest extends WebServerTestCase {
32  
33      /**
34       * Starts the web server prior to test execution.
35       * @throws Exception if an error occurs
36       */
37      @BeforeEach
38      public void setUp() throws Exception {
39          startWebServer("src/test/resources/org/htmlunit/javascript/host");
40      }
41  
42      /**
43       * @throws Exception if an error occurs
44       */
45      @Test
46      public void backAndForward() throws Exception {
47          final WebClient client = getWebClient();
48          final TopLevelWindow window = (TopLevelWindow) client.getCurrentWindow();
49          final History history = window.getHistory();
50  
51          final String urlA = URL_FIRST + "HistoryTest_a.html";
52          final String urlB = URL_FIRST + "HistoryTest_b.html";
53          final String urlBX = URL_FIRST + "HistoryTest_b.html#x";
54          final String urlC = URL_FIRST + "HistoryTest_c.html";
55  
56          HtmlPage page = client.getPage(urlA);
57          assertEquals(1, history.getLength());
58          assertEquals(0, history.getIndex());
59          assertEquals(urlA, page.getUrl());
60  
61          page = page.getAnchorByName("b").click();
62          assertEquals(2, history.getLength());
63          assertEquals(1, history.getIndex());
64          assertEquals(urlB, page.getUrl());
65  
66          page = page.getAnchorByName("x").click();
67          assertEquals(3, history.getLength());
68          assertEquals(2, history.getIndex());
69          assertEquals(urlBX, page.getUrl());
70  
71          page = page.getAnchorByName("back").click();
72          assertEquals(3, history.getLength());
73          assertEquals(1, history.getIndex());
74          assertEquals(urlB, page.getUrl());
75  
76          page = page.getAnchorByName("back").click();
77          assertEquals(3, history.getLength());
78          assertEquals(0, history.getIndex());
79          assertEquals(urlA, page.getUrl());
80  
81          page = page.getAnchorByName("forward").click();
82          assertEquals(3, history.getLength());
83          assertEquals(1, history.getIndex());
84          assertEquals(urlB, page.getUrl());
85  
86          page = page.getAnchorByName("c").click();
87          assertEquals(3, history.getLength());
88          assertEquals(2, history.getIndex());
89          assertEquals(urlC, page.getUrl());
90  
91          page = page.getAnchorByName("back").click();
92          assertEquals(3, history.getLength());
93          assertEquals(1, history.getIndex());
94          assertEquals(urlB, page.getUrl());
95  
96          page = page.getAnchorByName("forward").click();
97          assertEquals(3, history.getLength());
98          assertEquals(2, history.getIndex());
99          assertEquals(urlC, page.getUrl());
100     }
101 
102     /**
103      * @throws Exception if an error occurs
104      */
105     @Test
106     public void go() throws Exception {
107         final WebClient client = getWebClient();
108         final TopLevelWindow window = (TopLevelWindow) client.getCurrentWindow();
109         final History history = window.getHistory();
110 
111         final String urlA = URL_FIRST + "HistoryTest_a.html";
112         final String urlB = URL_FIRST + "HistoryTest_b.html";
113         final String urlBX = URL_FIRST + "HistoryTest_b.html#x";
114         final String urlC = URL_FIRST + "HistoryTest_c.html";
115 
116         HtmlPage page = client.getPage(urlA);
117         assertEquals(1, history.getLength());
118         assertEquals(0, history.getIndex());
119         assertEquals(urlA, page.getUrl());
120 
121         page = page.getAnchorByName("b").click();
122         assertEquals(2, history.getLength());
123         assertEquals(1, history.getIndex());
124         assertEquals(urlB, page.getUrl());
125 
126         page = page.getAnchorByName("x").click();
127         assertEquals(3, history.getLength());
128         assertEquals(2, history.getIndex());
129         assertEquals(urlBX, page.getUrl());
130 
131         page = page.getAnchorByName("minusTwo").click();
132         assertEquals(3, history.getLength());
133         assertEquals(0, history.getIndex());
134         assertEquals(urlA, page.getUrl());
135 
136         page = page.getAnchorByName("plusOne").click();
137         assertEquals(3, history.getLength());
138         assertEquals(1, history.getIndex());
139         assertEquals(urlB, page.getUrl());
140 
141         page = page.getAnchorByName("c").click();
142         assertEquals(3, history.getLength());
143         assertEquals(2, history.getIndex());
144         assertEquals(urlC, page.getUrl());
145 
146         page = page.getAnchorByName("minusOne").click();
147         assertEquals(3, history.getLength());
148         assertEquals(1, history.getIndex());
149         assertEquals(urlB, page.getUrl());
150 
151         page = page.getAnchorByName("plusTwo").click();
152         assertEquals(3, history.getLength());
153         assertEquals(1, history.getIndex());
154         assertEquals(urlB, page.getUrl());
155     }
156 }