且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Selenium2和webdriver的一个很好的工作示例

更新时间:2023-11-16 16:09:16

这些网站都举了一些例子:

These sites both give some examples:

http://luizfar.wordpress.com/2010/09/29/page-objects/

http:// www .wakaleo.com / blog / selenium-2-web-driver-the-land-where-page-objects-are-king

此页面给出有关使用PageFactory支持页面对象的一些详细信息:
http:// code。 google.com/p/selenium/wiki/PageFactory

This page gives some details on using PageFactory to support page objects: http://code.google.com/p/selenium/wiki/PageFactory

您可以通过为每个页面创建一个类来扩展您的示例以使用页面对象,例如:

You could extend your example to work with page objects by creating a class for each page, e.g.:

public class MainPage 
{ 
  private final WebDriver driver;  

  public MainPage(WebDriver driver) 
  {     
    this.driver = driver;  
  }   

  public void doSomething() 
  {      
    driver.findElement(By.id("something")).Click;     
  }
} 

并更改loginAs以返回代表页面的类浏览器在登录后导航到:

and changing loginAs to return a class that represents the page that the browser navigates to after login:

public MainPage loginAs(String username, String password) 
{       
    driver.get("http://url_to_my_webapp");             
    driver.findElement(By.id("username")).sendKeys(username);     
    driver.findElement(By.id("pwd")).sendKeys(password);     
    driver.findElement(By.className("button")).submit();
    // Add some error checking here for login failure
    return new MainPage(driver);                   
}