Skip to content

Instantly share code, notes, and snippets.

@andidev
Last active March 17, 2016 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andidev/6c5dc8033c019e4c069d to your computer and use it in GitHub Desktop.
Save andidev/6c5dc8033c019e4c069d to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname</groupId>
<artifactId>projectname</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.0</version>
</dependency>
</dependencies>
</project>
import java.util.Arrays;
import java.util.Collection;
import static java.util.concurrent.TimeUnit.*;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
@RunWith(Parameterized.class)
public class WebDriverExampleTest {
WebDriver driver;
@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{"Firefox"}, {"Chrome"}, {"InternetExplorer"}
});
}
public WebDriverExampleTest(String browserName) {
if (browserName.equals("Firefox")) {
driver = new FirefoxDriver();
} else if (browserName.equals("Chrome")) {
driver = new ChromeDriver();
} else if (browserName.equals("InternetExplorer")) {
driver = new InternetExplorerDriver();
}
PageFactory.initElements(driver, this);
}
@After
public void tearDown() {
driver.quit();
}
@FindBy(name = "q")
WebElement queryInput;
@FindBy(name = "btnG")
WebElement searchButton;
@FindBy(id = "search")
WebElement searchResult;
@Test
public void searchGoogleForHelloWorldTest() throws InterruptedException {
driver.get("http://www.google.com");
assert driver.getCurrentUrl().contains("google");
queryInput.sendKeys("Hello World");
searchButton.click();
SECONDS.sleep(3);
assert searchResult.getText().contains("Hello World");
}
}
@andidev
Copy link
Author

andidev commented Feb 13, 2015

  1. Download gist
  2. Extract
  3. cd into extracted folder
  4. move WebDriverExampleTest.java into src/test/java (you need to create the folders first since they do not exist)
  5. Download and extract the Chrome Driver and the Internet Explorer Driver (currently you can find the Chrome Driver here and the Internet Explorer Driver here)
  6. Run with mvn test -Dwebdriver.chrome.driver=path/to/chromedriver -Dwebdriver.ie.driver=path/to/iedriver

The test preforms a Google search for "Hello World" and then asserts that the search result contains the text "Hello World"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment