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

    xiaoxiao2022-07-12  147

    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实现上节中的例子

    百度首页类

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

    总结

    Page Factory 是POM模式的优化通过注解的方式定位元素通过页面类生成对象的时候会进行页面元素初始化操作通过观察可以发现我们用例层没有变化,只是帮助我们更好的优化管理页面对象库
    最新回复(0)