Sunday, April 5, 2015

First steps with Selenium WebDriver with Eclipse, Maven and JUnit

Environment
– OS X Yosemite
– We will use ChromeWebDriver as an example here to test against Chrome browser
– We will be running from one local machine with Chrome installed
– Eclipse Luna
– Java 1.7.0_60
Steps
– Open up Eclipse Luna and create a new Maven Project
– Open up the pom.xml file, and add the following dependency:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.45.0</version>
</dependency>

– Ensure ChromeDriver is already installed on your OS
– Create a new JUnit Test Case class
package com.cherryshoe.selenium;
import static org.junit.Assert.assertNotNull;
import org.junit.Before;
import org.junit.Test;
import org.junit.After;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstTest
{
private WebDriver driver;
@Before
public void setUp() throws Exception {
// chromedriver path is in the $PATH already
driver = new ChromeDriver();
}
@Test
public void firstTest() throws Exception {
driver.get(“http://www.google.com”);
// sleep to let DOM load
Thread.sleep(5000);
// find the search box by name
WebElement searchTextBox = driver.findElement(By.name(“q”));
assertNotNull(searchTextBox);
// enter in a search term and see results
searchTextBox.sendKeys(“cherryshoe selenium”);
searchTextBox.submit();
Thread.sleep(5000);
}
@After
public void tearDown() throws Exception {
System.out.println(“Tearing down”);
// close driver (and browser)
driver.quit();
}
}

No comments:

Post a Comment

I appreciate your time in leaving a comment!