1
1
from __future__ import annotations
2
2
3
+ from typing import TYPE_CHECKING
4
+
3
5
import pytest
4
6
5
7
from crawlee .browsers import BrowserPool , PlaywrightBrowserPlugin
6
8
9
+ if TYPE_CHECKING :
10
+ from httpx import URL
11
+
7
12
8
- async def test_default_plugin_new_page_creation (httpbin : str ) -> None :
13
+ async def test_default_plugin_new_page_creation (httpbin : URL ) -> None :
9
14
async with BrowserPool () as browser_pool :
10
15
page_1 = await browser_pool .new_page ()
11
- await page_1 .page .goto (f' { httpbin } /get' )
16
+ await page_1 .page .goto (str ( httpbin . copy_with ( path = ' /get')) )
12
17
assert page_1 .browser_type == 'chromium'
13
- assert page_1 .page .url == f' { httpbin } /get'
18
+ assert page_1 .page .url == str ( httpbin . copy_with ( path = ' /get'))
14
19
assert '<html' in await page_1 .page .content () # there is some HTML content
15
20
assert browser_pool .total_pages_count == 1
16
21
17
22
page_2 = await browser_pool .new_page ()
18
- await page_2 .page .goto (f' { httpbin } /status/200' )
23
+ await page_2 .page .goto (str ( httpbin . copy_with ( path = ' /status/200')) )
19
24
assert page_2 .browser_type == 'chromium'
20
- assert page_2 .page .url == f' { httpbin } /status/200'
25
+ assert page_2 .page .url == str ( httpbin . copy_with ( path = ' /status/200'))
21
26
assert '<html' in await page_1 .page .content () # there is some HTML content
22
27
assert browser_pool .total_pages_count == 2
23
28
24
29
await page_1 .page .close ()
25
30
await page_2 .page .close ()
26
31
27
32
28
- async def test_multiple_plugins_new_page_creation (httpbin : str ) -> None :
33
+ async def test_multiple_plugins_new_page_creation (httpbin : URL ) -> None :
29
34
plugin_chromium = PlaywrightBrowserPlugin (browser_type = 'chromium' )
30
35
plugin_firefox = PlaywrightBrowserPlugin (browser_type = 'firefox' )
31
36
32
37
async with BrowserPool ([plugin_chromium , plugin_firefox ]) as browser_pool :
33
38
assert browser_pool .plugins == [plugin_chromium , plugin_firefox ]
34
39
35
40
page_1 = await browser_pool .new_page ()
36
- await page_1 .page .goto (f' { httpbin } /get' )
41
+ await page_1 .page .goto (str ( httpbin . copy_with ( path = ' /get')) )
37
42
assert page_1 .browser_type == 'chromium'
38
- assert page_1 .page .url == f' { httpbin } /get'
43
+ assert page_1 .page .url == str ( httpbin . copy_with ( path = ' /get'))
39
44
assert '<html' in await page_1 .page .content () # there is some HTML content
40
45
41
46
page_2 = await browser_pool .new_page ()
42
- await page_2 .page .goto (f' { httpbin } /headers' )
47
+ await page_2 .page .goto (str ( httpbin . copy_with ( path = ' /headers')) )
43
48
assert page_2 .browser_type == 'firefox'
44
- assert page_2 .page .url == f' { httpbin } /headers'
49
+ assert page_2 .page .url == str ( httpbin . copy_with ( path = ' /headers'))
45
50
assert '<html' in await page_2 .page .content () # there is some HTML content
46
51
47
52
page_3 = await browser_pool .new_page ()
48
- await page_3 .page .goto (f' { httpbin } /user-agent' )
53
+ await page_3 .page .goto (str ( httpbin . copy_with ( path = ' /user-agent')) )
49
54
assert page_3 .browser_type == 'chromium'
50
- assert page_3 .page .url == f' { httpbin } /user-agent'
55
+ assert page_3 .page .url == str ( httpbin . copy_with ( path = ' /user-agent'))
51
56
assert '<html' in await page_3 .page .content () # there is some HTML content
52
57
53
58
await page_1 .page .close ()
@@ -57,7 +62,7 @@ async def test_multiple_plugins_new_page_creation(httpbin: str) -> None:
57
62
assert browser_pool .total_pages_count == 3
58
63
59
64
60
- async def test_new_page_with_each_plugin (httpbin : str ) -> None :
65
+ async def test_new_page_with_each_plugin (httpbin : URL ) -> None :
61
66
plugin_chromium = PlaywrightBrowserPlugin (browser_type = 'chromium' )
62
67
plugin_firefox = PlaywrightBrowserPlugin (browser_type = 'firefox' )
63
68
@@ -69,12 +74,12 @@ async def test_new_page_with_each_plugin(httpbin: str) -> None:
69
74
assert pages [0 ].browser_type == 'chromium'
70
75
assert pages [1 ].browser_type == 'firefox'
71
76
72
- await pages [0 ].page .goto (f' { httpbin } /get' )
73
- assert pages [0 ].page .url == f' { httpbin } /get'
77
+ await pages [0 ].page .goto (str ( httpbin . copy_with ( path = ' /get')) )
78
+ assert pages [0 ].page .url == str ( httpbin . copy_with ( path = ' /get'))
74
79
assert '<html' in await pages [0 ].page .content () # there is some HTML content
75
80
76
- await pages [1 ].page .goto (f' { httpbin } /headers' )
77
- assert pages [1 ].page .url == f' { httpbin } /headers'
81
+ await pages [1 ].page .goto (str ( httpbin . copy_with ( path = ' /headers')) )
82
+ assert pages [1 ].page .url == str ( httpbin . copy_with ( path = ' /headers'))
78
83
assert '<html' in await pages [1 ].page .content ()
79
84
80
85
for page in pages :
@@ -83,16 +88,16 @@ async def test_new_page_with_each_plugin(httpbin: str) -> None:
83
88
assert browser_pool .total_pages_count == 2
84
89
85
90
86
- async def test_with_default_plugin_constructor (httpbin : str ) -> None :
91
+ async def test_with_default_plugin_constructor (httpbin : URL ) -> None :
87
92
async with BrowserPool .with_default_plugin (headless = True , browser_type = 'firefox' ) as browser_pool :
88
93
assert len (browser_pool .plugins ) == 1
89
94
assert isinstance (browser_pool .plugins [0 ], PlaywrightBrowserPlugin )
90
95
91
96
page = await browser_pool .new_page ()
92
97
assert page .browser_type == 'firefox'
93
98
94
- await page .page .goto (f' { httpbin } /get' )
95
- assert page .page .url == f' { httpbin } /get'
99
+ await page .page .goto (str ( httpbin . copy_with ( path = ' /get')) )
100
+ assert page .page .url == str ( httpbin . copy_with ( path = ' /get'))
96
101
assert '<html' in await page .page .content () # there is some HTML content
97
102
98
103
await page .page .close ()
@@ -114,13 +119,13 @@ async def test_new_page_with_invalid_plugin() -> None:
114
119
await browser_pool .new_page (browser_plugin = plugin_2 )
115
120
116
121
117
- async def test_resource_management (httpbin : str ) -> None :
122
+ async def test_resource_management (httpbin : URL ) -> None :
118
123
playwright_plugin = PlaywrightBrowserPlugin (browser_type = 'chromium' )
119
124
120
125
async with BrowserPool ([playwright_plugin ]) as browser_pool :
121
126
page = await browser_pool .new_page ()
122
- await page .page .goto (f' { httpbin } /get' )
123
- assert page .page .url == f' { httpbin } /get'
127
+ await page .page .goto (str ( httpbin . copy_with ( path = ' /get')) )
128
+ assert page .page .url == str ( httpbin . copy_with ( path = ' /get'))
124
129
assert '<html' in await page .page .content () # there is some HTML content
125
130
assert browser_pool .total_pages_count == 1
126
131
0 commit comments