• PageObject模式实现页面元素分离


    Page Object模式

    Page Object将测试对象及单个的测试步骤封装在每个Page对象中,以page为单位进行管理。

    1、例如没有使用Page Object模式时对于163邮箱的登录操作代码如下:

    packagecom.test;

     

    importorg.openqa.selenium.By;

    importorg.openqa.selenium.WebDriver;

    importorg.openqa.selenium.WebElement;

    importorg.openqa.selenium.firefox.FirefoxDriver;

     

    publicclasstest163 {

       publicstaticvoidmain(String[] args)

     {

          //启动浏览器,进入163邮箱首页

          WebDriver driver =newFirefoxDriver();

          driver.get("http://mail.163.com/");

          

          Thread.sleep(5000);

          //输入用户名密码,登录邮箱

          WebElement youxiangzhanghao_element = driver.findElement(By.id("idInput"));

          youxiangzhanghao_element.clear();

      

           youxiangzhanghao_element.sendKeys("justForYourTesting");

           

           WebElement mima_element = driver.findElement(By.id("pwdInput"));

           mima_element.sendKeys("135135");

                   

           WebElement denglu_element = driver.findElement(By.id("loginBtn"));

           denglu_element.click();

           

           Thread.sleep(10000);

          

           driver.close();

       }

    }

    2、使用FindBy注解

    package com.mail163;

    import org.openqa.selenium.WebDriver;

    import org.openqa.selenium.WebElement;

    import org.openqa.selenium.support.FindBy;

    public class mail163 {

          @FindBy(id="idInput" )

          private WebElement username;

          @FindBy(id="pwdInput" )

          private WebElement password;

          @FindBy(id="loginBtn" )

          private WebElement loginBtn;

          public void login(WebDriver dr,String username,String pwd){

                 dr.get("http://mail.163.com");

                 this.username.sendKeys(username);

                 this.password.sendKeys(password);

                loginBtn.click();

          }

    }

    通过FindBy每一个页面元素都被定义为一个类中的私有变量,通过调用login()方法即可实现登陆页面的登录操作。

        通过对比1、2明显可以看出,2的逻辑结果比1更加简洁,以每个page为单位(类),以每个page元素为类的属性,以方法login()来实现每个页面元素操作的封装。程序的逻辑独立性更强,也方便后期的维护和修改。

    3、对Login类的login方法的初始化

    package com.test.java;

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.PageFactory;

    public class testLogin3 {
        public void login1(String username, String password)
        {
        WebDriver driver= new FirefoxDriver();
        // 对页面元素的初始化
        login m=PageFactory.initElements(driver, login.class);
        m.login(driver, username, password);
        }
       
        public static void main(String[] args)
        {
            testLogin3 tl = new testLogin3();
            tl.login1("justForYourTesting","135135");
        }
    }

  • 相关阅读:
    字典的增删改查
    基础数据类型
    Linux程序优化实践
    Linuxt性能调优指南
    【MySQL】Percona Toolkits工具介绍及使用
    【MySQL】如何构建高性能MySQL系统?
    【MySQL】Event使用案例
    【MySQL】事件调度器 (Event Scheduler)
    【MySQL】mydumper工具介绍
    spu与sku的含义
  • 原文地址:https://www.cnblogs.com/NextHop/p/4918103.html
Copyright © 2020-2023  润新知