从 selenium 导入 webdriver 从 selenium.webdriver.common.by 导入 从 selenium.webdriver.edge.service 导入服务 导入 pytest
@pytest.fixture()
def wrong_login_test():
website = 'https://www.saucedemo.com'
path = "/Users/Bernardo Cabrera/PycharmProjects/Project/resource/msedgedriver.exe"
options = webdriver.EdgeOptions()
options.add_experimental_option("Detach", True)
service = Service(executable_path=path)
driver = webdriver.Edge(service=service)
driver.get(website)
driver.implicitly_wait(5)
driver.maximize_window()
username= driver.find_element(By.XPATH, '//*[@id="user-name"]')
password= driver.find_element(By.XPATH, '//*[@id="password"]')
login= driver.find_element(By.XPATH, '//*[@id="login-button"]')
username.send_keys("username")
password.send_keys("password")
login.click()
badicon = driver.find_element(By.CLASS_NAME, 'error-button')
assert badicon.is_displayed()
driver.save_screenshot('FailedLogin')
不能直接使用夹具作为测试。为了让你的方法可以被 pytest 收集,你的名字应该以
test_
开头。在您的情况下,保留您的固定装置,但拆分您的测试并在测试方法中调用固定装置。
在你的固定装置改变中:
assert badicon.is_displayed()
致:
yield badicon
然后创建一个测试方法:
def test_wrong_login(wrong_login_test):
assert wrong_login_test.is_displayed()