Behavior Driven Testing with Selenium and Cucumber


Behavior Driven  Testing with Selenium and Cucumber




Behavior driven development is a way of describing what a system should do based on the behavior of the system in standard English that is not technical. 

In this post we will look at how we can use Behavior driven development with Selenium and Cucumber based on a Maven Project. For our scenario we will look at login into a sample website hosted online from Guru99. Ex:https://demo.guru99.com/v4/

Before we run this scenario we need to make sure that the IDE we are using (Eclipse) has the required software's installed. For this scenario we need to install Natural 0.7.6.




Once this is installed and the IDE is restarted(File >Restart), create a maven project and then open the pom.xml file of the project and include the below two dependencies. Note: it is assumed that the other selenium and Chrome dependencies are already added to the pom.xml

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>1.2.5</version>
</dependency>
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.9.10</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>1.2.4</version>
</dependency>
</dependencies>



Next, under the src>main>java folder path create a new folder and name it "features" within this folder, we will maintain all the required feature files. So lets create a feature file as below:

Feature: Login to Guru99
  Scenario: User Login to Guru99

    Given User goes to Guru99 Login

    When User enter 'user credentials' and click 'login' button


    Then User should get the 'Home' page





Note: if we don't give the extension as .feature then Cucumber will ignore this file.

Next, create a package called runner and and create a java class called TestRunner. This class will tell which  BDD feature should be invoked based on which step definition. The keyword 'glue' mentions where the actual test code resides.

package Runner;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;

@CucumberOptions(
   
        glue = {"stepdefs"},//Your step definitions package.
        features = {"/home/shavantha/eclipse-workspace/exercise17_new_customer_bdd/src/test/java/features/Guru99Login.feature"})

public class TestRunner extends AbstractTestNGCucumberTests {

}


Once the TestRunner is created we invoke this class. At this stage if our test class is not created the TestRunner class will provide us with 3 methods for each of the Given When and Then statements as shown below.

@Given("^User goes to Guru(\\d+) Login$")
public void user_goes_to_Guru_Login(int arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
 

}

@When("^User enter 'user credentials' and click 'login' button$")
public void user_enter_user_credentials_and_click_login_button() throws Throwable {
    // Write code here that turns the phrase above into concrete actions

}

@Then("^User should get the 'Home' page$")
public void user_should_get_the_Home_page() throws Throwable {


}



So our test class will look like as below with the sample test methods we had obtained. Hear based on the scenario we had included the necessary selenium codes that will allow us to login to the sample web application.

package stepdefs;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;

import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import pagefactory.HomePage;
import pagefactory.Login;
import read.ReadExcel;

public class UserLogin {
WebDriver driver;
ReadExcel excel=new ReadExcel();   
Login objLogin;
HomePage objHomePage;

@Given("^User goes to Guru(\\d+) Login$")
public void user_goes_to_Guru_Login(int arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
     System.setProperty("webdriver.chrome.driver", "<<path to Chrome driver>>/chromedriver");
     driver = new ChromeDriver();
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     driver.get(excel.readExcel(1,0, "/home/shavantha/eclipse-workspace/exercise17_new_customer_bdd/data/bddexample.xlsx", "Sheet1"));


}

@When("^User enter 'user credentials' and click 'login' button$")
public void user_enter_user_credentials_and_click_login_button() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    objLogin = new Login(driver);  
   
    objLogin.setUserName(excel.readExcel(1,1, "/home/shavantha/eclipse-workspace/exercise17_new_customer_bdd/data/bddexample.xlsx","Sheet1"));
    objLogin.setPassword(excel.readExcel(1,2, "/home/shavantha/eclipse-workspace/exercise17_new_customer_bdd/data/bddexample.xlsx","Sheet1"));
    objLogin.clickLogin();   

    throw new PendingException();
}


@Then("^User should get the 'Home' page$")
public void user_should_get_the_Home_page() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    objHomePage = new HomePage(driver);
    objHomePage.getHomePageDashboardUserName();

   // throw new PendingException();
}

}


Finally we access the testRunner class and execute the TestNG to obtain the required outputs as below:

[RemoteTestNG] detected TestNG version 6.14.2
Starting ChromeDriver 2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706) on port 12594
Only local connections are allowed.
Aug 13, 2018 8:01:52 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS

1 Scenarios ( [33m1 pending [0m)
3 Steps ( [36m1 skipped [0m, [33m1 pending [0m, [32m1 passed [0m)
0m13.395s

cucumber.api.PendingException: TODO: implement me
    at stepdefs.UserLogin.user_enter_user_credentials_and_click_login_button(UserLogin.java:40)
    at ✽.When User enter 'user credentials' and click 'login' button(/home/shavantha/eclipse-workspace/exercise_login/src/test/java/features/Guru99Login.feature:6)

PASSED: feature(Login to Guru99)
        Runs Cucumber Feature



To summarize the discussion lets look at some of the important components

1.The maven dependencies  info.cukes for cucumber-java and cucumber-testng

2.The feature files in which we write the Gherkin code which resides inside a features folder.

3.testRunner class to help invoke the feature files.

4.The test class which has the selenium scripts.

 

Comments