DEV Community 👩‍💻👨‍💻

Terence Pan
Terence Pan

Posted on • Updated on

Playwright with Cucumber/JUnit 5 - Step Definition

Step Definition

The Step Definition file is where you create steps that map to the Gherkin steps in the feature file. In Java, I am using the annotation method for mapping the steps. There is also a Java 8 Lambda style syntax which I am not covering here.

When Given User navigates to PHPTRAVELS Demo Page is called, userNavigatesToPHPTRAVELSDemoPage method is called and the browser will navigate to the page to be tested.

    @Given("User navigates to PHPTRAVELS Demo Page")
    public void userNavigatesToPHPTRAVELSDemoPage() {
        DemoPage demoPage = new DemoPage(page);
        demoPage.navigate();
    }
Enter fullscreen mode Exit fullscreen mode

When the step When User clicks submit is run, the method userClicksSubmit is called, which in this case clicks the submit button and stores the alert text for later verification if it pops up. This data state storing uses dependency injection which we will cover later in the series.

    @When("User clicks submit")
    public void userClicksSubmit() {
        DemoPage demoPage = new DemoPage(page);
        String alertText = demoPage.clickSubmit();
        testContext.setAlertText(alertText);
    }
Enter fullscreen mode Exit fullscreen mode

The next step is a Then that can be called to verify the alert text if it shows up. We are using JUnit5 Assertions.assertEquals method to check to make sure alert text is as expected. The first argument is the expected String and the second argument is the actual displayed String.

    @Then("Verify alert {string}")
    public void verifyAlertToFillInResultIsShown(String alertText) {
        Assertions.assertEquals(alertText, testContext.getAlertText());
    }
Enter fullscreen mode Exit fullscreen mode

Full DemoSteps.java code:

package io.tpan.steps;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import io.tpan.pages.DemoPage;
import org.junit.jupiter.api.Assertions;

public class DemoSteps {
    private final TestContext testContext;
    private final Browser browser;

    private BrowserContext browserContext;

    private Page page;

    public DemoSteps(TestContext testContext) {
        this.testContext = testContext;
        this.browser = testContext.getBrowser();
    }

    @Before
    public void createContextAndPage(){
        browserContext = browser.newContext();
        page = browserContext.newPage();
    }

    @After
    public void closeContext(){
        browserContext.close();
    }

    @Given("User navigates to PHPTRAVELS Demo Page")
    public void userNavigatesToPHPTRAVELSDemoPage() {
        DemoPage demoPage = new DemoPage(page);
        demoPage.navigate();
    }

    @When("User fills in Demo Request Form {string}, {string}, {string}, {string}")
    public void userFillsInDemoRequestForm(String firstName, String lastName, String businessName, String email) {
        DemoPage demoPage = new DemoPage(page);
        demoPage.fillRegistrationInfo(firstName, lastName, businessName, email);
    }

    @Then("Verify thank you text")
    public void verifyThis() {
        Locator thankYouText = page.locator("text=' Thank you!'");
        thankYouText.waitFor();
        Locator completionText = page.locator("text='We have sent your demo credentials to your email please check your email to test demo website. if message not found inbox please check spam folder'");
        Assertions.assertTrue(completionText.isVisible());
    }

    @When("User calculates and fills result")
    public void userCalculatesAndFillsResult() {
        DemoPage demoPage = new DemoPage(page);
        demoPage.fillResult();
    }

    @When("User calculates and fills wrong result")
    public void userCalculatesAndFillsWrongResult() {
        DemoPage demoPage = new DemoPage(page);
        demoPage.fillWrongResult();
    }

    @When("User clicks submit")
    public void userClicksSubmit() {
        DemoPage demoPage = new DemoPage(page);
        String alertText = demoPage.clickSubmit();
        testContext.setAlertText(alertText);
    }

    @Then("Verify alert {string}")
    public void verifyAlertToFillInResultIsShown(String alertText) {
        Assertions.assertEquals(alertText, testContext.getAlertText());
    }
}
Enter fullscreen mode Exit fullscreen mode

As always code is on available on Github

Top comments (2)

Collapse
 
citronbrick profile image
CitronBrick • Edited on

Please consider enabling syntax highlighting for your code blocks by adding

<3 backticks>java
before your code blocks.

Collapse
 
terencepan profile image
Terence Pan Author

Thanks! Didn't know about that

Update Your DEV Experience Level:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. 🛠