导入项目相关依赖
pom.xml:
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
</dependencies>
编写feature文件
MoxtraLogin.feature
Feature: Test login functionality
Scenario Outline: a Check login is successful with valid credentials
Given a browser is open
And a user is on login page
When a user enters <username> and <password>
Then a user is navigated to the home page
Examples:
| username | password |
| 2573784625@qq.com | 2573784625@qq.COM |
| lihao | 123 |
页面工厂类
public class LoginPage_PF {
@FindBy(id="email")
private WebElement username;
@FindBy(id="password")
private WebElement password;
@FindBy(id="loginBtn")
private WebElement btnLogin;
private WebDriver webDriver;
public LoginPage_PF(WebDriver webDriver){
this.webDriver=webDriver;
PageFactory.initElements(webDriver,this);
}
public void enterUsername(String username){
this.username.sendKeys(username);
}
public void enternPassword(String password){
this.password.sendKeys(password);
}
public void clickOnLogin(){
this.btnLogin.click();
}
}
测试步骤类MoxtraLoginSteps
public class MoxtraLoginSteps {
private WebDriver driver=null;
private LoginPage_PF loginPage_pf=null;
@Given("^a browser is open$")
public void a_browser_is_open() throws Throwable {
System.out.println("a browser is open");
System.setProperty("webdriver.chrome.driver","src/test/resources/driver/chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
}
@Given("^a user is on login page$")
public void a_user_is_on_login_page() throws Throwable {
driver.navigate().to("https://moxtra.moxtra.com/web/#/login");
}
@When("^a user enters (.*) and (.*)$")
public void a_user_enters_username_and_password(String username,String password) throws Throwable {
loginPage_pf=new LoginPage_PF(driver);
loginPage_pf.enterUsername(username);
loginPage_pf.enternPassword(password);
// driver.findElement(By.id("email")).sendKeys(username);
// driver.findElement(By.id("password")).sendKeys(password);
Thread.sleep(4000);
// driver.findElement(By.id("loginBtn")).click();
loginPage_pf.clickOnLogin();
}
@Then("^a user is navigated to the home page$")
public void a_user_is_navigated_to_the_home_page() throws Throwable {
}
}
启动类MoxtraLoginRunner
@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/com/moxtra/login/MoxtraLogin.feature",
glue = {"com.moxtra.login"},monochrome = true
)
public class MoxtraLoginRunner {
}