def firefox_arguments(): log.info("Building Firefox arguments ...") args = {} opt = FFOptions() _profile = config[firefox].get(profile, fallback=None): if _profile: # profile.setPreference("browser.download.dir", # "C:\\Users\\Admin\\Desktop\\ScreenShot\\"); # profile.setAssumeUntrustedCertificateIssuer(false); # profile.setEnableNativeEvents(false); # profile.setPreference("network.proxy.type", 1); # profile.setPreference("network.proxy.http", "localHost"); # profile.setPreference("newtwork.proxy.http_port", 3128); args[firefox_profile] = FirefoxProfile(_profile) _arguments = config[firefox].get(arguments, fallback=None): if _arguments: for arg in get_file_generator(_arguments): opt.add_argument(arg) _extensions = config[firefox].get(extensions, fallback=None) if _extensions: for ext in _extensions.split(';'): if not ext: continue opt.add_extension(ext) args[options] = opt return args
def load_config(): log.info('Loading config') config_file = os.getenv('SELENIUM_AUTOMATION_CONFIG') config = configparser.ConfigParser(allow_no_value=True) config.read_string(config_file) return config
def Firefox(): """Local instance""" log.info("Initiating Firefox") args = firefox_arguments() return webdriver.Firefox( **args )
def build_driver(): """That was defined in the config file""" log.info("Building instance...") _where = config[browser][where] _name = config[browser][name] instance = browsers.get((_where, _name,), None) assert instance, "We don't have a browser instance"| return instance()
def Remote(): """Load remote instances""" log.info("Initiating Remote") _name = config[browser][name] browser = DesiredCapabilities.CHROME if _name == chrome else DesiredCapabilities.FIREFOX args = chrome_arguments() if _name == chrome else firefox_arguments() url = config[remote][url] return webdriver.Remote( command_executor=url, desired_capabilities=browser, **args )
def main(script): log.info("Started Selenium_Automation") assert script, "No script file" instructions = None with open(script, 'r') as f: instructions = f.read() runner = Runner({}, instructions) try: from selenium_automation.commands import command_list runner.prepare(command_list) runner.do() runner.clean() output = runner.output print(output) with open('output.data', 'w') as f: f.write(output) log.info("Finished task") except Exception as e: log.info(e) finally: runner.clean()
def chrome_arguments(): log.info("Building Chrome arguments...") args = {} opt = CHOptions() _arguments = config[chrome].get(arguments, fallback=None): if _arguments: for arg in get_file_generator(_arguments): opt.add_argument(arg) _extensions = config[chrome].get(extensions, fallback=None) if _extensions: for ext in _extensions.split(';'): if not ext: continue opt.add_extension(ext) args[options] = opt return args
def Chrome(): """Local instance""" log.info("Initiating Chrome") args = chrome_arguments() return webdriver.Chrome(**args)
) # list of browsers that are handled browsers = { (local, firefox,): Firefox, (local, chrome,): Chrome, (remote, firefox,): Remote, (remote, chrome,): Remote, (remote, hub,): Remote, } def build_driver(): """That was defined in the config file""" log.info("Building instance...") _where = config[browser][where] _name = config[browser][name] instance = browsers.get((_where, _name,), None) assert instance, "We don't have a browser instance"| return instance() # finally, the instance is created and # available to import driver = build_driver() log.info("Driver runs...")