Я относительно новичок в python.
Я пытаюсь запустить весь тест в файле как один тест в разных браузерах, используя pytest на сетке селена (который в данном случае основан на селеноиде) ).
Я пытаюсь использовать два подхода, чтобы запустить 5 разных браузеров с URL-адресом, но тесты не продолжаются, и я получаю ошибку при обоих подходах.
Подход 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'
I Я тоже пытаюсь со своей стороны изменить некоторые вещи в обоих подходах, но пока не повезло.
Было бы здорово, если бы кто-нибудь мог мне помочь в решении этой проблемы.