我对python比较陌生。
我正在尝试使用硒网格上的pytest在不同的浏览器上以单个测试的形式在一个文件中运行所有测试。
我正在尝试两种方法,能够使用url启动5个不同的浏览器,但是测试没有继续进行,并且两种方法都出现错误。
方法1:
import logging
import ast
import unittest
import sys
import pytest
import traceback
import allure
import softest
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
logging.basicConfig(level = logging.INFO)
ch1_capabilities = {
"browserName": "chrome",
"version": "83.0",
"enableVNC": True,
"enableVideo": False
}
ch2_capabilities = {
"browserName": "chrome",
"version": "81.0",
"enableVNC": True,
"enableVideo": False
}
ch3_capabilities = {
"browserName": "chrome",
"version": "80.0",
"enableVNC": True,
"enableVideo": False
}
ch4_capabilities = {
"browserName": "chrome",
"version": "79.0",
"enableVNC": True,
"enableVideo": False
}
ch5_capabilities = {
"browserName": "chrome",
"version": "78.0",
"enableVNC": True,
"enableVideo": False
}
ch1_driver = webdriver.Remote(
command_executor="http://localhost:4444/wd/hub",
desired_capabilities=ch1_capabilities)
ch2_driver = webdriver.Remote(
command_executor="http://localhost:4444/wd/hub",
desired_capabilities=ch2_capabilities)
ch3_driver = webdriver.Remote(
command_executor="http://localhost:4444/wd/hub",
desired_capabilities=ch3_capabilities)
ch4_driver = webdriver.Remote(
command_executor="http://localhost:4444/wd/hub",
desired_capabilities=ch4_capabilities)
ch5_driver = webdriver.Remote(
command_executor="http://localhost:4444/wd/hub",
desired_capabilities=ch5_capabilities)
@pytest.mark.abc_test()
@pytest.mark.parametrize("driver_node", [ch1_driver,ch2_driver,ch3_driver,ch4_driver,ch5_driver])
class test_abc_Chrome(softest.TestCase):
@classmethod
def setUpClass(self):
self.driver=driver_node
self.driver.maximize_window()
self.driver.get('https://example.com')
sleep(7)
@allure.feature("Verify_XYZ")
@allure.description("verify xyz")
def test_000_Verify_XYZ(self):
logging.info("verify xyz")
with allure.step("verify xyz"):
sleep(1)
element = self.driver.find_element_by_xpath("//*[text()='expectedText']")
assert element.text == 'expectedText'
方法1的错误
@classmethod
def setUpClass(self):
> self.driver=driver_node
E NameError: name 'driver_node' is not defined
方法2:
import logging
import ast
import unittest
import sys
import pytest
import traceback
import allure
import softest
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
logging.basicConfig(level = logging.INFO)
@pytest.fixture(scope="class")
def setup(request):
ch1_capabilities = {
"browserName": "chrome",
"version": "83.0",
"enableVNC": True,
"enableVideo": False
}
ch2_capabilities = {
"browserName": "chrome",
"version": "81.0",
"enableVNC": True,
"enableVideo": False
}
ch3_capabilities = {
"browserName": "chrome",
"version": "80.0",
"enableVNC": True,
"enableVideo": False
}
ch4_capabilities = {
"browserName": "chrome",
"version": "79.0",
"enableVNC": True,
"enableVideo": False
}
ch5_capabilities = {
"browserName": "chrome",
"version": "78.0",
"enableVNC": True,
"enableVideo": False
}
ch1_driver = webdriver.Remote(
command_executor="http://localhost:4444/wd/hub",
desired_capabilities=ch1_capabilities)
ch2_driver = webdriver.Remote(
command_executor="http://localhost:4444/wd/hub",
desired_capabilities=ch2_capabilities)
ch3_driver = webdriver.Remote(
command_executor="http://localhost:4444/wd/hub",
desired_capabilities=ch3_capabilities)
ch4_driver = webdriver.Remote(
command_executor="http://localhost:4444/wd/hub",
desired_capabilities=ch4_capabilities)
ch5_driver = webdriver.Remote(
command_executor="http://localhost:4444/wd/hub",
desired_capabilities=ch5_capabilities)
driver_nodes = [ch1_driver,ch2_driver,ch3_driver,ch4_driver,ch5_driver]
for driver_node in driver_nodes:
driver = driver_node
request.instance.driver = driver
driver.maximize_window()
driver.get("https://example.com")
sleep(10)
yield driver
@pytest.mark.usefixtures("setup")
class test_abc_Chrome(softest.TestCase):
@allure.feature("Verify_XYZ")
@allure.description("verify xyz")
def test_000_Verify_XYZ(self):
logging.info("verify xyz")
with allure.step("verify xyz"):
sleep(1)
element = self.driver.find_element_by_xpath("//*[text()='expectedText']")
assert element.text == 'expectedText'
方法2的错误
for driver_node in driver_nodes:
driver = driver_node
> request.instance.driver = driver
E AttributeError: 'NoneType' object has no attribute 'driver'
我也在尝试通过改变这两种方法中的一些方法,但是还没有运气。
如果有人可以帮助我解决该问题,那将是一个很大的帮助。
答案 0 :(得分:0)
您尚未初始化WebDriver和 WebBrowser 实例。
在使用from webdriver_manager.chrome import ChromeDriverManager
时,您需要在def setUpClass(self):
内添加以下行:
driver = webdriver.Chrome(ChromeDriverManager().install())
有效地,您的代码块将是:
@classmethod
def setUpClass(self):
driver_node = webdriver.Chrome(ChromeDriverManager().install())
self.driver=driver_node
self.driver.maximize_window()
self.driver.get('https://example.com')