Pages

Showing posts with label SwitchTo. Show all posts
Showing posts with label SwitchTo. Show all posts

Sunday, 26 February 2012

How can I switch WebDriver control between Frames?

If the application under test contains frames, then we need to switch the WebDriver control to that frame before interacting with the element inside the frame.


We can switch to frame either with it’s name/id or by specifying frame index.


Sample code : By using frame name


driver.switchTo().frame("frameName");

By index (Zero-based):


driver.switchTo().frame(1);

And to come back to default frame:

driver.switchTo().defaultContent();

Sunday, 19 February 2012

How can I switch WebDriver control to new window?

To switch latest window opened use below code:

for(String winHandle : driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
}

Sampe code:


 package test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SwithcToNewWindow {

public static void main(String[] arg) {

// Instantiate the FirefoxDriver
WebDriver driver = new FirefoxDriver();

// Visit the Selenium-WebDriver FAQ site
driver.get("http://seleniumwebdriverfaq.tumblr.com/");

// Print the title of the page - It should print
// "Selenium-WebDriver FAQ's"
System.out.println("Title of the page before - switchingTo: " + driver.getTitle());

//Now click on RSS button; it will open in new window
driver.findElement(By.xpath("//a[text()='RSS']")).click();

//Switch to newly opened window -RSS and get the page titele
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
System.out.println("Title of the page after - switchingTo: " + driver.getTitle());

// Close the browser window
driver.close();

// Quit the driver
driver.quit();
}
}