示例#1
0
def test_example():
    chrome = Chromeless()
    chrome.attach(example)
    chrome.attach(second_method)
    result = chrome.example(demo_url)
    print(result)
    return result
示例#2
0
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()
示例#3
0
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
示例#4
0
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()
示例#5
0
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
示例#6
0
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
示例#8
0
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")
示例#9
0
    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")
示例#10
0
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)
示例#13
0
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))