Saturday, January 27, 2018

Selenium + Cucumber Integration


Cucumber is nice way of achieving automation. Below is a sample automation steps on logging into Facebook app & logging out of the application itself.

Note: Below sample includes :
            Selenium and Cucumber integration with passing parameters
            Data driven testing sample
            All reports generation for Cucumber
       

You have to have following Jar list :

cucumber-reporting-0.1.14-sources.jar
junit-4.12-sources.jar
cobertura-2.1.1.jar
cucumber-jvm-deps-1.0.3.jar
cucumber-core-1.2.2.jar
cucumber-junit-1.2.2.jar
cucumber-java-1.2.2.jar
cucumber-html-0.2.2-sources.jar

Data driven testing sample
===================

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
// glue - here you give the full qualified package name
@CucumberOptions(features="features",
glue="feature.description",
plugin={"html:target/cucumber-html-report",
"json:target/cucumber.json",
"pretty:target/cucumber-pretty.txt",
"usage:target/cucumber-usage.json",
"junit:target/cucumber-results.xml"})
public class TestRunner {

}


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class SampleTest {

    private WebDriver driver;

@Given("^Open \"([^\"]*)\" and the \"([^\"]*)\" start \"([^\"]*)\" application$")
public void Open_chrome_and_start_application(String driverName ,String driverLocation, String applicationName) throws Throwable {

    System.setProperty(driverName, driverLocation);

            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments("--disable-notifications");

    driver = new ChromeDriver(chromeOptions);

    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
         
    driver.manage().window().maximize();
            driver.get(applicationName);
}

@When("^Enter valid \"([^\"]*)\" and valid \"([^\"]*)\"$")
public void Enter_valid_username_and_valid_password(String userName, String password) throws Throwable {
    driver.findElement(By.id("email")).sendKeys(userName);
    driver.findElement(By.id("pass")).sendKeys(password);     
}

@Then("user should be able to login successfully")
public void user_should_be_able_to_login_successfully() throws Throwable {
    driver.findElement(By.id("loginbutton")).click();
}

@Then("user should be able to logout successfully")
public void user_should_be_able_to_logout_successfully() throws Throwable {
             driver.findElement(By.id("userNavigationLabel")).click();
     driver.findElement(By.linkText("Log Out")).click();
}
}

Feature: Test Facebook smoke scenario

    Scenario Outline: Test login with valid credentials
    Given Open "webdriver.chrome.driver" and the "C:\\xyz\\chromedriver.exe" start "http://www.facebook.com" application
    When Enter valid "<userName>" and valid "<password>"
    Then user should be able to login successfully
    And user should be able to logout successfully

    Examples: 
      | userName                 | password  |
      | abc_efgh@yahoo.com | xxxx |
      |xyz@yahoo.com | yyyy



No comments:

Post a Comment