Selenium: When the page is open in a new tab, is it possible to continue testing on a new page?
-
Yes, you can check on new pages opened from the current window.
-
Suppose you click on the menu, it will open the page in a new window. Your driver object will have access to the current page and the newly opened page as a window handle (or window ID). We can get this window handle and switch driver control to any page using a handle.
To get the current handle to the window that the driver is controlling, we can use
String currentWindowHandle= driver.getWindowHandle();
This returns a handle to the current window that uniquely identifies it in this driver instance. This can be used to switch to the current window at a later stage.
To get all windows the driver can access use
Set<String> allWindowHandles=driver.getWindowHandles();
This returns a set of window handles that can be used to iterate over all open windows.
Then we just iterate over the set
Iterator iter = allWindowHandles.iterator();
while (iter.hasNext()) {
String windowHandle=iter.next();//get window handle from set
System.out.println(windowHandle);//this will print uique window handle id
driver.switchTo().window(windowHandle);// switch driver control to window having this handle id.
// Get window title or url etc. and check whether it matches your expected page title.
//If yes, then you have navigated to correct page and then you can continue working on this page.
if(driver.getTitle().equals("New Menu page title"))
//then break the loop as you have got required page access.
break;
}
source to share
Yes, you can test on different tabs, although if you can (and want) to stick with a single tab, you might find this interesting:
http://automatictester.co.uk/2013/08/03/chromedriver-problem-with-opening-a-new-browser-tab/
I've found that using a single tab is much easier in test automation - unless you really need to test multiple tabs.
source to share