下面是用于运行测试步骤 defs 的步骤类
package serenity.page;
import net.serenitybdd.core.annotations.findby.By;
import net.thucydides.core.annotations.DefaultUrl;
import net.thucydides.core.pages.PageObject;
import org.openqa.selenium.WebElement;
@DefaultUrl("http://in-air-aciscoe1/webcoe2")
public class LoginPage extends PageObject {
public void enterUsername(String username) {
WebElement txtUsername = find(By.name("username"));
typeInto(txtUsername, username);
}
public void enterPassword(String password) {
WebElement txtPassword = find(By.name("password"));
typeInto(txtPassword, password);
}
public void clickOnSubmit() {
WebElement button = getDriver().findElement(By.xpath(".//* [@id='btnLogin']"));
button.click();
}
我在 test 文件夹中而不是在主文件夹中获得了所有 java 类文件
包serenity.stepDefinitions;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import net.serenitybdd.core.annotations.findby.By;
import net.thucydides.core.annotations.Step;
import net.thucydides.core.annotations.Steps;
import org.openqa.selenium.WebDriver;
import serenity.runTests.LoginSteps;
public class LoginFeatureStepDefinitions {
WebDriver driver;
@Steps
LoginSteps loginSteps;
@When("^I should see the text element 'username'$")
public void iShouldSeeTheTextElementUsername() /**throws Throwable**/ {
loginSteps.enterUsername("arushi");
}
@Then("^I should see the text element 'password'$")
public void iShouldSeeTheTextElementPassword() /**throws Throwable**/ {
loginSteps.enterPassword("Pass-123");
}
@And("^I click the 'Login' button$")
public void iClickTheLoginButton() {
loginSteps.clickOnSubmit();
}
@And("^I should see the text element 'firstname'$")
public void iShouldSeeTheTextElementFirstname() /**throws Throwable**/ {
driver.findElement(By.name("firstname")).sendKeys("Arushi");
}
}
以上是我使用的步骤定义类。
package serenity.runTests;
import net.thucydides.core.annotations.Step;
import net.thucydides.core.steps.ScenarioSteps;
import serenity.page.LoginPage;
public class LoginSteps extends ScenarioSteps {
private LoginPage loginPage; //automatically instantiated
@Step
public void enterUsername(String username) {
loginPage.enterUsername(username);
}
@Step
public void enterPassword(String password) {
loginPage.enterPassword(password);
}
@Step
public void clickOnSubmit() {
loginPage.clickOnSubmit();
}
}
以上是我用于测试的步骤类我使用 CucumberWithSerenity.class 运行测试运行器