Frage

When testing website login without credentials we're getting a tooltip message that email and password should be entered. I need to make an assertion that the text of the error is relevant when we try to login without any credentials. Unfortunately,I'm not sure how can I do so.

You see, in the LoginPage class there is a variable for error message box: tool_tip = '.error-container'. And in the test_login_without_credentials I have added the message variable. But the error message box has no static error text and it is difficult for me to make right assertions for each case. For instance, if you have entered no credentials you receive the following message:

<div class="error-container">
    <ul>
       <li>
           <b>Email</b>
           : Value is required and can't be empty 
       </li>
       <li>
           <b>Password</b>
           : Value is required and can't be empty 
       </li>
    </ul>
</div>

Another example when you have entered invalid password:

<div class="error-container">
    <ul>
       <li>
           <b>Email</b>
           : '123' is no a valid email address in the basic format local-part@hostname
       </li>
       <li>
           <b>Password</b>
           : Value is required and can't be empty 
       </li>
    </ul>
</div>

And one more example when the invalid password is entered:

<div class="error-container">
    <ul>
       <li>
           <b>Log in</b>
           : Unknown user or invalid password given 
       </li>
    </ul>
</div>

I'm using the PageObject pattern for testing. Here is the part of the loginPage.py file containing the description of testing without credentials:

class LoginPage(object):

    login_page_link = 'Log in'
    email_field_locator = 'email'
    password_field_locator = 'password'
    login_button_locator = 'submit'
    tool_tip = '.error-container'

    def __init__(self, driver, base_url):
        self.driver = driver
        self.driver.get(base_url)

    def login_without_credentials(self, email = '', password = ''):
        self.driver.find_element_by_link_text(self.login_page_link).click()
        self.driver.find_element_by_id(self.email_field_locator).clear()
        self.driver.find_element_by_id(self.email_field_locator).send_keys(email)
        self.driver.find_element_by_id(self.password_field_locator).clear()
        self.driver.find_element_by_id(self.password_field_locator).send_keys(password)
        self.driver.find_element_by_id(self.login_button_locator).click()
        tooltip_message = WebDriverWait(self.driver, 10).until(lambda s: s.find_element_by_css_selector(self.tool_tip).text)
        return tooltip_message

And here is the testLogin.py itself:

class TestLoginLogout(object):

    @classmethod
    def setup_class(cls):
        cls.verificationErrors = []
        cls.driver = selenium_driver.connect()
        cls.driver.implicitly_wait(10)
        cls.base_url = selenium_driver.base_url
        cls.email = configs.EMAIL
        cls.password = configs.PASSWORD

    @classmethod
    def teardown_class(cls):
        cls.driver.quit()
        assert cls.verificationErrors == []

    def test_login_without_credentials(self):
        login_page = LoginPage(self.driver, self.base_url)
        message = login_page.login_without_credentials()

if __name__ == '__main__':
    pytest.main([__file__, "-s"])
War es hilfreich?

Lösung

The easiest way, I guess:

## in your LoginPage class
error_msg = {'no credentials': text1, 'wrong password': text2} ## and so on.
def assert_login_error_msg(message, error_type):
    assert message == self.error_msg[error_type], 'Wrong error message text for %s' % error_type

Then, after you call message = login_page.login_without_credentials():

login_page.assert_login_error_msg(message, 'no credentials')

UPD: You told that it's not easy to you to make right assertions. I think you must know if values you're going to enter are valid, so this shouldn'be a problem to chose right error message text.

UPD2: BTW, I'd suggest you to make a login method with (login, password, valid=True) args, and, for this case, make the if else section at the end:

if not valid:
    tooltip_message = WebDriverWait(self.driver, 10).until(lambda s: s.find_element_by_css_selector(self.tool_tip).text)
    return tooltip_message
else:
    return ## Or return some page class, where you should get after login
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top