Cucumber/JUnit 5를 사용한 극작가 - 단계 정의
단계 정의
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();
}
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);
}
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());
}
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());
}
}
Reference
이 문제에 관하여(Cucumber/JUnit 5를 사용한 극작가 - 단계 정의), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/terencepan/playwright-with-cucumberjunit-5-step-definition-ccc텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)