Last active
March 12, 2021 15:48
-
-
Save ShamaUgale/827a77b279322dcdd6c6395605aabcb0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.openqa.selenium.WindowType; | |
import org.openqa.selenium.chrome.ChromeDriver; | |
import java.io.IOException; | |
import java.util.Iterator; | |
import java.util.Set; | |
/* | |
This is an example to demonstarte the usage of newWindow() method in Selenium 4 | |
New window or tabbed windows can be created and used in the single session and switch between them with | |
switchTo().window() method | |
*/ | |
public class WindowsTabsExample { | |
final static String PROJECT_PATH = System.getProperty("user.dir"); | |
public static void main(String[] args) { | |
System.setProperty("webdriver.chrome.driver", PROJECT_PATH+ "/src/main/resources/chromedriver"); | |
ChromeDriver driver = new ChromeDriver(); | |
driver.get("https://www.google.com/"); | |
// open a new window | |
driver.switchTo().newWindow(WindowType.WINDOW); | |
//navigate to a URL on newly opened window | |
driver.navigate().to("https://www.youtube.com/"); | |
// get the window ids to switch between them | |
Set<String> windows = driver.getWindowHandles(); | |
Iterator<String> it = windows.iterator(); | |
String googleWindow = it.next(); | |
String youtubeWindow = it.next(); | |
System.out.println(driver.getTitle()); | |
// switch the control to the google window | |
driver.switchTo().window(googleWindow); | |
System.out.println(driver.getTitle()); | |
// open a new tabbed window | |
driver.switchTo().newWindow(WindowType.TAB); | |
//navigate to a URL on newly opened tabbed window | |
driver.navigate().to("https://www.gmail.com/"); | |
System.out.println(driver.getTitle()); | |
driver.switchTo().window(youtubeWindow); | |
System.out.println(driver.getTitle()); | |
driver.quit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is WindowType still supported?