How to access datafile values without specifying in feature file and Step Defs

112 Views Asked by At

I using qaf-cucumber library for scenario outlines testdata with external files.

Repo: https://github.com/qmetry/qaf-cucumber Branch: cucumber-4

I want to access the TestData file content without specifying as parameters in feature file step definitions.

TestData.csv

FirstName,LastName,Title,Address,City,Zip,State
TestFName1,TestLName1,Mr,TestAddress1,TestCity1,11111,TestState1
TestFName2,TestLName2,Mr,TestAddress2,TestCity2,22222,TestState2
TestFName3,TestLName3,Mr,TestAddress3,TestCity3,33333,TestState3

feature file:

@smoke
  Scenario Outline: Register the user with the TestData File info
  Given User is in the registration page
  When user fills the registration data
  Examples:{'datafile':'resources/TestData.csv'}

StepDefs:

@Given("When user fills the registration data")
    @QAFTestStep(description="When user fills the registration data")
    public void userFillsTheRegistrationData(){

    
//Here anyway to get the TestData.csv Content
           
String fName = logic to get the FirstName //Please provide any way to get the FirstName from TestData File using QAF-Cucumber
           
String lName = logic to get the LastName
           
String title = logic to get the Title
           
String address = logic to get the Address
           
String city    = logic to get the City
           
String zip     = logic to get the Zip
           
String state.  = logic to get the State


          
System.out.println("FirstName == " + fName);
          
System.out.println("LastName == " + lName);
          
System.out.println("Title == " + title);
          
System.out.println("Address == " + address);
          
System.out.println("City == " + city);
          
System.out.println("ZIP == " + zip);
          
System.out.println("State == " + state);`


    }

**Expected Result: **

FirstName == TestFName1
LastName == TestLName1
Title == Mr
Address == TestAddress1
City == TestCity1
ZIP == 11111
State == TestState1

FirstName == TestFName2
LastName == TestLName2
Title == Mr
Address == TestAddress2
City == TestCity2
ZIP == 222222
State == TestState2


FirstName == TestFName3
LastName == TestLName3
Title == Mr
Address == TestAddress3
City == TestCity3
ZIP == 33333
State == TestState3

1

There are 1 best solutions below

2
user861594 On

Your step should accept argument. For instance:

Here is example with multiple arguments:

@QAFTestStep(description="When user fills the registration data with {fname} {lname} {title}, {address}, {city}-{zip} {state}")
public void userFillsTheRegistrationData(Stirng firstName, String lastName, String title, String address, String city, int zip, String state){

}

arguments needs to be passed when calling step. You can use column names as parameter. In you case it is FirstName,LastName,Title,Address,City,Zip,State.

  When user fills the registration data with "${FirstName}" "{LastName}" "${Title}", "${Address}", "${City}"-"${Zip}" "${state}"

another option is accepting entire record as map:

@QAFTestStep(description="When user fills the registration data with {data}")
public void userFillsTheRegistrationData(Map<String, Object> data){
   String firstName = (String)data.get("FirstName");
   ...
}

You can pass map using json string:

  When user fills the registration data with {'FirstName':'Shankar', ...}

or

  When user fills the registration data with {'FirstName':'${FirstName}', ...}

or when using data provider, you can use args[0] to refer data row while calling step.

  When user fills the registration data with "${args[0]}"