def AlertSendKeys(cls, value): log.step_normal("AlertSendKeys [%s]" % value) try: env.threadlocal.BROWSER.switch_to.alert.send_keys(value) env.threadlocal.BROWSER.switch_to.default_content() except: log.step_warning(str(sys.exc_info()))
def parse_conf_class(conf_class): ''' Parse the configure class, get all the properties of the class, then set their corresponding value in module "env". AUTHOR Henry.Wang, 2015-02-11 ''' if sys.getdefaultencoding() != 'utf-8': reload(sys) sys.setdefaultencoding('utf-8') new_conf = [] for var in dir(conf_class): if (not var.startswith("__")) and (not var.endswith("__")): new_conf.append(var) def_conf = [] for var in dir(env): if (not var.startswith("__")) and (not var.endswith("__")): def_conf.append(var) for conf in new_conf: if conf in def_conf: setattr(env, conf, getattr(conf_class, conf)) else: log.step_warning("conf [%s] not found in module env." % conf) # if result path not set in conf, set where the "run.py" file locates. if env.RESULT_PATH == "": env.RESULT_PATH = os.path.dirname( os.path.abspath(inspect.stack()[-1][1]))
def __wait(cls): t = 0 while t < 300: t = t + 1 try: elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) except NoSuchElementException: log.step_normal("Element [%s]: NoSuchElementException." % cls.__name__) elements = [] except UnexpectedAlertPresentException: log.step_warning("Element [%s]: UnexpectedAlertPresentException." % cls.__name__) if len(elements) == 0: time.sleep(0.5) log.step_normal("Element [%s]: Wait 0.5 second, By [%s :: %s :: %s]" % (cls.__name__, cls.by, cls.value, cls.index)) else: if len(elements) > 1: log.step_normal("Element [%s]: There are [%s] Elements!" % (cls.__name__, len(elements))) break if len(elements) < cls.index + 1: log.step_fail("Element [%s]: Element Index Issue! There are [%s] Elements! Index=[%s]" % (cls.__name__, len(elements), cls.index))
def parse_conf_class(conf_class): ''' Parse the configure class, get all the properties of the class, then set their corresponding value in module "env". AUTHOR Henry.Wang, 2015-02-11 ''' if sys.getdefaultencoding() != 'utf-8': reload(sys) sys.setdefaultencoding('utf-8') new_conf = [] for var in dir(conf_class): if (not var.startswith("__")) and (not var.endswith("__")): new_conf.append(var) def_conf = [] for var in dir(env): if (not var.startswith("__")) and (not var.endswith("__")): def_conf.append(var) for conf in new_conf: if conf in def_conf: setattr(env, conf, getattr(conf_class, conf)) else: log.step_warning("conf [%s] not found in module env." % conf) # if result path not set in conf, set where the "run.py" file locates. if env.RESULT_PATH == "": env.RESULT_PATH = os.path.dirname(os.path.abspath(inspect.stack()[-1][1]))
def copy(src, dst): try: if os.path.isdir(src): shutil.copytree(src, dst) else: shutil.copy(src, dst) except: log.step_warning(str(sys.exc_info()))
def SwitchToDefaultContent(cls): log.step_normal("SwitchToDefaultContent()") try: # env.threadlocal.BROWSER.switch_to_default_content() env.threadlocal.BROWSER.switch_to.default_content() except: log.step_warning("env.threadlocal.BROWSER.switch_to.default_content()") pass
def SwitchToDefaultWindow(cls): log.step_normal("SwitchToDefaultWindow()") log.step_normal("Switch To The Default Window of: %s" % str(env.threadlocal.BROWSER.window_handles)) try: env.threadlocal.BROWSER.switch_to.window(env.threadlocal.BROWSER.window_handles[0]) except: log.step_warning("env.threadlocal.BROWSER.switch_to.window(env.threadlocal.BROWSER.window_handles[0])") pass
def remove_readonly(fn, path, excinfo): if os.path.exists(path): try: if fn is os.rmdir: os.chmod(path, stat.S_IWRITE) os.rmdir(path) elif fn is os.remove: os.chmod(path, stat.S_IWRITE) os.remove(path) except: log.step_warning(str(sys.exc_info()))
def __wait_for_appearing(cls): t = 0 while t < 120: t = t + 1 try: elements = env.threadlocal.BROWSER.find_elements(cls.by, cls.value) except NoSuchElementException: log.step_normal("Element [%s]: NoSuchElementException." % cls.__name__) elements = [] continue except UnexpectedAlertPresentException: log.step_warning("Element [%s]: UnexpectedAlertPresentException." % cls.__name__) if len(elements) == 0: time.sleep(0.5) log.step_normal("Element [%s]: WaitForAppearing... Wait 1 second, By [%s]" % (cls.__name__, cls.value)) else: log.step_normal("Element [%s]: Found [%s] Element. Tried [%s] Times." % (cls.__name__, len(elements), t)) break
def mkdirs(dir_path): try: if not os.path.exists(dir_path): os.makedirs(dir_path) except: log.step_warning(str(sys.exc_info()))
def delete_folder(folder_path): try: shutil.rmtree(folder_path) except: log.step_warning(str(sys.exc_info()))
def launch_browser(url): ''' Launch a new browser, and set the parameters for the browser. ''' if env.threadlocal.TESTING_BROWSER.upper() == 'FIREFOX': fp = FirefoxProfile() fp.native_events_enabled = False if env.FIREFOX_BINARY == '': try: env.THREAD_LOCK.acquire() browser = webdriver.Firefox(firefox_profile=fp) except: if isinstance(env.RESERVED_FIREFOX_BINARY, str) and env.RESERVED_FIREFOX_BINARY != "": browser = webdriver.Firefox(firefox_profile=fp, firefox_binary=FirefoxBinary(firefox_path=env.RESERVED_FIREFOX_BINARY)) else: try: log.step_warning("try to start firefox again!") time.sleep(20) browser = webdriver.Firefox(firefox_profile=fp) except: log.handle_error() return False finally: env.THREAD_LOCK.release() else: browser = webdriver.Firefox(firefox_profile=fp, firefox_binary=FirefoxBinary(firefox_path=env.FIREFOX_BINARY)) elif env.threadlocal.TESTING_BROWSER.upper() == 'CHROME': if env.DRIVER_OF_CHROME == '': print ('DRIVER_OF_CHROME is empty.') return False os.environ['webdriver.chrome.driver'] = env.DRIVER_OF_CHROME browser = webdriver.Chrome(executable_path=env.DRIVER_OF_CHROME) elif env.threadlocal.TESTING_BROWSER.upper() == 'IE': if env.DRIVER_OF_IE == '': print ('DRIVER_OF_IE is empty.') return False # os.popen('TASKKILL /F /IM iexplore.exe') os.popen('TASKKILL /F /IM IEDriverServer.exe') dc = DesiredCapabilities.INTERNETEXPLORER.copy() dc['nativeEvents'] = False dc['acceptSslCerts'] = True os.environ['webdriver.ie.driver'] = env.DRIVER_OF_IE browser = webdriver.Ie(executable_path=env.DRIVER_OF_IE, capabilities=dc) elif env.threadlocal.TESTING_BROWSER.upper() == 'PHANTOMJS': browser = webdriver.PhantomJS(r'E:\\AutomationWork\\phantomjs-1.9.8-windows\\phantomjs.exe') if not env.threadlocal.TESTING_BROWSER in env.BROWSER_VERSION_INFO: env.BROWSER_VERSION_INFO[env.threadlocal.TESTING_BROWSER] = browser.capabilities['version'] browser.set_window_size(1366, 758) browser.set_window_position(0, 0) browser.set_page_load_timeout(300) browser.implicitly_wait(0) browser.get(url) return browser
def quit_browser(browser): try: browser.quit() except: log.step_warning(str(sys.exc_info()))