Selenium WebUI自动化测试--PO中传递driver
UI自动测试时,我们常会选择PO设置模式,在PO设计模式中,我们需要考虑如何将页面元素和页面提供的功能优雅的封装,今天简单的讲下两种页面封装方法,仅供参考。
- 第一种 Page中传递driver
在这之前,UI自动化测试中,一直使用传递方式进行driver,也是一种比较简洁的方法,代码如下:
/**
* @Author: zap
* @Date: 2020/3/27 23:58
* @Description:
*/
public class P0_LoginPage {
WebDriver driver;
public P0_LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
//username TextBox
@FindBy(xpath = "//*[@id=‘username‘]")
WebElement usernameTextBox;
/**
* @function Input username
* @param username
*/
public void setUsernameTextBox(String username) {
usernameTextBox.clear();
usernameTextBox.sendKeys(username);
}
//password TextBox
@FindBy(xpath = "//*[@id=‘password‘]")
WebElement passwordTextBox;
/**
* @function Input password
* @param password
*/
public void setPasswordTextBox(String password) {
passwordTextBox.clear();
passwordTextBox.sendKeys(password);
}
//login button
@FindBy(xpath = "//button[@class = ‘ant-btn ant-btn-lg‘]")
WebElement loginButton;
/**
* @function click LoginButton
*/
public void clickLoginButton() {
loginButton.click();
}
public HomePage toUserLogin(String user, String pwd){
setUsernameTextBox(user);
setPasswordTextBox(pwd);
clickLoginButton();
return new HomePage();
}
}- 第二种 Page中继承driver
先创建一个BasePage类,代码如下:
/**
* @Author: zap
* @Date: 2020/3/27 22:10
* @Description:
*/
public class BasePage {
public static WebDriver driver;
public void closeBrowser(){
driver.close();
}
}然后在新页面中 去继承BasePage,通过父类中定义的driver,在每个页面中来使用,继承如下:
/**
* @Author: zap
* @Date: 2020/3/27 22:12
* @Description:
*/
public class LoginPage extends BasePage{
/**
* @function: 用户登陆
* @return: 返回值需要考虑登陆成功和登陆失败 返回的页面的不同
*/
public HomePage toUserLogin(String user, String pwd){
String baseURL = "http://***********/#/"; //URL地址只做参考
System.setProperty("webdriver.chrome.driver",
"E:\\Study\\Test\\SeleniumUIDependentFiles\\WebDriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(baseURL);
//1.输入用户名
WebElement userTextBox = driver.findElement(By.xpath("//*[@id=‘username‘]"));
userTextBox.clear();
userTextBox.sendKeys(user);
// Thread.sleep(500);
//2.输入用户密码
WebElement pwdTextBox = driver.findElement(By.xpath("//*[@id=‘password‘]"));
pwdTextBox.clear();
pwdTextBox.sendKeys(pwd);
//3.点击登陆按钮
WebElement submitButton = driver.findElement(By.xpath("//button[@class=‘ant-btn ant-btn-lg‘]"));
submitButton.click();
//4.根据结果判断返回哪个页面 这里不深入考虑
return new HomePage();
}
}在使用PO设计模式时,一定要理解清楚,只有把页面封装好了,在后期的执行和维护时才不会头大;这里介绍几点理解:
对于PO类中的方法:
1.将UI页面中所能提供的功能进行封装
【理解】:先去了解页面中能够提供的功能,然后进行封装,比如登陆页面,一般包含用户登陆,找回密码操作,我们可以将登陆、找回密码分别作为方法封装在LoginPage页面中,使用时直接调用,代码如下(继承driver方式):
/**
* @Author: zap
* @Date: 2020/3/27 22:11
* @Description:
*/
public class TestCase_UserLogin {
LoginPage loginPage;
@BeforeMethod(alwaysRun=true)
public void beforeClass(){
loginPage = new LoginPage();
}
@Test(alwaysRun=true)
public void userLogin(){
loginPage.toUserLogin("test1", "test1");
}
@AfterMethod
public void afterClass(){
loginPage.closeBrowser();
}
}2.同样的行为 不同的结果,可以建模在不同的方法
【理解】在使用相同的操作,由于输入的参数不一致,可能会得到不同的结果和页面跳转;比如代码如下:
/**
* @Author: zap
* @Date: 2020/3/28 0:19
* @Description:
*/
public class P1_LoginPage {
/**
* @function 进入登陆页面
* @return
*/
public LoginPage toLoginPageFail (){
/*
失败登陆操作
*/
return new LoginPage();
}
/**
* @function 进入主页面
* @return
*/
public HomePage toLoginPageSuccess(){
/*
成功登陆操作
*/
return new HomePage();
}
}3..方法应该返回其他的PageObject或是用于断言的数据
【理解】这里大家在多试几个用例后,一定会体验到他的好处。
相关推荐
xiangxiaojun 2020-09-23
Reiki 2020-08-16
letheashura 2020-08-14
tiankele0 2020-07-29
curiousL 2020-07-18
王练 2020-07-18
tiankele0 2020-07-18
amei0 2020-07-08
Reiki 2020-07-06
Ronnyxie 2020-07-06
xiangxiaojun 2020-07-05
zhanghaibing00 2020-06-28
xiongyouqiang 2020-06-28
Ronnyxie 2020-06-27
amei0 2020-06-26
letheashura 2020-06-26
tiankele0 2020-06-26
王练 2020-06-25