Wednesday 24 September 2014

Simple Example for DataTables in cucumber



Simple Example for DataTables in cucumber

1.       Create a new maven project.

2.       Create a cucumber project in the maven setup.
 
3.       Now Create a feature file yahoologin.feature in src\test\resources.

4.       Write the below code in yahoologin.feature

Feature: yahoo Login.

  Scenario: User is not signed up
    Given I go to yahoo home page
    When i login with email id and password
      | email             | password |
      | test@yahoo.com | 1234     |
    Then i successfully login

5.       Note that email id and password are provided with pipe separated values in a data table.

6.       Now run the feature file as à Cucumber Feature. It will give the below error.

 


7.       Now copy the snippet steps and create  a step definition file with the below code.

8.       Name the step definition file as yahoodatastep.java

package test.cucumber;

import java.util.List;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import cucumber.api.DataTable;
import cucumber.api.PendingException;
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 yahoo {
                 
                public WebDriver driver;
               
               
                @Given("^I go to yahoo home page$")
      public void I_go_to_yahoo_home_page() throws Throwable {
            driver = new FirefoxDriver();
             driver.get("https://login.yahoo.com/?.src=ym&.intl=in&.lang=en-IN&.done=http://mail.yahoo.com");
      }

      @When("^i login with email id and password$")
      public void i_login_with_email_id_and_password(DataTable table) throws Throwable {
           
            List<List<String>> data =table.raw();
            System.out.println(data.get(0).get(1));
            System.out.println(data.get(1).get(1));
           
            driver.findElement(By.xpath("//*[@id='username']")).sendKeys(data.get(1).get(0));
           
            driver.findElement(By.xpath("//*[@id='passwd']")).sendKeys(data.get(1).get(1));
         
      }

      @Then("^i successfully login$")
      public void i_successfully_login() throws Throwable {
}
       
      }
 

 

Explanation of code : In this example, the email and password are provided with pipe separated values and the values provided are used in the code.

10.   Conclusion : Cucumber supports in built data tables which can be used to define normal scenarios in gherkin language. In cucumber, whenever data is provided separated by pipes, we can use the values in the data table and assign it to a list and use the data.
 



No comments:

Post a Comment