Tuesday 23 September 2014

Simple Example of Parameter Handling in Cucumber, Selenium WebDriver and Junit



Simple Example of Parameter Handling in Cucumber
   1.       Create a new maven project.

    Refer link for reference LINK
   2.       Add cucumber and junit dependencies in pom.xml.

                <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.0.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

3.       Create a feature file parameter..feature in src\test\resources.
4.       Write the below code in parameter.feature

  Feature: Demo Site Login.

Scenario: User wants to login to the Demo Site
  Given I go to Demo Site Home page
  When i login with email id "archs19"
  And Password  "1234"
  Then i am successfully login

5.       Note, the parameter here is “email id”  and “password” and is enclosed with inverted commas.
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.
                     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;
     
     
      public WebDriver Driver ;
     
      @Given("^I go to Demo Site Home page$")
      public void I_go_to_Demo_Site_Home_page() throws Throwable {
             driver = new FirefoxDriver();
             driver.get("http://demo.mahara.org/");
         
      }

      @When("^i login with email id \"([^\"]*)\"$")
      public void i_login_with_email_id(String email) throws Throwable {
          driver.findElement(By.xpath("//*[@id='login_login_username']")).sendKeys(email);
           
      }

      @When("^Password  \"([^\"]*)\"$")
      public void Password(String password) throws Throwable {
             driver.findElement(By.xpath("//*[@id='login_login_password']")).sendKeys(password);
             
      }

      @Then("^i am successfully logged in$")
      public void i_am_successfully_logged_in() throws Throwable {
            driver.findElement(By.xpath("//*[@id='login_submit']")).click();
            String expected_text="Student";
                 
                  String actual_text=driver.findElement(By.xpath("//*[@id='right-nav']/ul/li[1]/a")).getText();
                 
                  Assert.assertTrue(actual_text.contains(expected_text));
           
         
      }



}

8.       Whichever statement has the parameter, replace it with an argument and pass it in place of the paramater. Eg: In this case email and password are parameterized and passed as an argument

 


9.       Now run the script and see the result as below.  


10.       Explanation of the code : The “email”  and “password were a parameter in the feature file and was enclosed with inverted commas (“”). The step definition written has a regular expression to replace the parameter. When the code is run, it prints the parameter in place of the argument used in method. 


This easy we need not change dynamic values in the code, just changing it in the feature file would be sufficient. This increases maintainability of the script and user friendliness.
 

 



No comments:

Post a Comment