[英]Getting errors when trying to run selenium tests using pytest for selenium grid
I am relatively new to python.我对 python 比较陌生。
I am trying to run all the test in a file as a single test on different browsers using pytest on selenium grid(which is based on selenoid in this case).我正在尝试使用 selenium 网格上的 pytest 将文件中的所有测试作为单个测试在不同的浏览器上运行(在这种情况下基于 selenoid)。
I am trying with two approaches, able to launch 5 different browsers with the url, but tests doesn't proceed further and I am getting error in both approaches.我正在尝试两种方法,能够使用 url 启动 5 个不同的浏览器,但是测试没有进一步进行,并且两种方法都出现错误。
Approach 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'
Error for Approach 1:方法 1 的错误:
@classmethod
def setUpClass(self):
> self.driver=driver_node
E NameError: name 'driver_node' is not defined
Approach 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'
Error for Approach 2:方法 2 的错误:
for driver_node in driver_nodes:
driver = driver_node
> request.instance.driver = driver
E AttributeError: 'NoneType' object has no attribute 'driver'
I am trying from my side as well by changing some things in the both the approaches but no luck yet.我也在尝试通过改变这两种方法中的一些东西,但还没有运气。
It would be a great help if anyone can help me regarding how to solve this problem.如果有人可以帮助我解决这个问题,那将是一个很大的帮助。
You haven't initialized the WebDriver and WebBrowser instances.您尚未初始化WebDriver和WebBrowser实例。
As you are using from webdriver_manager.chrome import ChromeDriverManager
you need to add the following line within def setUpClass(self):
:当您使用from webdriver_manager.chrome import ChromeDriverManager
时,您需要在def setUpClass(self):
中添加以下行:
driver = webdriver.Chrome(ChromeDriverManager().install())
Effectively, your code block will be:实际上,您的代码块将是:
@classmethod
def setUpClass(self):
driver_node = webdriver.Chrome(ChromeDriverManager().install())
self.driver=driver_node
self.driver.maximize_window()
self.driver.get('https://example.com')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.