Sunday, December 22, 2013

Selenium WebDriver and jUnit

Today I decided to play with jUnit and Selenium WebDriver. To do so I used the same "Test" from the previous post 
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.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.chrome.ChromeDriver;

public class jUnitTest {
private WebDriver driverTest;

@Before /* Setting the WebDriver for each Test */
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "c:\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://plus.google.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driverTest = driver;
}

@Test  /*Test Sign In Process */
public void signInTest () throws Exception {
if (driverTest.findElement(By.id("Email")).isEnabled()) /* search bar */
{
System.out.println("Google Email text box is editable");
driverTest.findElement(By.id("Email")).sendKeys("xxxxxxx@gmail.com"); /* Replace with valid Email */
driverTest.findElement(By.id("Passwd")).sendKeys("XXXxxxXXX"); /* Replace with valid Password */
driverTest.findElement(By.id("signIn")).click(); 
System.out.println("Log in Has been Completed Successfully");

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

}

@Test /*Test search  while using signInTest method */
public void searchTest() throws Exception{
signInTest();
driverTest.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
if (driverTest.findElement(By.id("gbqfq")).isEnabled()) /* search bar */
{
System.out.println("Google Search text box is editable");
driverTest.findElement(By.id("gbqfq")).sendKeys("xxxxx"); /* Replace with something to search */
driverTest.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");

}
}
@After /* Closes webDriver after each test */
public void tearDown() throws Exception {
driverTest.close();
}
}

After running the code as a jUnit the feedback is way better.


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.

Installing and Running Selenium WebDriver in Google Chrome


These are the steps I took to install and have Selenium WebDriver running on Google Chrome

In my case I already had eclipse and java up and running on my environment so I'll just focus on what's next.
  • Go to http://docs.seleniumhq.org/download/ and in this case download the WebDriver Java Client  at the moment of writting this post the version was 2.38.0 with release date 20013-12-05
  • Unzip selenium-java-2.38.0.zip to a folder of your choice
  • Download the Selenium server from http://docs.seleniumhq.org/download/ version 2.38 and save it on the same folder from above.
  • Open eclipse and Create a new Java project
  • Create a new package 
  • Create a new class like this

  • Configure the java build path of the project by including the 39(37 from the lib folder + 2) jar files from the WebDriver folder (step 2).
  • Download Chrome WebDriver from http://chromedriver.storage.googleapis.com/index.html I downloaded version 2.8 and move it to the same directory especified above.
  • Start the Selenium Server to use chrome driver with 
c:\Whatever\path\java -jar selenium-server-standalone-2.38.0.jar  -Dwebdriver.chrome.driver=c:\path\to\your\chromedriver.exe

  • Run.  
If everything is running correctly your console in eclipse should show: https://www.google.com