Cucumber Project
Cucumber Project Maven Structure:
-----------------------------------------------------------------------------------------------------------------------
src/test/java
Pom.xml:
<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>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<suite>config/testng.xml</suite>
<maven.build.timestamp.format>yyyyMMdd-HHmm</maven.build.timestamp.format>
<reports.sub.directory>${maven.build.timestamp}</reports.sub.directory>
<reports.directory>reports/${reports.sub.directory}</reports.directory>
<selenium.version>2.53.1</selenium.version>
<java.source.version>1.7</java.source.version>
<java.target.version>1.7</java.target.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${suite}</suiteXmlFile>
</suiteXmlFiles>
<properties>
<property>
<name>usedefaultlisteners</name>
<value>false</value>
</property>
<property>
<name>listener</name>
<value>org.uncommons.reportng.HTMLReporter,
org.uncommons.reportng.JUnitXMLReporter
</value>
</property>
</properties>
<systemPropertyVariables>
<org.uncommons.reportng.escape-output>false</org.uncommons.reportng.escape-output>
<reports.directory>${reports.directory}</reports.directory>
</systemPropertyVariables>
<reportsDirectory>${reports.directory}</reportsDirectory>
</configuration>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<configuration>
<filesets>
<fileset>
<directory>test-output</directory>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.source.version}</source>
<target>${java.target.version}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.4</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.uncommons</groupId>
<artifactId>reportng</artifactId>
<version>1.1.4</version>
<!-- <exclusions>
<exclusion>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</exclusion>
</exclusions> -->
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-testng -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.1.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
<type>jar</type>
<version>15.0</version>
</dependency>
</dependencies>
</project>
-----------------------------------------------------------------------------------------------------------------------
browser=firefox
domain=http://www.whiteboxtest.com
-----------------------------------------------------------------------------------------------------------------------
pagefactory : SeleniumTest1:-
package pagefactory;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.*;
public class SeleniumTest1 {
WebDriver driver;
@FindBy(how = How.ID, using = "id-012")
WebElement verifybutton;
public SeleniumTest1(WebDriver driver) {
this.driver = driver;
}
public void clickButtonPresentByID_id012() {
verifybutton.click();
}
// Find and click selenium-test2
public void clickSeleniumTest2() {
// Find the text link by its name
WebElement someElement = driver.findElement(By.linkText("selenium-test2"));
// Click the link text
someElement.click();
}
// Find and click selenium-test3
}
SeleniumTest2 :-
package pagefactory;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.*;
public class SeleniumTest2 {
WebDriver driver;
@FindBy(how = How.ID, using = "q2")
WebElement textBox;
public SeleniumTest2(WebDriver driver) {
this.driver = driver;
}
// Find the text box by ID
public void inputTextBox(String txt) {
textBox.clear();
textBox.sendKeys(txt);
}
// Find and click selenium-test1
public void clickSeleniumTest1() {
// Find the text link by its name
WebElement someElement = driver.findElement(By.linkText("selenium-test1"));
// Click the link text
someElement.click();
}
// Find and click selenium-test3
}
-----------------------------------------------------------------------------------------------------------------------
step_definitions : CommonSteps :-
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 (.*)$")
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 (.*)$")
public void user_navigated_to(String expectedTitle){
BrowserCommon.verifyPageTitle(expectedTitle);
}
}
RunCukesTest :-
package step_definitions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@Cucumber.Options(features = "classpath:step_definitions",
tags = {})
public class RunCukesTest {
}
SeleniumTest1Steps :-
package step_definitions;
import org.openqa.selenium.support.PageFactory;
import pagefactory.SeleniumTest1;
import util.BrowserCommon;
import cucumber.api.java.en.And;
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$")
public void clickButtonPresentByID_id012(){
getSeleniumTest1().clickButtonPresentByID_id012();
}
@And("^click on selenium-test2 link$")
public void clickSeleniumTest2(){
getSeleniumTest1().clickSeleniumTest2();
}
}
SeleniumTest2Steps :
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 (.*)$")
public void inputTextBox(String txt){
getSeleniumTest2().inputTextBox(txt);
}
@And("^click on selenium-test1 link$")
public void clickSeleniumTest1(){
getSeleniumTest2().clickSeleniumTest1();
}
}
-----------------------------------------------------------------------------------------------------------------------
util : BrowserCommon
package util;
import java.util.List;
import java.util.logging.Logger;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.UnreachableBrowserException;
import org.openqa.selenium.support.ui.Select;
public class BrowserCommon {
private static final Logger LOGGER = Logger.getLogger(BrowserCommon.class.getName());
private static WebDriver currentDriver;
public synchronized static WebDriver getCurrentDriver() {
if (currentDriver==null) {
try {
currentDriver = DriverFactory.getWebDriver();
} catch (Exception e) {
LOGGER.info("Exeption in current web driver" + e);
}
}
return currentDriver;
}
public static void close() {
try {
getCurrentDriver().quit();
currentDriver = null;
LOGGER.info("closing browser");
} catch (UnreachableBrowserException e) {
LOGGER.info("Exception in closing browser" + e);
}
}
private static class BrowserCleanup implements Runnable {
public void run() {
close();
}
}
public static void loadPage(String url){
getCurrentDriver();
LOGGER.info("Directing browser to:" + url);
LOGGER.info("try to loadPage [" + url + "]");
getCurrentDriver().get(url);
}
public static void reopenAndLoadPage(String url) {
currentDriver = null;
getCurrentDriver();
loadPage(url);
}
public static void verifyPageTitle(String expectedTitle){
// you may Check title of the page and assert if page title is incorrect
LOGGER.info("Page title is: " + getCurrentDriver().getTitle());
Assert.assertEquals(getCurrentDriver().getTitle(), expectedTitle);
}
/* public static WebElement waitForElement(WebElement elementToWaitFor){
return waitForElement(elementToWaitFor, null);
}*/
/* public static WebElement waitForElement(WebElement elementToWaitFor, Integer waitTimeInSeconds) {
if (waitTimeInSeconds == null) {
waitTimeInSeconds = 10;
}
WebDriverWait wait = new WebDriverWait(getCurrentDriver(), waitTimeInSeconds);
return wait.until(ExpectedConditions.visibilityOf(elementToWaitFor));
}*/
public static WebElement getParent(WebElement element) {
return element.findElement(By.xpath(".."));
}
public static List<WebElement> getDropDownOptions(WebElement webElement) {
Select select = new Select(webElement);
return select.getOptions();
}
public static WebElement getDropDownOption(WebElement webElement, String value) {
WebElement option = null;
List<WebElement> options = getDropDownOptions(webElement);
for (WebElement element : options) {
if (element.getAttribute("value").equalsIgnoreCase(value)) {
option = element;
break;
}
}
return option;
}
}
ConfigParser :
package util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Logger;
public class ConfigParser {
public static String browser;
public static String domain;
private static final Logger LOGGER = Logger.getLogger(ConfigParser.class.getName());
public static void readConfig(){
String current;
try {
current = new java.io.File( "." ).getCanonicalPath();
System.out.println("Current dir:"+current);
String currentDir = System.getProperty("user.dir");
System.out.println("Current dir using System:" +currentDir);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Properties prop = new Properties();
String propFileName = "src/test/java/config/config.properties";
InputStream inputStream = null;
try {
inputStream = new FileInputStream(propFileName);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
prop.load(inputStream);
} catch (IOException e) {
LOGGER.info("property file '" + propFileName + "' not found in the classpath");
}
browser = prop.getProperty("browser");
domain = prop.getProperty("domain");
}
public static String getDomain(){
if(domain == null){
readConfig();
}
return domain;
}
public static String getBrowser(){
if(browser == null){
readConfig();
}
return browser;
}
}
DriverFactory :
package util;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.safari.SafariDriver;
public class DriverFactory {
public static WebDriver driver = null;
private static final Logger LOGGER = Logger.getLogger(ConfigParser.class.getName());
public static void setupDriver(){
if (driver == null) {
String browser = ConfigParser.getBrowser();
if (browser.equalsIgnoreCase("safari"))
{
driver = new SafariDriver();
}
else if (browser.equalsIgnoreCase("chrome"))
{
System.setProperty("webdriver.chrome.driver", "E:\\ProjectCumumber\\cucumber-test\\chromedriver.exe");
driver = new ChromeDriver();
}
else
{
System.out.println("Using default browser as firefox");
driver = new FirefoxDriver();
}
setupBrowser(driver);
}
}
private static FirefoxProfile getFirefoxProfile(){
FirefoxProfile firefoxProfile = new FirefoxProfile();
try {
firefoxProfile.addExtension(new File("firebug/firebug-1.9.2.xpi"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.9.2");
firefoxProfile.setPreference("extensions.firebug.script.enableSites", true);
firefoxProfile.setPreference("extensions.firebug.console.enableSites", true);
firefoxProfile.setPreference("extensions.firebug.allPagesActivation", true);
firefoxProfile.setPreference("extensions.firebug.delayLoad", false);
return firefoxProfile;
}
private static void setupBrowser(WebDriver driver) {
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
public static WebDriver getWebDriver(){
if(driver == null){
setupDriver();
}
return driver;
}
}
-----------------------------------------------------------------------------------------------------------------------
src/test/resources
step_definitions : test.feature
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
-----------------------------------------------------------------------------------------------------------------------
Add chromeDriver.exe ,SafariDriver.exe and FireFoxDriver.exe in your project and change the path where driver is instantiated.
In our project its instantiated in : DriverFactory.class
No comments:
Post a Comment