def test_non_toplevel_func(): def func(self, url): self.get(url) return self.title chrome = Chromeless(function_name="local") chrome.attach(func) assert supposed_title in chrome.func(demo_url).lower()
def test_non_toplevel_func(): def child_func(self, url): self.get(url) return self.title chrome = Chromeless() chrome.attach(child_func) assert supposed_title in chrome.child_func(demo_url).lower()
def test_language(): chrome = Chromeless() def wrapper(self): self.get("http://example.selenium.jp/reserveApp/") return self.get_screenshot_as_png() chrome.attach(wrapper) png = chrome.wrapper() with open('./jpn.png', 'wb') as f: f.write(png) tool = pyocr.get_available_tools()[0] txt = tool.image_to_string(Image.open('./jpn.png'), lang='jpn', builder=pyocr.builders.TextBuilder()) assert "予約フォーム" in txt or "朝食バイキング" in txt
def test_README_example(): """ test chromeless pypi package with example function. """ gateway_domain, gateway_apikey = get_credentials() gateway_url = f"https://{gateway_domain}/dev/chromeless" def get_title(self, url): self.get(url) return self.title from chromeless import Chromeless chrome = Chromeless(gateway_url, gateway_apikey) chrome.attach_method(get_title) title = chrome.get_title("https://google.com") print(title) assert title == "Google"
def test_example(): chrome = Chromeless() chrome.attach(example) chrome.attach(second_method) result = chrome.example(demo_url) print(result) return result
def url_to_gif(url, filename): error_place = None gif_success = False try: requests.get(url, timeout=10).raise_for_status() except Exception as e: error_place = 'requests.get' return gif_success, error_place chrome = Chromeless(chromelesss_url, chromelesss_apikey, chrome_options=chrome_options) chrome.attach_method(scrolling_capture) chrome.attach_method(scroll_each_iter) try: pngs = chrome.scrolling_capture(url) except Exception as e: print(e) print('failed', url) gif_success = False error_place = 'chromeless' return gif_success, error_place gif_success = raw_pngs_to_gif(pngs, filename) if not gif_success: error_place = 'raw_pngs_to_gif' return gif_success, error_place
def test_api(): chrome = Chromeless(os.getenv('API_URL', "None"), os.getenv('API_KEY', "None")) chrome.attach(example) chrome.attach(second_method) result = chrome.example(demo_url) print(result) return result
def test_reserved_method_name_attached(): def func(self, url): self.get(url) return self.title chrome = Chromeless() chrome.attach(func) try: chrome.func(demo_url).lower() except Exception: import traceback detail = traceback.format_exc() REQUIRED_SERVER_VERSION = chrome.REQUIRED_SERVER_VERSION if hasattr( chrome, "REQUIRED_SERVER_VERSION") else None if REQUIRED_SERVER_VERSION == 1 or REQUIRED_SERVER_VERSION is None: assert "return pickle.loads(zlib.decompress(base64.b64decode(obj.encode())))" in detail else: assert "CHROMELESS TRACEBACK IN LAMBDA START" in detail assert "'func' might be reserved variable name in chromeless. Please retry after re-naming." in detail assert "CHROMELESS TRACEBACK IN LAMBDA END" in detail
def test_error(chrome=None): def method(self, url): self.get(url) self.find_element_by_xpath("//invalid") chrome = Chromeless() if chrome is None else chrome chrome.attach(method) try: chrome.method(demo_url) except Exception: import traceback detail = traceback.format_exc() REQUIRED_SERVER_VERSION = chrome.REQUIRED_SERVER_VERSION if hasattr( chrome, "REQUIRED_SERVER_VERSION") else None if REQUIRED_SERVER_VERSION == 1 or REQUIRED_SERVER_VERSION is None: assert "return pickle.loads(zlib.decompress(base64.b64decode(obj.encode())))" in detail else: assert "CHROMELESS TRACEBACK IN LAMBDA START" in detail assert "NoSuchElementException" in detail assert "CHROMELESS TRACEBACK IN LAMBDA END" in detail else: assert False
def attaching_from_interactive_mode(): chrome = Chromeless() try: chrome.attach(get_title) chrome.get_title("https://example.com/") except RuntimeError as e: if "Chromeless does not support interactive mode. Please run from files." in str(e): print("OK") else: raise e except OSError as e: import chromeless from packaging import version chromeless_version = chromeless.__version__ if hasattr( chromeless, '__version__') else '0.0.1' if version.parse(chromeless_version) > version.parse("0.7.15"): raise e if "could not get source code" in str(e): print("OK") except Exception: raise else: raise Exception("Expected exception not raised")
def test_error(): chrome = Chromeless(function_name="local") from example import test_error test_error(chrome)
def test_example_locally_named_arg(): chrome = Chromeless(function_name="local") chrome.attach(example) chrome.attach(second_method) title, png, divcnt = chrome.example(url=demo_url) assert_response(title, png, divcnt)
def test_api(): chrome = Chromeless(os.environ['API_URL'], os.environ['API_KEY']) chrome.attach(example) chrome.attach(second_method) title, png, divcnt = chrome.example(demo_url) assert_response(title, png, divcnt)
def test_example(): chrome = Chromeless() chrome.attach(example) chrome.attach(second_method) title, png, divcnt = chrome.example(demo_url) assert_response(title, png, divcnt)
def test_example_with_session_arg(): session = Session() # valid default session chrome = Chromeless(boto3_session=session) chrome.attach(example) chrome.attach(second_method) title, png, divcnt = chrome.example(demo_url) assert_response(title, png, divcnt) # should work session = Session(aws_access_key_id='FOO', aws_secret_access_key='BAR', region_name='ap-northeast-1') # invalid session chrome = Chromeless(boto3_session=session) chrome.attach(example) chrome.attach(second_method) try: chrome.example(demo_url) # shouldn't work except Exception as e: assert str(e).startswith("Invalid session or AWS credentials") else: raise Exception("No expected exception")
from chromeless import Chromeless import base64 def print_pdf(self, url): self.get(url) import json resource = "/session/%s/chromium/send_command_and_get_result" % self.session_id url = self.command_executor._url + resource body = json.dumps({'cmd': "Page.printToPDF", 'params': {}}) response = self.command_executor._request('POST', url, body) return response.get('value')['data'] if __name__ == '__main__': chrome = Chromeless() chrome.attach(print_pdf) url = "https://github.com/umihico/pythonista-chromeless" data = chrome.print_pdf(url) with open("result.pdf", 'wb') as file: file.write(base64.b64decode(data))