断言错误文本是否正确

时间:2013-07-09 10:54:24

标签: python python-2.7 selenium-webdriver pageobjects

在没有凭据的情况下测试网站登录时,我们会收到一个工具提示消息,即应输入电子邮件和密码。当我们尝试在没有任何凭据的情况下登录时,我需要断言错误的文本是相关的。不幸的是,我不知道怎么能这样做。

你会看到,在LoginPage类中有一个错误消息框的变量:tool_tip = '.error-container'。在test_login_without_credentials我添加了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>

输入无效密码时的另一个例子:

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

输入无效密码时再举一个例子:

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

我正在使用PageObject模式进行测试。以下是loginPage.py文件的一部分,其中包含没有凭据的测试说明:

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

这是testLogin.py本身:

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

1 个答案:

答案 0 :(得分: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