0

I've browsed over internet to get the relevant info but no luck. The example code is given below :

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

    }   
}

I want to know why we are returning the object while calling fillFormDetails(String firstname, String emailid) method ?

What could be the usecases so we can use this to manage our code efficiency ?

NarendraR
  • 7,577
  • 10
  • 44
  • 82

2 Answers2

1

The returned object is used when you want to us methods chaining

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

As a side note, better design is to split all the actions to separated methods, instead of just some of them

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

And in the test

HomePage homePage = new HomePage();
homePage
    .fillFirstName(firstName)
    .fillEmail(email)
    .fillPass(pass);
Guy
  • 46,488
  • 10
  • 44
  • 88
1

This is called method chaining. more here. https://en.wikipedia.org/wiki/Method_chaining#Java

Suppose you have three methods in one class to fill username, to fill password and to finally submit a form.

Normally you would create the object of the page and will call them separetly using the object. something like this,

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

however if you return the object of the same class this you can chain the methods in following way.

Sometimes it is convenient, and looks good :)

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

Now weather to use that in Automation or not, is entirely unto person and behavior of the application.

if you have a strict single flow, multiple flows are really hard to handle. This could be good.

For multiple flow. suppose when you submit this form, you either logged in or you stay on the same page which shows error. Now to do that you will have to handle this in Submit function with some logic. This sometime becomes complex and creates issue when you have to maintain it.

Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137