How to run python webdriver test scripts using Grid2

Prerequisite:
  1. Java and python should be installed and setup path in your machine..
  2. Download xmlrunner.py from below url: http://pypi.python.org/pypi/XmlTestRunner/0.16654
  3. Download and install “setuptools 0.6c11” from below link: http://pypi.python.org/pypi/setuptools
  4. Install selenium 2 in your system using command: > easy_install selenium
  5. Selenium server file should be in machine
Set up and launching Selenium Grid:

      1. Run below command in on your console to launch Grid hub console:
java -jar selenium-server-standalone-2.21.0.jar -host localhost -port 4444 -role hub
       2. Open two console and run below command to launch and register node with Grid hub.
java -jar selenium-server-standalone-2.21.0.jar -host localhost -port 5555 -role webdriver -hub http://localhost:4444/grid/register -browser browserName=firefox,platform=WINDOWS

java -jar selenium-server-standalone-2.21.0.jar -host localhost -port 5556 -role webdriver -hub http://192.168.1.22:4444/grid/register -browser browserName=iexplore,platform=WINDOWS
           Replace local host to system IP of Hob machine. If you want to launch node in different machine.

Test script::

Put “xmlrunner.py” downloaded file in your project and create a scripts as like below script.

from selenium import webdriver
import unittest
import sys
from  xmlrunner import *

class Grid2(unittest.TestCase):
   
    capabilities = None
   
    def setUp(self):       
        self.driver = webdriver.Remote(desired_capabilities={           
            "browserName": browser,
            "platform":platform,
            "node":port
        })
        print "set up executed"
       
   
    def test_example(self):
        self.driver.get("http://www.google.com")
        print "open google url"
        self.assertEqual(self.driver.title, "Google")
        print "verify title"
        self.driver.find_element_by_id("gbqfq").clear()
        self.driver.find_element_by_id("gbqfq").send_keys("testing")
        print "enter text in serach field"
        self.driver.find_element_by_id("gbqfb").click()
        print "click on search button"   

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

if __name__ == "__main__":
    args = sys.argv   
    port  = args[1]
    platform  = args[2]
    browser = args[3]
    suite = unittest.TestSuite()
    suite.addTest(Grid2('test_example'))
    runner = XMLTestRunner(file('results_ExampleTestCase_%s.xml' % (browser), "w"))
    runner.run(suite)

Execution:

Open two consoles go to your test scripts project and run below command one by one.

python Grid2.py 5555 WINDOWS firefox
python Grid2.py 5556 WINDOWS iexplore

Where Grid2.py is created test script.
After execution two xml files are generated under project for each execution.

4 comments:

  1. Awesome post for Grid implementation with python....

    ReplyDelete
  2. This was really helpful for learning the Python syntax. Thanks!!!

    ReplyDelete
  3. I am looking for Nose Framework one is there any one help me
    Thanks in advance

    ReplyDelete
  4. Couldn't register this node: The hub is down or not responding: Connect to localhost:4444 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect

    I am getting this issue .....Help me please

    ReplyDelete

Leave your comments, queries, suggestion I will try to provide solution