Page-Object思想
为什么要使用page-object
集中管理元素对象
集中管理一个page内的公共方法
后期维护方便
集中管理元素对象
实现方法:
调用方法:
WebElement element = driver.findElement(Test7.input);
Page类的实现
目的:
有了page类后,在具体的脚本中,要用到哪个page,就new这个page的对象,然后调用里面的公共方法即可
实现方法:
Page类的实现
说明:
Page类是一个基础类,其它的Page类都要继承该类,比如:
在具体的脚本中的使用方法:
具体代码如下:
Page.java具体代码:
1 package com.selenium.utils; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.WebDriver; 5 import org.openqa.selenium.WebElement; 6 import org.openqa.selenium.support.ui.ExpectedCondition; 7 import org.openqa.selenium.support.ui.WebDriverWait; 8 9 public class Page { 10 protected WebDriver driver; 11 12 public Page(WebDriver driver) { 13 this.driver = driver; 14 } 15 16 private boolean waitForToDisplayed(final By key) { 17 boolean waitDisplayed = new WebDriverWait(driver, 10) 18 .until(new ExpectedCondition<Boolean>() { 19 public Boolean apply(WebDriver d) { 20 return d.findElement(key).isDisplayed(); 21 } 22 }); 23 return waitDisplayed; 24 } 25 26 protected WebElement getElement(WebDriver driver, By key) { 27 WebElement element = null; 28 if (this.waitForToDisplayed(key)) { 29 element = driver.findElement(key); 30 } 31 return element; 32 } 33 34 }
DemoPage.java:
package com.selenium.utils; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; public class DemoPage extends Page { public static By input = By.id("user"); public static By link = By.xpath("//div[id='link']/a"); public static By select = By.cssSelector("select[name='select']"); public static By radio = By.name("identity"); public static By check = By.xpath("//div[@id='checkbox']/input"); public static By button = By.className("button"); public static By alert = By.className("alert"); public static By action = By.className("over"); public static By upload = By.id("load"); public static String iframe = "aa"; public static By multiWin = By.className("open"); public static By wait = By.className("wait"); public DemoPage(WebDriver driver) { super(driver); // TODO Auto-generated constructor stub } public void input(WebDriver Driver, String message) { WebElement element = this.getElement(Driver, input); element.sendKeys(message); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void input_clear(WebDriver Driver) { WebElement element = this.getElement(Driver, input); element.clear(); } }
IframePage.java:
package com.selenium.utils; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; public class IframePage extends Page { public static By input = By.id("user"); public static By link = By.xpath("//div[id='link']/a"); public static By select = By.cssSelector("select[name='select']"); public static By radio = By.name("identity"); public static By check = By.xpath("//div[@id='checkbox']/input"); public static By button = By.className("button"); public static By alert = By.className("alert"); public static By action = By.className("over"); public static By upload = By.id("load"); public static By iframe = By.xpath("//iframe[@name='aa']"); public static By multiWin = By.className("open"); public static By wait = By.className("wait"); public IframePage(WebDriver driver) { super(driver); // TODO Auto-generated constructor stub } public void input(WebDriver Driver, String message) { WebElement element = this.getElement(Driver, input); element.sendKeys(message); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void input_clear(WebDriver Driver) { WebElement element = this.getElement(Driver, input); element.clear(); } public void switchToIframe(WebDriver Driver) { WebElement element = this.getElement(Driver, iframe); Driver.switchTo().frame(element); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void swipeOutFromIframe(WebDriver Driver) { Driver.switchTo().defaultContent(); } }
具体用起来是这样的:
TestPageObject.java
package com.selenium.test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import com.selenium.utils.DemoPage; import com.selenium.utils.IframePage; public class TestPageObject { private WebDriver driver; @BeforeClass public void setUp() { driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.navigate().to("file:///D:/BaiduYunDownload/selenium2/demo.html"); } @AfterClass public void tearDown() { driver.close(); // driver.quit(); } @Test public void testInput() { DemoPage dp = new DemoPage(driver); dp.input(driver, "i love you"); dp.input_clear(driver); IframePage ifp = new IframePage(driver); ifp.switchToIframe(driver); ifp.input(driver, "iframe"); ifp.input_clear(driver); ifp.swipeOutFromIframe(driver); dp.input(driver, "i love you again"); dp.input_clear(driver); } }
最后打个广告,不要介意哦~
最近我在Dataguru学了《软件自动化测试Selenium2》网络课程,挺不错的,你可以来看看!要是想报名,可以用我的优惠码 G863,立减你50%的固定学费!
链接:http://www.dataguru.cn/invite.php?invitecode=G863