5

My test case :

  1. Open a browser and visit a URL
  2. Click on a link on the homepage -> This opens a new window/new tab.
  3. Go back to the homepage.
  4. Click another link.
  5. Ensure new content shows up on previously opened child window/child tab from step 2.

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?

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
sanaku
  • 309
  • 2
  • 6
  • 18
  • 1
    You seem to be overcomplicating this. Selenium does no more, no less than a real user would do (good). Having clicked a link, why can't you *then* verify its contents, and proceed from there, rather than trying to switch windows and tabs. Unless you need to enforce some particular way of clicking links, simply "go with the flow" and test the **current** tab (or window - you shouldn't care) – Andrew Regan Feb 27 '16 at 23:16
  • Tabs _are_ windows simply differently displayed for the convenience of the human, but browser extensions/plugins, including selenium, see them all as windows. So you will never be able to tell which is which - it depends on browser defaults and settings user specified. – timbre timbre Feb 27 '16 at 23:26
  • @AndrewRegan KirilS but then how can i get a count of the tabs? do they have any unique property that i can use to identify them ? they will have the same window id as the homepage, so i can't use windowHandle count - is that correct ? – sanaku Feb 27 '16 at 23:33
  • I don't think a tester should be **trying** to count tabs - when this only appears to be necessary because you're circumventing the user's flow. – Andrew Regan Feb 27 '16 at 23:39
  • 1
    I kind of agree with Andrew Regan, counting doesn't proof anything and is not what user would do. Say, if you manually wanted to check that the link opened in existing window, you'd switch to that window and check what it displays. Same with selenium: switch to that window and check if it displays the page you expect. And if you manually wanted to check that link opens a new window, you'd notice which windows you have, then open link, and then see that all "old" windows are unchanged, and there's a new window with expected content. Same in selenium. – timbre timbre Feb 27 '16 at 23:54

2 Answers2

2

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());
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
1

To get the count of number of tabs open in a window

    ArrayList<String> multipleTabs = new ArrayList<String>   (robot.getWebDriver().getWindowHandles());

Get the size of the arraylist i.e) count value

    System.out.println(multipleTabs.size());

Hence the count of number of tabs opened in a window is nothing but the size of the above array list