Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/test/org/openqa/selenium/FrameSwitchingTest.java
1865 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
package org.openqa.selenium;
19
20
import static org.assertj.core.api.Assertions.assertThat;
21
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
22
import static org.openqa.selenium.support.ui.ExpectedConditions.frameToBeAvailableAndSwitchToIt;
23
import static org.openqa.selenium.support.ui.ExpectedConditions.not;
24
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;
25
import static org.openqa.selenium.support.ui.ExpectedConditions.textToBe;
26
import static org.openqa.selenium.support.ui.ExpectedConditions.titleIs;
27
import static org.openqa.selenium.testing.drivers.Browser.CHROME;
28
import static org.openqa.selenium.testing.drivers.Browser.EDGE;
29
import static org.openqa.selenium.testing.drivers.Browser.IE;
30
import static org.openqa.selenium.testing.drivers.Browser.SAFARI;
31
32
import java.util.Random;
33
import org.junit.jupiter.api.AfterEach;
34
import org.junit.jupiter.api.Test;
35
import org.junit.jupiter.api.Timeout;
36
import org.openqa.selenium.testing.Ignore;
37
import org.openqa.selenium.testing.JupiterTestBase;
38
import org.openqa.selenium.testing.NotYetImplemented;
39
40
class FrameSwitchingTest extends JupiterTestBase {
41
42
private Random random;
43
44
@AfterEach
45
public void tearDown() {
46
try {
47
driver.switchTo().defaultContent();
48
} catch (Exception e) {
49
// May happen if the driver went away.
50
}
51
}
52
53
// ----------------------------------------------------------------------------------------------
54
//
55
// Tests that WebDriver doesn't do anything fishy when it navigates to a page with frames.
56
//
57
// ----------------------------------------------------------------------------------------------
58
@Test
59
void testShouldAlwaysFocusOnTheTopMostFrameAfterANavigationEvent() {
60
driver.get(pages.framesetPage);
61
driver.findElement(By.tagName("frameset")); // Test passes if this does not throw.
62
}
63
64
@Test
65
void testShouldNotAutomaticallySwitchFocusToAnIFrameWhenAPageContainingThemIsLoaded() {
66
driver.get(pages.iframePage);
67
driver.findElement(By.id("iframe_page_heading"));
68
}
69
70
@Test
71
@Timeout(10)
72
public void testShouldOpenPageWithBrokenFrameset() {
73
driver.get(appServer.whereIs("framesetPage3.html"));
74
75
WebElement frame1 = driver.findElement(By.id("first"));
76
driver.switchTo().frame(frame1);
77
78
driver.switchTo().defaultContent();
79
80
WebElement frame2 = driver.findElement(By.id("second"));
81
82
try {
83
driver.switchTo().frame(frame2);
84
} catch (WebDriverException e) {
85
// IE9 can not switch to this broken frame - it has no window.
86
}
87
}
88
89
// ----------------------------------------------------------------------------------------------
90
//
91
// Tests that WebDriver can switch to frames as expected.
92
//
93
// ----------------------------------------------------------------------------------------------
94
@Test
95
void testShouldBeAbleToSwitchToAFrameByItsIndex() {
96
driver.get(pages.framesetPage);
97
driver.switchTo().frame(1);
98
99
assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("2");
100
}
101
102
@Test
103
void testShouldBeAbleToSwitchToAnIframeByItsIndex() {
104
driver.get(pages.iframePage);
105
driver.switchTo().frame(0);
106
107
assertThat(driver.findElement(By.name("id-name1")).getAttribute("value")).isEqualTo("name");
108
}
109
110
@Test
111
void testShouldBeAbleToSwitchToAFrameByItsName() {
112
driver.get(pages.framesetPage);
113
driver.switchTo().frame("fourth");
114
115
assertThat(driver.findElement(By.tagName("frame")).getAttribute("name")).isEqualTo("child1");
116
}
117
118
@Test
119
void testShouldBeAbleToSwitchToAnIframeByItsName() {
120
driver.get(pages.iframePage);
121
driver.switchTo().frame("iframe1-name");
122
123
assertThat(driver.findElement(By.name("id-name1")).getAttribute("value")).isEqualTo("name");
124
}
125
126
@Test
127
void testShouldBeAbleToSwitchToAFrameByItsID() {
128
driver.get(pages.framesetPage);
129
driver.switchTo().frame("fifth");
130
assertThat(driver.findElement(By.name("windowOne")).getText()).isEqualTo("Open new window");
131
}
132
133
@Test
134
void testShouldBeAbleToSwitchToAnIframeByItsID() {
135
driver.get(pages.iframePage);
136
driver.switchTo().frame("iframe1");
137
138
assertThat(driver.findElement(By.name("id-name1")).getAttribute("value")).isEqualTo("name");
139
}
140
141
@Test
142
void testShouldBeAbleToSwitchToFrameWithNameContainingDot() {
143
driver.get(pages.framesetPage);
144
driver.switchTo().frame("sixth.iframe1");
145
assertThat(driver.findElement(By.tagName("body")).getText()).contains("Page number 3");
146
}
147
148
@Test
149
void testShouldBeAbleToSwitchToAFrameUsingAPreviouslyLocatedWebElement() {
150
driver.get(pages.framesetPage);
151
WebElement frame = driver.findElement(By.tagName("frame"));
152
driver.switchTo().frame(frame);
153
154
assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("1");
155
}
156
157
@Test
158
void testShouldBeAbleToSwitchToAnIFrameUsingAPreviouslyLocatedWebElement() {
159
driver.get(pages.iframePage);
160
WebElement frame = driver.findElement(By.tagName("iframe"));
161
driver.switchTo().frame(frame);
162
163
WebElement element = driver.findElement(By.name("id-name1"));
164
assertThat(element.getAttribute("value")).isEqualTo("name");
165
}
166
167
@Test
168
void testShouldEnsureElementIsAFrameBeforeSwitching() {
169
driver.get(pages.framesetPage);
170
WebElement frame = driver.findElement(By.tagName("frameset"));
171
172
assertThatExceptionOfType(NoSuchFrameException.class)
173
.isThrownBy(() -> driver.switchTo().frame(frame));
174
}
175
176
@Test
177
void testFrameSearchesShouldBeRelativeToTheCurrentlySelectedFrame() {
178
driver.get(pages.framesetPage);
179
180
driver.switchTo().frame("second");
181
assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("2");
182
183
assertThatExceptionOfType(NoSuchFrameException.class)
184
.isThrownBy(() -> driver.switchTo().frame("third"));
185
186
driver.switchTo().defaultContent();
187
driver.switchTo().frame("third");
188
189
assertThatExceptionOfType(NoSuchFrameException.class)
190
.isThrownBy(() -> driver.switchTo().frame("second"));
191
192
driver.switchTo().defaultContent();
193
driver.switchTo().frame("second");
194
assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("2");
195
}
196
197
@Test
198
void testShouldSelectChildFramesByChainedCalls() {
199
driver.get(pages.framesetPage);
200
201
driver.switchTo().frame("fourth").switchTo().frame("child2");
202
assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("11");
203
}
204
205
@Test
206
void testShouldThrowFrameNotFoundExceptionLookingUpSubFramesWithSuperFrameNames() {
207
driver.get(pages.framesetPage);
208
driver.switchTo().frame("fourth");
209
210
assertThatExceptionOfType(NoSuchFrameException.class)
211
.isThrownBy(() -> driver.switchTo().frame("second"));
212
}
213
214
@Test
215
void testShouldThrowAnExceptionWhenAFrameCannotBeFound() {
216
driver.get(pages.xhtmlTestPage);
217
218
assertThatExceptionOfType(NoSuchFrameException.class)
219
.isThrownBy(() -> driver.switchTo().frame("Nothing here"));
220
}
221
222
@Test
223
void testShouldThrowAnExceptionWhenAFrameCannotBeFoundByIndex() {
224
driver.get(pages.xhtmlTestPage);
225
226
assertThatExceptionOfType(NoSuchFrameException.class)
227
.isThrownBy(() -> driver.switchTo().frame(27));
228
}
229
230
@Test
231
void testShouldBeAbleToSwitchToParentFrame() {
232
driver.get(pages.framesetPage);
233
234
driver.switchTo().frame("fourth").switchTo().parentFrame().switchTo().frame("first");
235
assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("1");
236
}
237
238
@Test
239
@NotYetImplemented(SAFARI)
240
public void testShouldBeAbleToSwitchToParentFrameFromASecondLevelFrame() {
241
driver.get(pages.framesetPage);
242
243
driver
244
.switchTo()
245
.frame("fourth")
246
.switchTo()
247
.frame("child1")
248
.switchTo()
249
.parentFrame()
250
.switchTo()
251
.frame("child2");
252
assertThat(driver.findElement(By.id("pageNumber")).getText()).isEqualTo("11");
253
}
254
255
@Test
256
void testSwitchingToParentFrameFromDefaultContextIsNoOp() {
257
driver.get(pages.xhtmlTestPage);
258
driver.switchTo().parentFrame();
259
assertThat(driver.getTitle()).isEqualTo("XHTML Test Page");
260
}
261
262
@Test
263
void testShouldBeAbleToSwitchToParentFromAnIframe() {
264
driver.get(pages.iframePage);
265
driver.switchTo().frame(0);
266
267
driver.switchTo().parentFrame();
268
driver.findElement(By.id("iframe_page_heading"));
269
}
270
271
// ----------------------------------------------------------------------------------------------
272
//
273
// General frame handling behavior tests
274
//
275
// ----------------------------------------------------------------------------------------------
276
277
@Test
278
void testShouldContinueToReferToTheSameFrameOnceItHasBeenSelected() {
279
driver.get(pages.framesetPage);
280
281
driver.switchTo().frame(2);
282
WebElement checkbox = driver.findElement(By.xpath("//input[@name='checky']"));
283
checkbox.click();
284
checkbox.submit();
285
286
wait.until(textToBe(By.xpath("//p"), "Success!"));
287
}
288
289
@Test
290
@NotYetImplemented(SAFARI)
291
public void testShouldFocusOnTheReplacementWhenAFrameFollowsALinkToA_TopTargetedPage() {
292
driver.get(pages.framesetPage);
293
294
driver.switchTo().frame(0);
295
driver.findElement(By.linkText("top")).click();
296
297
String expectedTitle = "XHTML Test Page";
298
299
wait.until(titleIs(expectedTitle));
300
}
301
302
@Test
303
void testShouldAllowAUserToSwitchFromAnIframeBackToTheMainContentOfThePage() {
304
driver.get(pages.iframePage);
305
driver.switchTo().frame(0);
306
307
driver.switchTo().defaultContent();
308
driver.findElement(By.id("iframe_page_heading"));
309
}
310
311
@Test
312
void testShouldAllowTheUserToSwitchToAnIFrameAndRemainFocusedOnIt() {
313
driver.get(pages.iframePage);
314
driver.switchTo().frame(0);
315
316
driver.findElement(By.id("submitButton")).click();
317
318
assertThat(getTextOfGreetingElement()).isEqualTo("Success!");
319
}
320
321
public String getTextOfGreetingElement() {
322
return wait.until(presenceOfElementLocated(By.id("greeting"))).getText();
323
}
324
325
@Test
326
void testShouldBeAbleToClickInAFrame() {
327
driver.get(pages.framesetPage);
328
driver.switchTo().frame("third");
329
330
// This should replace frame "third" ...
331
driver.findElement(By.id("submitButton")).click();
332
// driver should still be focused on frame "third" ...
333
assertThat(getTextOfGreetingElement()).isEqualTo("Success!");
334
// Make sure it was really frame "third" which was replaced ...
335
driver.switchTo().defaultContent().switchTo().frame("third");
336
assertThat(getTextOfGreetingElement()).isEqualTo("Success!");
337
}
338
339
@Test
340
void testShouldBeAbleToClickInAFrameThatRewritesTopWindowLocation() {
341
driver.get(appServer.whereIs("click_tests/issue5237.html"));
342
driver.switchTo().frame("search");
343
driver.findElement(By.id("submit")).click();
344
driver.switchTo().defaultContent();
345
wait.until(titleIs("Target page for issue 5237"));
346
}
347
348
@Test
349
void testShouldBeAbleToClickInASubFrame() {
350
driver.get(pages.framesetPage);
351
driver.switchTo().frame("sixth").switchTo().frame("iframe1");
352
353
// This should replace frame "iframe1" inside frame "sixth" ...
354
driver.findElement(By.id("submitButton")).click();
355
// driver should still be focused on frame "iframe1" inside frame "sixth" ...
356
assertThat(getTextOfGreetingElement()).isEqualTo("Success!");
357
// Make sure it was really frame "iframe1" inside frame "sixth" which was replaced ...
358
driver.switchTo().defaultContent().switchTo().frame("sixth").switchTo().frame("iframe1");
359
assertThat(driver.findElement(By.id("greeting")).getText()).isEqualTo("Success!");
360
}
361
362
@Test
363
void testShouldBeAbleToFindElementsInIframesByXPath() {
364
driver.get(pages.iframePage);
365
366
driver.switchTo().frame("iframe1");
367
368
WebElement element = driver.findElement(By.xpath("//*[@id = 'changeme']"));
369
370
assertThat(element).isNotNull();
371
}
372
373
@Test
374
void testGetCurrentUrlReturnsTopLevelBrowsingContextUrl() {
375
driver.get(pages.framesetPage);
376
assertThat(driver.getCurrentUrl()).isEqualTo(pages.framesetPage);
377
378
driver.switchTo().frame("second");
379
assertThat(driver.getCurrentUrl()).isEqualTo(pages.framesetPage);
380
}
381
382
@Test
383
void testGetCurrentUrlReturnsTopLevelBrowsingContextUrlForIframes() {
384
driver.get(pages.iframePage);
385
assertThat(driver.getCurrentUrl()).isEqualTo(pages.iframePage);
386
387
driver.switchTo().frame("iframe1");
388
assertThat(driver.getCurrentUrl()).isEqualTo(pages.iframePage);
389
}
390
391
@Test
392
@Ignore(SAFARI)
393
public void testShouldBeAbleToSwitchToTheTopIfTheFrameIsDeletedFromUnderUs() {
394
driver.get(appServer.whereIs("frame_switching_tests/deletingFrame.html"));
395
396
driver.switchTo().frame("iframe1");
397
398
WebElement killIframe = driver.findElement(By.id("killIframe"));
399
killIframe.click();
400
driver.switchTo().defaultContent();
401
402
assertFrameNotPresent("iframe1");
403
404
WebElement addIFrame = driver.findElement(By.id("addBackFrame"));
405
addIFrame.click();
406
wait.until(presenceOfElementLocated(By.id("iframe1")));
407
408
driver.switchTo().frame("iframe1");
409
410
wait.until(presenceOfElementLocated(By.id("success")));
411
}
412
413
@Test
414
@Ignore(SAFARI)
415
public void testShouldBeAbleToSwitchToTheTopIfTheFrameIsDeletedFromUnderUsWithFrameIndex() {
416
driver.get(appServer.whereIs("frame_switching_tests/deletingFrame.html"));
417
int iframe = 0;
418
wait.until(frameToBeAvailableAndSwitchToIt(iframe));
419
// we should be in the frame now
420
WebElement killIframe = driver.findElement(By.id("killIframe"));
421
killIframe.click();
422
423
driver.switchTo().defaultContent();
424
425
WebElement addIFrame = driver.findElement(By.id("addBackFrame"));
426
addIFrame.click();
427
wait.until(frameToBeAvailableAndSwitchToIt(iframe));
428
429
wait.until(presenceOfElementLocated(By.id("success")));
430
}
431
432
@Test
433
@Ignore(SAFARI)
434
public void testShouldBeAbleToSwitchToTheTopIfTheFrameIsDeletedFromUnderUsWithWebElement() {
435
driver.get(appServer.whereIs("frame_switching_tests/deletingFrame.html"));
436
WebElement iframe = driver.findElement(By.id("iframe1"));
437
wait.until(frameToBeAvailableAndSwitchToIt(iframe));
438
// we should be in the frame now
439
WebElement killIframe = driver.findElement(By.id("killIframe"));
440
killIframe.click();
441
442
driver.switchTo().defaultContent();
443
444
WebElement addIFrame = driver.findElement(By.id("addBackFrame"));
445
addIFrame.click();
446
447
iframe = driver.findElement(By.id("iframe1"));
448
wait.until(frameToBeAvailableAndSwitchToIt(iframe));
449
wait.until(presenceOfElementLocated(By.id("success")));
450
}
451
452
@Test
453
@NotYetImplemented(value = CHROME, reason = "Throws NoSuchElementException")
454
@NotYetImplemented(value = EDGE, reason = "Throws NoSuchElementException")
455
@Ignore(IE)
456
@Ignore(SAFARI)
457
public void testShouldNotBeAbleToDoAnythingTheFrameIsDeletedFromUnderUs() {
458
driver.get(appServer.whereIs("frame_switching_tests/deletingFrame.html"));
459
460
driver.switchTo().frame("iframe1");
461
driver.findElement(By.id("killIframe")).click();
462
463
assertThatExceptionOfType(NoSuchWindowException.class)
464
.isThrownBy(() -> driver.findElement(By.id("killIframe")));
465
}
466
467
@Test
468
void testShouldReturnWindowTitleInAFrameset() {
469
driver.get(pages.framesetPage);
470
driver.switchTo().frame("third");
471
assertThat(driver.getTitle()).isEqualTo("Unique title");
472
}
473
474
@Test
475
void testJavaScriptShouldExecuteInTheContextOfTheCurrentFrame() {
476
JavascriptExecutor executor = (JavascriptExecutor) driver;
477
478
driver.get(pages.framesetPage);
479
assertThat((Boolean) executor.executeScript("return window == window.top")).isTrue();
480
driver.switchTo().frame("third");
481
assertThat((Boolean) executor.executeScript("return window != window.top")).isTrue();
482
}
483
484
@Test
485
void testShouldNotSwitchMagicallyToTheTopWindow() {
486
String baseUrl = appServer.whereIs("frame_switching_tests/");
487
driver.get(baseUrl + "bug4876.html");
488
driver.switchTo().frame(0);
489
wait.until(presenceOfElementLocated(By.id("inputText")));
490
491
for (int i = 0; i < 20; i++) {
492
try {
493
WebElement input = wait.until(presenceOfElementLocated(By.id("inputText")));
494
WebElement submit = wait.until(presenceOfElementLocated(By.id("submitButton")));
495
input.clear();
496
random = new Random();
497
input.sendKeys("rand" + random.nextInt());
498
submit.click();
499
} finally {
500
String url =
501
(String) ((JavascriptExecutor) driver).executeScript("return window.location.href");
502
// IE6 and Chrome add "?"-symbol to the end of the URL
503
if (url.endsWith("?")) {
504
url = url.substring(0, url.length() - 1);
505
}
506
assertThat(url).isEqualTo(baseUrl + "bug4876_iframe.html");
507
}
508
}
509
}
510
511
@Test
512
void testGetShouldSwitchToDefaultContext() {
513
driver.get(pages.iframePage);
514
driver.switchTo().frame(driver.findElement(By.id("iframe1")));
515
driver.findElement(By.id("cheese")); // Found on formPage.html but not on iframes.html.
516
517
driver.get(pages.iframePage); // This must effectively switchTo().defaultContent(), too.
518
driver.findElement(By.id("iframe1"));
519
}
520
521
private void assertFrameNotPresent(String locator) {
522
driver.switchTo().defaultContent();
523
wait.until(not(frameToBeAvailableAndSwitchToIt(locator)));
524
driver.switchTo().defaultContent();
525
}
526
}
527
528