original_docstring: Open a browser.
  
      When opening more than one browser instance per test
      provide a browser_id to switch between browsers later on
      
docstring: Open a browser.
  When opening more than one browser instance per test
  provide a browser_id to switch between browsers later on
docstring_tokens: Open a browser . When opening more than one browser instance per test provide a browser_id to switch between browsers later on
code: def open_browser(browser_id=None):
      """Open a browser.
  
      When opening more than one browser instance per test
      provide a browser_id to switch between browsers later on
      """
      @contextmanager
      def validate_exec_path(browser_name, exec_path_setting, settings):
          executable_path = settings[exec_path_setting]
          if executable_path:
              matched_executable_path = utils.match_latest_executable_path(executable_path)
              if matched_executable_path:
                  try:
                      yield matched_executable_path
                  except:
                      msg = ('Could not start {} driver using the path \'{}\'\n'
                             'verify that the {} setting points to a valid webdriver executable.'
                             .format(browser_name, executable_path, exec_path_setting))
                      execution.logger.error(msg)
                      execution.logger.info(traceback.format_exc())
                      raise Exception(msg)
              else:
                  msg = 'No executable file found using path {}'.format(executable_path)
                  execution.logger.error(msg)
                  raise Exception(msg)
          else:
              msg = '{} setting is not defined'.format(exec_path_setting)
              execution.logger.error(msg)
              raise Exception(msg)
  
      @contextmanager
      def validate_remote_url(remote_url):
          if remote_url:
              yield remote_url
          else:
              msg = 'remote_url setting is required'
              execution.logger.error(msg)
              raise Exception(msg)
  
      driver = None
      browser_definition = execution.browser_definition
      settings = execution.settings
      # remote
      if browser_definition['remote']:
          with validate_remote_url(settings['remote_url']) as remote_url:
              driver = GolemRemoteDriver(command_executor=remote_url,
                                         desired_capabilities=browser_definition['capabilities'])
      # Chrome
      elif browser_definition['name'] == 'chrome':
          with validate_exec_path('chrome', 'chromedriver_path', settings) as ex_path:
              chrome_options = webdriver.ChromeOptions()
              if settings['start_maximized']:
                  chrome_options.add_argument('start-maximized')
              driver = GolemChromeDriver(executable_path=ex_path,
                                         chrome_options=chrome_options)
      # Chrome remote
      elif browser_definition['name'] == 'chrome-remote':
          with validate_remote_url(settings['remote_url']) as remote_url:
              driver = GolemRemoteDriver(command_executor=remote_url,
                                         desired_capabilities=DesiredCapabilities.CHROME)
      # Chrome headless
      elif browser_definition['name'] == 'chrome-headless':
          with validate_exec_path('chrome', 'chromedriver_path', settings) as ex_path:
              chrome_options = webdriver.ChromeOptions()
              chrome_options.add_argument('headless')
              chrome_options.add_argument('--window-size=1600,1600')
              driver = GolemChromeDriver(executable_path=ex_path,
                                         chrome_options=chrome_options)
      # Chrome remote headless
      elif browser_definition['name'] == 'chrome-remote-headless':
          with validate_remote_url(settings) as remote_url:
              chrome_options = webdriver.ChromeOptions()
              chrome_options.add_argument('headless')
              desired_capabilities = chrome_options.to_capabilities()
              driver = GolemChromeDriver(command_executor=remote_url,
                                         desired_capabilities=desired_capabilities)
      # Edge
      elif browser_definition['name'] == 'edge':
          with validate_exec_path('edge', 'edgedriver_path', settings) as ex_path:
              driver = GolemEdgeDriver(executable_path=ex_path)
      # Edge remote
      elif browser_definition['name'] == 'edge-remote':
          with validate_remote_url(settings['remote_url']) as remote_url:
              driver = GolemRemoteDriver(command_executor=remote_url,
                                         desired_capabilities=DesiredCapabilities.EDGE)
      # Firefox
      elif browser_definition['name'] == 'firefox':
          with validate_exec_path('firefox', 'geckodriver_path', settings) as ex_path:
              driver = GolemGeckoDriver(executable_path=ex_path)
      # Firefox remote
      elif browser_definition['name'] == 'firefox-remote':
          with validate_remote_url(settings['remote_url']) as remote_url:
              driver = GolemRemoteDriver(command_executor=remote_url,
                                         desired_capabilities=DesiredCapabilities.FIREFOX)
      # IE
      elif browser_definition['name'] == 'ie':
          with validate_exec_path('internet explorer', 'iedriver_path', settings) as ex_path:
              driver = GolemIeDriver(executable_path=ex_path)
      # IE remote
      elif browser_definition['name'] == 'ie-remote':
          with validate_remote_url(settings['remote_url']) as remote_url:
              driver = GolemRemoteDriver(command_executor=remote_url,
                                         desired_capabilities=DesiredCapabilities.INTERNETEXPLORER)
      # Opera
      elif browser_definition['name'] == 'opera':
          with validate_exec_path('opera', 'operadriver_path', settings) as ex_path:
              opera_options = webdriver.ChromeOptions()
              if 'opera_binary_path' in settings:
                  opera_options.binary_location = settings['opera_binary_path']
              driver = GolemOperaDriver(executable_path=ex_path, options=opera_options)
      # Opera remote
      elif browser_definition['name'] == 'opera-remote':
          with validate_remote_url(settings['remote_url']) as remote_url:
              driver = GolemRemoteDriver(command_executor=remote_url,
                                         desired_capabilities=DesiredCapabilities.OPERA)
      else:
          raise Exception('Error: {} is not a valid driver'.format(browser_definition['name']))
  
      if settings['start_maximized']:
          # currently there is no way to maximize chrome window on OSX, adding workaround
          # https://bugs.chromium.org/p/chromedriver/issues/detail?id=2389
          if not('headless' in browser_definition['name'] or ('chrome' in browser_definition['name'] and
                                                              (platform.system() == 'Darwin'))):
              driver.maximize_window()
  
      if not browser_id:
          if len(execution.browsers) == 0:
              browser_id = 'main'
          else:
              browser_id = 'browser{}'.format(len(execution.browsers))
      execution.browsers[browser_id] = driver
      if not execution.browser:
          execution.browser = driver
