-3

In Java Selenium why do I need to instantiate the constructor in the method?

public class Navigation extends BasePage {
public Navigation(WebDriver driver) {
    super(driver);
}

public Navigation visitUsSite() {
    driver.navigate().to("https://www.randomwebsite.com"));
    return new Navigation(driver);
}
}

Isn't the above just as valid as below:

public class Navigation extends BasePage {
public Navigation(WebDriver driver) {
    super(driver);
}

public void visitUsSite() {
    driver.navigate().to("https://www.randomwebsite.com");
}
}
  • 3
    Why wouldn't you try it ? – Yassine Badache Dec 20 '17 at 10:52
  • One returns a `Navigation` the other returns nothing. If you don't use the return value, there may be no difference *to you*, but they are definitely not the same. Btw, you don't return a constructor, nor instantiate a constructor. You use (call/invoke) the constructor to instantiate an object. Not the constructor, but the object instance is returned from the method. – GolezTrol Dec 20 '17 at 10:53
  • I've executed both and they both work. I'm just curious as to why so many tutorials online suggest returning the constructer in the method? – Samranator Dec 20 '17 at 10:54
  • @Samranator it is a good coding practise based on the concept of pageobjects. When you navigate away from a current page by clicking on a button or link, you want to return the pageobject of the new page to the calling method. In your case you are returning the same page so seems like a overkill. – Grasshopper Dec 20 '17 at 11:28

1 Answers1

1

You don't have to return an instance of that class, but in many cases it's quite useful. As @Grasshopper wrote in the comments, it's useful to already create an instance of the class that corresponds to a new page that you're opening. For example, you have a Login page that contains the following: 1) username field 2) password field 3) login button

The methods for entering the username and the password are as following:

public class LoginPage extends BasePage {

    public LoginPage (WebDriver driver) {
        super(driver);
    }

    public LoginPage enterUsername(String username) {
        WebElement usernameField = driver.findElementBy...;
        usernameField.sendKeys(username);
        return this; // returns this instance because it stays on the LoginPage
    }

    public LoginPage enterPassword (String password) {
        WebElement passwordField = driver.findElementBy...;
        passwordField.sendKeys(password);
        return this; // returns this instance because it stays on the LoginPage
    }
}

Then you have a method that clicks on the login button, which redirects you to, let's say, homepage.

public HomePage clickLoginButton () {
        WebElement loginButton = driver.findElementBy...;
        loginButton.click();
        return new HomePage(driver); // returns a new instance on the HomePage because homepage will be opened
    }

The cool thing about returning 'this' instance and returning a new instance, is that in your test class, you can use your methods one after another, without having messy looking code:

LoginPage loginPage = new LoginPage(driver);
loginPage.enterUsername("username")
      .enterPassword("password");
HomePage homepage = loginPage.clickLoginButton();
StopTheRain
  • 367
  • 1
  • 5
  • 15
  • That was very helpful! Learnt something new and it's nice to that step definition code can be made to look less verbose. Many thanks to you and @Grasshopper – Samranator Dec 20 '17 at 13:41