Use of parameters to run our Cucumber test


Use of parameters to run our Cucumber test


Hi, all welcome to another of my lockdown blog series posts.  When testing UI most of the time we need to test with multiple data sets. In this blog post, we will see how we can achieve this when we are writing behavior-driven development using Cucumber using Scenario Outline.

For this post, I will be using a dummy web application that mocks the behavior of an online banking application called parabank [1]. In our scenario, we will be writing a selenium web driver code that will pass the user name and password to login to the banking application.

As the first step, we need to create a maven project and add the below dependencies to the pom.xml. To try out this scenario make sure that your pom.xml has the below-listed dependencies.


   <dependencies>
      <dependency>
         <groupId>org.testng</groupId>
         <artifactId>testng</artifactId>
         <version>7.1.0</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-java</artifactId>
         <version>5.6.0</version>
      </dependency>
      <dependency>
         <groupId>io.cucumber</groupId>
         <artifactId>cucumber-testng</artifactId>
         <version>5.6.0</version>
      </dependency>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>3.141.59</version>
      </dependency>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-server</artifactId>
         <version>3.141.59</version>
      </dependency>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-firefox-driver</artifactId>
         <version>3.141.59</version>
      </dependency>
   </dependencies>
  
Next, we need to create our feature file as shown below the feature covers the basic login 
functionality 

Feature: Login functionality feature

  In order to do internet banking
  As a valid para bank customer
  I want to login successfully

Scenario Outline: login successfully

  Given I am in the login page of the para bank application
  When I enter valid <username> and <password>
  Then I should be taken to the overview page

Examples:  |username|password| 
           |"tester123"|"tester123"|
 
As we can see from the above code, within When key word where we are specifying our action
we are telling to call the user name and password as parameters on the when statement 
 
When passing parameters we use Scenario Outline instead of Scenario and Cucumber mandates that
we call the Examples keyword with the required parameter list.
 
When we execute the feature file, Cucumber generates the missing step defenisions. So we need 
to add the Selenium code as below to achieve the test requirements.
 
 
package patasoft;
import io.cucumber.java.en.Given; 
import io.cucumber.java.en.When; 
import io.cucumber.java.en.Then; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait; 
 
public class Steps {

    private WebDriver driver;   
    private WebDriverWait wait;

    @Given("I am in the login page of the para bank application")
    public void i_am_in_the_login_page_of_the_para_bank_application() {


        System.setProperty("webdriver.firefox.driver","/Users/shavanthaweerasinghe/projects/training/training\n"); 
        driver=new FirefoxDriver(); 
        driver.get("https://parabank.parasoft.com/parabank/index.htm"); 
    }

    @When("I enter valid {string} and {string}")
    public void i_enter_valid_crentials(String username,String password) {

        driver.findElement(By.name("username")).sendKeys(username);
        driver.findElement(By.name("password")).sendKeys(password);         
        driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[1]/div/form/div[3]/input")).click(); 
    }

    @Then("I should be taken to the overview page")
    public void i_should_be_taken_to_the_overview_page() {

        driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div/div/h1")).isDisplayed(); 
        driver.findElement(By.xpath("/html/body/div[1]/div[3]/div[1]/ul/li[8]/a")).click(); 
        driver.quit();
     }
}
 
As seen from the above code the @When keyword is changed to state that it accepts two 
parameters of String type and within the void i_enter_valid_credentials we are passing
two parameters passed by the feature file. The two parameters are then passed to the 
.sendKeys, to enable Selenium to read the two parameters and pass to the login UI.
 
Note: This blog post does not cover the step for creating the TestRunner class. 
 
[1]https://parabank.parasoft.com/parabank/index.htm 
 
 

Comments