在沒有憑據的情況下測試網站登錄時,我們收到一條工具提示消息,應該輸入電子郵件和密碼。我需要聲明,當我們嘗試在沒有任何憑證的情況下登錄時,錯誤的文本是相關的。不幸的是,我不知道我該怎麼做。斷言錯誤文本正確
您會在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 [email protected]
</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"])
我已經更新了問題的消息。對不起,如果不清楚。 – Showme