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