import execjs #执行本地的js def get_js(): # f = open("D:/WorkSpace/MyWorkSpace/jsdemo/js/des_rsa.js",‘r‘,encoding=‘UTF-8‘) f = open("./js/des_rsa.js", ‘r‘, encoding=‘UTF-8‘) line = f.readline() htmlstr = ‘‘ while line: htmlstr = htmlstr + line line = f.readline() return htmlstr jsstr = get_js() ctx = execjs.compile(jsstr) print(ctx.call(‘enString‘,‘123456‘))
class erp_canvas_driver( browserDriver): __inject_js_files__ = [] def __init__(self): browserDriver.__init__(self) self.inject_js_files = [ os.path.join(self.rootPath, ‘lib/erp/javascripts/ERPOAL.js‘), os.path.join(self.rootPath, ‘lib/erp/javascripts/ERPActors.js‘), os.path.join(self.rootPath, ‘lib/erp/javascripts/browser_utils.js‘) ] def openPage(self, url=‘/ERP‘): if browserDriver.openPage(self, url): self.__initHelper__() def __injectJSCode__(self, path): js_lines = [] file = open(os.path.abspath(path)) for line in file: js_lines.append(line) self.browser.execute_script(‘\n‘.join(js_lines)) def __initHelper__(self): for path in self.inject_js_files: self.__injectJSCode__(path) time.sleep(1) def navigateByButton(self, button_name): self.clickERPButton(button_name) time.sleep(5) try: WebDriverWait(self.browser, 10).until( EC.presence_of_element_located((By.ID, "obj1")) ) finally: self.__initHelper__() def clickERPButton(self, name): code = ‘mouseClick("{0}")‘.format(name) self.browser.execute_script(code) def pressERPButton(self, name): code = ‘mousePress("{0}")‘.format(name) self.browser.execute_script(code) def releaseERPButton(self, name): code = ‘mouseRelease("{0}")‘.format(name) self.browser.execute_script(code) def waitDisplayUpdate(self, name, property, expect_value, timeout=20): n = min(int(timeout / 0.2), 50) for i in range(n): value = self.browser.execute_script(‘return ERPGetElementProperty("{0}", "{1}")‘.format(name, property)) if value == expect_value: return True time.sleep(0.2) return False def getElementProperty(self, name, property): return self.browser.execute_script(‘return ERPGetElementProperty("{0}", "{1}")‘.format(name, property)) def setElementProperty(self, name, property, value, isDigit=False): if isDigit: self.browser.execute_script(‘ERPSetElementProperty("{0}", "{1}", {2});‘.format(name, property, value)) else: self.browser.execute_script(‘ERPSetElementProperty("{0}", "{1}", "{2}");‘.format(name, property, value))