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-
51from __future__ import annotations
62
73import asyncio
84import json
95import logging
10- import sys
116from datetime import timedelta
127from typing import TYPE_CHECKING , Any , Literal
138from 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