Should Page Objects Return Page Objects?

爱⌒轻易说出口 提交于 2021-02-07 08:03:02

问题


We're currently working on building up a good test frame in our company. It's for a medium-to-large-sized webapp, perhaps with a couple dozen pages. We're currently writing mostly WebDriver Selenium UI-based tests.

We are trying to decide on some coding standards, and one thing we're discussing is whether to use Page Objects (PO) that always return PO (even if the page is the same), only return PO when you leave the current page for a new one, or even to not return PO. I've always thought returning PO is a key feature of the PO design pattern, but I may be incorrect about this.

Basically, we're trying to decide between the following patterns:

class SomePage {
     // constructor
     public SomePage(Driver) { //... } 

     // always return a page object
     public SomePage fillInTextField(String val){
          // details
          return new SomePage(driver);

     // only return a PO if it's a different page
     public void fillInTextField(String val){
          // details
          return;

     }

Is one preferable over the other?


回答1:


It's a matter of style.

Using:

class SomePage {
    ...
    // always return a page object
    public SomePage fillInTextField(String val){
         ...
         return this; // Note: NOT "new SomePage(driver)"
    }
}

allows you to write things like:

SomePage somePage = HoweverYouGetASomePage();
NextPage nextPage = somePage.fillInTextField1("value1").fillInTextField2("value2").submit();

Using:

class SomePage {
    ...
    // only return a PO if it's a different page
    public void fillInTextField(String val){
        ...
        return;
    }
}

forces you to write things like:

SomePage somePage = HoweverYouGetASomePage();
somePage.fillInTextField1("value1");
somePage.fillInTextField2("value2");
NextPage nextPage = somePage.submit();

Which one you like depends on ... which one you like. But if you like being able to write the former, you need to always return the page object.




回答2:


Short answer is that don't return same page objects if you are on the same page and state of the page is not changing. You would return new page objects if you are navigating from one page to another. It wouldn't make sense to return the same object when lets say you want to get some text or get a selected option from a page, since essentially nothing changed. If the state of the page is changing, then you would need to return the new page object otherwise you may likely face StaleElementException. In google docs, If you notice the LoginPage, getErrorMessage() does not return the same page object back

A little off from your original question, but I would recommend to use PageFactory, if you already aren't and are in the process of formalizing standards.




回答3:


I've found it extremely maintainable to do this. Especially with Java 8. Using default methods in Java 8, components and widgets now become Mixins. By always returning a Page, you end up with test cases that look like this:

public class HomePageITest {
  @Test
  public we_should_be_able_to_search_from_the_homepage() {
    pageFactory.getHomePage()
      .get()
      .doSomethingOnTheHomePage()
      .clickSearchWidgetSubmitButton()
      .doSomethingElseOnTheHomePage()      
    ;
  }
}

I have a blog post describing this in more detail here: http://blog.jsdevel.me/2015/04/pageobjects-done-right-in-java-8.html




回答4:


As the PageObject Selenium help page said "Methods return other PageObjects"



来源:https://stackoverflow.com/questions/17976491/should-page-objects-return-page-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!