|
| 1 | +import pytest |
| 2 | +import pytest_httpbin |
| 3 | + |
| 4 | +from scrapling import StealthyFetcher |
| 5 | + |
| 6 | + |
| 7 | +@pytest_httpbin.use_class_based_httpbin |
| 8 | +@pytest.mark.asyncio |
| 9 | +class TestStealthyFetcher: |
| 10 | + @pytest.fixture(scope="class") |
| 11 | + def fetcher(self): |
| 12 | + return StealthyFetcher(auto_match=False) |
| 13 | + |
| 14 | + @pytest.fixture(scope="class") |
| 15 | + def urls(self, httpbin): |
| 16 | + url = httpbin.url |
| 17 | + return { |
| 18 | + 'status_200': f'{url}/status/200', |
| 19 | + 'status_404': f'{url}/status/404', |
| 20 | + 'status_501': f'{url}/status/501', |
| 21 | + 'basic_url': f'{url}/get', |
| 22 | + 'html_url': f'{url}/html', |
| 23 | + 'delayed_url': f'{url}/delay/10', # 10 Seconds delay response |
| 24 | + 'cookies_url': f"{url}/cookies/set/test/value" |
| 25 | + } |
| 26 | + |
| 27 | + async def test_basic_fetch(self, fetcher, urls): |
| 28 | + """Test doing basic fetch request with multiple statuses""" |
| 29 | + assert (await fetcher.async_fetch(urls['status_200'])).status == 200 |
| 30 | + assert (await fetcher.async_fetch(urls['status_404'])).status == 404 |
| 31 | + assert (await fetcher.async_fetch(urls['status_501'])).status == 501 |
| 32 | + |
| 33 | + async def test_networkidle(self, fetcher, urls): |
| 34 | + """Test if waiting for `networkidle` make page does not finish loading or not""" |
| 35 | + assert (await fetcher.async_fetch(urls['basic_url'], network_idle=True)).status == 200 |
| 36 | + |
| 37 | + async def test_blocking_resources(self, fetcher, urls): |
| 38 | + """Test if blocking resources make page does not finish loading or not""" |
| 39 | + assert (await fetcher.async_fetch(urls['basic_url'], block_images=True)).status == 200 |
| 40 | + assert (await fetcher.async_fetch(urls['basic_url'], disable_resources=True)).status == 200 |
| 41 | + |
| 42 | + async def test_waiting_selector(self, fetcher, urls): |
| 43 | + """Test if waiting for a selector make page does not finish loading or not""" |
| 44 | + assert (await fetcher.async_fetch(urls['html_url'], wait_selector='h1')).status == 200 |
| 45 | + assert (await fetcher.async_fetch( |
| 46 | + urls['html_url'], |
| 47 | + wait_selector='h1', |
| 48 | + wait_selector_state='visible' |
| 49 | + )).status == 200 |
| 50 | + |
| 51 | + async def test_cookies_loading(self, fetcher, urls): |
| 52 | + """Test if cookies are set after the request""" |
| 53 | + response = await fetcher.async_fetch(urls['cookies_url']) |
| 54 | + assert response.cookies == {'test': 'value'} |
| 55 | + |
| 56 | + async def test_automation(self, fetcher, urls): |
| 57 | + """Test if automation break the code or not""" |
| 58 | + |
| 59 | + async def scroll_page(page): |
| 60 | + await page.mouse.wheel(10, 0) |
| 61 | + await page.mouse.move(100, 400) |
| 62 | + await page.mouse.up() |
| 63 | + return page |
| 64 | + |
| 65 | + assert (await fetcher.async_fetch(urls['html_url'], page_action=scroll_page)).status == 200 |
| 66 | + |
| 67 | + async def test_properties(self, fetcher, urls): |
| 68 | + """Test if different arguments breaks the code or not""" |
| 69 | + assert (await fetcher.async_fetch( |
| 70 | + urls['html_url'], |
| 71 | + block_webrtc=True, |
| 72 | + allow_webgl=True |
| 73 | + )).status == 200 |
| 74 | + |
| 75 | + assert (await fetcher.async_fetch( |
| 76 | + urls['html_url'], |
| 77 | + block_webrtc=False, |
| 78 | + allow_webgl=True |
| 79 | + )).status == 200 |
| 80 | + |
| 81 | + assert (await fetcher.async_fetch( |
| 82 | + urls['html_url'], |
| 83 | + block_webrtc=True, |
| 84 | + allow_webgl=False |
| 85 | + )).status == 200 |
| 86 | + |
| 87 | + assert (await fetcher.async_fetch( |
| 88 | + urls['html_url'], |
| 89 | + extra_headers={'ayo': ''}, |
| 90 | + os_randomize=True |
| 91 | + )).status == 200 |
| 92 | + |
| 93 | + async def test_infinite_timeout(self, fetcher, urls): |
| 94 | + """Test if infinite timeout breaks the code or not""" |
| 95 | + assert (await fetcher.async_fetch(urls['delayed_url'], timeout=None)).status == 200 |
0 commit comments