'Selenium Hub, Launches 2 browser only runs test in 1 of them (selenium python)

TL/DR: Right now it launches 2 browsers but only runs the test in 1. What am I missing?

So I'm trying to get selenium hub working on a mac (OS X 10.11.5). I installed with this, then launch hub in a terminal tab with:

selenium-standalone start -- -role hub

Then in another tab of terminal on same machine register a node.

selenium-standalone start -- -role node -hub http://localhost:4444/grid/register -port 5556

It shows up in console with 5 available firefox and chrome browsers. enter image description here

So here's my code. In a file named globes.py I have this.

class globes:
    def __init__(self, number):
        self.number = number

    base_url = "https://fake-example.com"

    desired_cap = []
    desired_cap.append ({'browserName':'chrome', 'javascriptEnabled':'true', 'version':'', 'platform':'ANY'})
    desired_cap.append ({'browserName':'firefox', 'javascriptEnabled':'true', 'version':'', 'platform':'ANY'})
    selenium_server_url = 'http://127.0.0.1:4444/wd/hub'

Right now I'm just trying to run a single test that looks like this.

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from globes import *

class HeroCarousel(unittest.TestCase):

    def setUp(self):
        for driver_instance in globes.desired_cap:
            self.driver = webdriver.Remote(
                command_executor=globes.selenium_server_url,
                desired_capabilities=driver_instance)

        self.verificationErrors = []

    def test_hero_carousel(self):
        driver = self.driver
        driver.get(globes.base_url)

        hero_carousel = driver.find_element(By.CSS_SELECTOR, 'div.carousel-featured')
        try: self.assertTrue(hero_carousel.is_displayed())
        except AssertionError, e: self.verificationErrors.append("home_test: Hero Carousel was not visible")

    def tearDown(self):
        self.driver.close()
        self.assertEqual([], self.verificationErrors)


if __name__ == "__main__":
    unittest.main()

Right now it launches both Firefox and Chrome, but only runs the test in Firefox. Chrome opens and just sits on a blank page and doesn't close. So I figure there's something wrong with how I wrote the test. So what am I missing? I apologize if this is obvious but I'm just learning how to setup hub and just learned enough python to write selenium tests a couple weeks ago.

I think Hubs working as it launches both, but I did try adding a second node on the same machine on a different port and got the same thing. Just in case here's what hub prints out.

INFO - Got a request to create a new session: Capabilities [{browserName=chrome, javascriptEnabled=true, version=, platform=ANY}]
INFO - Available nodes: [http://192.168.2.1:5557]
INFO - Trying to create a new session on node http://192.168.2.1:5557
INFO - Trying to create a new session on test slot {seleniumProtocol=WebDriver, browserName=chrome, maxInstances=5, platform=MAC}
INFO - Got a request to create a new session: Capabilities [{browserName=firefox, javascriptEnabled=true, version=, platform=ANY}]
INFO - Available nodes: [http://192.168.2.1:5557]
INFO - Trying to create a new session on node http://192.168.2.1:5557
INFO - Trying to create a new session on test slot {seleniumProtocol=WebDriver, browserName=firefox, maxInstances=5, platform=MAC}


Solution 1:[1]

Forgive me if I am way off as I haven't actually worked with selenium, this answer is purely based on the issue related to only keeping the reference to the last created driver in setUp

Instead of keeping one self.driver you need to have a list of all drivers, lets say self.drivers, then when dealing with them instead of driver = self.driver you would do for driver in self.drivers: and indent all the relevent code into the for loop, something like this:

class HeroCarousel(unittest.TestCase):

    def setUp(self):
        self.drivers = [] #could make this with list comprehension
        for driver_instance in globes.desired_cap:
            driver = webdriver.Remote(
                command_executor=globes.selenium_server_url,
                desired_capabilities=driver_instance)
            self.drivers.append(driver)

        self.verificationErrors = []

    def test_hero_carousel(self):
        for driver in self.drivers:
            driver.get(globes.base_url)

            hero_carousel = driver.find_element(By.CSS_SELECTOR, 'div.carousel-featured')
            try: self.assertTrue(hero_carousel.is_displayed())
            except AssertionError, e: self.verificationErrors.append("home_test: Hero Carousel was not visible")

    def tearDown(self):
        for driver in self.drivers:
            driver.close()
        self.assertEqual([], self.verificationErrors)

Solution 2:[2]

You need to use self.driver.quit() because otherwise the browser will not quit and will only close the current window.

You will soon end-up with multiple browser running, and you will have to pay for them.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tadhg McDonald-Jensen
Solution 2 Community