博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
selenium2_page object学习积累
阅读量:6787 次
发布时间:2019-06-26

本文共 1399 字,大约阅读时间需要 4 分钟。

   在学习selenium中page obiect记录,采用分层思想,页面元素与测试方法分离出来;

   page方法可以把页面元素放到该方法中:

页面类:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class BDPage {
        
    //定义元素变量
    /**定义百度搜索的输入框*/
    @FindBy(id="kw")   //查找定义 @FindBy(name/className/xpath/css="kw")  
    @CacheLookup  //找到元素之后缓存起来,重复使用这些元素,加快测试速度
    public WebElement keyword_input;
    
    /**定义百度搜索的搜索按钮*/
    @FindBy(id="su")
    @CacheLookup
    public WebElement search_button;
      
    public BDPage(WebDriver driver)
    {
        PageFactory.initElements(driver, this);
    }
}

实体类:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class BDPageTest {
    
    WebDriver driver;
    
    @Test
    public void Testpd()
    {
        //实例化BDPage对象
        BDPage bdp = new BDPage(driver);
        //bdp调用keyword_input元素,然后使用元素的sendKeys方法向输入框输入“你好”
          bdp.keyword_input.sendKeys("你好");
        //bdp调用search_button元素,然后调用元素的click方法点击搜索按钮
          bdp.search_button.click();
    }
     @BeforeTest
      public void beforeTest() {
          driver = new FirefoxDriver();
          driver.manage().window().maximize();
          driver.get("https://www.baidu.com");
      }
      @AfterTest
      public void afterTest() {
          driver.quit();
      }
}

本文转自 知止内明 51CTO博客,原文链接:http://blog.51cto.com/357712148/1908710,如需转载请自行联系原作者
你可能感兴趣的文章
[转]Windows Server 2012 RC 之 Hyper-V 3.0 PowerShell 命令详解 (04)
查看>>
memory leak
查看>>
C#字符操作一些代码
查看>>
[Big Data - ELK] ELK(ElasticSearch, Logstash, Kibana)搭建实时日志分析平台
查看>>
jdbc baseDAO.java
查看>>
linux sed 用法
查看>>
MySQL语法大全
查看>>
【转】Netty系列之Netty安全性
查看>>
jquery的$().each,$.each的区别
查看>>
[awk] 用-F指定多分隔符实例_备忘
查看>>
datanode报错FATAL datanode.DataNode
查看>>
redis db0-db15
查看>>
centos配置postfix extmail
查看>>
Linux之FTP篇
查看>>
我的友情链接
查看>>
通过案例对SparkStreaming 透彻理解三板斧之一
查看>>
Windows Server 2012服务器管理器详解
查看>>
在onCreate 中获取View 高度
查看>>
Cocos Studio 学习指引
查看>>
我的友情链接
查看>>