Merge pull request #534 from surister/versions_test_fixes

Versions test fixes
This commit is contained in:
surister
2023-02-26 13:27:30 +01:00
committed by GitHub
3 changed files with 1021 additions and 440 deletions
Generated
+978 -392
View File
File diff suppressed because it is too large Load Diff
+28 -29
View File
@@ -1,48 +1,47 @@
import pytest
from requests_html import HTMLSession, AsyncHTMLSession, HTMLResponse
session = HTMLSession()
urls = [
'https://xkcd.com/1957/',
'https://www.reddit.com/',
'https://github.com/psf/requests-html/issues',
'https://discord.com/category/engineering',
'https://stackoverflow.com/',
'https://www.frontiersin.org/',
'https://azure.microsoft.com/en-us'
]
@pytest.mark.parametrize('url', urls)
@pytest.mark.internet
def test_pagination():
pages = (
'https://xkcd.com/1957/',
'https://smile.amazon.com/',
'https://theverge.com/archives'
)
def test_pagination(url: str):
session = HTMLSession()
r = session.get(url)
assert next(r.html)
for page in pages:
r = session.get(page)
assert next(r.html)
@pytest.mark.parametrize('url', urls)
@pytest.mark.internet
@pytest.mark.asyncio
async def test_async_pagination(event_loop):
async def test_async_pagination(event_loop, url):
asession = AsyncHTMLSession()
pages = (
'https://xkcd.com/1957/',
'https://smile.amazon.com/',
'https://theverge.com/archives'
)
for page in pages:
r = await asession.get(page)
assert await r.html.__anext__()
r = await asession.get(url)
assert await r.html.__anext__()
@pytest.mark.internet
def test_async_run():
asession = AsyncHTMLSession()
async def test1():
return await asession.get('https://xkcd.com/1957/')
async_list = []
for url in urls:
async def _test():
return await asession.get(url)
async_list.append(_test)
async def test2():
return await asession.get('https://reddit.com/')
r = asession.run(*async_list)
async def test3():
return await asession.get('https://smile.amazon.com/')
r = asession.run(test1, test2, test3)
assert len(r) == 3
assert len(r) == len(urls)
assert isinstance(r[0], HTMLResponse)
+15 -19
View File
@@ -13,14 +13,14 @@ session.mount('file://', FileAdapter())
def get():
path = os.path.sep.join((os.path.dirname(os.path.abspath(__file__)), 'python.html'))
url = 'file://{}'.format(path)
url = f'file://{path}'
return session.get(url)
@pytest.fixture
def async_get(event_loop):
""" AsyncSession cannot be created global since it will create
"""AsyncSession cannot be created global since it will create
a different loop from pytest-asyncio. """
async_session = AsyncHTMLSession()
async_session.mount('file://', FileAdapter())
@@ -54,8 +54,8 @@ def test_css_selector():
about = r.html.find('#about', first=True)
for menu_item in (
'About', 'Applications', 'Quotes', 'Getting Started', 'Help',
'Python Brochure'
'About', 'Applications', 'Quotes', 'Getting Started', 'Help',
'Python Brochure'
):
assert menu_item in about.text.split('\n')
assert menu_item in about.full_text.split('\n')
@@ -290,16 +290,24 @@ async def test_bare_js_async_eval():
def test_browser_session():
""" Test browser instaces is created and properly close when session is closed.
""" Test browser instances is created and properly close when session is closed.
Note: session.close method need to be tested together with browser creation,
since no doing that will left the browser running. """
since not doing that will leave the browser running. """
session = HTMLSession()
assert isinstance(session.browser, Browser)
assert hasattr(session, "loop") == True
assert hasattr(session, "loop")
session.close()
# assert count_chromium_process() == 0
def test_browser_process():
for _ in range(3):
r = get()
r.html.render()
assert r.html.page is None
@pytest.mark.asyncio
async def test_browser_session_fail():
""" HTMLSession.browser should not be call within an existing event loop> """
@@ -308,21 +316,9 @@ async def test_browser_session_fail():
session.browser
def test_browser_process():
for _ in range(3):
r = get()
r.html.render()
assert r.html.page == None
@pytest.mark.asyncio
async def test_async_browser_session():
session = AsyncHTMLSession()
browser = await session.browser
assert isinstance(browser, Browser)
await session.close()
if __name__ == '__main__':
test_containing()