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");
        }
    }

  • 相关阅读:
    svn导出文件夹到另外目录export
    关键词提取自动摘要相关开源项目,自动化seo
    高级前端面试题,部分答案待补充
    三句话感受一本书,让脑子变聪明的7本书,每本只需理解3句话!
    关于系统设置分辨率比例影响到网页宽度大小的问题
    QQ在线客服,默认到要加好友,授权也不起作用需要先开通QQ营销服务
    网络营销相关缩写名称CPM CPT CPC CPA CPS SEM SEO解析
    让nodepad++编辑时链接能双击打开
    工作是一种修行,工作本身,就是一种修行(深度好文)
    foxmail收取163企业邮箱设置,不能直接用foxmail默认的配置,否则一直提示帐号密码错误
  • 原文地址:https://www.cnblogs.com/NextHop/p/4918103.html
Copyright © 2011-2022 走看看