def set_javascript_bindings(browser): external = External(browser) bindings = cef.JavascriptBindings(bindToFrames=False, bindToPopups=False) bindings.SetProperty("python_property", "This property was set in Python") bindings.SetProperty("cefpython_version", cef.GetVersion()) bindings.SetFunction("html_to_data_uri", html_to_data_uri) bindings.SetObject("external", external) browser.SetJavascriptBindings(bindings)
def set_javascript_bindings(uid): bindings = cef.JavascriptBindings(bindToFrames=False, bindToPopups=False) bindings.SetProperty("cefPython3", cef.GetVersion()) bindings.SetProperty('windowId', uid) bindings.SetProperty('system', platform.system()) bindings.SetProperty('systemLanguage', get_system_language()) bindings.SetObject('windowInstance', BrowserView.instances[uid]) BrowserView.instances[uid].view.SetJavascriptBindings(bindings)
def check_versions(): ver = cef.GetVersion() print("[hello_world.py] CEF Python {ver}".format(ver=ver["version"])) print("[hello_world.py] Chromium {ver}".format(ver=ver["chrome_version"])) print("[hello_world.py] CEF {ver}".format(ver=ver["cef_version"])) print("[hello_world.py] Python {ver} {arch}".format( ver=platform.python_version(), arch=platform.architecture()[0])) assert cef.__version__ >= "57.0", "CEF Python v57.0+ required to run this"
def print_versions(self): ver = cef.GetVersion() print("CefPython Version Information") print("CEF Python {ver}".format(ver=ver["version"])) print("Chromium {ver}".format(ver=ver["chrome_version"])) print("CEF {ver}".format(ver=ver["cef_version"])) print("Python {ver} {arch}".format(ver=platform.python_version(), arch=platform.architecture()[0]))
def checkVersion(): ver = cef.GetVersion() logging.getLogger('Main').info( "CEF Python {ver}".format(ver=ver["version"])) logging.getLogger('Main').info( "Chromium {ver}".format(ver=ver["chrome_version"])) logging.getLogger('Main').info("CEF {ver}".format(ver=ver["cef_version"])) logging.getLogger('Main').info("Python {ver} {arch}".format( ver=platform.python_version(), arch=platform.architecture()[0])) assert cef.__version__ >= "57.0", "CEF Python v57.0+ required to run this"
def _make_html(s=""): s = _HTML[0] s += _HTML[1].format(FCNT) s += _HTML[2] if len(gl) < 1: s += _HTML[3].format(ico_splash, _MESSAGE[2]) else: for g in gl: g_f = file_name(g) s += _HTML[4].format(g, g_f[0], g_f[1], g_f[2], g_f[3]) s += _HTML[5].format(wd, outF) s += _HTML[6] s += _HTML[7].format(wd, outF) for i in KBINDS: s += _HTML[8].format(i, KBINDS[i]) DO_TAGS = parser.conf.getDoTags() XML_TAGS = parser.conf.getXMLTags() TXT_TAGS = parser.conf.getTXTTags() s += _HTML[9] for tag in DO_TAGS: if tag in XML_TAGS and DO_TAGS[tag]: s += _HTML[10].format(XML_TAGS[tag]) s += _HTML[11] for tag in DO_TAGS: if tag in TXT_TAGS and DO_TAGS[tag]: if (tag != '_HEADER'): s += _HTML[12].format(TXT_TAGS[tag], tag[1:].lower()) else: s += _HTML[13].format(TXT_TAGS[tag].strip()) s += _HTML[14] s += _HTML[15].format(VERSION, '.'.join(['{}'.format(i) for i in sys.version_info]), cef.GetVersion()['chrome_version'], pickle.format_version, cef.GetVersion()['cef_version']) for i in TEAM: s += _HTML[16].format(i) s += _HTML[17] return s
def set_javascript_bindings(browser): external = External(browser) bindings = cef.JavascriptBindings(bindToFrames=False, bindToPopups=False) bindings.SetProperty("python_property", "This property was set in Python") bindings.SetProperty("cefpython_version", cef.GetVersion()) bindings.SetFunction("html_to_data_uri", html_to_data_uri) bindings.SetFunction("lol", lol) bindings.SetFunction("add_file", add_file) bindings.SetFunction("switch_file", switch_file) bindings.SetFunction("remove_file", remove_file) bindings.SetFunction("parse", _parse) bindings.SetFunction("setWD", setWD) bindings.SetFunction("setOut", setOut) bindings.SetFunction("set", set) bindings.SetFunction("refresh", _refresh) bindings.SetObject("external", external) browser.SetJavascriptBindings(bindings)
def set_javascript_bindings(browser): #symbol = Symbol(browser, ibcon) #symbol.getContractDetails('hsi',1) #symbol.t() #c1 = Contract(conId=305074193, exchange='HKFE') #c = Forex("USDJPY") #12087792 #symbol.subMktData(12087792) #time.sleep(5) #symbol.unsubMktData(12087792) #order = Order(ibcon) bindings = cef.JavascriptBindings(bindToFrames=False, bindToPopups=False) bindings.SetProperty("python_property", "This property was set in Python") bindings.SetProperty("cefpython_version", cef.GetVersion()) bindings.SetFunction("html_to_data_uri", html_to_data_uri) #bindings.SetObject("symbol", symbol) #bindings.SetObject("ibcon", ibcon) #bindings.SetObject("order", order) #bindings.SetObject("config", cfgHandler) browser.SetJavascriptBindings(bindings)
def test_main(self): """Main entry point.""" # All this code must run inside one single test, otherwise strange # things happen. print("") print("CEF Python {ver}".format(ver=cef.__version__)) print("Python {ver}".format(ver=sys.version[:6])) # Test initialization of CEF settings = { "debug": False, "log_severity": cef.LOGSEVERITY_ERROR, "log_file": "", } if "--debug" in sys.argv: settings["debug"] = True settings["log_severity"] = cef.LOGSEVERITY_INFO cef.Initialize(settings) subtest_message("cef.Initialize() ok") # Test global handler global_handler = GlobalHandler(self) cef.SetGlobalClientCallback("OnAfterCreated", global_handler._OnAfterCreated) subtest_message("cef.SetGlobalClientCallback() ok") # Test creation of browser browser = cef.CreateBrowserSync(url=g_datauri) self.assertIsNotNone(browser, "Browser object") subtest_message("cef.CreateBrowserSync() ok") # Test other handlers: LoadHandler, DisplayHandler, DialogHandler etc. client_handlers = [ LoadHandler(self), DisplayHandler(self), DialogHandler(self) ] for handler in client_handlers: browser.SetClientHandler(handler) subtest_message("browser.SetClientHandler() ok") # Test javascript bindings external = External(self) bindings = cef.JavascriptBindings(bindToFrames=False, bindToPopups=False) bindings.SetFunction("test_function", external.test_function) bindings.SetProperty("test_property1", external.test_property1) bindings.SetProperty("test_property2", external.test_property2) bindings.SetProperty("cefpython_version", cef.GetVersion()) bindings.SetObject("external", external) browser.SetJavascriptBindings(bindings) subtest_message("browser.SetJavascriptBindings() ok") # Run message loop for some time. # noinspection PyTypeChecker cef_waiting(MESSAGE_LOOP_RANGE) subtest_message("cef.MessageLoopWork() ok") #Simulating file dialog click event for testing OnFileDialog handler browser.SendMouseClickEvent(67, 20, cef.MOUSEBUTTON_LEFT, False, 1) browser.SendMouseClickEvent(67, 20, cef.MOUSEBUTTON_LEFT, True, 1) # Run message loop for some time. cef_waiting(MESSAGE_LOOP_RANGE) # Test browser closing. Remember to clean reference. browser.CloseBrowser(True) del browser subtest_message("browser.CloseBrowser() ok") # Give it some time to close before calling shutdown. # noinspection PyTypeChecker cef_waiting(25) # Automatic check of asserts in handlers and in external automatic_check_handlers(self, [] + client_handlers + [global_handler, external]) # Test shutdown of CEF cef.Shutdown() subtest_message("cef.Shutdown() ok") # Display real number of tests there were run display_number_of_unittest("sub-tests in test_main") sys.stdout.flush()
def test_osr(self): """Main entry point. All the code must run inside one single test, otherwise strange things happen.""" print("") print("CEF Python {ver}".format(ver=cef.__version__)) print("Python {ver}".format(ver=sys.version[:6])) # Application settings settings = { "debug": False, "log_severity": cef.LOGSEVERITY_ERROR, "log_file": "", "windowless_rendering_enabled": True } if not LINUX: # On Linux you get a lot of "X error received" messages # from Chromium's "x11_util.cc", so do not show them. settings["log_severity"] = cef.LOGSEVERITY_WARNING if "--debug" in sys.argv: settings["debug"] = True settings["log_severity"] = cef.LOGSEVERITY_INFO if "--debug-warning" in sys.argv: settings["debug"] = True settings["log_severity"] = cef.LOGSEVERITY_WARNING switches = { # GPU acceleration is not supported in OSR mode, so must disable # it using these Chromium switches (Issue #240 and #463) "disable-gpu": "", "disable-gpu-compositing": "", # Tweaking OSR performance by setting the same Chromium flags # as in upstream cefclient (Issue #240). "enable-begin-frame-scheduling": "", "disable-surfaces": "", # This is required for PDF ext to work } browser_settings = { # Tweaking OSR performance (Issue #240) "windowless_frame_rate": 30, # Default frame rate in CEF is 30 } # Initialize cef.Initialize(settings=settings, switches=switches) subtest_message("cef.Initialize() ok") # Accessibility handler accessibility_handler = AccessibilityHandler(self) cef.SetGlobalClientHandler(accessibility_handler) subtest_message("cef.SetGlobalClientHandler() ok") # Global handler global_handler = GlobalHandler(self) cef.SetGlobalClientCallback("OnAfterCreated", global_handler._OnAfterCreated) subtest_message("cef.SetGlobalClientCallback() ok") # Create browser window_info = cef.WindowInfo() window_info.SetAsOffscreen(0) browser = cef.CreateBrowserSync(window_info=window_info, settings=browser_settings, url=g_datauri) # Javascript bindings bindings = cef.JavascriptBindings(bindToFrames=False, bindToPopups=False) bindings.SetFunction("js_code_completed", js_code_completed) bindings.SetProperty("cefpython_version", cef.GetVersion()) browser.SetJavascriptBindings(bindings) subtest_message("browser.SetJavascriptBindings() ok") # Enable accessibility browser.SetAccessibilityState(cef.STATE_ENABLED) subtest_message("cef.SetAccessibilityState(STATE_ENABLED) ok") # Client handlers client_handlers = [ LoadHandler(self, g_datauri), DisplayHandler(self), RenderHandler(self) ] for handler in client_handlers: browser.SetClientHandler(handler) # Initiate OSR rendering browser.SendFocusEvent(True) browser.WasResized() # Test selection on_load_end(select_h1_text, browser) # Message loop run_message_loop() # Close browser and clean reference browser.CloseBrowser(True) del browser subtest_message("browser.CloseBrowser() ok") # Give it some time to close before checking asserts # and calling shutdown. do_message_loop_work(25) # Asserts before shutdown # noinspection PyTypeChecker check_auto_asserts(self, [] + client_handlers + [global_handler, accessibility_handler]) # Test shutdown of CEF cef.Shutdown() subtest_message("cef.Shutdown() ok") # Display summary show_test_summary(__file__) sys.stdout.flush()
from cefpython3 import cefpython as cef import platform ver = cef.GetVersion() print("[hello_world.py] CEF Python {ver}".format(ver=ver["version"])) print("[hello_world.py] Chromium {ver}".format(ver=ver["chrome_version"])) print("[hello_world.py] CEF {ver}".format(ver=ver["cef_version"])) print("[hello_world.py] Python {ver} {arch}".format(ver=platform.python_version(),arch=platform.architecture()[0])) assert cef.__version__ >= "57.0", "CEF Python v57.0+ required to run this"
def setJavascriptBindings(browser): bindings = cef.JavascriptBindings(bindToFrames=False, bindToPopups=False) bindings.SetProperty("py_cefpython_version", cef.GetVersion()) for functionName in _py_expose_list: bindings.SetFunction(functionName, _py_expose_list[functionName]) browser.SetJavascriptBindings(bindings)
def check_versions(self): ver = cef.GetVersion() assert cef.__version__ >= "57.0", "CEF Python v57.0+ required to run this"
def test_main(self): """Main entry point.""" # All this code must run inside one single test, otherwise strange # things happen. print("") print("CEF Python {ver}".format(ver=cef.__version__)) print("Python {ver}".format(ver=sys.version[:6])) # Test initialization of CEF settings = { "debug": False, "log_severity": cef.LOGSEVERITY_ERROR, "log_file": "", } if "--debug" in sys.argv: settings["debug"] = True settings["log_severity"] = cef.LOGSEVERITY_INFO cef.Initialize(settings) subtest_message("cef.Initialize() ok") # Test global handler global_handler = GlobalHandler(self) cef.SetGlobalClientCallback("OnAfterCreated", global_handler._OnAfterCreated) subtest_message("cef.SetGlobalClientCallback() ok") # Test creation of browser browser = cef.CreateBrowserSync(url=g_datauri) self.assertIsNotNone(browser, "Browser object") subtest_message("cef.CreateBrowserSync() ok") # Test other handlers: LoadHandler, DisplayHandler etc. client_handlers = [LoadHandler(self), DisplayHandler(self)] for handler in client_handlers: browser.SetClientHandler(handler) subtest_message("browser.SetClientHandler() ok") # Test javascript bindings external = External(self) bindings = cef.JavascriptBindings(bindToFrames=False, bindToPopups=False) bindings.SetFunction("test_function", external.test_function) bindings.SetProperty("test_property1", external.test_property1) bindings.SetProperty("test_property2", external.test_property2) # Property with a function value can also be bound. CEF Python # supports passing functions as callbacks when called from # javascript, and as a side effect any value and in this case # a property can also be a function. bindings.SetProperty("test_property3_function", external.test_property3_function) bindings.SetProperty("cefpython_version", cef.GetVersion()) bindings.SetObject("external", external) browser.SetJavascriptBindings(bindings) subtest_message("browser.SetJavascriptBindings() ok") # Run message loop for some time. # noinspection PyTypeChecker for i in range(MESSAGE_LOOP_RANGE): cef.MessageLoopWork() time.sleep(0.01) subtest_message("cef.MessageLoopWork() ok") # Test browser closing. Remember to clean reference. browser.CloseBrowser(True) del browser subtest_message("browser.CloseBrowser() ok") # Give it some time to close before calling shutdown. # noinspection PyTypeChecker for i in range(25): cef.MessageLoopWork() time.sleep(0.01) # Automatic check of asserts in handlers and in external for obj in [] + client_handlers + [global_handler, external]: test_for_True = False # Test whether asserts are working correctly for key, value in obj.__dict__.items(): if key == "test_for_True": test_for_True = True continue if "_True" in key: self.assertTrue( value, "Check assert: " + obj.__class__.__name__ + "." + key) subtest_message(obj.__class__.__name__ + "." + key.replace("_True", "") + " ok") elif "_False" in key: self.assertFalse( value, "Check assert: " + obj.__class__.__name__ + "." + key) subtest_message(obj.__class__.__name__ + "." + key.replace("_False", "") + " ok") self.assertTrue(test_for_True) # Test shutdown of CEF cef.Shutdown() subtest_message("cef.Shutdown() ok") # Display real number of tests there were run print("\nRan " + str(g_subtests_ran) + " sub-tests in test_main") sys.stdout.flush()
def check_versions(): ver = cef.GetVersion()
def test_main(self): """Main entry point. All the code must run inside one single test, otherwise strange things happen.""" print("") print("CEF Python {ver}".format(ver=cef.__version__)) print("Python {ver}".format(ver=sys.version[:6])) # Test initialization of CEF settings = { "debug": False, "log_severity": cef.LOGSEVERITY_ERROR, "log_file": "", } if not LINUX: # On Linux you get a lot of "X error received" messages # from Chromium's "x11_util.cc", so do not show them. settings["log_severity"] = cef.LOGSEVERITY_WARNING if "--debug" in sys.argv: settings["debug"] = True settings["log_severity"] = cef.LOGSEVERITY_INFO if "--debug-warning" in sys.argv: settings["debug"] = True settings["log_severity"] = cef.LOGSEVERITY_WARNING cef.Initialize(settings) subtest_message("cef.Initialize() ok") # CRL set file certrevoc_dir = "" if WINDOWS: certrevoc_dir = r"C:\localappdata\Google\Chrome\User Data" \ r"\CertificateRevocation" elif LINUX: certrevoc_dir = r"/home/*/.config/google-chrome" \ r"/CertificateRevocation" elif MAC: certrevoc_dir = r"/Users/*/Library/Application Support/Google" \ r"/Chrome/CertificateRevocation" crlset_files = glob.iglob(os.path.join(certrevoc_dir, "*", "crl-set")) crlset = "" for crlset in crlset_files: pass if os.path.exists(crlset): cef.LoadCrlSetsFile(crlset) subtest_message("cef.LoadCrlSetsFile ok") # High DPI on Windows if WINDOWS: self.assertIsInstance(cef.DpiAware.GetSystemDpi(), tuple) window_size = cef.DpiAware.CalculateWindowSize(800, 600) self.assertIsInstance(window_size, tuple) self.assertGreater(window_size[0], 0) self.assertGreater(cef.DpiAware.Scale((800, 600))[0], 0) cef.DpiAware.EnableHighDpiSupport() self.assertTrue(cef.DpiAware.IsProcessDpiAware()) # Make some calls again after DPI Aware was set self.assertIsInstance(cef.DpiAware.GetSystemDpi(), tuple) self.assertGreater(cef.DpiAware.Scale([800, 600])[0], 0) self.assertIsInstance(cef.DpiAware.Scale(800), int) self.assertGreater(cef.DpiAware.Scale(800), 0) subtest_message("cef.DpiAware ok") # Global handler global_handler = GlobalHandler(self) cef.SetGlobalClientCallback("OnAfterCreated", global_handler._OnAfterCreated) subtest_message("cef.SetGlobalClientCallback() ok") # Create browser browser_settings = { "inherit_client_handlers_for_popups": False, } browser = cef.CreateBrowserSync(url=g_datauri, settings=browser_settings) self.assertIsNotNone(browser, "Browser object") browser.SetFocus(True) subtest_message("cef.CreateBrowserSync() ok") # Client handlers display_handler2 = DisplayHandler2(self) client_handlers = [LoadHandler(self, g_datauri), DisplayHandler(self), display_handler2] for handler in client_handlers: browser.SetClientHandler(handler) subtest_message("browser.SetClientHandler() ok") # Javascript bindings external = External(self) bindings = cef.JavascriptBindings( bindToFrames=False, bindToPopups=False) bindings.SetFunction("js_code_completed", js_code_completed) bindings.SetFunction("test_function", external.test_function) bindings.SetProperty("test_property1", external.test_property1) bindings.SetProperty("test_property2", external.test_property2) # Property with a function value can also be bound. CEF Python # supports passing functions as callbacks when called from # javascript, and as a side effect any value and in this case # a property can also be a function. bindings.SetProperty("test_property3_function", external.test_property3_function) bindings.SetProperty("cefpython_version", cef.GetVersion()) bindings.SetObject("external", external) browser.SetJavascriptBindings(bindings) subtest_message("browser.SetJavascriptBindings() ok") # Set auto resize. Call it after js bindings were set. browser.SetAutoResizeEnabled(enabled=True, min_size=[800, 600], max_size=[1024, 768]) subtest_message("browser.SetAutoResizeEnabled() ok") # Test Request.SetPostData(list) # noinspection PyArgumentList req = cef.Request.CreateRequest() req_file = os.path.dirname(os.path.abspath(__file__)) req_file = os.path.join(req_file, "main_test.py") if sys.version_info.major > 2: req_file = req_file.encode() req_data = [b"--key=value", b"@"+req_file] req.SetMethod("POST") req.SetPostData(req_data) self.assertEqual(req_data, req.GetPostData()) subtest_message("cef.Request.SetPostData(list) ok") # Test Request.SetPostData(dict) # noinspection PyArgumentList req = cef.Request.CreateRequest() req_data = {b"key": b"value"} req.SetMethod("POST") req.SetPostData(req_data) self.assertEqual(req_data, req.GetPostData()) subtest_message("cef.Request.SetPostData(dict) ok") # Cookie manager self.assertIsInstance(cef.CookieManager.CreateManager(path=""), cef.PyCookieManager) self.assertIsInstance(cef.CookieManager.GetGlobalManager(), cef.PyCookieManager) self.assertIsInstance(cef.CookieManager.GetBlockingManager(), cef.PyCookieManager) subtest_message("cef.CookieManager ok") # Window Utils if WINDOWS: hwnd = 1 # When using 0 getting issues with OnautoResize self.assertFalse(cef.WindowUtils.IsWindowHandle(hwnd)) cef.WindowUtils.OnSetFocus(hwnd, 0, 0, 0) cef.WindowUtils.OnSize(hwnd, 0, 0, 0) cef.WindowUtils.OnEraseBackground(hwnd, 0, 0, 0) cef.WindowUtils.GetParentHandle(hwnd) cef.WindowUtils.SetTitle(browser, "Main test") subtest_message("cef.WindowUtils ok") elif LINUX: cef.WindowUtils.InstallX11ErrorHandlers() subtest_message("cef.WindowUtils ok") elif MAC: hwnd = 0 cef.WindowUtils.GetParentHandle(hwnd) cef.WindowUtils.IsWindowHandle(hwnd) subtest_message("cef.WindowUtils ok") # Run message loop run_message_loop() # Make sure popup browser was destroyed self.assertIsInstance(cef.GetBrowserByIdentifier(MAIN_BROWSER_ID), cef.PyBrowser) self.assertIsNone(cef.GetBrowserByIdentifier(POPUP_BROWSER_ID)) subtest_message("cef.GetBrowserByIdentifier() ok") # Close browser and clean reference browser.CloseBrowser(True) del browser subtest_message("browser.CloseBrowser() ok") # Give it some time to close before checking asserts # and calling shutdown. do_message_loop_work(25) # Asserts before shutdown self.assertEqual(display_handler2.OnLoadingProgressChange_Progress, 1.0) # noinspection PyTypeChecker check_auto_asserts(self, [] + client_handlers + [global_handler, external]) # Test shutdown of CEF cef.Shutdown() subtest_message("cef.Shutdown() ok") # Display summary show_test_summary(__file__) sys.stdout.flush()
def check_versions(): ver = cef.GetVersion() print("[app.py] CEF Python {ver}".format(ver=cef.__version__)) print("[app.py] Python {ver} {arch}".format( ver=platform.python_version(), arch=platform.architecture()[0])) assert cef.__version__ >= "66.1", "CEF Python v66.1+ required to run this"