DEV Community

Terence Pan
Terence Pan

Posted on • Edited on

Playwright with Cucumber/JUnit 5 - Feature File

Feature File

Cucumber steps are actions that a user can be imagined to take. Given is what actions are used to set up the test. When is what actions are taken to do the testing. Then is what result you want to see. For the 3rd example, we are intentionally filling out the form with the wrong result and expecting a certain error message to show up.

Cucumber allows annotations to tag what grouping you want your tests to belong to. This allows easier searching when you generate your reports later on.

Scenario outlines allow the same scenario to be run multiple times using the Examples section as a dataset.

In the below example we are testing a happy path. The user starts off navigating to the page using the Given step. The user then fills in the form with the When step. The site requires the user to make a calculation to prove they are not a robot so we calculate it, fill in the sum and hit submit. These are implemented with When steps. The Then step is to verify the submission was successful.

Feature: Register for Demo Request
  @HappyPath
  Scenario Outline: User is able to request a Demo
    Given User navigates to PHPTRAVELS Demo Page
    When User fills in Demo Request Form <firstName>, <lastName>, <businessName>, <email>
    When User calculates and fills result
    When User clicks submit
    Then Verify thank you text
    Examples:
      | firstName | lastName | businessName | email               |
      | "ABC"     | "DEF"    | "Main Inc."  | "whatever@main.com" |
Enter fullscreen mode Exit fullscreen mode

Now for the below step, it's almost the exact same except we are intentionally not filling out the calculation. In this case we are testing the page's validation. The Then step in this case is verifying the correct alert is displayed to the user.

  @Validation
  Scenario Outline: User is able to request a Demo but does not fill out result
    Given User navigates to PHPTRAVELS Demo Page
    When User fills in Demo Request Form <firstName>, <lastName>, <businessName>, <email>
    When User clicks submit
    Then Verify alert "Please input result number"
    Examples:
      | firstName | lastName | businessName | email               |
      | "ABC"     | "DEF"    | "Main Inc."  | "whatever@main.com" |
Enter fullscreen mode Exit fullscreen mode

As always code is on available on Github

Top comments (0)