class BaseSuite(object): def __init__(self, run_classes): self.run_classes = run_classes # type: dict self.log = BaseLog().log def run(self): self.log.info( "\n\n Test Start \n ------------------------------------------------------------> " ) suite = unittest.TestSuite() for case, is_run in self.run_classes.items(): test = unittest.TestLoader().loadTestsFromTestCase(case) if is_run == 1: suite.addTest(test) self.log.info("Run Case: %s", test) elif is_run == 0: self.log.info("Not Run Case: %s", test) pass else: raise Exception("Wrong Arguments! 0:not run, 1:run") unittest.TextTestRunner().run(suite) self.log.info( "\n\n <------------------------------------------------------------ \nTest End\n\n\n " )
def __init__(self, selenium_driver): self.driver = selenium_driver # type: DRIVER["type"] self.log = BaseLog().log
class BaseAction(object): def __init__(self, selenium_driver): self.driver = selenium_driver # type: DRIVER["type"] self.log = BaseLog().log def open(self, url): self.log.info("open url: %s", url) self.driver.get(url) def sleep(self, sleep_time=0.5): self.log.info("sleep: %.1fs", sleep_time) time.sleep(sleep_time) def find_element(self, *loc): self.log.info("find element by: " + str(loc)) return self.driver.find_element(*loc) def script(self, src): self.log.info("execute script: %s", src) return self.driver.execute_script(src) def click(self, *loc, sleep=True): element = self.find_element(*loc) self.log.info("click element: %s", loc) element.click() if sleep: self.sleep() def send_keys(self, *loc, value, click_first=False, clear_first=False): if click_first: self.find_element(*loc).click() self.sleep() if clear_first: self.find_element(*loc).clear() element = self.find_element(*loc) self.log.info("send value: %s to element: %s", value, loc) element.send_keys(value)
def __init__(self): self.log = BaseLog().log
class DriverManager(object): def __init__(self): self.log = BaseLog().log def get_driver(self): """get WebDriver""" self.log.info(">>> init driver ") driver = self.__init_driver(DRIVER["type"]) if driver is None: raise Exception("Driver init failed, Driver is None!") self.log.info("<<< init done ") return driver def close(self, driver): """close WebDriver""" self.log.info("close driver: %s", driver) if driver is not None: driver.quit() def __set_basic_web_property(self, driver): self.log.info("__WINDOW: max") driver.maximize_window() self.log.info("__DEFAULT_FIND_ELEMENT_TIMEOUT: %ds", DEFAULT_FIND_ELEMENT_TIMEOUT) driver.implicitly_wait(DEFAULT_FIND_ELEMENT_TIMEOUT) self.log.info("__DEFAULT_PAGE_LOAD_TIMEOUT: %ds", DEFAULT_PAGE_LOAD_TIMEOUT) driver.set_page_load_timeout(DEFAULT_PAGE_LOAD_TIMEOUT) return driver def __init_driver(self, driver_type): if driver_type == webdriver.Chrome: self.log.info("Create Chrome Driver...") options = webdriver.ChromeOptions() self.log.info("Allow \"Trying to download multiple files\"") prefs = { 'profile.content_settings.exceptions.automatic_downloads.*.setting': 1, # 允许自动下载多个文件 "profile.default_content_settings.popups": 0, # 下载不弹框 "download.directory_upgrade": True, "download.default_directory": DEFAULT_DOWNLOAD_PATH, # 指定下载路径 } options.add_experimental_option("prefs", prefs) self.log.info("Set disable-infobars") options.add_argument('disable-infobars') driver = webdriver.Chrome( chrome_options=options) # type: webdriver.Chrome return self.__set_basic_web_property(driver) elif driver_type == webdriver.Firefox: self.log.info("Create FireFox Driver...") driver = webdriver.Firefox( log_path=GECKO_DRIVER_LOG_PATH) # type: webdriver.Firefox return self.__set_basic_web_property(driver) else: raise Exception("unimplemented driver type")
def __init__(self, run_classes): self.run_classes = run_classes # type: dict self.log = BaseLog().log
import unittest from base.baseLog import BaseLog from testcase.demo.testcase1 import TestDemo if __name__ == "__main__": demo_test = unittest.TestLoader().loadTestsFromTestCase(TestDemo) log = BaseLog().log log.info("\n\n Test Start \n ------------------------------------------------------------> ") unittest.TextTestRunner().run(demo_test) log.info("\n\n <------------------------------------------------------------ \nTest End\n\n\n ")