0

I am relatively new to 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).

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.

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:

 @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:

 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.

SRM21
  • 453
  • 5
  • 14

1 Answers1

0

You haven't initialized the WebDriver and WebBrowser instances.


Solution

As you are using from webdriver_manager.chrome import ChromeDriverManager you need to add the following line within 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')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I am sorry that I forgot to remove the unused import from ChromeDriver manager. I am using selenium grid and that's why I have used webdriver.Remote command. Now I have edited the question by removing the unused import. Please let me know how can I proceed with this. – SRM21 Jul 14 '20 at 09:41