Objective

We are going to pass parameters from Cucumber feature file to its corresponding steps file. What we are going to use:

  1. IntelliJ as IDE
  2. Selenium (for testing)
  3. TestNG
  4. Cucumber for TestNG dependency
  5. Cucumber Java dependency

Simple parameter passing

FirstTest.feature

Lets create a feature file for Cucumber steps. Make sure that Cucumber and Gherkin plugin is installed in IntelliJ. Create feature file “FirstTest.feature” and add following code to it.

Feature: Opening home page

 Scenario:
  Given I am on Home Page of "http://room5.trivago.com/"
  Then I see Trivago's logo

In the above code, the parameter we are passing is the navigation URL which is in parenthesis.

Now we are going to catch this parameter in our steps file.

Steps.java

Create a new java file “Steps.java” and add following code to it.

import cucumber.api.java.en.Given;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Steps {

    public static WebDriver driver;
    @Given("^I am on Home Page of \"([^\"]*)\"$")
    public void i_am_on_Home_Page_of(String arg1) throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        System.setProperty("webdriver.chrome.driver", "D:\\Selenium Webdriver/chromedriver.exe");
        driver = new ChromeDriver();
        driver.get(arg1);
    }

    @Then("^I see Trivago's logo$")
    public void i_m_taken_to_Contact_Page() throws Throwable {
        // Some code to grab and assert the logo visibility
    }
}

In the above snippet for Given statement, notice that we have

  • String arg1 in the class. This is where we are catching the sent parameter and utilizing it in the steps.
  • \”([^”]*)\” in the Given clause, which denotes that parameter is present at this point in the statement

Using “Examples”

The “Examples” keyword is available in Cucumber for passing multiple values for the parameter (just like we have Data Operator in TestNG).

SecondTest.feature

Create another feature file “SecondTest.feature” and add following code to it.

Feature: Opening multiple pages

  Scenario Outline:
    Given I am on Home Page of "<url>"
    Then I'm taken to Home Page

    Examples:
    |url|
    |http://google.com/|
    |http://facebook.com/|

And we are done. Our Steps file will be same which we created previously.

Note that this time we have used Scenario Outline instead of Scenario

If you are having multiple parameters in the same step, then your Examples will look like

|username|password|
|user1|pass1|
|user2|pass2|

This is just like creating a table for the values.

Also note that in this case the scenario will be executed every time for every value i.e. if there are 2 values for a parameter then scenario will run twice.

Using DataTable

Another way is to set the values of parameter within the step. We utilize Lists to grab the values sent using Data Table.

ThirdTest.feature

Create another feature file “ThirdTest.feature” and add following code to it.

Feature: Opening multiple pages

Scenario: :
Given I am on Home Page of Website
|http://google.com/|
|http://facebook.com/|
Then I'm taken to Home Page

Notice that we have added parameter values in the Given statement.

Steps2.java

This time we will be creating a new steps file for above feature. Create “Steps2.java” and add following code to it.

import cucumber.api.DataTable;
import cucumber.api.java.en.Given;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Steps2 {
    public static WebDriver driver;
    @Given("^I am on Home Page of Website$")
    public void i_am_on_Home_Page_of_Website(DataTable arg1) throws Throwable {
        List<String> data = arg1.raw();
        System.setProperty("webdriver.chrome.driver", "D:\\Selenium Webdriver/chromedriver.exe");
        driver = new ChromeDriver();
        driver.get(data.get(0));
    }
}

And we are done. We stored the passed values in List and accessed it accordingly.

The advanced version of Data Table is using it with Maps. This will be explained some other time, or you can easily google out what Maps are 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s