请基于TESTng框架写一个PO模式的UI自动化测试框架
时间: 2023-05-15 17:05:57 浏览: 122
优秀的UI自动化测试框架
5星 · 资源好评率100%
非常感谢您的提问。我可以回答您的问题。以下是基于TESTng框架的PO模式的UI自动化测试框架的代码示例:
```
public class LoginPage {
private WebDriver driver;
private By usernameLocator = By.id("username");
private By passwordLocator = By.id("password");
private By loginButtonLocator = By.id("loginButton");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void setUsername(String username) {
driver.findElement(usernameLocator).sendKeys(username);
}
public void setPassword(String password) {
driver.findElement(passwordLocator).sendKeys(password);
}
public void clickLoginButton() {
driver.findElement(loginButtonLocator).click();
}
}
public class HomePage {
private WebDriver driver;
private By welcomeMessageLocator = By.id("welcomeMessage");
public HomePage(WebDriver driver) {
this.driver = driver;
}
public String getWelcomeMessage() {
return driver.findElement(welcomeMessageLocator).getText();
}
}
public class TestBase {
protected WebDriver driver;
@BeforeClass
public void setUp() {
driver = new ChromeDriver();
driver.manage().window().maximize();
}
@AfterClass
public void tearDown() {
driver.quit();
}
}
public class LoginTest extends TestBase {
private String baseUrl = "http://example.com";
private String username = "testuser";
private String password = "testpass";
@Test
public void testLogin() {
driver.get(baseUrl);
LoginPage loginPage = new LoginPage(driver);
loginPage.setUsername(username);
loginPage.setPassword(password);
loginPage.clickLoginButton();
HomePage homePage = new HomePage(driver);
String welcomeMessage = homePage.getWelcomeMessage();
Assert.assertEquals(welcomeMessage, "Welcome, " + username + "!");
}
}
```
这个框架使用了Page Object模式,将页面元素和操作封装在了对应的Page类中,使得测试用例更加清晰易读。同时,使用了TESTng框架来管理测试用例的执行顺序和测试结果的输出。
阅读全文