zoukankan      html  css  js  c++  java
  • 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");
        }
    }

  • 相关阅读:
    window.onload方法 和jquery中的$(document).ready()方法区别
    jQuery ligerGrid 打造通用的分页排序查询表格(提供下载)
    ASP.NET MVC 4 移动特性
    javaScript RegExp常用正则表达式
    jQuery解密之执行过程分析
    1.1 jQuery总体框架
    jqMobi指南系列教程是《 jqMobi Javascript Frameworks Cheat Sheet》 的中文版
    网页设计前端页面制作的规范要求和注意事项
    js控制position:fiexd 浏览器定位 兼容个浏览器
    通用权限管理设计 之 数据权限
  • 原文地址:https://www.cnblogs.com/NextHop/p/4918103.html
Copyright © 2011-2022 走看看