Skip to content

Instantly share code, notes, and snippets.

@pragmatictesters
Created December 29, 2018 06:40
Show Gist options
  • Save pragmatictesters/2e41655e69ed6b2726434509f6dc2d76 to your computer and use it in GitHub Desktop.
Save pragmatictesters/2e41655e69ed6b2726434509f6dc2d76 to your computer and use it in GitHub Desktop.
Selenium WebDriver Examples : Switching between browser windows
package com.pragmatic.selenium.examples;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
/**
* Created by hansi on 2018-12-28
**/
public class SwichWindowExample {
private WebDriver driver;
/**
* Get ready for the testing
* Creating a browser instance , set implicit wait and maximize the browser window
*/
@BeforeMethod
public void beforeMethod() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
/**
* Closes all the web browser windows opened by WebDriver
*/
@AfterMethod
public void afterMethod() {
// driver.close();
driver.quit();
}
/**
* Demonstrate switching to a new window and verify the title
*/
@Test
public void testSwitchWindows() {
driver.get("http://demosite.pragmatictestlabs.com/Frames%20and%20windows%20_%20Demoqa.html");
driver.findElement(By.linkText("Click Hear to Open New Browser Tab")).click();
//Must switch to the new window before working with the new window
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
System.out.println("Number of windows handles " + tabs.size());
for (String handle : tabs) {
System.out.println("Window handle " + handle);
}
//Switch to the last tab
driver.switchTo().window(tabs.get(1));
Assert.assertEquals(driver.getTitle(), "Home | Pragmatic Test Labs");
}
/**
* Demonstrate working with a new blank window opened
*/
@Test
public void testTypeURLNewTab() {
String url1 = "http://hrm.pragmatictestlabs.com";
String url2 = "http://demosite.pragmatictestlabs.com";
driver.get(url1);
Assert.assertEquals(driver.getTitle(), "PTL-DemoHRM");
//Opening a new window
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
javascriptExecutor.executeScript("window.open();");
//Get list of window handles
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(tabs.size() - 1));
driver.get(url2);
Assert.assertEquals(driver.getTitle(), "PracticeHomePage");
}
/**
* Demonstrates switching to the original window after switching to another window
*/
@Test
public void testSwitchToOrginalWindows() {
String url1 = "http://hrm.pragmatictestlabs.com";
String url2 = "http://demosite.pragmatictestlabs.com";
String firstWindowsHandle;
driver.get(url1);
firstWindowsHandle = driver.getWindowHandle();
//driver.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND + "t");
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
javascriptExecutor.executeScript("window.open();");
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(tabs.size() - 1));
driver.get(url2);
driver.switchTo().window(firstWindowsHandle);
Assert.assertEquals(driver.getTitle(), "PTL-DemoHRM");
}
/**
* Demonstrate working with a new window opened
*/
@Test
public void testSwitchToPopupWindows() {
driver.get("http://demosite.pragmatictestlabs.com/Frames%20and%20windows%20_%20Demoqa.html");
driver.findElement(By.linkText("Separate New Window")).click();
driver.findElement(By.linkText("Click Hear to Open New Separate Window")).click();
//Must switch to the new window before working with the new window
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
System.out.println("Number of windows handles " + tabs.size());
for (String handle : tabs) {
System.out.println("Window handle " + handle);
}
//Switch to the last tab
driver.switchTo().window(tabs.get(1));
Assert.assertEquals(driver.getTitle(), "Pragmatic Test Labs");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment