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

发布于 2024-10-30 18:25:32 字数 896 浏览 1 评论 0原文

我一直在使用 selenium 1,但现在想迁移到 selenium2/webdriver。说实话,我觉得 selenium2/webdriver 入门有点困难。本质上我不知道如何在页面对象之间工作。这是我的示例:

public class LoginPage {
    private final WebDriver driver;

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

    public void 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();                  
    }

    public static void main(String[] args){
        LoginPage login = new LoginPage(new FirefoxDriver());
        login.loginAs("user", "pass");
    }
}

现在,用户登录后,会重定向到不同的页面。据我了解,我现在应该创建一个代表当前页面的新页面对象......事实是我不知道怎么做?我在哪里可以找到一些超越“hello world”级别的良好工作示例? 我应该如何继续这个例子?

提前致谢!

I've been using selenium 1, but now want to migrate to selenium2/webdriver. To be honest, I find a little bit difficult to start with selenium2/webdriver. In essence I don't know how to work between page objects. Here is my example:

public class LoginPage {
    private final WebDriver driver;

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

    public void 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();                  
    }

    public static void main(String[] args){
        LoginPage login = new LoginPage(new FirefoxDriver());
        login.loginAs("user", "pass");
    }
}

Now, after user is logged in, a redirection to different page occurs. As far as I understand, I should now make a new page object that represents current page... The fact is I don't know how? Where can I find some good working examples which are going beyond "hello world" level?
How should I continue this example?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南汐寒笙箫 2024-11-06 18:25:32

这些网站都提供了一些示例:

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

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

public class MainPage 
{ 
  private final WebDriver driver;  

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

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

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

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);                   
}

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

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;     
  }
} 

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);                   
}
悲欢浪云 2024-11-06 18:25:32

这个问题很老了,但我认为它仍然值得分享。

一般来说,我会首先创建所需的页面对象类。
然后,我为测试逻辑创建一个单独的类,您可以在其中放置点击和其他页面交互的“用户工作流程”。根据提供的示例代码,我假设此类将替换 main()。这也是我包含 testNG/junit、测试注释和 dataProviders 等内容的类(不是严格要求的,但如果您使用这些内容,注释可能会有所帮助)在此类中,您可以实例化页面的类您将根据需要与它们进行交互,因为您创建的 webdriver 对象控制浏览器,而不是页面类。

通过这种方式进行操作,可以对测试工作流程以及页面对象进行简单的更改,以防实际页面发生更改,或者您只是有新的测试需求。

我最喜欢这种方法的副作用是,带有工作流的类可以是一个非常可读的测试“脚本”,实际测试中的所有丑陋细节都隐藏在像 loginPage.Login() 和 loginPage.LoginSucceeded() 这样的调用下因此,临时通行证不会看到用户凭据查找、处理 404/400、查找并单击登录按钮等的详细信息。

This question is pretty old, but I thought it might still be worth sharing.

Generally, I'll first create the required page object classes.
Then I create a separate class for the test logic, where you would put your 'user workflow' of clicks and other page interactions. From the sample code provided, I'm assuming that this class would replace main(). This is also the class where I include things like testNG/junit, test annotations, and dataProviders (not strictly required, but if you use those things, that may be helpful to note) In this class, you can instantiate the classes for the pages you will interact with as you need them since the webdriver object you created controls the browser, not the page classes.

Doing things this way allows for simple changes to test workflows, and also to the page objects in case the actual pages are changed, or you just have new test requirements.

My favorite side effect of this method is that the class with the workflow can be a very readable 'script' of the test with all of the ugly details in the actual tests hidden under calls like loginPage.Login() and loginPage.LoginSucceeded() so a casual pass doesn't see the details of user credential lookups, handling 404's/400's, finding and clicking the login button, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文