def create(self, url_key): def inner(): try: self.wharf.checkout() return super(WharfFactory, self).create(url_key) except urllib2.URLError as ex: # connection to selenum was refused for unknown reasons log.error('URLError connecting to selenium; recycling container. URLError:') write_line('URLError caused container recycle, see log for details', red=True) log.exception(ex) self.wharf.checkin() raise return tries(10, urllib2.URLError, inner)
def upload(self, file_name, content, comment=''): print "Uploading file: ", file_name, "...", sys.stdout.flush() if isinstance(content, unicode): content = content.encode('utf8') site = self.site try: tries( 3, lambda: site.upload(content, file_name, comment, ignore=True) ) except httplib.IncompleteRead: # The upload is success even we got an IncompleteRead pass try: uploaded = site.Images[file_name] except: # This will be failed... don't know why. pass uploaded = site.Images[file_name] print 'Done.' return uploaded
def create(self, url_key): try: browser = tries(3, WebDriverException, self.webdriver_class, **self.processed_browser_args()) except urllib2.URLError as e: if e.reason.errno == 111: # Known issue raise RuntimeError("Could not connect to Selenium server. Is it up and running?") else: # Unknown issue raise browser.file_detector = UselessFileDetector() browser.maximize_window() browser.get(url_key) browser.url_key = url_key return browser
def create(self, url_key): try: browser = tries( 3, WebDriverException, self.webdriver_class, **self.processed_browser_args()) except urllib2.URLError as e: if e.reason.errno == 111: # Known issue raise RuntimeError('Could not connect to Selenium server. Is it up and running?') else: # Unknown issue raise browser.file_detector = UselessFileDetector() browser.maximize_window() browser.get(url_key) browser.url_key = url_key return browser
def start(webdriver_name=None, base_url=None, **kwargs): """Starts a new web browser If a previous browser was open, it will be closed before starting the new browser Args: webdriver_name: The name of the selenium Webdriver to use. Default: 'Firefox' base_url: Optional, will use ``utils.conf.env['base_url']`` by default **kwargs: Any additional keyword arguments will be passed to the webdriver constructor """ # Try to clean up an existing browser session if starting a new one if thread_locals.browser is not None: quit() browser_conf = conf.env.get('browser', {}) if webdriver_name is None: # If unset, look to the config for the webdriver type # defaults to Firefox webdriver_name = browser_conf.get('webdriver', 'Firefox') webdriver_class = getattr(webdriver, webdriver_name) if base_url is None: base_url = store.base_url # Pull in browser kwargs from browser yaml browser_kwargs = browser_conf.get('webdriver_options', {}) # Handle firefox profile for Firefox or Remote webdriver if webdriver_name == 'Firefox': browser_kwargs['firefox_profile'] = _load_firefox_profile() elif (webdriver_name == 'Remote' and browser_kwargs['desired_capabilities']['browserName'] == 'firefox'): browser_kwargs['browser_profile'] = _load_firefox_profile() # Update it with passed-in options/overrides browser_kwargs.update(kwargs) if webdriver_name != 'Remote' and 'desired_capabilities' in browser_kwargs: # desired_capabilities is only for Remote driver, but can sneak in del(browser_kwargs['desired_capabilities']) if webdriver_name == 'Remote' and 'webdriver_wharf' in browser_conf and not thread_locals.wharf: # Configured to use wharf, but it isn't configured yet; check out a webdriver container wharf = Wharf(browser_conf['webdriver_wharf']) # TODO: Error handling! :D wharf.checkout() atexit.register(wharf.checkin) thread_locals.wharf = wharf if browser_kwargs['desired_capabilities']['browserName'] == 'chrome': # chrome uses containers to sandbox the browser, and we use containers to # run chrome in wharf, so disable the sandbox if running chrome in wharf co = browser_kwargs['desired_capabilities'].get('chromeOptions', {}) arg = '--no-sandbox' if 'args' not in co: co['args'] = [arg] elif arg not in co['args']: co['args'].append(arg) browser_kwargs['desired_capabilities']['chromeOptions'] = co if thread_locals.wharf: # Wharf is configured, make sure to use its command_executor wharf_config = thread_locals.wharf.config browser_kwargs['command_executor'] = wharf_config['webdriver_url'] view_msg = 'tests can be viewed via vnc on display %s' % wharf_config['vnc_display'] logger.info('webdriver command executor set to %s' % wharf_config['webdriver_url']) logger.info(view_msg) write_line(view_msg, cyan=True) try: browser = tries(3, WebDriverException, webdriver_class, **browser_kwargs) browser.maximize_window() browser.get(base_url) thread_locals.browser = browser except urllib2.URLError as ex: # connection to selenium was refused for unknown reasons if thread_locals.wharf: # If we're running wharf, try again with a new container logger.error('URLError connecting to selenium; recycling container. URLError:') # Plus, since this is a really weird thing that we need to figure out, # throw a message out to the terminal for visibility write_line('URLError caused container recycle, see log for details', red=True) logger.exception(ex) thread_locals.wharf.checkin() thread_locals.wharf = None start(webdriver_name, base_url, **kwargs) else: # If we aren't running wharf, raise it raise return thread_locals.browser
def start(webdriver_name=None, base_url=None, **kwargs): """Starts a new web browser If a previous browser was open, it will be closed before starting the new browser Args: webdriver_name: The name of the selenium Webdriver to use. Default: 'Firefox' base_url: Optional, will use ``utils.conf.env['base_url']`` by default **kwargs: Any additional keyword arguments will be passed to the webdriver constructor """ # Try to clean up an existing browser session if starting a new one if thread_locals.browser is not None: quit() browser_conf = conf.env.get('browser', {}) if webdriver_name is None: # If unset, look to the config for the webdriver type # defaults to Firefox webdriver_name = browser_conf.get('webdriver', 'Firefox') webdriver_class = getattr(webdriver, webdriver_name) if base_url is None: base_url = store.base_url # Pull in browser kwargs from browser yaml browser_kwargs = browser_conf.get('webdriver_options', {}) # Handle firefox profile for Firefox or Remote webdriver if webdriver_name == 'Firefox': browser_kwargs['firefox_profile'] = _load_firefox_profile() elif (webdriver_name == 'Remote' and browser_kwargs['desired_capabilities']['browserName'] == 'firefox'): browser_kwargs['browser_profile'] = _load_firefox_profile() # Update it with passed-in options/overrides browser_kwargs.update(kwargs) if webdriver_name != 'Remote' and 'desired_capabilities' in browser_kwargs: # desired_capabilities is only for Remote driver, but can sneak in del (browser_kwargs['desired_capabilities']) if webdriver_name == 'Remote' and 'webdriver_wharf' in browser_conf and not thread_locals.wharf: # Configured to use wharf, but it isn't configured yet; check out a webdriver container wharf = Wharf(browser_conf['webdriver_wharf']) # TODO: Error handling! :D wharf.checkout() atexit.register(wharf.checkin) thread_locals.wharf = wharf if browser_kwargs['desired_capabilities']['browserName'] == 'chrome': # chrome uses containers to sandbox the browser, and we use containers to # run chrome in wharf, so disable the sandbox if running chrome in wharf co = browser_kwargs['desired_capabilities'].get( 'chromeOptions', {}) arg = '--no-sandbox' if 'args' not in co: co['args'] = [arg] elif arg not in co['args']: co['args'].append(arg) browser_kwargs['desired_capabilities']['chromeOptions'] = co if thread_locals.wharf: # Wharf is configured, make sure to use its command_executor wharf_config = thread_locals.wharf.config browser_kwargs['command_executor'] = wharf_config['webdriver_url'] view_msg = 'tests can be viewed via vnc on display {}'.format( wharf_config['vnc_display']) logger.info('webdriver command executor set to %s', wharf_config['webdriver_url']) logger.info(view_msg) write_line(view_msg, cyan=True) try: browser = tries(3, WebDriverException, webdriver_class, **browser_kwargs) browser.file_detector = UselessFileDetector() browser.maximize_window() browser.get(base_url) thread_locals.browser = browser except urllib2.URLError as ex: # connection to selenium was refused for unknown reasons if thread_locals.wharf: # If we're running wharf, try again with a new container logger.error( 'URLError connecting to selenium; recycling container. URLError:' ) # Plus, since this is a really weird thing that we need to figure out, # throw a message out to the terminal for visibility write_line( 'URLError caused container recycle, see log for details', red=True) logger.exception(ex) thread_locals.wharf.checkin() thread_locals.wharf = None start(webdriver_name, base_url, **kwargs) else: # If we aren't running wharf, raise it raise return thread_locals.browser
def urlopen(url, ntries=3): #import sys #print >>sys.stderr, url return tries(ntries, urllib2.urlopen, url)