def get_testcase(mode="smoke"): mode_lower = mode.lower() if mode_lower in ("smoke", "config", "demo"): mongo = MongoDB() return mongo.get_testcases_from_mongo(mode_lower) elif mode == "all": global test_cases test_case_dir = get_testcase_dir() all_folders = os.listdir(test_case_dir) all_case_files = [] for folder in all_folders: sub_test_case_dir = test_case_dir + os.sep + folder if os.path.isdir(sub_test_case_dir): all_case_files.append({folder: os.listdir(sub_test_case_dir)}) all_case_module = deepcopy(all_case_files) for item in all_case_module: for product in item: for case in item[product]: if ".pyc" not in case and "__init__.py" not in case: get_testcase_from_module(product, case) return test_cases else: print_log("Cannot recognize mode '%s', please take a double check!" % mode, "error") sys.exit(-1)
def get_testcase_from_module(product_name, module_name): """ :summary: Return the test suite via the module of testcase :param product_name: The name of product :param module_name: The name of test module :return: Global list test_cases """ global test_cases module_path = get_testcase_dir() + os.sep + product_name + \ os.sep + module_name print_log("The path of module to analyze is %s." % module_path, "debug") with open(module_path) as f: for line in f.readlines(): # Find all the names of testcase if re.match("^class", line) and "BaseTestCase)" in line: test_cases.append( "testcase.%s.%s.%s" % ( product_name, module_name[:-3], re.sub("\(.+\):\n", "", line[6:]) ) ) return test_cases
def test_valid_login(self): dict_data = self.init_data() print(dict_data) bw = BaseWizard(self.driver) print_log("Starting to login to the system.") bw.login_as(dict_data["login_name"], dict_data["password"]) self.assertIs(bw.is_logout(), False)
def get_testcase(mode="smoke"): mode_lower = mode.lower() if mode_lower in ("smoke", "config", "demo"): mongo = MongoDB() return mongo.get_testcases_from_mongo(mode_lower) elif mode == "all": global test_cases test_case_dir = get_testcase_dir() all_folders = os.listdir(test_case_dir) all_case_files = [] for folder in all_folders: sub_test_case_dir = test_case_dir + os.sep + folder if os.path.isdir(sub_test_case_dir): all_case_files.append({folder: os.listdir(sub_test_case_dir)}) all_case_module = deepcopy(all_case_files) for item in all_case_module: for product in item: for case in item[product]: if ".pyc" not in case and "__init__.py" not in case: get_testcase_from_module(product, case) return test_cases else: print_log( "Cannot recognize mode '%s', please take a double check!" % mode, "error") sys.exit(-1)
def post_data( self, type_="testcase", project="velocity", module=None, case=None, description=None, data_driven=False, data=None): """ :summary: Post data to MongoDB. :param type_: The type of data, could be testcase or suite :param project: Project name :param module: Module name or component name :param case: Name of testcase :param description: Description of this data :param data_driven: Use data driven or not :param data: Real test data :return: The Object ID of this data in MongoDB """ if self.find_data(type_, project, module, case): print_log("The data of testcase %s has already existed." % module) return -1 if type_ == "testcase": collection = self._get_collection(type_) testcase = copy.deepcopy(testcase_template) if project != "velocity": testcase["project"] = project testcase["module"] = module testcase["case"] = case testcase["description"] = description testcase["data_driven"] = data_driven testcase["data"] = data return collection.insert(testcase)
def is_logout(self): if "logout" in self.driver.current_url.lower(): print_log("Logout successfully!") return True else: print_log("You are still in the system!", "debug") return False
def get_testcases_from_mongo(self, type_): testcases = [] collection = self._get_collection("testsuite.%s" % type_) cur = collection.find({}) for i in cur: testcases.append(i["case"]) print_log("The testcases are %s" % str(testcases), "debug") return testcases
def get_opt(): try: options, args = getopt.getopt(sys.argv[1:], "hm:", ["help", "mode="]) except getopt.GetoptError: print_log("Critical error, please contact the administrator!", "error") return None else: return_value = {} for name, value in options: if name in ("-h", "--help"): usage() if name in ("-m", "--mode"): return_value["mode"] = value if return_value: return return_value else: print_log("No valuable command line option is specified!", "warning") return {}
def get_testcase_from_module(product_name, module_name): """ :summary: Return the test suite via the module of testcase :param product_name: The name of product :param module_name: The name of test module :return: Global list test_cases """ global test_cases module_path = get_testcase_dir() + os.sep + product_name + \ os.sep + module_name print_log("The path of module to analyze is %s." % module_path, "debug") with open(module_path) as f: for line in f.readlines(): # Find all the names of testcase if re.match("^class", line) and "BaseTestCase)" in line: test_cases.append("testcase.%s.%s.%s" % (product_name, module_name[:-3], re.sub("\(.+\):\n", "", line[6:]))) return test_cases
def execute_test(test_suite): """ :summary: Execute test cases. :param test_suite: Test suite. """ exec_type = get_config("run-time", "mode") print_log("="*80, "info") print_log( "Execution started, execution type is %s, " % exec_type ) print_log("Testcase(s) to run is(are): \n%s" % str(test_suite)) result_file = get_test_report_path() fp = open(result_file, 'wb') runner = HTMLTestRunner( stream=fp, title="Automation test report", description=( "Automation test report\n" "Execution type is %s" % exec_type ) ) runner.run(test_suite)
def find_data( self, type_="testcase", project="web", module=None, case=None): collection = self._get_collection(type_) data = collection.find_one( {"project": project, "module": module, "case": case} ) if data: print_log( "The data of testcase %s has been found." % case, "debug" ) return data["data"] else: print_log( "The data of testcase %s.%s hasn't been found!" % (module, case), "warn" ) return False
def get_opt(): try: options, args = getopt.getopt( sys.argv[1:], "hm:", ["help", "mode="] ) except getopt.GetoptError: print_log("Critical error, please contact the administrator!", "error") return None else: return_value = {} for name, value in options: if name in ("-h", "--help"): usage() if name in ("-m", "--mode"): return_value["mode"] = value if return_value: return return_value else: print_log("No valuable command line option is specified!", "warning") return {}
def execute_test(test_suite): """ @summary: Execute test cases. @param test_suite: Test suite. """ exec_type = get_config("run-time", "mode") print_log("Testcase(s) to run is(are): %s" % test_suite) result_file = "D:\\test_result.html" fp = file(result_file, 'wb') runner = HTMLTestRunner( stream=fp, title=u"Automation test report", description=( u"Automation test report\n" u"Execution type is %s" % exec_type ) ) runner.run(test_suite)
def setUp(self): host = get_config("selenium", "host") port = get_config("selenium", "port") browser = get_config("selenium", "browser").lower() if browser == "firefox": # Set profile of Firefox profile = webdriver.FirefoxProfile() profile.set_preference( "intl.accept_languages", "en-us, en, en-us, en" ) profile.set_preference( "extensions.addonnotification.showDaytip", "false" ) # Turn off daily tips of Firefox profile.set_preference( "*****@*****.**", "0" ) # Turn off the tip of Firefox passport self.driver = webdriver.Firefox(profile) elif browser == "ie": self.driver = webdriver.Ie() elif browser == "chrome": self.driver = webdriver.Chrome() else: print_log("The browser is not supported! " "Please check your AFT.ini!", "error") raise NoSuchWindowException("The browser you set is not supported, " "please check your config file.") self.driver.maximize_window() self.driver.implicitly_wait( get_config("selenium", "implicitly_wait_time") ) if port: self.driver.get(host + ":" + port) else: print_log("No port is specified.", "debug") self.driver.get(host)
def set_test_result(self, result='pass', message='No more message.'): if result.lower() == 'pass': print_log("Test case has been executed successfully, " "here is the message: %s" % message, 'pass') # self.assertTrue(True, "True") elif result.lower() == 'fail': print_log(message) raise self.failureException("Test case execute failed, " "here is the message: %s" % message) else: print_log("The test result should be 'pass' or 'fail'," "your input is not supported.", "error") return False
def execute_test(test_suite): """ :summary: Execute test cases. :param test_suite: Test suite. """ exec_type = get_config("run-time", "mode") print_log("=" * 80, "info") print_log("Execution started, execution type is %s, " % exec_type) print_log("Testcase(s) to run is(are): \n%s" % str(test_suite)) result_file = get_test_report_path() fp = open(result_file, 'wb') runner = HTMLTestRunner(stream=fp, title="Automation test report", description=("Automation test report\n" "Execution type is %s" % exec_type)) runner.run(test_suite)
def click_button(self, button): """ @summary: Click the destination button @param: button: The button element @return: Return True if click successfully, else False """ if self.is_usable(button): try: button.click() return True except (NoSuchElementException, TimeoutException): print_log("Can not find the button by xpath: '%s'.", "Error") return False except StaleElementReferenceException: print_log( "The element(xpath: %s) is not as same as it has been " "located, please check your code." % button, "Error") return False else: print_log("The button is unusable, and can not be clicked!", "Error") return False
description=None, data_driven=False, data=None): """ :summary: Post data to MongoDB. :param type_: The type of data, could be testcase or suite :param project: Project name :param module: Module name or component name :param case: Name of testcase :param description: Description of this data :param data_driven: Use data driven or not :param data: Real test data :return: The Object ID of this data in MongoDB """ if self.find_data(type_, project, module, case): print_log("The data of testcase %s has already existed." % module) return -1 if type_ == "testcase": collection = self._get_collection(type_) testcase = copy.deepcopy(testcase_template) if project != "web": testcase["project"] = project testcase["module"] = module testcase["case"] = case testcase["description"] = description testcase["data_driven"] = data_driven testcase["data"] = data return collection.insert(testcase)
def f_timer(*args, **kwargs): start = time.time() result_ = f(*args, **kwargs) end = time.time() print_log("Function '%s' took %f second(s)." % (f.__name__, end-start)) return result_
def logout(self): if not self.is_logout(): self.link_logout.click() return self.is_logout() else: print_log("No need, you are already logging out.")