Thursday, December 19, 2013

Running WebDriver test in Google Chrome


Now that Selenium is up and running and we have the Chrome WebDriver the next logical step is to start playing with it.
My first test:
1. Open Chrome Browser
2. Go to http://plus.google.com
3. Log in
4. Search for something
5. Close browser

Code:

package myTestPack;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class signIn {
static Thread th = new Thread();
public static void main(String[] args){
/* Notice location of chromedriver and replace with your own */
System.setProperty("webdriver.chrome.driver", "c:\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://plus.google.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
if (driver.findElement(By.id("Email")).isEnabled()) 
{
System.out.println("Google Email text box is editable");

driver.findElement(By.id("Email")).sendKeys("xxxx@gmail.com"); /* Add valid email Address */
driver.findElement(By.id("Passwd")).sendKeys("xxxxxx"); /*Add valid Password*/
driver.findElement(By.id("signIn")).click();  
System.out.println("Log in Has been Completed Succesfully");

} else {
System.out.println("Google Email text box is not editable");
}

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
if (driver.findElement(By.id("gbqfq")).isEnabled()) /* identify the search bar */
{
System.out.println("Google Search text box is editable");
driver.findElement(By.id("gbqfq")).sendKeys("XXXX"); /* Replace XXXX to search for something */
driver.findElement(By.id("gbqfb")).click();  
System.out.println("Google Search Has been Completed Succesfully");

} else {
System.out.println("Google Search text box is not editable");

}
try {
th.sleep(5000);
}
catch (Exception e){
System.out.println("Error");
}
driver.close();
}

}


Note: To find the id of elements on the website I use the inspect element when right clicking on chrome and then select the magnified glass icon at the bottom left corner.

No comments:

Post a Comment