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.


No comments:

Post a Comment