1
2
3
4
5
6
7
8
9
10
11
12
13
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
27
28
29
30
31 public class HistoryTest extends WebServerTestCase {
32
33
34
35
36
37 @BeforeEach
38 public void setUp() throws Exception {
39 startWebServer("src/test/resources/org/htmlunit/javascript/host");
40 }
41
42
43
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
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 }