DEV Community

Terence Pan
Terence Pan

Posted on • Edited on

1

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)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.