-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathTestWindowHandles.java
66 lines (57 loc) · 2.25 KB
/
TestWindowHandles.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.SeleniumPractice;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
/**
* @author Yadagiri Reddy
* How to handle multiple windows or tabs using Selenium WebDriver?
*/
public class TestWindowHandles {
public static void main(String[] args) throws Exception {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.hyrtutorials.com/p/window-handles-practice.html");
//Single window
// String parentWindowHandle = driver.getWindowHandle();
// System.out.println("Parent window handle - "+parentWindowHandle + driver.getTitle());
// driver.findElement(By.id("newWindowBtn")).click();
// Set<String> windowHandles = driver.getWindowHandles();
// for (String windowHandle : windowHandles) {
// if(!windowHandle.equals(parentWindowHandle)) {
// driver.switchTo().window(windowHandle);
// driver.manage().window().maximize();
// driver.findElement(By.id("firstName")).sendKeys("Yadagiri");
// Thread.sleep(3000);
// driver.close();
// Thread.sleep(2000);
// }
// }
//
// driver.switchTo().window(parentWindowHandle);
// driver.findElement(By.id("name")).sendKeys("HYR TUtorials");
// Single Tab
String parentWindowHandle = driver.getWindowHandle();
System.out.println("Parent window handle - "+parentWindowHandle + driver.getTitle());
driver.findElement(By.id("newTabBtn")).click();
Set<String> windowHandles = driver.getWindowHandles();
for (String windowHandle : windowHandles) {
if(!windowHandle.equals(parentWindowHandle)) {
driver.switchTo().window(windowHandle);
System.out.println(driver.findElement(By.id("output")).getText());
driver.findElement(By.id("alertBox")).click();
System.out.println(driver.switchTo().alert().getText());
driver.switchTo().alert().accept();
System.out.println(driver.findElement(By.id("output")).getText());
Thread.sleep(3000);
driver.close();
}
}
driver.switchTo().window(parentWindowHandle);
driver.findElement(By.id("name")).sendKeys("HYR TUtorials");
Thread.sleep(3000);
driver.quit();
}
}