如何在python selenium中为每个测试启动单独的浏览器



在运行test_login.py时,应该启动3个浏览器并验证测试,但只有一个正在启动,第二个测试失败,因为第二个在第一个测试步骤后仍在继续。

***********下面的Conftest.py**********是Conftest文件

import pytest
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
@pytest.fixture(scope="class")
def setup(request):
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://itera-qa.azurewebsites.net/Login")
driver.maximize_window()
request.cls.driver = driver

yield
driver.close()

test_login.py/strong>此测试文件包含3个验证的测试

import pytest
from pages.loginpage import PageLogin

@pytest.mark.usefixtures("setup")
class TestLogin:
@pytest.fixture(autouse=True)
def class_setup(self):
self.tl = PageLogin(self.driver)

@pytest.mark.login
def test_login1(self):
self.tl.enter_user_name("sysadm")
self.tl.enter_password("sysadm")
self.tl.click_login()

def test_empty_user_pwd(self):
# self.driver.get(self.url)
self.tl.enter_user_name("")
self.tl.enter_password("")
self.tl.click_login()
if self.tl.error_text() == 'Login error: Enter your username.':
assert True
else:
assert False
def test_empty_pwd(self):
# self.driver.get(self.url)
self.tl.enter_user_name("sysadm")
self.tl.enter_password("")
self.tl.click_login()
if self.tl.error_text() == 'Login error: Enter your password.':
assert True
else:
assert False

*********PageLogin***********这是所有xpath都定义为的登录页面

import time
from selenium.webdriver.common.by import By

class PageLogin(SelectCustomer):
textbox_username_id = "user"
textbox_password_id = "pass"
button_login_xpath = "//input[@value='Log In']"
get_error_text_xpath = "//p[@class='error']"
def __init__(self, driver):
self.driver = driver
def enter_user_name(self, username):
self.driver.find_element(By.ID, self.textbox_username_id).send_keys(username)
def enter_password(self, password):
self.driver.find_element(By.ID, self.textbox_password_id).send_keys(password)
def click_login(self):
self.driver.find_element(By.XPATH, self.button_login_xpath).click()
def error_text(self):
returned_error_text = self.driver.find_element(By.XPATH, self.get_error_text_xpath).text
return returned_error_text
time.sleep(4)

在conftest.py中更改代码后解决了以下问题:能够为每个测试用例启动单独的浏览器

import pytest
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
driver = None

@pytest.fixture(autouse=True)
def setup(request, browser, url):
global driver
if browser == "chrome":
driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
elif browser == "firefox":
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())
elif browser == "edge":
driver = webdriver.Edge(EdgeChromiumDriverManager().install())
driver.get(url)
driver.maximize_window()
request.cls.driver = driver
yield
driver.close()

#this will get the value of CLI/Hooks
def pytest_addoption(parser):
parser.addoption("--browser")
parser.addoption("--url")

#This will return the browser value to setup method
@pytest.fixture(scope="class", autouse=True)
def browser(request):
return request.config.getoption("--browser")

#This will return the url value to setup method
@pytest.fixture(scope="class", autouse=True)
def url(request):
return request.config.getoption("--url")

相关内容

  • 没有找到相关文章

最新更新