qasimzee
qasimzee

Reputation: 650

Multiple browsers in Selenium with Python

I am trying to test a chat application that need two different browser sessions. I tried this in the following way:

Following is the code:

from selenium import selenium
from selenium import webdriver
from threading import Thread
import unittest, time, re
import time


class envolveChatCheck(unittest.TestCase):

    def get_sauce_browser(self, port=4444, browser="*firefox"):
        return selenium('localhost', port, browser, 'http://example.com/')

    def get_browser_and_wait(self, browser, browser_num):
        print "starting browser %s" % browser_num
        browser.start()
        browser.open("/")
        print "browser %s ready" % browser_num

    def setUp(self):
        self.verificationErrors = []
        self.b1 = self.get_sauce_browser(browser='*googlechrome', port=4444)
        self.b2 = self.get_sauce_browser(browser='*firefox', port=4444)
        print "all browsers ready"

    def test_envolve_chat_check(self):
        print "starting b1"
        self.b1.start()
        print "starting b2"
        self.b2.start()

        print "opening homepage b1"
        self.b1.open("/")
        self.b1.wait_for_page_to_load("30000")

        print "opening homepage b2"
        self.b2.open("/")
        self.b2.wait_for_page_to_load("30000")


    def tearDown(self):
        self.b1.stop()
        self.b2.stop()
        self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
    unittest.main()

Following is the error I am getting:

Traceback (most recent call last):
File "envolveChatCheck.py", line 32, in test_envolve_chat_check
self.b1.open("/")
File "/usr/local/lib/python2.6/dist-packages/selenium/selenium.py", line 774, in open
self.do_command("open", [url,ignoreResponseCode])
File "/usr/local/lib/python2.6/dist-packages/selenium/selenium.py", line 214, in do_command
raise Exception, data
Exception: ERROR: Got a null result

Any hints about this?

Upvotes: 2

Views: 4153

Answers (1)

Mitchell Malpartida
Mitchell Malpartida

Reputation: 141

My suggestion would be to run your script in Jenkins(Hudson) using a multi-config project

  1. Download and install Jenkins.
  2. Go to Manage Plugins.
  3. Under Available Plugins select "Python Plugin".
  4. Optional (There are various Selenium specific plugins as well, though I havent used them effectively. Assumed you already have Selenium on system)
  5. Create a new job and select "Build multi-configuration project".
  6. Under the heading "Configuration Matrix" click on the "Add Axis".
  7. Select "User-defined Axis".
  8. Set the "Name" of the Axis something like "BROWSERS".
  9. Set the "Values" to be the browsers you want to test with separated by a space. Ex: firefox "internet explorer" chrome
    Note: I put quotes around to make sure internet explorer is treated as one value
  10. Click "Add Build Step"
  11. Select "Execute Python Script"

In the python block you can insert your code and to pull the browser values set earlier:

#!/usr/bin/python
import os
BROWSER = os.getenv('BROWSERS')

What happens is that Jenkins will create separate jobs using the values of defined in the Axis created earlier and your python script will have that value injected for each job.

You can further extend this with other methods but this will at least help you focus on the test case logic and not worry as much about execution. The example above was based on Linux.

Note: I use SeleniumGrid plugin with a Windows slave to run my IE test scripts from Linux.

Upvotes: 5

Related Questions