My test case :
I can check the number of windows open by getting a count of the windowhandles, and assert that it is equal to 2 - to ensure that on clicking the second link, the content refreshes on the same child window and doesn't open another new window.
If in case the links open in new tabs, how can I check this test case ( New tab opened the first time a link is clicked on the homepage. And on further clicking any links on the homepage, content is refreshed on the same new tab)? Is there a way to count the number of tabs in a window?
Or does selenium force new tabs to be opened as new windows instead?
Go to Home page and click on a link to open new tab/window. You can follow the steps below:
1. Open a browser and navigate to TestURL
2. Click Login link/button at the top right corner -> It will take you to a new
tab/window.
The followings are the Selenium Java code:
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
System.out.println("No. of tabs: " + tabs.size());
OR,
Set<String> allWindowHandles = driver.getWindowHandles();
ArrayList<String> tabs = new ArrayList<String>(allWindowHandles);
System.out.println("No. of tabs: " + tabs.size());