White Box Testing
Test your code
   Home      selenium2WebDriver-Cucumber
 

Selenium Web Driver, Page-factory and Cucumber


Source structure


Maven project dependency

<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.test.bdd</groupId>
<artifactId>cucumber-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<cucumber.version>1.2.2</cucumber.version>
<selenium.version>2.53.0</selenium.version>
<junit.version>4.12</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.53.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.53.0</version>
</dependency>
</dependencies>
</project>
 

Configuration Files

Config file have various configuration you want to provide your test suite for example user credential, browser to test, DB credential etc. Better approach is also to pass them as environment variable and override values provided in config file. Other approach is to tag all env dependencies in config file and pass the tag in execution command. In our example we are just considering through config file only and skipping env dependencies tags
 
Example config file for our test suite example
browser=firefox
domain=http://www.whiteboxtest.com
 

Utility Classes

ConfigParser.java
Used to parse config file and provide utility methods to get all config values
 
DriverFactory.java
Have util methods to setting up web driver depending upon browser provided in config file and other web driver common setup like implicit wait, profile etc.
 
BrowserCommon.java
Have util methods specific to web driver for example get current WebDriver, quit driver and complex util method to take any action on element locators
 
 
 

Page factory Classes

To read more about page factory pattern                         
 
Here we have modified page classes somewhat to not return web driver from any methods
 
 
 

Step definitions

RunCukesTest.java
This class used to run your cucumber test as junit. Use below code as it is
 
package step_definitions;

import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
public class RunCukesTest {
}
 
 
CommonSteps.java
This class have common step definitions used across multiple features
package step_definitions;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

import java.io.File;
import java.util.logging.Logger;

import util.BrowserCommon;
import util.ConfigParser;

public class CommonSteps {
private static final Logger LOGGER = Logger.getLogger(CommonSteps.class.getName());

@Given("^User navigate to (.*)
quot;
)
public void user_navigate_to(String link){
System.out.println(link);
String url = ConfigParser.getDomain() + File.separator + link;
BrowserCommon.loadPage(url);
}

@Then("^Verify User navigated to (.*)
quot;
)
public void user_navigated_to(String expectedTitle){
BrowserCommon.verifyPageTitle(expectedTitle);
}

}
 
SeleniumTest1Steps.java
This class calls page factory init elements for SeleniumTest1 page and have step definitions for SeleniumTest1 page
package step_definitions;

import org.openqa.selenium.support.PageFactory;

import pagefactory.SeleniumTest1;
import pagefactory.SeleniumTest2;
import util.BrowserCommon;
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 SeleniumTest1Steps {

public static SeleniumTest1 seleniumTest1 = null;

public static SeleniumTest1 getSeleniumTest1()
{
if (seleniumTest1 == null)
seleniumTest1 = PageFactory.initElements(BrowserCommon.getCurrentDriver(), SeleniumTest1.class);
return seleniumTest1;
}

@And("^on selenium-test1 User click on Button Present By ID_id012
quot;
)
public void clickButtonPresentByID_id012(){
getSeleniumTest1().clickButtonPresentByID_id012();
}


@And("^click on selenium-test2 link
quot;
)
public void clickSeleniumTest2(){
getSeleniumTest1().clickSeleniumTest2();
}

}
 
SeleniumTest2Steps.java
This class calls page factory init elements for SeleniumTest2 page and have step definitions for SeleniumTest2 page
package step_definitions;

import java.io.File;

import org.openqa.selenium.support.PageFactory;

import pagefactory.SeleniumTest1;
import pagefactory.SeleniumTest2;
import util.BrowserCommon;
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 SeleniumTest2Steps {

public static SeleniumTest2 seleniumTest2 = null;

public static SeleniumTest2 getSeleniumTest2()
{
if (seleniumTest2 == null)
seleniumTest2 = PageFactory.initElements(BrowserCommon.getCurrentDriver(), SeleniumTest2.class);
return seleniumTest2;
}

@And("^input text box (.*)
quot;
)
public void inputTextBox(String txt){
getSeleniumTest2().inputTextBox(txt);
}

@And("^click on selenium-test1 link
quot;
)
public void clickSeleniumTest1(){
getSeleniumTest2().clickSeleniumTest1();
}
}
 
 

Cucumber Feature

Test cases in Cucumber written in gherkin language and test classes should be named as .feature As per Dan North acceptance criteria should be written in terms of scenarios and implemented as classes: 
 
Given [initial context]
when [event occurs]
then [ensure some outcomes]
For all Feature files folder structure should be as same for step definitions
 
Feature file example in our test framework
Feature: Selenium Test
I would like to navigate whiteboxtest selenium test pages

Background: User navigate to whiteboxtest
Given User navigate to selenium-test1.php
Then Verify User navigated to White Box Testing

Scenario: with description
And on selenium-test1 User click on Button Present By ID_id012
And click on selenium-test2 link
Then Verify User navigated to White Box Testing
And input text box hello
And click on selenium-test1 link
Then Verify User navigated to White Box Testing
And on selenium-test1 User click on Button Present By ID_id012
Then Verify User navigated to White Box Testing
Download example source code Selenium Web Driver Page Pattern and Cucumber