分享

如何使用PageFactory

 小猪窝969 2015-09-25

一个简单的例子


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


 


Java代码 复制代码 收藏代码
  1. package org.openqa.selenium.example;  
  2.   
  3. import org.openqa.selenium.WebElement;  
  4.   
  5. public class GoogleSearchPage  
  6. {  
  7.     private WebElement q;  
  8.   
  9.     public void searchFor(String text)  
  10.     {  
  11.         q.sendKeys(text);  
  12.         q.submit();  
  13.     }  
  14. }  

 

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


 


Java代码 复制代码 收藏代码
  1. package org.openqa.selenium.example;  
  2.   
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.WebElement;  
  5. import org.openqa.selenium.htmlunit.HtmlUnitDriver;  
  6. import org.openqa.selenium.support.PageFactory;  
  7.   
  8. public class UsingGoogleSearchPage  
  9. {  
  10.     public static void main(String[] args)  
  11.     {  
  12.         WebDriver driver = new HtmlUnitDriver();  
  13.         driver.get("http://www.google.com/");  
  14.         GoogleSearchPage page = PageFactory.initElements(driver, GoogleSearchPage.class);  
  15.         page.searchFor("Cheese");  
  16.     }  
  17. }  

 

 


 


 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多