def determine_browser(test_run_id): """Given a test run ID, attempts to determine the browser for the given test run based on the test run's configuration. If no browser can be determined, None is returned. """ run_info = testrail_api_glados.get_run(test_run_id) run_config = run_info['config'] if not run_config: # if config is None or empty set it to empty string to avoid errors run_config = '' run_config = run_config.lower() if (re.search(r'\bchrome\b', run_config) or re.search(r'\bgooglechrome\b', run_config) or re.search(r'\bgc\b', run_config)): browser = 'chrome' elif (re.search(r'\bfirefox\b', run_config) or re.search(r'\bff\b', run_config)): browser = 'firefox' elif (re.search(r'\bie\b', run_config) or re.search(r'\binternet explorer\b', run_config)): browser = 'internetexplorer' elif (re.search(r'\bphantomjs\b', run_config)): browser = 'phantomjs' elif (re.search(r'\bsafari\b', run_config)): browser = 'safari' elif (re.search(r'\bedge\b', run_config)): browser = 'edge' else: browser = None return browser
def determine_platform_version(test_run_id, browser, args): """Given a test run ID, attempts to determine the platform for the given test run based on the test run's configuration. Note: Browser is used to determine if it needs firefox version of platform Yes, could remove browser to use args.browser, but just left it there since browser was already defined If no platform or version can be determined, None is returned. """ run_info = testrail_api_glados.get_run(test_run_id) print(run_info) print(run_info['config']) run_config = run_info['config'] # Default platform and version defined if not run_config: # if config is None or empty set it to empty string to avoid errors run_config = '' # Set the default platform and version if args.platform is None: platform = 'windows' # Avoid errors else: platform = args.platform.lower() if args.platform is None: version = '' else: version = args.version # Set to arg version by default run_config = run_config.lower() if (re.search(r'\bwindows7\b', run_config) or re.search(r'\bwin7\b', run_config) or re.search(r'\bvista\b', run_config) or re.search(r'\bwindows 7\b', run_config)): platform = 'windows' version = 7 elif (re.search(r'\bwindows10\b', run_config) or re.search(r'\bwin10\b', run_config) or re.search(r'\bwindows 10\b', run_config)): platform = 'windows' version = 10 elif (re.search(r'\bwindows\b', run_config)): platform = 'windows' elif (re.search(r'\bmac\b', run_config)): platform = 'mac' elif (re.search(r'\bios(\d*)?\b', run_config) or re.search(r'\biphone(\s?(\d|x)s?)?(\s?max)?\b', run_config) or re.search(r'\bipad\s?(air|pro)?\s?\d?\b', run_config)): platform = 'ios' elif (re.search(r'\bandroid\b', run_config) or re.search( r'\bandroid(\s?emulator)?(\s?\d*(.\d)*)\b', run_config)): platform = 'android' return platform, version
def determine_landscape_mode(test_run_id): """Given a test run ID, attempts to determine if the browser for the given test run based on the test run's configuration needs to be in landscape mode. Returns True or False if the testrun should be in landscape mode. """ run_info = testrail_api_glados.get_run(test_run_id) run_config = run_info['config'] if not run_config: # if config is None or empty set it to empty string to avoid errors run_config = '' # Determine if the configuration contains landscape option return re.search('landscape', run_config, re.IGNORECASE) is not None
def determine_device(test_run_id): """Given a test run ID, attempts to determine the device for the given test run based on the test run's configuration. If no device can be determined, None is returned. """ run_info = testrail_api_glados.get_run(test_run_id) run_config = run_info['config'] if not run_config: # if config is None or empty set it to empty string to avoid errors run_config = '' run_config = run_config.lower() # Set the regex of the string regex_iphone = r'\biphone(\s?(\d|x)s?)?(\s?max)?\b' regex_ipad = r'\bipad\s?(air|pro)?\s?\d?\b' regex_android = r'\bandroid(\s?emulator)?(\s?\d*(.\d)*)\b' # Get the device of the testcase if re.search(regex_iphone, run_config): device = re.search(regex_iphone, run_config).group(0) elif re.search(regex_ipad, run_config): device = re.search(regex_ipad, run_config).group(0) elif re.search(regex_android, run_config): device = re.search(regex_android, run_config).group(0) # Automotive's version of default iphone elif re.search(r'\bphone.*ios(\d*)?\b', run_config): device = 'iphone' else: device = None print("Device: " + str(device)) # Remove any whitespace if device text if not none if device is not None: device = device.replace(' ', '') return device
def create_pytest_commands(args): pytest_commands = [] all_output_paths = [] no_suite_key = "_no_suite" tests_to_run = {no_suite_key: []} date_string = datetime.now().strftime("%m%d%Y_%H%M%S") slash = os.sep for test_run_id in args.test_run_ids: tests_to_run = get_tests_to_run(args, test_run_id, no_suite_key) run_info = testrail_api_glados.get_run(test_run_id) run_name = sanitize_input_for_robot(run_info['name']) # platform = determine_platform(test_run_id) # if not platform: # platform = args.platform browser = determine_browser(test_run_id) if not browser: browser = args.browser if browser: browser = " --browser=" + browser landscape_mode = determine_landscape_mode(test_run_id) for suite in tests_to_run: # create robot commands for no_suite_key tests if suite == no_suite_key: for test_data in tests_to_run[suite]: base_pytest_command = ("python -m pytest " + "{suite_name_ph}" + ".py::" + "{suite_name_ph}" + "::" + "{test_name_ph}" + " " + "{base_variables_ph}") base_variables = (" --test_run_id " + str(test_run_id) + " --test_case_id " + str(test_data['case_id']) + " --capture=sys --tb=native --env " + args.environment + browser) print("variables = " + str(args.variable)) for custom_var in args.variable: print("custom_var = " + str(custom_var)) var_name, var_value = custom_var.split(':', 1) base_variables += " --vars " + var_name + "=" + var_value test_name = test_data['custom_automation_test_name'] test_name = test_name.replace(" ", "_") case_id_var = (test_name + "_case_id:" + str(test_data['case_id'])) ## Get the suite name if exist, set the log output location as well suite_name_var = "" if (test_data['custom_automation_suite_name']): suite_name = str( test_data['custom_automation_suite_name'].replace( " ", "_")) # changed for pytest ### suite_name_var = suite_name + ".py" #add for folders ### path_var = suite_name.split('_')[1] output_dir = ("logs" + slash + str(test_run_id) + "_" + date_string + slash + suite_name + "_" + test_data['custom_automation_test_name']) else: output_dir = ("logs" + slash + str(test_run_id) + "_" + date_string + slash + test_data['custom_automation_test_name']) all_output_paths.append(output_dir) robot_command = base_pytest_command.format( suite_name_ph=suite_name, test_name_ph=test_name, base_variables_ph=base_variables, case_id_var_ph=case_id_var, output_dir_ph=output_dir, slash_ph=slash, search_path_ph=args.search_path, runId=test_run_id) pytest_commands.append(robot_command) else: base_pytest_command = ("python -m pytest -k " + "{test_name_ph} " + "tests") base_variables = ( " --test_run_id " + str(test_run_id) + # test for case id " --test_case_id " + str(test_data['case_id']) + #" --output_dir " + str(output_dir) + " --capture=sys --tb=native") for custom_var in args.variable: var_name, var_value = custom_var.split(':', 1) base_variables += " -v " + var_name + ":" + var_value # Get suite and sub suite name from tests suite_name = "no_suite" sub_suite_name = "no_sub_suite" if (tests_to_run[suite][0]['custom_automation_suite_name']): suite_name = tests_to_run[suite][0][ 'custom_automation_suite_name'].replace(" ", "_") if (('custom_automation_sub_suite_name' in tests_to_run[suite][0]) and (tests_to_run[suite][0]['custom_automation_sub_suite_name'] )): sub_suite_name = tests_to_run[suite][0][ 'custom_automation_sub_suite_name'].replace(" ", "_") # Set default variables test_name_vars = "" case_id_vars = "" for test_data in tests_to_run[suite]: test_name = test_data['custom_automation_test_name'] test_name = test_name.replace(" ", "_") test_name_vars = test_name case_id_var = (" -v " + test_name + "_case_id:" + str(test_data['case_id'])) case_id_vars += case_id_var # Create output directory output_dir = ("logs" + slash + str(test_run_id) + "_" + browser + "_" + date_string + slash + sub_suite_name) all_output_paths.append(output_dir) pytest_command = base_pytest_command.format( suite_name_ph=suite_name, test_name_vars_ph=test_name_vars, base_variables_ph=base_variables, case_id_vars_ph=case_id_vars, output_dir_ph=output_dir, slash_ph=slash, search_path_ph=args.search_path) pytest_commands.append(robot_command) return (pytest_commands, all_output_paths)
def create_robot_commands(args): """Creates a list of Pybot commands to run tests based on data from the ArgumentParser object. """ robot_commands = [] all_output_paths = [] slash = os.sep date_string = datetime.now().strftime("%m%d%Y_%H%M%S") base_robot_command = ("robot" + "{test_name_vars_ph}" + "{suite_name_ph}" + " {base_variables_ph}" + "{case_id_vars_ph}" + " -d {output_dir_ph}" + " {search_path_ph}") more_variables = "" default_device = "" for custom_var in args.variable: var_name, var_value = custom_var.split(':', 1) if var_name.lower() == "device": default_device = var_value else: more_variables += " -v " + var_name + ":" + var_value #Set Tags if args.settag is not None: more_variables += " -G " + args.settag for test_run_id in args.test_run_ids: # create a dictionary whose keys are Robot Framework (RF) suite names # and values are a list of test cases in that suite using the data from # TestRail tests on TestRail that have no suite name specified have the # key set by no_suite_key no_suite_key = "_no_suite" tests_to_run = get_tests_to_run(args, test_run_id, no_suite_key) ### NEW ### # CREATE PYBOT COMMANDS run_info = testrail_api_glados.get_run(test_run_id) run_name = sanitize_input_for_robot(run_info['name']) # use default browser set in args if one cannot be determined browser = determine_browser(test_run_id) if not browser: browser = args.browser # use default platform & version set in args if one cannot be determined platform, version = determine_platform_version(test_run_id, browser, args) device = determine_device(test_run_id) if not device: device = default_device landscape_mode = determine_landscape_mode(test_run_id) base_variables = (" -v browser:" + browser + " -v environment:" + args.environment + " -v run_id:" + str(test_run_id) + " -v run_name:" + run_name + " -v remote:" + args.remote_url + " -v jenkins_url:" + args.jenkins_url + " -v landscape_mode:" + str(landscape_mode) + " -v platformname:" + platform + " -v device:" + device + more_variables) template_string_partial = '' if version is not None: base_variables += " -v version:" + str(version) template_string_partial = str(version) if device is not None and device != '': template_string_partial = template_string_partial + "_" + str( device) output_dir_template = ("logs" + slash + str(test_run_id) + "_" + platform + template_string_partial + "_" + browser + "_" + date_string + slash + "{suite_test_name_ph}") for suite in tests_to_run: # create robot commands for no_suite_key tests suite_name = "" suite_name_var = "" if suite == no_suite_key: for test_data in tests_to_run[suite]: # removed base_robot_command and base_variables from here ## Get the suite name if exist, set the log output location as well test_name = test_data['custom_automation_test_name'] test_name = test_name.replace(" ", "_") if (test_data['custom_automation_suite_name']): suite_name = str( test_data['custom_automation_suite_name'].replace( " ", "_")) suite_name_var = " -s " + suite_name suite_and_test_name = suite_name + "_" + test_name else: suite_name = str( test_data['custom_automation_suite_name'].replace( " ", "_")) suite_name_var = " -s " + suite_name suite_and_test_name = suite_name + "_" + test_name output_dir = output_dir_template.format( suite_test_name_ph=suite_and_test_name) case_id_var = (test_name + "_case_id:" + str(test_data['case_id'])) all_output_paths.append(output_dir) robot_command = base_robot_command.format( suite_test_name_ph=suite_and_test_name, suite_name_ph=suite_name_var, test_name_vars_ph=" -t " + test_name, base_variables_ph=base_variables, case_id_vars_ph=" -v " + case_id_var, output_dir_ph=output_dir, slash_ph=slash, search_path_ph=args.search_path) robot_commands.append(robot_command) else: # create robot commands for the rest # removed base_robot_command and base_variables from here # Get suite and sub suite name from tests suite_name = "no_suite" sub_suite_name = "no_sub_suite" if (tests_to_run[suite][0]['custom_automation_suite_name']): suite_name = tests_to_run[suite][0][ 'custom_automation_suite_name'].replace(" ", "_") if (('custom_automation_sub_suite_name' in tests_to_run[suite][0]) and (tests_to_run[suite][0]['custom_automation_sub_suite_name'] )): sub_suite_name = tests_to_run[suite][0][ 'custom_automation_sub_suite_name'].replace(" ", "_") # Set default variables test_name_vars = "" case_id_vars = "" for test_data in tests_to_run[suite]: test_name = test_data['custom_automation_test_name'] test_name = test_name.replace(" ", "_") test_name_vars += " -t " + test_name case_id_var = (" -v " + test_name + "_case_id:" + str(test_data['case_id'])) case_id_vars += case_id_var # Create output directory output_dir = output_dir_template.format( suite_test_name_ph=sub_suite_name) all_output_paths.append(output_dir) robot_command = base_robot_command.format( suite_name_ph=" -s " + suite_name, test_name_vars_ph=test_name_vars, base_variables_ph=base_variables, case_id_vars_ph=case_id_vars, output_dir_ph=output_dir, slash_ph=slash, search_path_ph=args.search_path) robot_commands.append(robot_command) # return list of commands and a list of output directories return (robot_commands, all_output_paths)