Earlier we have learnt about how to switching between windows In selenium webdriver as described on THIS PAGE. and work with multiple IFrames In THIS POST. As all of us are used to work with multuple tabs, some peoples don't like to work with multiple windows. So main question Is -> How to open new tab In selenium webdriver test and then how to switch between two tabs and take required actions. Also you can close all tabs using Robot class as described In THIS POST.
How to open new tab
WebDriver do not have any built In method using which we can open new tab. Normally we are using CTRL + t Keys to open new tab In Browser. We can do same thing In webdriver test for opening new tab In selenium webdriver, Bellow given syntat will open new tab In your driver browser Instance.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
How to switch between two tabs
For switching between tabs of browser, We are using CTRL + Tab keys. Same way, bellow given syntax will switch between tabs and select the content of selected tab.
//Switching between tabs using CTRL + tab keys.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
//Switch to current selected tab's content.
driver.switchTo().defaultContent();
I have created simple example on tab switching for your better understanding. It will Open new tab and then switch between them to perform different actions on both tab's pages.
package Testing_Pack;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Tabs {
WebDriver driver;
@BeforeTest
public void setup() throws Exception {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("http://only-testing-blog.blogspot.in/2014/04/calc.html");
}
@Test
public void openTab() {
//Open tab 2 using CTRL + t keys.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
//Open URL In 2nd tab.
driver.get("http://only-testing-blog.blogspot.in/2014/05/form.html");
//Call switchToTab() function to switch to 1st tab
switchToTab();
//perform required actions on tab 1.
driver.findElement(By.xpath("//input[@id='6']")).click();
driver.findElement(By.xpath("//input[@id='plus']"));
driver.findElement(By.xpath("//input[@id='3']"));
driver.findElement(By.xpath("//input[@id='equals']"));
//Call switchToTab() function to switch to 2nd tab.
switchToTab();
//perform required actions on tab 2.
driver.findElement(By.xpath("//input[@name='FirstName']")).sendKeys("hi");
driver.findElement(By.xpath("//input[@name='LastName']")).sendKeys("test");
//Call switchToTab() function to switch to 1st tab
switchToTab();
//perform required actions on tab 1.
String str = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
System.out.println("Sum result Is -> "+str);
}
public void switchToTab() {
//Switching between tabs using CTRL + tab keys.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
//Switch to current selected tab's content.
driver.switchTo().defaultContent();
}
}
This way, We can use keyboard actions to switch between tabs In selenium webdriver.
No comments:
Post a Comment