import os
import sys

from django.test import LiveServerTestCase
from selenium import webdriver

RUN_LOCAL = ('SAUCE_USERNAME' not in os.environ)

if RUN_LOCAL:
    # could add Chrome, PhantomJS etc... here
    BROWSERS = ['Chrome']
else:
    from sauceclient import SauceClient
    USERNAME = os.environ.get('SAUCE_USERNAME')
    ACCESS_KEY = os.environ.get('SAUCE_ACCESS_KEY')
    sauce = SauceClient(USERNAME, ACCESS_KEY)

    BROWSERS = [
        {"platform": "Mac OS X 10.9",
         "browserName": "chrome",
         "version": "35"},
        {"platform": "Windows 8.1",
         "browserName": "internet explorer",
         "version": "11"},
        {"platform": "Linux",
         "browserName": "firefox",
         "version": "29"}]


def on_platforms():
    if RUN_LOCAL:
        def decorator(base_class):
            module = sys.modules[base_class.__module__].__dict__
            for i, platform in enumerate(BROWSERS):
                d = dict(base_class.__dict__)
                d['browser'] = platform
                name = "%s_%s" % (base_class.__name__, i + 1)
                module[name] = type(name, (base_class,), d)
            pass
        return decorator

    def decorator(base_class):
        module = sys.modules[base_class.__module__].__dict__
        for i, platform in enumerate(BROWSERS):
            d = dict(base_class.__dict__)
            d['desired_capabilities'] = platform
            name = "%s_%s" % (base_class.__name__, i + 1)
            module[name] = type(name, (base_class,), d)
    return decorator


class BrowserTest(LiveServerTestCase):

    def setUp(self):
        if RUN_LOCAL:
            self.setUpLocal()
        else:
            self.setUpSauce()

    def tearDown(self):
        if RUN_LOCAL:
            self.tearDownLocal()
        else:
            self.tearDownSauce()

    def setUpSauce(self):
        self.desired_capabilities['tunnel-identifier'] = \
            os.environ['TRAVIS_JOB_NUMBER']
        self.desired_capabilities['build'] = os.environ['TRAVIS_BUILD_NUMBER']
        self.desired_capabilities['tags'] = \
            [os.environ['TRAVIS_PYTHON_VERSION'], 'CI']
        self.desired_capabilities['name'] = self.id()

        sauce_url = "http://%s:%s@ondemand.saucelabs.com:80/wd/hub"
        self.driver = webdriver.Remote(
            desired_capabilities=self.desired_capabilities,
            command_executor=sauce_url % (USERNAME, ACCESS_KEY)
        )
        self.driver.implicitly_wait(5)

    def setUpLocal(self):
        self.driver = getattr(webdriver, self.browser)()
        self.driver.implicitly_wait(3)

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

    def tearDownSauce(self):
        print("\nLink to your job: \n "
              "https://saucelabs.com/jobs/%s \n" % self.driver.session_id)
        try:
            if sys.exc_info() == (None, None, None):
                sauce.jobs.update_job(self.driver.session_id, passed=True)
            else:
                sauce.jobs.update_job(self.driver.session_id, passed=False)
        finally:
            self.driver.quit()

@@ -1849,32 +1849,82 @@
 tUpSauce(self):%0A
+        if 'tunnel-identifier' in os.environ:%0A    
         self.des
@@ -1978,16 +1978,20 @@
         
+    
 os.envir
@@ -2006,32 +2006,36 @@
 IS_JOB_NUMBER'%5D%0A
+    
         self.des
@@ -2097,32 +2097,36 @@
 UMBER'%5D%0A        
+    
 self.desired_cap
@@ -2143,24 +2143,28 @@
 'tags'%5D = %5C%0A
+    
             

Fix Sauce tests