首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

怎么使用PageFactory

2012-06-26 
如何使用PageFactory一个简单的例子为了使用PageFactory,首先需要在PageObject(页面对象)中声明一些元素(W

如何使用PageFactory

一个简单的例子

为了使用PageFactory,首先需要在PageObject(页面对象)中声明一些元素(WebElements 或 RenderedWebElements),例如:

?

package org.openqa.selenium.example;import org.openqa.selenium.WebElement;public class GoogleSearchPage{    private WebElement q;    public void searchFor(String text)    {        q.sendKeys(text);        q.submit();    }}
?

为了让这段代码工作,而不是因为“Q”是不能实例化抛出一个NullPointerException,所以我们需要初始化PageObject:

?

package org.openqa.selenium.example;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.htmlunit.HtmlUnitDriver;import org.openqa.selenium.support.PageFactory;public class UsingGoogleSearchPage{    public static void main(String[] args)    {        WebDriver driver = new HtmlUnitDriver();        driver.get("http://www.google.com/");        GoogleSearchPage page = PageFactory.initElements(driver, GoogleSearchPage.class);        page.searchFor("Cheese");    }}

说明

?

?

热点排行