From 69bc5dcb20ee4e977489fa104f935cfd515a7a27 Mon Sep 17 00:00:00 2001 From: Ordanis Sanchez Date: Wed, 21 Mar 2018 18:46:57 -0400 Subject: [PATCH] Fix HTML class to use async iter and render on bare mode --- requests_html.py | 5 ++-- tests/test_requests_html.py | 46 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/requests_html.py b/requests_html.py index 9eeb64f..1fa48d6 100644 --- a/requests_html.py +++ b/requests_html.py @@ -410,7 +410,7 @@ class HTML(BaseParser): :param default_encoding: Which encoding to default to. """ - def __init__(self, *, session: Union['HTMLSession', 'AsyncHTMLSession'] = None, url: str = DEFAULT_URL, html: _HTML, default_encoding: str = DEFAULT_ENCODING) -> None: + def __init__(self, *, session: Union['HTMLSession', 'AsyncHTMLSession'] = None, url: str = DEFAULT_URL, html: _HTML, default_encoding: str = DEFAULT_ENCODING, async_: bool = False) -> None: # Convert incoming unicode HTML into bytes. if isinstance(html, str): @@ -423,7 +423,7 @@ class HTML(BaseParser): url=url, default_encoding=default_encoding ) - self.session = session or HTMLSession() + self.session = session or async_ and AsyncHTMLSession() or HTMLSession() self.page = None self.next_symbol = DEFAULT_NEXT_SYMBOL @@ -610,6 +610,7 @@ class HTML(BaseParser): async def arender(self, retries: int = 8, script: str = None, wait: float = 0.2, scrolldown=False, sleep: int = 0, reload: bool = True, timeout: Union[float, int] = 8.0, keep_page: bool = False): """ Async version of render. Takes same parameters. """ + self.browser = await self.session.browser content = None diff --git a/tests/test_requests_html.py b/tests/test_requests_html.py index 4a40c2d..0c9fe0f 100644 --- a/tests/test_requests_html.py +++ b/tests/test_requests_html.py @@ -236,6 +236,29 @@ def test_bare_render(): assert 'https://httpbin.org' in html.links +@pytest.mark.render +@pytest.mark.asyncio +async def test_bare_arender(): + doc = """""" + html = HTML(html=doc, async_=True) + script = """ + () => { + return { + width: document.documentElement.clientWidth, + height: document.documentElement.clientHeight, + deviceScaleFactor: window.devicePixelRatio, + } + } + """ + val = await html.arender(script=script, reload=False) + for value in ('width', 'height', 'deviceScaleFactor'): + assert value in val + + assert html.find('html') + assert 'https://httpbin.org' in html.links + await html.browser.close() + + @pytest.mark.render def test_bare_js_eval(): doc = """ @@ -257,6 +280,29 @@ def test_bare_js_eval(): assert html.find('#replace', first=True).text == 'yolo' +@pytest.mark.render +@pytest.mark.asyncio +async def test_bare_js_async_eval(): + doc = """ + + + +
This gets replaced
+ + + + + """ + + html = HTML(html=doc, async_=True) + await html.arender() + + assert html.find('#replace', first=True).text == 'yolo' + await html.browser.close() + + @pytest.mark.ok def test_browser_session(): """ Test browser instaces is created and properly close when session is closed.