Sample Selenium Test Automation Project with Cucumber
Project Classes And Files:
Cucumber Runner Class
package com.selenium.it.test;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/java/features",
dryRun = false,
monochrome = true,
plugin = {"pretty", "html:target/cucumber-html-report", "json:target/cucumber-json-report.json" },
tags = {"@Web"}
)
public class TestRunner {
public TestRunner() {
}
}
Test Steps Class
package com.selenium.it.test;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
import cucumber.api.PendingException;
import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class WebTest {
WebDriver driver;
@Before
public void set_initials() {
try {
String driverType = "FF";
driver = getDriver(driverType);
} catch (Exception e) {
System.err.println("The error : " + e.getMessage());
Assert.assertTrue("The set_initials is not reachable!", false);
}
}
@After
public void get_initials(Scenario scenario) {
try {
if (scenario.isFailed()) {
System.out.println("The scenario name failed : " + scenario.getName());
}
safeCloseBrowser();
} catch (Exception e) {
System.err.println("The error : " + e.getMessage());
Assert.assertTrue("The get_initials is not reachable!", false);
}
}
@Given("^I am on my blog$")
public void i_am_on_my_blog() {
String url = "https://metinzafer.blogspot.ie/";
try {
driver.get(url);
getScreenShot();
} catch (Exception e) {
System.err.println("The error : " + e.getMessage());
safeCloseBrowser();
Assert.assertTrue("The web page is not reachable!", false);
}
}
@When("^I click on \"([^\"]*)\"$")
public void i_click_on_search(String element) {
try {
System.out.println("element : " + element);
driver.findElement(By.xpath(".//*[@id='BlogSearch1_form']/form/table/tbody/tr/td[1]/input")).click();
getScreenShot();
} catch (Exception e) {
System.err.println(e.getMessage());
safeCloseBrowser();
Assert.assertTrue("The link not reachable!", false);
}
}
@And("^I wrote in search as \"([^\"]*)\"$")
public void i_wrote_in_search_as(String search) {
try {
driver.findElement(By.xpath(".//*[@id='BlogSearch1_form']/form/table/tbody/tr/td[1]/input")).sendKeys(search);
getScreenShot();
} catch (Exception e) {
System.err.println(e.getMessage());
safeCloseBrowser();
Assert.assertTrue("The link not reachable!", false);
}
}
@And("^I click on search button$")
public void i_click_on_search_button() {
try {
driver.findElement(By.cssSelector(".gsc-search-button")).click();
getScreenShot();
} catch (Exception e) {
System.err.println(e.getMessage());
safeCloseBrowser();
Assert.assertTrue("The link not reachable!", false);
}
}
@Then("^I see the search results$")
public void i_see_the_search_results() {
try {
String msg = driver.findElement(By.xpath(".//*[@id='Blog1']/div[1]/div[1]/div[1]")).getText();
System.out.println("msg : " + msg);
if(!msg.equals("Showing posts sorted by relevance for query spring. Sort by date Show all posts")){
Assert.assertTrue("i_see_the_search_results has failed!", false);
}
getScreenShot();
Thread.sleep(2000);
} catch (Exception e) {
System.err.println(e.getMessage());
safeCloseBrowser();
Assert.assertTrue("Setting the start date is not reachable!", false);
}
}
/*
@Then("^I populate the contact form$")
public void i_populate_the_contact_form() throws Throwable {
try {
driver.findElement(By.name("name_field")).sendKeys("name test");
getScreenShot();
driver.findElement(By.id("rdona")).click();
getScreenShot();
driver.findElement(By.id("cadop")).click();
} catch (Exception e) {
safeCloseBrowser();
System.err.println(e.getMessage());
Assert.assertTrue("Setting the start date is not reachable!", false);
}
}
*/
@Then("^I close the browser$")
public void i_close_the_browser() {
try {
safeCloseBrowser();
} catch (Exception e) {
System.err.println(e.getMessage());
Assert.assertTrue("closing the browser is not reachable!", false);
}
}
public static WebDriver getDriver(String browser) {
File file = null;
WebDriver driver = null;
if (browser.equals("CHROME")) {
file = new File("src/test/java/selenium/chromedriver.exe");
System.setProperty("webdriver.chrome.driver",
file.getAbsolutePath());
driver = new ChromeDriver();
} else if (browser.equals("IE")) {
file = new File("src/test/java/selenium/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver = new InternetExplorerDriver();
} else if (browser.equals("FF")) {
file = new File("src/test/java/selenium/geckodriver.exe");
System.setProperty("webdriver.gecko.driver", file.getAbsolutePath());
driver = new FirefoxDriver();
}
return driver;
}
private void safeCloseBrowser(){
if(driver!=null){
driver.quit();
}
}
private void getScreenShot() throws IOException, InterruptedException{
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String fileLocation = "d:/tmp/";
FileUtils.copyFile(screenshot, new File(fileLocation + "screenshot_" + getCurrentDate() + ".png"));
Thread.sleep(2000);
}
private String getCurrentDate(){
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
Date date = new Date();
return dateFormat.format(date).toString();
}
}
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
import cucumber.api.PendingException;
import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class WebTest {
WebDriver driver;
@Before
public void set_initials() {
try {
String driverType = "FF";
driver = getDriver(driverType);
} catch (Exception e) {
System.err.println("The error : " + e.getMessage());
Assert.assertTrue("The set_initials is not reachable!", false);
}
}
@After
public void get_initials(Scenario scenario) {
try {
if (scenario.isFailed()) {
System.out.println("The scenario name failed : " + scenario.getName());
}
safeCloseBrowser();
} catch (Exception e) {
System.err.println("The error : " + e.getMessage());
Assert.assertTrue("The get_initials is not reachable!", false);
}
}
@Given("^I am on my blog$")
public void i_am_on_my_blog() {
String url = "https://metinzafer.blogspot.ie/";
try {
driver.get(url);
getScreenShot();
} catch (Exception e) {
System.err.println("The error : " + e.getMessage());
safeCloseBrowser();
Assert.assertTrue("The web page is not reachable!", false);
}
}
@When("^I click on \"([^\"]*)\"$")
public void i_click_on_search(String element) {
try {
System.out.println("element : " + element);
driver.findElement(By.xpath(".//*[@id='BlogSearch1_form']/form/table/tbody/tr/td[1]/input")).click();
getScreenShot();
} catch (Exception e) {
System.err.println(e.getMessage());
safeCloseBrowser();
Assert.assertTrue("The link not reachable!", false);
}
}
@And("^I wrote in search as \"([^\"]*)\"$")
public void i_wrote_in_search_as(String search) {
try {
driver.findElement(By.xpath(".//*[@id='BlogSearch1_form']/form/table/tbody/tr/td[1]/input")).sendKeys(search);
getScreenShot();
} catch (Exception e) {
System.err.println(e.getMessage());
safeCloseBrowser();
Assert.assertTrue("The link not reachable!", false);
}
}
@And("^I click on search button$")
public void i_click_on_search_button() {
try {
driver.findElement(By.cssSelector(".gsc-search-button")).click();
getScreenShot();
} catch (Exception e) {
System.err.println(e.getMessage());
safeCloseBrowser();
Assert.assertTrue("The link not reachable!", false);
}
}
@Then("^I see the search results$")
public void i_see_the_search_results() {
try {
String msg = driver.findElement(By.xpath(".//*[@id='Blog1']/div[1]/div[1]/div[1]")).getText();
System.out.println("msg : " + msg);
if(!msg.equals("Showing posts sorted by relevance for query spring. Sort by date Show all posts")){
Assert.assertTrue("i_see_the_search_results has failed!", false);
}
getScreenShot();
Thread.sleep(2000);
} catch (Exception e) {
System.err.println(e.getMessage());
safeCloseBrowser();
Assert.assertTrue("Setting the start date is not reachable!", false);
}
}
/*
@Then("^I populate the contact form$")
public void i_populate_the_contact_form() throws Throwable {
try {
driver.findElement(By.name("name_field")).sendKeys("name test");
getScreenShot();
driver.findElement(By.id("rdona")).click();
getScreenShot();
driver.findElement(By.id("cadop")).click();
} catch (Exception e) {
safeCloseBrowser();
System.err.println(e.getMessage());
Assert.assertTrue("Setting the start date is not reachable!", false);
}
}
*/
@Then("^I close the browser$")
public void i_close_the_browser() {
try {
safeCloseBrowser();
} catch (Exception e) {
System.err.println(e.getMessage());
Assert.assertTrue("closing the browser is not reachable!", false);
}
}
public static WebDriver getDriver(String browser) {
File file = null;
WebDriver driver = null;
if (browser.equals("CHROME")) {
file = new File("src/test/java/selenium/chromedriver.exe");
System.setProperty("webdriver.chrome.driver",
file.getAbsolutePath());
driver = new ChromeDriver();
} else if (browser.equals("IE")) {
file = new File("src/test/java/selenium/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver = new InternetExplorerDriver();
} else if (browser.equals("FF")) {
file = new File("src/test/java/selenium/geckodriver.exe");
System.setProperty("webdriver.gecko.driver", file.getAbsolutePath());
driver = new FirefoxDriver();
}
return driver;
}
private void safeCloseBrowser(){
if(driver!=null){
driver.quit();
}
}
private void getScreenShot() throws IOException, InterruptedException{
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String fileLocation = "d:/tmp/";
FileUtils.copyFile(screenshot, new File(fileLocation + "screenshot_" + getCurrentDate() + ".png"));
Thread.sleep(2000);
}
private String getCurrentDate(){
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
Date date = new Date();
return dateFormat.format(date).toString();
}
}
Feature File (Cucumber Test Steps File with BDD) : webPage.feature
Feature: WebPageTest
This feature deals with the web site test
@Web
Scenario: web login page test
Given I am on my blog
When I click on "search"
And I wrote in search as "spring"
And I click on search button
Then I see the search results
Then I close the browser
POM File (Maven)
<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.selenium.it</groupId>
<artifactId>SeleniumTutorial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SeleniumTutorialWorkShop</name>
<description>SeleniumTutorial</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>1.5.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>io.github.seleniumquery</groupId>
<artifactId>seleniumquery</artifactId>
<version>0.18.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-picocontainer -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/net.masterthought/cucumber-reporting -->
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>3.13.0</version>
</dependency>
</dependencies>
</project>
**You need to use FireFox browser Ver.54.0.1 which is suitable with guava Ver.21.0!
Project Structure
Creation Of Basic Project (Eclipse)
File --> Maven Project
** You need to add "geckodriver.exe" for firefox browser.
Comments
Post a Comment