0

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}
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Cynic
  • 6,779
  • 2
  • 30
  • 49
  • 1
    `self.driver = ...` inside the for loop will only keep a reference to the last one... – Tadhg McDonald-Jensen May 27 '16 at 17:40
  • so it launches chrome and sets `self.driver` to the `webdriver.Remote` and then overrides `self.driver` with the instance for firefox instead, losing the reference to the chrome instance. – Tadhg McDonald-Jensen May 27 '16 at 17:41
  • Thanks for the comment, it seems like that would explain it but how do I launch multiple Webdriver instances and keep them linked. I was kind of trying to follow this [example](http://stackoverflow.com/questions/9453327/using-selenium-web-driver-to-run-test-on-multiple-browsers) but don't really know Java so I don't get why mine differs from it. I've seen example where people just pass in the browser in the command line but [people](http://stackoverflow.com/questions/14991393/how-do-i-set-up-a-selenium-grid-python-test-case-to-test-across-multiple-machine) suggest against that. – Cynic May 31 '16 at 18:17
  • I've also seen people specify driver1 and driver2 then duplicate their tests for each browser but it seems like there's got to be a better way than that. – Cynic May 31 '16 at 18:18

2 Answers2

2

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)
Tadhg McDonald-Jensen
  • 20,699
  • 5
  • 35
  • 59
1

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.

Community
  • 1
  • 1
sorin
  • 161,544
  • 178
  • 535
  • 806
  • Thanks for the tip, but this does not help with it not running the test in Chrome, the reason the window's not closing is because the test doesn't run in chrome. – Cynic May 31 '16 at 17:52