Test Diary

Archives


Social Profile


Recent Comments


My Eight Selenium WebDriver Test (new windows and tabs)

Olufemi Ade-OlusileOlufemi Ade-Olusile

Menu

In order for websites to provide good user experience, external links (links that take users to another website) are now designed to open in new tabs or windows. During testing, this behaviour is sometimes inevitable as we may have to deal with such links. Therefore, it is necessary that we understand how to handle new windows and tabs with selenium.

Selenium, provides us with the getWindowhandle() method which can be used to get the current window handle. It also provides us with the getWindowhandles() method which can be used to get a set of window handles that can be iterated over. The getWindowhandle() when called will return a unique string of alphanumeric text for the current window handle, while the getWindowhandles() will return a set of unique strings of alphanumeric text of all window handles. 

Example:

  1. String currentWindow = driver.getWindowHandle()  // returns a String of alphanumeric text
  2. Set<String> windows = driver.getWindowHandles() // returns a set of alphanumeric text

In addition, by using the switch() method, which was explained in our previous post, we can switch between windows by parsing the string of alphanumeric text into the switch() method.

Now let’s follow the steps below and then dive into our code to understand how we test/implement a new window or tab.

  1. Navigate to the sample test page.
  2. Scroll down to the “Open page in a new window” link and click it.
  3. Switch to the new window or tab 
  4. Verify that you are on the new window and close it
  5. Switch to the previous window
  6. Verify that you are on this window
Eight Test (Windows or Tabs) 
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.Set;
public class TestdiaryWindows {

public static void main(String[] args) {

// Declare a local variable of type "String"
String firstPageWindowHandle;

// Declare a local variable of type "String" and initialize it to store null
String secondTestWindowHandle = null;

WebDriver driver = new FirefoxDriver();

driver.manage().window().maximize();

String contactUrl = "http://www.testdiary.com/training/selenium/selenium-test-page/";

driver.get(contactUrl);

//wait until Selenium can find the link with locator linktext "Open page in a new window" on the test page.

(new WebDriverWait(driver, 10)).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Open page in a new window")));

// Store the window handle of the current Sample test page
// in the variable firstPageWindowHandle
firstPageWindowHandle = driver.getWindowHandle();

// Explained in the previous post
int hyperlinkYCoordinate = driver.findElement(By.linkText("Open page in a new window")).getLocation().getY();
int hyperlinkXCoordinate = driver.findElement(By.linkText("Open page in a new window")).getLocation().getX();

JavascriptExecutor jsexecutor = (JavascriptExecutor) driver;
jsexecutor.executeScript("window.scrollBy(" + hyperlinkXCoordinate + "," + hyperlinkYCoordinate + ")", "");

(new WebDriverWait(driver, 100)).until(ExpectedConditions.elementToBeClickable(By.linkText("Open page in a new window")));

// click on the link, which opens up a new window
driver.findElement(By.linkText("Open page in a new window")).click();

//At this point we have two windows opened.
// Store both window handles in the variable testPageWindowHandle of type Set<String>
Set<String> testPageWindowHandle = driver.getWindowHandles();

//iterate through the set testPageWindowHandle
for (String windowHandle : testPageWindowHandle) {
//while iterating through the set, check if firstPageWindowHandle which should be
// the sample test page, is equal to any of the two window handles in testPageWindowHandle set.
if (!firstPageWindowHandle.equals(windowHandle)) {
// if firstPageWindowHandle is not equal to a window handle in the set
// then make secondTestWindowHandle equal to that window handle
secondTestWindowHandle = windowHandle;
}
}

//using the switchTo method you can parse "secondTestWindowHandle" webElement as a parameter
// into the window method
// This will point selenium to the second test window, where you can test on
driver.switchTo().window(secondTestWindowHandle);

//wait until Selenium can find the link with locator name "testpagelink" on the second test page.
(new WebDriverWait(driver, 10))
.until(ExpectedConditions.visibilityOfElementLocated(By.id("testpagelink")));

// This will close the second test page.
driver.switchTo().window(secondTestWindowHandle).close();

//using the switchTo method you can parse "firstPageWindowHandle" webElement as a parameter
// into the window method
//This will point selenium to the first test window, where you can test on
driver.switchTo().window(firstPageWindowHandle);

//wait until Selenium can find the link with locator linktext "Open page in a new window" on the test page.
(new WebDriverWait(driver, 10))
.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Open page in a new window")));

driver.close();

}

}

In conclusion, we have been able to handle new windows or tabs on the sample test page . You can use this same approach to handle new windows or tabs while testing your webpages. You can download or clone this test from the Testdiary Github

Click: Windows Happy Testing!!!

Menu

 

“My name is Olufemi Ade-Olusile, and I am a Software Developer in Test. I am the owner of "Test diary" which is aimed at inspiring and teaching software testers.