zoukankan      html  css  js  c++  java
  • Selenium WebUI自动化测试--PO中传递driver

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

    • 第一种 Page中传递driver  

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

      

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

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

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

  • 相关阅读:
    一起谈.NET技术,JAVA与.NET的相互调用——TCP/IP相互调用基本架构 狼人:
    一起谈.NET技术,.NET异步编程:IO完成端口与BeginRead 狼人:
    一起谈.NET技术,VS2010自定义新建文件模版 狼人:
    一起谈.NET技术,理解.NET程序集的执行过程 狼人:
    一起谈.NET技术,JAVA与.NET的相互调用——利用JNBridge桥接模式实现远程通讯 狼人:
    一起谈.NET技术,用NuGet掌管你的Visual Studio扩展 狼人:
    一起谈.NET技术,Visual Studio 2010 中的代码约定设置 狼人:
    一起谈.NET技术,C#中的语言特性都是从何而来? 狼人:
    一起谈.NET技术,关于Winform下,获取Treeview中CheckBox选中项的技巧 狼人:
    一起谈.NET技术,ASP.NET MVC 3和Razor中的@helper 语法 狼人:
  • 原文地址:https://www.cnblogs.com/zhap/p/12585334.html
Copyright © 2011-2022 走看看