1

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

1

Tests should only test one use case of your product. It's bad practice to expect variance in this context as the test should either test a correct login or an incorrect login.

In my opinion you should write two tests that test both these cases and should not mix them.

  • Why is it a bad practice for you? Reading and maintaining such tests was a nightmare for me, but I wonder why it's a bad practice for you. – dzieciou Apr 17 '18 at 13:09
  • 1
    If you start mixing tests it becomes a nightmare to perform maintenance. When tests are properly distinguished from one another you can quickly assess what didn't go according to the app's standards. – Runescape Apr 17 '18 at 14:09
0

You could reuse the waitUntil using a timeout, meaning it will wait only for the specified amount of time to be logged in before failing login. You might consider adding a log message telling you which user & password was unable to login. Finally, I'd highly recommend making sure you know for sure which users can, or cannot, login - to prevent lots of flaky tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.