Skip to content

Commit 0a0995c

Browse files
authored
chore: Fix test isolation cookies (#1666)
- Closes: #1651
1 parent 430c090 commit 0a0995c

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

tests/unit/crawlers/_playwright/test_playwright_crawler.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
# TODO: The current PlaywrightCrawler tests rely on external websites. It means they can fail or take more time
2-
# due to network issues. To enhance test stability and reliability, we should mock the network requests.
3-
# https://github.com/apify/crawlee-python/issues/197
4-
51
from __future__ import annotations
62

73
import asyncio
84
import json
95
import logging
10-
import sys
116
from datetime import timedelta
127
from typing import TYPE_CHECKING, Any, Literal
138
from unittest import mock
@@ -326,10 +321,6 @@ async def some_hook(context: PlaywrightPreNavCrawlingContext) -> None:
326321
assert handler_data.get('proxy') == proxy_value
327322

328323

329-
@pytest.mark.skipif(
330-
sys.platform != 'linux',
331-
reason='Test is flaky on Windows and MacOS, see https://github.com/apify/crawlee-python/issues/1651.',
332-
)
333324
@pytest.mark.parametrize(
334325
'use_incognito_pages',
335326
[
@@ -357,7 +348,15 @@ async def handler(context: PlaywrightCrawlingContext) -> None:
357348
sessions_ids.append(context.session.id)
358349
sessions[context.session.id] = context.session
359350

360-
if context.request.unique_key not in {'1', '2'}:
351+
if context.request.unique_key == '1':
352+
# With the second request, we check the cookies in the session and set retire
353+
await context.add_requests(
354+
[
355+
Request.from_url(
356+
str(server_url.with_path('/cookies')), unique_key='2', user_data={'retire_session': True}
357+
)
358+
]
359+
)
361360
return
362361

363362
response_data = json.loads(await context.response.text())
@@ -366,14 +365,14 @@ async def handler(context: PlaywrightCrawlingContext) -> None:
366365
if context.request.user_data.get('retire_session'):
367366
context.session.retire()
368367

368+
if context.request.unique_key == '2':
369+
# The third request is made with a new session to make sure it does not use another session's cookies
370+
await context.add_requests([Request.from_url(str(server_url.with_path('/cookies')), unique_key='3')])
371+
369372
await crawler.run(
370373
[
371374
# The first request sets the cookie in the session
372-
str(server_url.with_path('set_cookies').extend_query(a=1)),
373-
# With the second request, we check the cookies in the session and set retire
374-
Request.from_url(str(server_url.with_path('/cookies')), unique_key='1', user_data={'retire_session': True}),
375-
# The third request is made with a new session to make sure it does not use another session's cookies
376-
Request.from_url(str(server_url.with_path('/cookies')), unique_key='2'),
375+
Request.from_url(str(server_url.with_path('set_cookies').extend_query(a=1)), unique_key='1'),
377376
]
378377
)
379378

0 commit comments

Comments
 (0)