【问题标题】:What could be the use case to return a object in Page Factory design pattern? [duplicate]在页面工厂设计模式中返回对象的用例可能是什么? [复制]
【发布时间】:2017-08-09 13:11:04
【问题描述】:

我已通过互联网浏览以获取相关信息,但没有运气。示例代码如下:

public class HomePage 
{
    @FindBy(id = "fname")
    WebElement name;

    @FindBy(id = "email")
    WebElement email;

    @FindBy(id = "password")
    WebElement password;

    @FindBy(id = "passwordConf")
    WebElement confPassword;

    public HomePage fillFormDetails(String firstname, String emailid)
    {
        name.sendKeys(firstname);
        email.sendKeys(emailid);

        return this;
    }

    public void fillPass(String pass)
    {
        password.sendKeys(pass);

    }   
}

我想知道为什么我们在调用fillFormDetails(String firstname, String emailid) 方法时返回对象?

我们可以使用哪些用例来管理我们的代码效率?

【问题讨论】:

    标签: java selenium pageobjects page-factory


    【解决方案1】:

    当你想使用方法链接时使用返回的对象

    HomePage homePage = new HomePage();
    homePage
        .fillFormDetails(firstName, email)
        .fillPass(pass);
    

    附带说明一下,更好的设计是将所有操作拆分为单独的方法,而不是只拆分其中的一部分

    public HomePage fillFirstName(String firstName)
    {
        name.sendKeys(firstName);
    
        return this;
    }
    
    public HomePage fillEmail(String email)
    {
        email.sendKeys(email);
    
        return this;
    }
    
    public void fillPass(String pass)
    {
        password.sendKeys(pass);
    }
    

    在测试中

    HomePage homePage = new HomePage();
    homePage
        .fillFirstName(firstName)
        .fillEmail(email)
        .fillPass(pass);
    

    【讨论】:

      【解决方案2】:

      这称为方法链接。 更多在这里。 https://en.wikipedia.org/wiki/Method_chaining#Java

      假设您在一个类中有三个方法来填写用户名、填写密码并最终提交表单。

      通常您会创建页面对象并使用该对象单独调用它们。像这样的,

      MethodChaining methodChaining = new MethodChaining();
      methodChaining.fillUserName("username");
      methodChaining.fillPassword("password");
      methodChaining.submit();
      

      但是,如果您返回同一类的对象this,您可以按以下方式链接方法。

      有时它很方便,而且看起来不错:)

      public class MethodChaining {
      
          public MethodChaining fillUserName(String username){
              return this;
          }
      
          public MethodChaining fillPassword(String password){
              return this;
          }
      
          public MethodChaining submit(){
              return this;
          }
      
          public static void main(String args[]){
              MethodChaining methodChaining = new MethodChaining();
              methodChaining.fillUserName("abc")
                      .fillPassword("pass")
                      .submit();
          }
      }
      

      现在天气是否在自动化中使用它完全取决于应用程序的人和行为。

      如果你有一个严格的单一流程,那么多个流程真的很难处理。这可能很好。

      对于多流。假设当您提交此表单时,您要么登录,要么停留在显示错误的同一页面上。现在要做到这一点,您必须在Submit 函数中使用一些逻辑来处理这个问题。这有时会变得复杂并在您必须维护它时产生问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多