Selenium WebDriver教程-POM & Page Factory(下)

Selenium WebDriver教程-POM & Page Factory(下)

在本教程中你将学习

  • 什么是页面对象模式?
  • 为什么使用页面对象模型?
  • POM优点
  • 如何在项目中引入POM?
  • 什么是Page Factory?
  • 使用Page Factory优化测试项目
  • 页面工厂优点

在大型项目中针对页面对象管理的方式推荐PageFactory与PageObject配合使用

什么是Page Factory?

Page Factory是在POM模式基础上的优化。 引入PageFactory后,我们可以使用@FindBy注释来查找定位WebElement。使用initElements方法初始化Web元素。、

记着添加一张图片

@FindBy可以使用id、name、css、className、xpath 等等作为属性。

使用Page Factory实现上节中的例子

百度首页类

  1. package PageFactory;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.PageFactory;
    
    public class BaiduHomePage {
    
    /*
    *
    * 当前页面中的所有元素都使用@FindBy注解
    *
    * */
    
    WebDriver driver;
    
    //文本框
    @FindBy(name = "wd")
    WebElement inputText;
    
    //搜索按钮
    @FindBy(id = "su")
    WebElement searchButton;
    
    public BaiduHomePage(WebDriver driver){
    this.driver = driver;
    //初始化当前页面类的所有元素
    PageFactory.initElements(driver,this);
    
    }
    
    //输入关键字
    public void setKeyWords(String keyWords){
    
    inputText.sendKeys(keyWords);
    
    }
    
    //点击搜索按钮
    public void clickSearchButton(){
    
    searchButton.click();
    
    }
    }

搜索结果类

package PageFactory;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;


public class SearchResultPage {

/*
*
* 当前页面中的所有元素都使用@FindBy注解
*
* */

WebDriver driver;

//文本框
@FindBy(name = "wd")
WebElement inputText;
//搜索按钮
@FindBy(id = "su")
WebElement searchButton;
//下一页按钮
@FindBy(xpath = "//div[@id=\"page\"]/a[10]")
WebElement nextPage;
//页面标题
@FindBy(xpath = "/html/head/title")
WebElement titleText;

public SearchResultPage(WebDriver driver){

this.driver = driver;
//初始化当前页面类的所有元素
PageFactory.initElements(driver,this);
}

//输入关键字
public void setKeyWords(String keyWords){

inputText.sendKeys(keyWords);

}

//点击搜索按钮
public void clickSearchButton(){

searchButton.click();

}
//获取标题
public String getTitleText(){

return titleText.getText();
}

//翻页
public void clickNextPage(){

nextPage.click();


}
}
测试用例
package testSuites;

import PageFactory.BaiduHomePage;
import PageFactory.SearchResultPage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


import java.util.concurrent.TimeUnit;


public class PageFactoryDemo {
WebDriver driver;

BaiduHomePage homePage;

SearchResultPage resultPage;

@BeforeClass
public void setUp() throws Exception{
System.setProperty("webdriver.ie.driver",System.getProperty("user.dir")+"\\driver\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.baidu.com");
}
@Test
public void testSearch() throws InterruptedException {
//创建首页对象
homePage = new BaiduHomePage(driver);
//输入搜索关键字
homePage.setKeyWords("测试");
//点击搜索按钮
homePage.clickSearchButton();
//创建搜索结果页对象
resultPage = new SearchResultPage(driver);
Thread.sleep(3000);
//获取搜索页标题
String titleText = resultPage.getTitleText();
System.out.println(titleText);
//验证页面标题中是否包含测试关键字
Assert.assertTrue(titleText.contains("测试"));
}

@AfterClass
public void tearDown(){

driver.quit();
}
}

总结

  1. Page Factory 是POM模式的优化
  2. 通过注解的方式定位元素
  3. 通过页面类生成对象的时候会进行页面元素初始化操作
  4. 通过观察可以发现我们用例层没有变化,只是帮助我们更好的优化管理页面对象库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值