Selenium WebUI自动化测试--PO中传递driver

  UI自动测试时,咱们常会选择PO设置模式,在PO设计模式中,咱们须要考虑如何将页面元素和页面提供的功能优雅的封装,今天简单的讲下两种页面封装方法,仅供参考。web

  • 第一种 Page中传递driver  

  在这以前,UI自动化测试中,一直使用传递方式进行driver,也是一种比较简洁的方法,代码以下:chrome

  

 1 /**
 2  * @Author: zap
 3  * @Date: 2020/3/27 23:58
 4  * @Description:
 5  */
 6 public class P0_LoginPage {
 7     WebDriver driver;
 8     public P0_LoginPage(WebDriver driver) {
 9         this.driver = driver;
10         PageFactory.initElements(driver, this);
11     }
12 
13     //username TextBox
14     @FindBy(xpath = "//*[@id='username']")
15     WebElement usernameTextBox;
16 
17     /**
18      * @function Input username
19      * @param username
20      */
21     public void setUsernameTextBox(String username) {
22         usernameTextBox.clear();
23         usernameTextBox.sendKeys(username);
24     }
25 
26     //password TextBox
27     @FindBy(xpath = "//*[@id='password']")
28     WebElement passwordTextBox;
29 
30     /**
31      * @function Input password
32      * @param password
33      */
34     public void setPasswordTextBox(String password) {
35         passwordTextBox.clear();
36         passwordTextBox.sendKeys(password);
37     }
38 
39     //login button
40     @FindBy(xpath = "//button[@class = 'ant-btn ant-btn-lg']")
41     WebElement loginButton;
42 
43     /**
44      * @function click LoginButton
45      */
46     public void clickLoginButton() {
47         loginButton.click();
48     }
49 
50     public HomePage toUserLogin(String user, String pwd){
51         setUsernameTextBox(user);
52         setPasswordTextBox(pwd);
53         clickLoginButton();
54         return new HomePage();
55     }
56 }

 

  • 第二种 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设计模式时,必定要理解清楚,只有把页面封装好了,在后期的执行和维护时才不会头大;这里介绍几点理解:this

  对于PO类中的方法:spa

    1.将UI页面中所能提供的功能进行封装设计

      【理解】:先去了解页面中可以提供的功能,而后进行封装,好比登录页面,通常包含用户登录,找回密码操做,咱们能够将登录、找回密码分别做为方法封装在LoginPage页面中,使用时直接调用,代码以下(继承driver方式):code

/**
 * @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.一样的行为 不一样的结果,能够建模在不一样的方法blog

      【理解】在使用相同的操做,因为输入的参数不一致,可能会获得不一样的结果和页面跳转;好比代码以下:继承

/**
 * @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或是用于断言的数据

      【理解】这里你们在多试几个用例后,必定会体验到他的好处。

相关文章
相关标签/搜索