Wednesday, September 4, 2013

Selenium : Cover basic Browser opening and using Xpath


Selenium : Cover basic Browser opening and using Xpath 


Good Post From Gautirao's Blog





package my.example.test;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class BasicAppTest {

WebDriver currentBrowser;

@Test
public void runTestForInternetExpolrer()
 {
// Instantiate internet explorer browser instance
this.currentBrowser =new InternetExplorerDriver();
this.clickOnSubmitButton();
}

@Test
public void runTestForFireFox()
 {
this.currentBrowser = new FirefoxDriver();
this.searchSomethingOnGoogle();
}

private void searchSomethingOnGoogle() 
{
 
// this ensure the driver queries dom  for 20 seconds before throwing //NoSuchElementFoundException if an element is not found
this.currentBrowser.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
//Types http://google.co.uk in the address bar and hits enter
this.currentBrowser.get("http://google.co.uk");
//findElement method of the driver takes and By instance . We can search for the element //using By.id, By.xpath,By.name etc
WebElement searchInput = this.currentBrowser.findElement(By.xpath("//input[@name='q']"));
searchInput.sendKeys("Hello World");
WebElement myButton = this.currentBrowser.findElement(By.xpath("//input[@name='btnG' and @value='Google Search']"));
myButton.click();
}

@After
public void tearDown() throws Exception 
{
this.currentBrowser.quit();
}
}

No comments:

Post a Comment