简体   繁体   English

尝试使用 selenium 网格的 pytest 运行 selenium 测试时出现错误

[英]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.您尚未初始化WebDriverWebBrowser实例。


Solution解决方案

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.

相关问题 使用 pytest 在不同浏览器上运行 Selenium 测试 - Running Selenium Tests on different browsers using pytest 无法运行 selenium pytest 测试与 webdriver-manager 使用 docker 容器与 Z23EEEB37947BDD25BDDZ6: - Unable to run the selenium pytest tests with webdriver-manager using the docker container with python:3.9-alpine image Python Selenium-我正在尝试使用pytest框架,遇到错误 - Python Selenium - I'm trying to use pytest framework , running into errors 无法运行 python selenium 测试 - Trouble getting python selenium tests to run 尝试在 Docker 容器中运行 Selenium-pytest 时出现 KeyError - KeyError while trying to run Selenium-pytest in Docker Container 转发新的 session 时出现错误找不到:使用远程 Webdriver 在 Python 中运行 Selenium 网格时的功能 - Getting Error forwarding the new session cannot find : Capabilities while trying to run Selenium Grid in Python using Remote Webdriver 我在尝试使用 pytest for FastAPI + Gino 应用程序运行测试时遇到一个奇怪的错误 - I'm getting a weird error trying to run tests using pytest for FastAPI + Gino app 如果大于 50% 的 Selenium 测试通过,则 Pytest 总体通过 - Pytest pass overall if >50% of Selenium tests pass 使用appium和selenium网格运行自动化测试只能在一台设备上运行 - Running automate tests with appium and selenium grid only run in one device 尝试打开 Firefox 以通过 Django 的 manage.py 运行 Selenium 测试时权限被拒绝 - Permission denied when trying to open Firefox to run Selenium tests via Django's manage.py
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM