6

I have successfully set up SeleniumGrid to run my Python tests across multiple machines that have different OS and browsers. However, I am still having to write the same test case 3 times, once for each node, because the reference to the node is inside the test case.

I've looked at all sorts of online suggestions for Python eg. seperating node ips out into external files and importing this into the test case but none of them seem to work or the instructions are for Java.

With this one from Mozilla, I'm not sure how I set this file up with my test cases/how to run it: http://viewvc.svn.mozilla.org/vc/projects/sumo/tests/frontend/python_tests/suite_sumo.py?view=markup

How do I set my Python test cases up so I only write it once?

My Hub command prompt instruction is:

java -jar selenium-server-standalone-2.29.0.jar -host http://localmachineipaddress -port 4444 -role hub

My Nodes command prompt instructions are:

*FireFox PC, Chrome PC, Safari PC, and IE9 PC on local machine*
 java -jar selenium-server-standalone-2.29.0.jar -host localhost -role webdriver -hub http://theHubIP:4444/grid/register  -port 5555 -browser browserName=firefox,maxInstances=5,platform=WINDOWS -browser browserName=chrome,maxInstances=5,platform=WINDOWS  -Dwebdriver.chrome.driver=c:\SeleniumGrid\chromedriver.exe -browser browserName=iehta,maxInstances=5,platform=WINDOWS -Dwebdriver.ie.driver=c:\SeleniumGrid\IEDriverServer.exe -browser browserName=safari,maxInstances=5,platform=WINDOWS -Dwebdriver.safari.driver=c:\Python27\SafariDriver2.28.0.safariextz    

*FireFox MAC, Safari MAC, and Chrome MAC machine*
java -jar selenium-server-standalone-2.29.0.jar -role webdriver -hub http://theHubIP:4444/grid/register -debug -port 5556 -browser browserName=firefox,maxInstances=5,platform=MAC -browser browserName=chrome,maxInstances=5,platform=MAC -browser browserName=safari,maxInstances=5,platform=MAC -Dwebdriver.safari.driver=c:\Python27\SafariDriver2.28.0.safariextz 

*IE8 PC machine*
java -jar selenium-server-standalone-2.29.0.jar  -role webdriver -hub http://theHubIP:4444/grid/register -port 5559 -browser browserName=iehta,maxInstances=5,platform=WINDOWS -Dwebdriver.ie.driver=c:\SeleniumGrid\IEDriverServer.exe   

My Test Case command prompt instructions are:

python Python27/Test_Cases/SeleniumTest.py 5555 firefox WINDOWS 
python Python27/Test_Cases/SeleniumTest.py 5555 chrome WINDOWS
python Python27/Test_Cases/SeleniumTest.py 5555 iehta WINDOWS
python Python27/Test_Cases/SeleniumTest.py 5555 safari WINDOWS
python Python27/Test_Cases/SeleniumTestIE8.py 5559 iehta WINDOWS
python Python27/Test_Cases/SeleniumTestApple.py 5556 chrome MAC
python Python27/Test_Cases/SeleniumTestApple.py 5556 firefox MAC
python Python27/Test_Cases/SeleniumTestApple.py 5556 safari MAC

My Test Case is:

# coding: utf-8
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.keys import Keys
import HTMLTestRunner
import unittest, time
import sys

class SeleniumTest1(unittest.TestCase):
    def setUp(self):
    self.driver = webdriver.Remote(command_executor="http://theNodeIP:5555/wd/hub",desired_capabilities={ "browserName": browser, "platform": platform, "node":node })
   self.driver.implicitly_wait(2)

def mytest(self):
    self.driver.get("http://url.com")
    self.driver.find_element_by_css_xpath("test_some_stuff").click()

def tearDown(self):
    self.driver.quit()

def suite():
    s1 = unittest.TestLoader().loadTestsFromTestCase(SeleniumTest1)
    return unittest.TestSuite([s1])

def run(suite, report = "C:\\Python27\\Test_Cases\\Reports\\SeleniumTest1.html"):
with open(report, "w") as f:
    HTMLTestRunner.HTMLTestRunner(
                stream = f,
                title = 'SeleniumTest1',
                verbosity = 2,
                description = 'SeleniumTest1'
                ).run(suite)

if __name__ == "__main__":  
args = sys.argv

node=args[1]

browser = args[2]

platform = args[3]

run(suite())
user2093231
  • 61
  • 1
  • 3
  • Why not just put the repeated test code into function e.g. def someRepeatTest(webdriver): #your code lines go here... – Nam G VU Jul 16 '17 at 09:29

2 Answers2

2

I was able to test two browsers at once using the nose_parameterized module. (You don't need to use the nose test runner to use the nose_parameterized module.)

from django.test import LiveServerTestCase
from nose_parameterized import parameterized
from selenium import webdriver


class UITest(LiveServerTestCase):

    def setUp(self):
        self.selenium = {
            'chrome': webdriver.Chrome(),
            'firefox': webdriver.Firefox(),
        }

    def tearDown(self):
        for browser in self.selenium:
            self.selenium[browser].quit()

    testdata = [
        ('chrome',),
        ('firefox',),
    ]

    @parameterized.expand(testdata)
    def test_something(self, browser):
        driver = self.selenium[browser]
        # [...]

To use Selenium Grid, as your question asks, just change the webdrivers to suit.

Travis
  • 1,998
  • 1
  • 21
  • 36
  • As you can "successfully set up SeleniumGrid to run my Python tests", please help me with my issue here https://stackoverflow.com/q/45127482/248616. Great thanks! – Nam G VU Jul 16 '17 at 10:35
1

Instead of passing the arguments for browser and platform through your shell call, you can have your Python script read a configuration file. Essentially, you would have a config file that lists the browsers that you'd like to run on along with a list of platforms.

The trick is that you need to have a higher level suite file that will call the other tests with every combination. So, you would have a suite file that polls this config file for browser and platform combinations, executing suites with the different combos.

You can even parallelize the test execution if there is multi-thread support in Python.

For example, in Ruby, I would be reading my configuration from a .yml file and then execute rake calls in multiple threads with each browser platform combination.

Dan Chan
  • 494
  • 4
  • 14