Jagadeesh
Jagadeesh

Reputation: 143

How to use a method to call various methods inside and return true in selenium

I want to have a method public boolean verifySignIn() and call below methods inside and return true,

setUserName(String) This method is used to set the username. setPassword(String) This method is used to set the password. clickLogin() This method is used to click login button.

here is how my code looks now,

public boolean verifySignIn() 
{       
setUserName("user");
setPassword("admin123");
clickLogin();
return true;

}

Upvotes: 1

Views: 344

Answers (3)

StopTheRain
StopTheRain

Reputation: 387

I think to verify login the most important thing in this method is to check if clicking on that login button really takes you where you're supposed to get.

So, I'd do a method like:

public boolean verifySignIn() {
    setUserName("user");
    setPassword("admin123");
    return clickLogin();
  }

And then I'd define the clickLogin() method a bit better:

public boolean clickLogin() {
    WebElement loginButton = driver.findElement(By.id("someId"));
    loginButton.click;
    return !driver.findElements(By.id("someElementOnPage")).isEmpty();
  }

The solution with try/catch by DebanjanB is good, but the only thing it verifies is that there is no exception. It does not actually verify if the login is taking you to the correct page.

Upvotes: 0

Kovacic
Kovacic

Reputation: 1481

If I would do something like this, I would do something like this:

public boolean verifySignIn(){       
   return clickLogin(email, password);
}

method impl.

public boolean clickLogin(email, password){
    setUserName("user");
    setPassword("admin123");
    //do some assert, return result
    Assert.assertTrue(); // if it fails, You wont be able to execute any further.
    // or have some kind of check and return boolean

    return true;   
}

But anyhow try to use PageObject design and instead of returning boolean value .Return Page object (eg. main page) You logged in if everything is ok. So You can interact with next page, or just simply check it if You're successfully logged in, and thus extend and modularise Your testing.

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193058

To return true from verifySignIn() you can wrap the function within a try-catch{} block as follows:

public boolean verifySignIn() {  
       try{  
            setUserName("user");
            setPassword("admin123");
            clickLogin();
            return true;  
       }catch(Exception e){
            return false;
       }  
} 

Upvotes: 2

Related Questions