Let's deal with toggling between tabs .Here there will be only one window handle for any number of tabs that get opened in that session, unlike the multiple window handles that are present when number of windows are opened.
Here's the sample program,
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SwitchTabsDemo {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
// This page will be tab1 in this scenario
driver.navigate()
.to("http://www.toolsqa.com/automation-practice-form/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
// Finding username webelement in tab1
WebElement we = driver.findElement(By.name("firstname"));
// Both tabs will have the same window handle in this case
String han = driver.getWindowHandle();
// To open a new tab
we.sendKeys(Keys.chord(Keys.CONTROL, "t"));
// Launch another url in the new tab, tab2
driver.navigate().to("http://www.google.com");
// Locating the search box
WebElement we2 = driver.findElement(By.id("gbqfq"));
// Sending text to search box element
we2.sendKeys("sachin");
// Shifting control back to tab1
we2.sendKeys(Keys.chord(Keys.CONTROL, "\t"));
// Switching to first tab using handle
driver.switchTo().window(han);
// Sending text to user name text box in tab1
we.sendKeys("admin");
Thread.sleep(4000l);
// Shifting control to tab2
we.sendKeys(Keys.chord(Keys.CONTROL, "\t"));
// Switching to tab2 using handle
driver.switchTo().window(han);
// Both driver.close() and driver.quit() will just do the same task,
// driver instance gets killed
driver.quit();
}
}
Pay It Forward
DR
No comments:
Post a Comment