在本教程中你将学习
什么是页面对象模式?
为什么使用页面对象模型?
POM优点
如何在项目中引入POM?
什么是Page Factory?
使用Page Factory优化测试项目
页面工厂优点
Page Object Model是一种为Web UI元素创建对象仓库的设计模式。使用此设计模式,为Web应用程序中每个页面创建对应的页面类。该页面类提供了查找该页面上所有元素的方法及元素对应的操作方法。
使用Selenium进行Web UI自动化测试相对来说比较简单,你只需要找到要对应的元素,操控它即可。
通过上面简单的示例,我们发现在脚本中我们所做的只是找到元素,为元素赋值填充内容。 针对这个小脚本维护起来很简单,但是随着我们在项目中增加更多的测试用例,代码会越来越多,维护也越来越困难。 例如:在多个脚本文件中都需要进行登录操作,一旦元素发生改变,我们需要同时维护更新多个脚本,很容易出现错误。 更好的脚本维护方法是创建单独的类,通过该类找到Web元素,操控元素,验证元素。这样我们就可以在脚本中重用此类,将来如果Web元素发生了变化,我们只需要在该类文件中维护更改即可,不需要在其他引用该类的代码中进行修改维护。
这种方法我们称为 页面对象模型(POM),提高代码的可维护性、可读性、重用性。
页面元素操作与流程、结果验证分离。代码清晰,易于理解。
页面对象类独立于测试用例脚本。这样我们可以很方便的与其他的测试框架集成。
类中提供的方法与真实的操作映射关系清晰,提高代码易读性。
测试用例: 打开百度首页,搜索指定关键字,验证搜索页面标题是否包含关键字 第一步:打开百度首页 第二步:输入查询的“测试”关键字,点击“百度一下”按钮 第三步:页面跳转至搜索结果页 第四步:检查搜索页标题是否包含“测试” 通过上面的用例步骤我们发现涉及到2个页面:
百度首页
搜索结果页
因此我们需要创建2个POM类
百度首页类
package pageObject; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; public class BaiduHomePage { WebDriver driver; //文本框 By inputText = By.name("wd"); //搜索按钮 By searchButton = By.id("su"); public BaiduHomePage(WebDriver driver){ this.driver = driver; } //输入关键字 public void setKeyWords(String keyWords){ driver.findElement(inputText).sendKeys(keyWords); } //点击搜索按钮 public void clickSearchButton(){ driver.findElement(searchButton).click(); } }搜索结果类
package pageObject; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; public class SearchResultPage { WebDriver driver; //文本框 By inputText = By.name("wd"); //搜索按钮 By searchButton = By.id("su"); //下一页按钮 By nextPage = By.xpath("//div[@id=\"page\"]/a[10]"); public SearchResultPage(WebDriver driver){ this.driver = driver; } //输入关键字 public void setKeyWords(String keyWords){ driver.findElement(inputText).sendKeys(keyWords); } //点击搜索按钮 public void clickSearchButton(){ driver.findElement(searchButton).click(); } //获取标题 public String getTitleText(){ return driver.getTitle(); } //翻页 public void clickNextPage(){ driver.findElement(nextPage).click(); } }测试用例
package testSuites; 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 pageObject.BaiduHomePage; import pageObject.SearchResultPage; import java.util.concurrent.TimeUnit; public class SearchTest { 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(); //验证页面标题中是否包含测试关键字 Assert.assertTrue(titleText.contains("测试")); } @AfterClass public void tearDown(){ driver.quit(); } }Page Object Model是Selenium WebDriver中的Object Repository设计模式。
POM帮助我们提高测试代码可维护,可重用。