I have a Login method that accepts an ENUM e.g Customer.VALID or Customer.INVALID and I use cucumber to login with a valid or invalid customer using that, however I want to change it to just accept username/password for more re-use cases.
How do I handle this situation:
hit the login box with credentials (user/password) valid is obviously logged in, invalid is obviously not.
What kind of wait.until / if statement combo can I utilise to make this easy? using the ENUM i was certain if the login would be a pass or fail so I could easily just handle it with customer.VALID wait.until n
But how do I best handle this when Im not certain if they will be able to login or not? but at the same time making a robust enough method that will not be brittle/flaky and cater for both scenarios.
CustomerLoginPage.userNameField().clear();
CustomerLoginPage.passwordField().clear();
CustomerLoginPage.userNameField().sendKeys(userName);
CustomerLoginPage.passwordField().sendKeys(password);
CustomerLoginPage.loginButton().click();
if(customer.equals(customer.VALID)) {
wait.until(ExpectedConditions.titleContains("Welcome"));
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
wait.until(ExpectedConditions.visibilityOf(CustomerLoginPage.failedLoginLabel()));
}
}
How can I make the above A) Better and B) Suitable to a more reusable approach. Obviously waiting for an element that will only be present if successfully logged in will require some sort of logic in place there that isnt going to throw an exception if the user doesn't get logged in, this is what im most curious about and how to solve this with best practice. Something like this would work, but is it bad? I feel there is something better available.
if(loginPageUrl.equals(driver.getCurrentUrl())) {
wait.until(ExpectedConditions.visibilityOf(CustomerLoginPage.failedLoginLabel()));
} else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
wait.until(ExpectedConditions.titleContains("Welcome"));
}
}
Thanks