-
-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add onpagecomplete.py snippet (#403) and others.
Add tools/run_snippets.py.
- Loading branch information
Showing
7 changed files
with
105 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
""" | ||
Execute custom Python code on a web page when page loading is complete. | ||
Implements a custom "_OnPageComplete" event in the LoadHandler object. | ||
""" | ||
|
||
from cefpython3 import cefpython as cef | ||
|
||
|
||
def main(): | ||
cef.Initialize() | ||
browser = cef.CreateBrowserSync(url="https://www.google.com/", | ||
window_title="_OnPageComplete event") | ||
browser.SetClientHandler(LoadHandler()) | ||
cef.MessageLoop() | ||
del browser | ||
cef.Shutdown() | ||
|
||
|
||
class LoadHandler(object): | ||
def OnLoadingStateChange(self, browser, is_loading, **_): | ||
"""For detecting if page loading has ended it is recommended | ||
to use OnLoadingStateChange which is most reliable. The OnLoadEnd | ||
callback also available in LoadHandler can sometimes fail in | ||
some cases e.g. when image loading hangs.""" | ||
if not is_loading: | ||
self._OnPageComplete(browser) | ||
|
||
def _OnPageComplete(self, browser): | ||
print("Page loading is complete!") | ||
browser.ExecuteFunction("alert", "Message from Python: Page loading" | ||
" is complete!") | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright (c) 2018 CEF Python, see the Authors file. | ||
# All rights reserved. Licensed under BSD 3-clause license. | ||
# Project website: https://github.com/cztomczak/cefpython | ||
|
||
""" | ||
Run all snippets from the examples/snippets/ directory | ||
and display a summary at the end. | ||
""" | ||
|
||
from common import * | ||
|
||
import glob | ||
import os | ||
import subprocess | ||
import sys | ||
|
||
|
||
def main(): | ||
# Iterate over all snippets | ||
snippets_iter = glob.glob(os.path.join(SNIPPETS_DIR, "*.py")) | ||
succeeded = [] | ||
failed = [] | ||
for snippet in snippets_iter: | ||
print("[run_snippets.py] Running '{snippet}'..." | ||
.format(snippet=os.path.basename(snippet))) | ||
retcode = subprocess.call([sys.executable, snippet]) | ||
if retcode == 0: | ||
succeeded.append(os.path.basename(snippet)) | ||
else: | ||
print("[run_snippets.py] ERROR while running snippet: {snippet}" | ||
.format(snippet=snippet)) | ||
failed.append(os.path.basename(snippet)) | ||
|
||
# Print summary | ||
summary = "" | ||
for snippet in succeeded: | ||
summary += " OK {snippet}{nl}"\ | ||
.format(snippet=snippet, nl=os.linesep) | ||
for snippet in failed: | ||
summary += " ERROR {snippet}{nl}"\ | ||
.format(snippet=snippet, nl=os.linesep) | ||
summary = summary[:-(len(os.linesep))] | ||
print("[run_snippets.py] SUMMARY:") | ||
print(summary.format()) | ||
if len(failed): | ||
print("[run_snippets.py] ERRORS ({failed}) while running snippets" | ||
.format(failed=len(failed))) | ||
sys.exit(1) | ||
else: | ||
print("[run_snippets.py] OK ({succeeded})" | ||
.format(succeeded=len(succeeded))) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |