Like us on Facebook

LightBlog

December 31, 2012

Switch between Frames using webdriver

December 31, 2012 18
Switch between Frames using webdriver
Here is the simple example how to switch between frames.


There are three ways to switch between frames.

INDEX:
Frame(int index)
Select a frame by its (zero-based) index. That is, if a page has three frames, the first frame would be at index "0", the second at index "1" and the third at index "2". Once the frame has been selected, all subsequent calls on the WebDriver interface are made to that frame.

ID OR NAME:
 frame(java.lang.String nameOrId)
Select a frame by its name or ID. Frames located by matching name attributes are always given precedence over those matched by ID.
Frame(WebElement frameElement)
Select a frame using its previously located webelement
Parameters: frameElement - The frame element to switch to.
DEFAULTCONTENT()
Selects either the first frame on the page, or the main document when a page contains iframes.
Below example explain you how to switch between frames.
=============================================================
package testng; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class Frames { public WebDriver driver; @BeforeClass public void beforeClass() { driver=new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); } @Test public void testFrames() throws Exception { driver.navigate().to("http://jqueryui.com/"); driver.findElement(By.linkText("Autocomplete")).click(); //index // driver.switchTo().frame(0); //id or Name //in this example Frame is not having ID or Name so this is invalid here //driver.switchTo().frame("nameorid"); //frameElement driver.switchTo().frame(driver.findElement(By.className("demo-frame"))); //type in text box.. driver.findElement(By.id("tags")).sendKeys("java"); Thread.sleep(5000); //switch back to default content driver.switchTo().defaultContent(); driver.findElement(By.linkText("Accordion")).click(); Thread.sleep(5000); } @AfterClass public void afterClass() { driver.quit(); } }

December 17, 2012

Run Webdriver script in Google Chrome

December 17, 2012 2
This post explains you how to run your webdriver script in google chrome..

Firefox Browser:
driver=new FirefoxDriver();  --it will work and will launch your firefox browser,

Google Chrome:

driver= new Chromedriver() --- it will throw an error.

Error:


FAILED CONFIGURATION: @BeforeClass beforeClass
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list

To over come this we need to download the chrome driver.exe and we have to specify the apth of chrome driver in the script

Download path:
http://code.google.com/p/chromedriver/downloads/list

take the latest exe file. see the below image




save the chrome driver in a folder and you need to specify the path of driver in your webdriver script.

Below is the Sample code:


package testng;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class ChromedriverTest {

public WebDriver driver;
  @BeforeClass
  public void beforeClass() {
// Create chrome driver instance...
 System.setProperty("webdriver.chrome.driver", "C:\\xxx\\Jar\\chromedriver.exe");
 driver=new ChromeDriver();
  }


  @Test
  public void testChromedriverTest() throws Exception {
 driver.navigate().to("http://bing.com");
 Thread.sleep(5000);
  }

  @AfterClass
  public void afterClass() {
 driver.quit();
  }

}


Use the above code your script will run in google chrome.