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