class SeleniumDriverPlugin(plugins.Plugin): '''Provide a WebDriver to the tests.''' name = 'selenium-driver' def options(self, parser, env): ''' Add plug-in options. ''' # Call super super(SeleniumDriverPlugin, self).options(parser, env) # Add environment input parser.add_option('--selenium-driver', action='store', dest='env', help='Enable the provided environment.') def configure(self, options, conf): ''' Configure the plug-in. ''' # Call super super(SeleniumDriverPlugin, self).configure(options, conf) # Check if enabled if self.enabled: # Check if an environment is provided if not options.env: # Not provided, raise raise ValueError('please provide a driver environment') # Get the environment self.env = DriverConfig(all_config_files(options, conf)).getenv(options.env) def eligible(self, test): ''' Check if this is a SELENIUM test. ''' # Check if this test has a context and that the flag is available return hasattr(test, 'context') and getattr(test.context, 'enable_selenium_driver', False) def startTest(self, test): ''' Before test starts, create a driver. ''' # Check if this is a SELENIUM test if self.eligible(test): # If it is, inject a driver if hasattr(self.env, 'set_test_name'): self.env.set_test_name(str(test)) # handle situation where provider limits number of concurrent selenium # sessions. If this error is returned, wait 30 seconds and then # try creating the session again. This is better than if the tests # fail due to this condition. while True: try: driver = self.env.create() except selenium.common.exceptions.WebDriverException, e: if str(e).find('please wait for a test to finish') != -1: print 'waiting for available selenium session...' time.sleep(30) else: raise else: # Success. Store the driver and break out of loop. test.context.driver = driver break