vitalets / playwright-bdd

BDD testing with Playwright runner
https://vitalets.github.io/playwright-bdd/
MIT License
318 stars 40 forks source link

Question: How to do data-driven testing with Excel? #245

Open TomasRibeiro96 opened 6 days ago

TomasRibeiro96 commented 6 days ago

Hello,

Thank you for this package. We are currently using it in our projects but a new need arose. We want to do data-driven testing and execute the same test with different test data. However, and here's the catch, the Test Data must come from an Excel file that other people can edit to change what's being tested. Hence, the generated "*.feature.spec.js" files must be dynamic.

The Examples functionality could be useful, however the test data needs to come from the Excel file and can't be static in the feature file. I want to avoid other people updating the feature files because that would imply that they need to have VS Code and do a commit and push. It's easier for them to just change an Excel file in Jira. Additionally, I wanted to avoid having to run a pre-script separately to generate tests. I was looking for a solution that integrates with a simple npx bddgen && npx playwright test and avoids bigger changes do the pipeline.

Given this, do you have any suggestion about how to address this situation?

Thank you.

vitalets commented 6 days ago

Hi @TomasRibeiro96 Could you provide a small sample of excel and feature file that consumes it? Does excel contains only test data for particular step or it contains steps itself?

Similar approach exists in Specflow, it loads *.feature.xlsx files and converts them into scenarios under the hood.

TomasRibeiro96 commented 6 days ago

Hi @vitalets, here's an example of a feature file:

Given I login as "XXX" When I create a case And I fill the field "Name" with "${Name}" And I fill the field "ID" with "${ID} Then the text "${ExpectedText}" appears on the page

The Excel file would contain a table such as: | Name | ID | ExpectedText | | John Doe | 1 | Johnny | | Marilyn Doe | 2 | Mary | | James Smith | 3 | Jamie |

The intention is to execute the test from the feature file but use the test data from the Excel file. Hence, in this situation, from 1 feature file there should be 3 tests executed, each test for each row in the Excel file. Any suggestions about how to implement this?

vitalets commented 5 days ago

So the goal is to load examples data from external xls file. For now, it is not possible out-of-box in playwright-bdd, and most likely in none of Cucumber implementations.

I've found this SO question with the same request. One of suggestions is to use preprocessing: treat feature file as a template and generate 3 actual feature files. I'd suggest to follow it for now, as it's straightforward and keeps excel parsing on your side. Btw, why are you against pre-script?

Another approach we can think for the future - to link external data source inside feature file. To not break gherkin syntax we can put file path to Examples name:

Scenario Outline: my scenario
  Given I login as "XXX"
  When I create a case
  And I fill the field "Name" with "<Name>"
  And I fill the field "ID" with "<ID>"
  Then the text "<ExpectedText>" appears on the page

Examples: path/to/examples.xls