Home > Mobile >  Read data from excel file in selenium BDD cucumber test cases
Read data from excel file in selenium BDD cucumber test cases

Time:04-14

I have bunch of gherkin test steps in a feature file scenario. How to read test data from excel file and execute the same test steps for each and every data in the row

CodePudding user response:

Considering you are reading username and password

#GherkinStep

 When I read username and password

Step definition should contain steps to read data using excel utility

@When(^I read username and password$)
String userName = excel.getData(sheetName,col,row);
String password = excel.getData(sheetName,col,row);

Create excel utility to read data.

CodePudding user response:

In general you should avoid trying to drive cucumber scenarios from Excel spreadsheets. People have been trying this for years and failing.

If you have to use Excel to store important data for driving tests then you should do one scenario per table, rather than try and do one scenario per row.

After that some things to consider are

  1. Can you test the data in the sheet at a lower level i.e. write a unit test that iterates over the table data.

  2. Is every row in your data set viable and distinct. If not why are testing the same thing several times.

If you get past these criteria and still want to use your Excel table you can write a single scenario to test the whole table as follows.

Assuming a set of credentials

Given a set of good credentials
When I login in with the credentials
Then I should see no failures

and an implementation something like

Given 'a set of good credentials' do
  @credentails = load_credentials_from_excel_table
end

When 'I login with credentials' do
  @results = login_with_credentials(@credentails)
end

Then 'I should see no failures' do
  # look for failures in @results
end

You will need some helper methods to avoid putting loads of code in your steps.

module CredsStepHelper
  def load_credentials_from_excel_table(...
    ...
  end

  def login_with_credentials(@credentails)
    results=[]
    credentials.each do |user, password|
      results << login_with(user: user, password: password)
    end
    results
  end
 
  def login_with(user: , password)
    ...
  end
end
World CredsStepHelper

Implementation is ruby pseudocode, you'll have to translate if you are using another language.

  • Related