code_tokens: def open_browser ( browser_id = None ) : @ contextmanager def validate_exec_path ( browser_name , exec_path_setting , settings ) : executable_path = settings [ exec_path_setting ] if executable_path : matched_executable_path = utils . match_latest_executable_path ( executable_path ) if matched_executable_path : try : yield matched_executable_path except : msg = ( 'Could not start {} driver using the path \'{}\'\n' 'verify that the {} setting points to a valid webdriver executable.' . format ( browser_name , executable_path , exec_path_setting ) ) execution . logger . error ( msg ) execution . logger . info ( traceback . format_exc ( ) ) raise Exception ( msg ) else : msg = 'No executable file found using path {}' . format ( executable_path ) execution . logger . error ( msg ) raise Exception ( msg ) else : msg = '{} setting is not defined' . format ( exec_path_setting ) execution . logger . error ( msg ) raise Exception ( msg ) @ contextmanager def validate_remote_url ( remote_url ) : if remote_url : yield remote_url else : msg = 'remote_url setting is required' execution . logger . error ( msg ) raise Exception ( msg ) driver = None browser_definition = execution . browser_definition settings = execution . settings if browser_definition [ 'remote' ] : with validate_remote_url ( settings [ 'remote_url' ] ) as remote_url : driver = GolemRemoteDriver ( command_executor = remote_url , desired_capabilities = browser_definition [ 'capabilities' ] ) elif browser_definition [ 'name' ] == 'chrome' : with validate_exec_path ( 'chrome' , 'chromedriver_path' , settings ) as ex_path : chrome_options = webdriver . ChromeOptions ( ) if settings [ 'start_maximized' ] : chrome_options . add_argument ( 'start-maximized' ) driver = GolemChromeDriver ( executable_path = ex_path , chrome_options = chrome_options ) elif browser_definition [ 'name' ] == 'chrome-remote' : with validate_remote_url ( settings [ 'remote_url' ] ) as remote_url : driver = GolemRemoteDriver ( command_executor = remote_url , desired_capabilities = DesiredCapabilities . CHROME ) elif browser_definition [ 'name' ] == 'chrome-headless' : with validate_exec_path ( 'chrome' , 'chromedriver_path' , settings ) as ex_path : chrome_options = webdriver . ChromeOptions ( ) chrome_options . add_argument ( 'headless' ) chrome_options . add_argument ( '--window-size=1600,1600' ) driver = GolemChromeDriver ( executable_path = ex_path , chrome_options = chrome_options ) elif browser_definition [ 'name' ] == 'chrome-remote-headless' : with validate_remote_url ( settings ) as remote_url : chrome_options = webdriver . ChromeOptions ( ) chrome_options . add_argument ( 'headless' ) desired_capabilities = chrome_options . to_capabilities ( ) driver = GolemChromeDriver ( command_executor = remote_url , desired_capabilities = desired_capabilities ) elif browser_definition [ 'name' ] == 'edge' : with validate_exec_path ( 'edge' , 'edgedriver_path' , settings ) as ex_path : driver = GolemEdgeDriver ( executable_path = ex_path ) elif browser_definition [ 'name' ] == 'edge-remote' : with validate_remote_url ( settings [ 'remote_url' ] ) as remote_url : driver = GolemRemoteDriver ( command_executor = remote_url , desired_capabilities = DesiredCapabilities . EDGE ) elif browser_definition [ 'name' ] == 'firefox' : with validate_exec_path ( 'firefox' , 'geckodriver_path' , settings ) as ex_path : driver = GolemGeckoDriver ( executable_path = ex_path ) elif browser_definition [ 'name' ] == 'firefox-remote' : with validate_remote_url ( settings [ 'remote_url' ] ) as remote_url : driver = GolemRemoteDriver ( command_executor = remote_url , desired_capabilities = DesiredCapabilities . FIREFOX ) elif browser_definition [ 'name' ] == 'ie' : with validate_exec_path ( 'internet explorer' , 'iedriver_path' , settings ) as ex_path : driver = GolemIeDriver ( executable_path = ex_path ) elif browser_definition [ 'name' ] == 'ie-remote' : with validate_remote_url ( settings [ 'remote_url' ] ) as remote_url : driver = GolemRemoteDriver ( command_executor = remote_url , desired_capabilities = DesiredCapabilities . INTERNETEXPLORER ) elif browser_definition [ 'name' ] == 'opera' : with validate_exec_path ( 'opera' , 'operadriver_path' , settings ) as ex_path : opera_options = webdriver . ChromeOptions ( ) if 'opera_binary_path' in settings : opera_options . binary_location = settings [ 'opera_binary_path' ] driver = GolemOperaDriver ( executable_path = ex_path , options = opera_options ) elif browser_definition [ 'name' ] == 'opera-remote' : with validate_remote_url ( settings [ 'remote_url' ] ) as remote_url : driver = GolemRemoteDriver ( command_executor = remote_url , desired_capabilities = DesiredCapabilities . OPERA ) else : raise Exception ( 'Error: {} is not a valid driver' . format ( browser_definition [ 'name' ] ) ) if settings [ 'start_maximized' ] : if not ( 'headless' in browser_definition [ 'name' ] or ( 'chrome' in browser_definition [ 'name' ] and ( platform . system ( ) == 'Darwin' ) ) ) : driver . maximize_window ( ) if not browser_id : if len ( execution . browsers ) == 0 : browser_id = 'main' else : browser_id = 'browser{}' . format ( len ( execution . browsers ) ) execution . browsers [ browser_id ] = driver if not execution . browser : execution . browser = driver
short_docstring: Open a browser.
short_docstring_tokens: Open a browser .
comment: """Open a browser.
  
      When opening more than one browser instance per test
      provide a browser_id to switch between browsers later on
      """$SEP$# remote$SEP$# Chrome$SEP$# Chrome remote$SEP$# Chrome headless$SEP$# Chrome remote headless$SEP$# Edge$SEP$# Edge remote$SEP$# Firefox$SEP$# Firefox remote$SEP$# IE$SEP$# IE remote$SEP$# Opera$SEP$# Opera remote$SEP$# currently there is no way to maximize chrome window on OSX, adding workaround$SEP$# https://bugs.chromium.org/p/chromedriver/issues/detail?id=2389