Tab navigation using Selenium – a stable way:

Brief:

Another blog about my experience, analysis and concluding browser ‘TAB’ navigation in automation.Hope this will help you to design an stable tab navigation framework:

Basics about tabs before jumping into code:

Browser window and tab are different! Each window have separate unique window handle where tabs are array of a same handle. Hence by switching between the handle we will achieve window navigation (comparatively simple!), not tab navigation. Just to elaborate with example: internally when we open new browser window, both the window will have separate handle like CDwindow-4f2d1560-16bf-40a3-81be-dd91e13d6390 (for browser window-1) and CDwindow-4fc2d1560-16bf-40a3-81be-dd91e13e6390 (for browser window-2), whereas two tabs are considered as array of same handle like CDwindow-4f2d1560-16bf-40a3-81be-dd91e13d6390[0] (for tab-1) and CDwindow-4f2d1560-16bf-40a3-81be-dd91e13d6390[1] (for tab-2).

Common tab operation:

  • Right click and open a link in new tabs.
  • Pointing the driver to different tabs.
  • Closing the tabs.

Technical implementation and best practices:

  • Right click and open a link in new tabs: This is purely mouse operation on a particular WebElement. As we do all complex mouse and keyboard operation using ‘Actions’ class, similarly here also we will use ‘Actions’ class to right click on a WebElement (mostly anchor tag). Hence this operation is browser independent.
//Wait until the element is visible
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(<>));
//Initiating Actions class and performing right click
Actions action = new Actions(driver);
action.moveToElement(<>);
action.contextClick(<>).perform();// this will perform right click
//Select first option in the right click menu (context menu), 
//Selecting the first option can be implemented in multiple way, see which works better for you:
//Implementation-1: Robot class implementation worked best for my scenario.
//Implementation-2: using Actions class.*****(below)
//Note that for almost all browser the first option is open new tab.
Robot rb = new Robot();
rb.keyPress(KeyEvent.VK_DOWN);rb.keyRelease(KeyEvent.VK_DOWN);
Thread.sleep(2000);
rb.keyPress(KeyEvent.VK_ENTER);rb.keyRelease(KeyEvent.VK_ENTER);
//***** Implementation-2:
/*action.contextClick(Webelement).sendKeys(Keys.ARROW_DOWN)
.sendKeys(Keys.RETURN).build().perform();*/
  • Pointing the driver to different tabs: This step took most of my time. There may be multiple way of implementing, below are the two way of doing:

Implementation-1: works well in Mozilla Firefox.

//Best practice: before pointing the driver to a tab make sure that you 
//are pointing to the correct tab. (there may be 'n' number of open tab).
//Identify the correct tab by some unique text.
int maxTabCount = 5;//to stop infinite loop condition 
do{
    //(ctrl + t) for changing tab in windows, include 'if' conditions for other OS
    driver.findElement(By.CssSelector("body")).sendKeys(Keys.CONTROL + "\t");
    //pointing driver to current tab
    driver.switchTo().defaultContent();
    maxTabCount--;
}while((!driver.getTitle().contains(<>)) && (maxTabCount>0)));
//I have used title to verify correct tab
/* <<<< write your own step >>>> */
// return driver; // if you want to create a method return the driver.

Implementation-2: works well in Google Chrome.

//Best practice: before pointing the driver to a tab make sure that you 
//are pointing to the correct tab. (there may be 'n' number of open tab).
//Identify the correct tab by some unique text.
//get the array size of the current handle
Set tabHandles = driver.getWindowHandles();
int numberOfTabs = tabHandles.size();
do{
  driver.switchTo().window(tab_handles.toArray()[numberOfTabs-1].toString()); 
  numberOfTabs--;
}while((!driver.getTitle().contains(<>)) && (numberOfTabs>0)));
//I have used title to verify correct tab
/* <<<< write your own step >>>> */
  • Closing tabs: If you’re using Implementation-1 for pointing driver then use Robot class to close the tab. (ctrl + w) shortcut for closing tab.
Robot rb = new Robot();
if (browser == "Mozilla Firefox"){
   rb.keyPress(KeyEvent.VK_CTRL);rb.keyPress(KeyEvent.VK_W);
   rb.keyRelease(KeyEvent.VK_W);rb.keyRelease(KeyEvent.VK_CTRL);
}

If you are using  Implementation-2 for pointing driver then use below line 😛

driver.close();

Conclusion:

Hope you like this blog! Please do like, comment, follow and let me know if there are any other way of implementing this.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s