简体   繁体   中英

Trying to write test case in Cucumber where I use one or more pieces of data

I'm trying to write out a test case where the input could be one or more items.

Example:

Scenario: Changes to a user's address in the system are saved to the database
   
Feature: Make a change to one or more of a user's address fields.
Given I'm logged into the system.
When I make a change to <field>.
And Click Save.
Then Changes are saved into database.

|Field|
|Street Address|
|City|
|State|
|ZIP Code|

This scenario works if its only one field I'm changing, ex. ZIP Code.

But how do I write it for scenarios where I want to change multiple items? Example, change City and State or City, State, and ZIP code.

You need a Scenario Outline rather than a scenario alone. This is how you create a scenario outline:

Feature: Make a change to one or more of a user's address fields.

  Scenario Outline: Changes to a user's address in the system are saved to the database
    Given I'm logged into the system.
    When I make a change to <Field>.
    And Click Save.
    Then Changes are saved into database.

    Examples: 
      |Field|
      |Street Address|
      |City|
      |State|
      |ZIP Code|

A scenario outline is easily identified by the key phrase Scenario Outline . The purpose of a scenario outline is to loop through a test case with different input parameters. These input parameters are included in a data.table underneath a section labeled " Examples ". Lastly, the label of the table header must match a parameter used in one or more test steps, enclosed in angular brackets <> .

There are two ways you can do that

1. simple scenario outline . (fields are horizontal)

   Scenario Outline:  login registration   
      When I submit login details <Username> <Password> <UsernameDesc>        
       Examples:
        |Username|Password|UsernameDesc|
        |"uname1" |"password"|"testuserName1"|
        |"uname2" |"password"|"testuserName2"|

step def for above method is

     @When("I submit login details {string} {string} {string}")
    public void register(String usrname, String pwd, String desc){
       // use usrname, pwd, desc as desired

        }

2. using data.table (@DatatableType) . (fields are vertical)

  Scenario:  login registration using cucumber data table
      When I submit login details
        |Username     |"username"|"username1"|"username2"|
        |Password     |"password"|"password1"|"password2"|
        |UsernameDesc |"testDesc"|"testDesc1"| "password2"|

step defs for above method

 @DataTableType
        public loginDetails logindetails(Map<String, String> entry) {
        return new loginDetails(entry.get("Username"),entry.get("Password"),entry.get("UsernameDesc"));
        } 
     
        @When ("^I submit login details$")
     public void submitLoginDetails(DataTable dataTable ){
        List<loginDetails> list = dataTable.transpose().asList(loginDetails.class);
        list.stream().forEach(System.out::println);
        for (loginDetails l : list) {
            System.out.println("username::"+l.getUserName());
            System.out.println("pwd::"+l.getPassword());
            System.out.println("usernamedesc::"+l.getUsernameDesc());
        }
    }

pojo class used in above method is like this

public class loginDetails {
    
    private String Username;
    private String Password;
    private String UsernameDesc;
    
    public  loginDetails(String uname, String pwd, String udesc){
        this.Username=uname;
        this.Password = pwd;
        this.UsernameDesc = udesc;
    }
    public String getUserName() {
        return Username;
    }
    public String getPassword() {
        return Password;
    }
    public String getUsernameDesc() {
        return UsernameDesc;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM