0

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"])
4

1 回答 1

0

最简单的方法,我猜:

## 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

然后,在您致电后message = login_page.login_without_credentials()

login_page.assert_login_error_msg(message, 'no credentials')

UPD:你说过做出正确的断言对你来说并不容易。我认为您必须知道要输入的值是否有效,因此选择正确的错误消息文本应该是个问题。

UPD2:顺便说一句,我建议您使用(login, password, valid=True)args 创建登录方法,并且在这种情况下,请在末尾添加 if else 部分:

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
于 2013-07-10T10:51:43.720 回答