When we are testing web applications each web app will have different behavior. Sometimes when we click on a link it might get open in a new window or may be in the same page after refreshing the current page.
When we are doing testing in these type of scenarios it is mandatory to know which window we are in. this can easily be handle with selenium inbuilt classes and functionalists. below code illustrate how we can manage multiple window opening and how we can switch on them.
//Goes to the site
WebDriver A = new FirefoxDriver();
A.get("http://www.hdfc.com/");
//Return a set of window handles which can be used to iterate over all open windows of this WebDriver instance by passing them to
Set<String> windowIds = A.getWindowHandles();
Iterator<String> itr = windowIds.iterator();
//Prints the window IDs until the next value is not null
while (itr.hasNext ()){
System.out.println(itr.next());
}
A.findElement(By.xpath("//img[@title='InstantHomeLoan']")).click();
windowIds = A.getWindowHandles();
itr = windowIds.iterator();
//Assigning the window ID to String variable
String mainWindowId = itr.next();
String secondWindow = itr.next();
System.out.println("mainWindowId"+mainWindowId);
System.out.println("secondWindow"+secondWindow);
//Switching to the new Window
A.switchTo().window(secondWindow);
A.findElement(By.linkText("Your Applications")).click();
A.quit();
in this code the main role is played by "getWindowHandles()" method. "getWindowHandles()" Return a set of window handles which can be used to iterate over all open windows of this WebDriver instance by passing them to "SwitchTo().WebDriver.Options.window()" and this will return A set of window handles which can be used to iterate over all open windows.
If you want to read further about the java methods, and classes please ref this link Selenium - Definition Of All Classes
No comments:
Post a Comment