Cucumber scenario outline with examples


Cucumber scenario outline with examples


Scenario outline basically replace the value with the datatable value.  Here each row of the data table consider as a new scenario.  For example suppose I want to login into the www.facebook.com site.  As of now we have execute only one scenario. We have provided username and password for login into the facebook site.  If you closely look into the site you can see there 3 different ways to login into the application.

  • We cant provide email id and password.
  • We can provide phone number and password.
  • We can provide user name and password. 
Now we can achieve above mentioned scenarios in 3 different scenario with 3 different input type 

Scenario 1:

Feature: Login Application
  As a user
  I want to login to the application

  Scenario: Valid user email id and password
    Given I launch the url "https://www.fb.com"
    When I enter password and email
    And I click on login button
    Then I should see the login page

Scenario 2:


Feature: Login Application
  As a user
  I want to login to the application

  Scenario: Valid user phone number and password
    Given I launch the url "https://www.fb.com"
    When I enter password and phone no.
    And I click on login button
    Then I should see the login page

Scenario 3:

Feature: Login Application
  As a user
  I wa
  Scenario: Valid user name and password
    Given I launch the url "https://www.fb.com"
    When I enter password and username
    And I click on login button
    Then I should see the login pagent to login to the application

Here you can see the scenario statements are same for all there scenario, only difference is that the parameter(user name / phone number/ email id). Here is the scenario outline feature comes into the picture. 

We can design this login feature in such a way where  scenario will be only one but test data will be 3 and the scenario will be execute 3 times. 

Till now we have used Scenario keyword in feature file but iteration purpose we should use Scenario Outline instead of Scenario.


Feature file : ScenarioOutline.feature

Feature: Login Application
  As a user
  I want to login to the application

  Scenario Outline: Valid user name and password
    Given I launch the url "https://www.fb.com"
    When I provide "<username>" and "<password>"
    And I click on login button
    Then I should see the login page

    Examples: 
      | username          | password |
      |        987654210 | [email protected] |
      | username1          | [email protected] |

Here the Examples annotation  describe the range of iteration , means how many times the test case will execute. In this example the test case will execute 3 times.

Complete steps 
 package com.Cucumber.steps;  
 import java.util.List;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.OutputType;  
 import org.openqa.selenium.TakesScreenshot;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebDriverException;  
 import org.openqa.selenium.chrome.ChromeDriver;  
 import org.testng.Assert;  
 import cucumber.api.DataTable;  
 import cucumber.api.Scenario;  
 import cucumber.api.java.After;  
 import cucumber.api.java.en.Given;  
 import cucumber.api.java.en.Then;  
 import cucumber.api.java.en.When;  
 public class DemoLoginSteps2 {  
  WebDriver driver;  
  @Given("^I launch the url \"([^\"]*)\"$")  
  public void i_launch_the_url(String arg1) throws Throwable {  
    // Write code here that turns the phrase above into concrete actions  
  System.setProperty("webdriver.chrome.driver", "C:\\Users\\anjan\\Desktop\\cucmber\\chromedriver\\chromedriver.exe");  
  driver = new ChromeDriver();  
  driver.get("https://www.fb.com");  
  }  
  @When("^I provide \"([^\"]*)\" and \"([^\"]*)\"$")  
  public void i_provide_and(String arg1, String arg2) throws Throwable {  
    // Write code here that turns the phrase above into concrete actions  
  driver.findElement(By.name("email")).sendKeys(arg1);  
  driver.findElement(By.name("pass")).sendKeys(arg2);  
  }  
  @When("^I click on login button$")  
  public void i_click_on_login_button() throws Throwable {  
    // Write code here that turns the phrase above into concrete actions  
  driver.findElement(By.xpath("//*[@data-testid='royal_login_button']")).click();  
  }  
  @Then("^I should see the login page$")  
  public void i_should_see_the_Error_message() throws Throwable {  
  try{  
    // Write code here that turns the phrase above into concrete actions  
   if(driver.findElement(By.name("email")).isDisplayed()){  
   Assert.assertTrue(true);  
   }else  
   Assert.assertTrue(false);  
  }catch(Exception NoSuchElementException){  
   Assert.assertTrue(false);  
  }finally{  
   driver.quit();  
  }    
  }  
 }  


You can run the test from feature file or from testng xml. If you want to execute using testng the you need to write runner class first and then testng xml.

Runner class
 package com.Cucumber.Runners;  
 import cucumber.api.CucumberOptions;  
 import cucumber.api.testng.AbstractTestNGCucumberTests;  
 @CucumberOptions(  
  features = "src/com/Cucumber/features/ScenarioOutline.feature",   
  glue = "com.Cucumber.steps",   
  plugin = { "pretty", "html:target/cucumber-report" },   
  monochrome = true)  
 public class RunTest extends AbstractTestNGCucumberTests {  
 }  


TestNgXML







Share this

Related Posts

Previous
Next Post »

1 comments:

comments
April 24, 2018 at 11:17 PM delete

Really cool post, highly informative and professionally written and I am glad to be a visitor of this perfect blog, thank you for this rare info!

software testing course in chennai

Reply
avatar