18
18
import pytest
19
19
20
20
21
+ @pytest .fixture (autouse = True )
22
+ def _set_default_ini_file (testdir : pytest .Testdir ) -> None :
23
+ testdir .makeconftest (
24
+ """
25
+ import pytest
26
+
27
+ from pytest_asyncio import is_async_test
28
+
29
+ def pytest_collection_modifyitems(items):
30
+ pytest_asyncio_tests = (item for item in items if is_async_test(item))
31
+ session_scope_marker = pytest.mark.asyncio(loop_scope="session")
32
+ for async_test in pytest_asyncio_tests:
33
+ async_test.add_marker(session_scope_marker, append=False)
34
+ """
35
+ )
36
+
37
+
38
+ def makeconftest (testdir : pytest .Testdir , content : str ) -> None :
39
+ lines = content .split ("\n " )
40
+ spaces = [len (line ) - len (line .lstrip ()) for line in lines if line .strip ()]
41
+ min_spaces = min (spaces ) if spaces else 0
42
+ lines = [line [min_spaces :] for line in lines ]
43
+
44
+ testdir .makeconftest (
45
+ testdir .tmpdir .join ("conftest.py" ).read_text ("utf8" ) + "\n " + "\n " .join (lines )
46
+ )
47
+
48
+
21
49
def test_default (testdir : pytest .Testdir ) -> None :
22
50
testdir .makepyfile (
23
51
"""
@@ -112,14 +140,13 @@ async def test_multiple_browsers(page):
112
140
113
141
114
142
def test_browser_context_args (testdir : pytest .Testdir ) -> None :
115
- testdir .makeconftest (
143
+ makeconftest (
144
+ testdir ,
116
145
"""
117
- import pytest
118
-
119
146
@pytest.fixture(scope="session")
120
147
def browser_context_args():
121
148
return {"user_agent": "foobar"}
122
- """
149
+ """ ,
123
150
)
124
151
testdir .makepyfile (
125
152
"""
@@ -134,14 +161,15 @@ async def test_browser_context_args(page):
134
161
135
162
136
163
def test_user_defined_browser_context_args (testdir : pytest .Testdir ) -> None :
137
- testdir .makeconftest (
164
+ makeconftest (
165
+ testdir ,
138
166
"""
139
167
import pytest
140
168
141
169
@pytest.fixture(scope="session")
142
170
def browser_context_args():
143
171
return {"user_agent": "foobar"}
144
- """
172
+ """ ,
145
173
)
146
174
testdir .makepyfile (
147
175
"""
@@ -159,14 +187,15 @@ async def test_browser_context_args(page):
159
187
160
188
161
189
def test_user_defined_browser_context_args_clear_again (testdir : pytest .Testdir ) -> None :
162
- testdir .makeconftest (
190
+ makeconftest (
191
+ testdir ,
163
192
"""
164
193
import pytest
165
194
166
195
@pytest.fixture(scope="session")
167
196
def browser_context_args():
168
197
return {"user_agent": "foobar"}
169
- """
198
+ """ ,
170
199
)
171
200
testdir .makepyfile (
172
201
"""
@@ -428,15 +457,16 @@ async def test_base_url(page):
428
457
429
458
430
459
def test_browser_context_args_device (testdir : pytest .Testdir ) -> None :
431
- testdir .makeconftest (
460
+ makeconftest (
461
+ testdir ,
432
462
"""
433
463
import pytest
434
464
435
465
@pytest.fixture(scope="session")
436
466
def browser_context_args(browser_context_args, playwright):
437
467
iphone_11 = playwright.devices['iPhone 11 Pro']
438
468
return {**browser_context_args, **iphone_11}
439
- """
469
+ """ ,
440
470
)
441
471
testdir .makepyfile (
442
472
"""
@@ -451,7 +481,8 @@ async def test_browser_context_args(page):
451
481
452
482
453
483
def test_launch_persistent_context_session (testdir : pytest .Testdir ) -> None :
454
- testdir .makeconftest (
484
+ makeconftest (
485
+ testdir ,
455
486
"""
456
487
import pytest_asyncio
457
488
from playwright.sync_api import BrowserType
@@ -470,7 +501,7 @@ async def context(
470
501
})
471
502
yield context
472
503
await context.close()
473
- """
504
+ """ ,
474
505
)
475
506
testdir .makepyfile (
476
507
"""
@@ -485,7 +516,8 @@ async def test_browser_context_args(page):
485
516
486
517
487
518
def test_context_page_on_session_level (testdir : pytest .Testdir ) -> None :
488
- testdir .makeconftest (
519
+ makeconftest (
520
+ testdir ,
489
521
"""
490
522
import pytest
491
523
from playwright.sync_api import Browser, BrowserContext
@@ -509,7 +541,7 @@ async def page(
509
541
):
510
542
page = await context.new_page()
511
543
yield page
512
- """
544
+ """ ,
513
545
)
514
546
testdir .makepyfile (
515
547
"""
@@ -529,7 +561,8 @@ async def test_b(page):
529
561
530
562
531
563
def test_launch_persistent_context_function (testdir : pytest .Testdir ) -> None :
532
- testdir .makeconftest (
564
+ makeconftest (
565
+ testdir ,
533
566
"""
534
567
import pytest
535
568
from playwright.sync_api import BrowserType
@@ -549,7 +582,7 @@ async def context(
549
582
})
550
583
yield context
551
584
await context.close()
552
- """
585
+ """ ,
553
586
)
554
587
testdir .makepyfile (
555
588
"""
@@ -751,11 +784,12 @@ def _assert_folder_structure(root: str, expected: str) -> None:
751
784
752
785
753
786
def test_is_able_to_set_expect_timeout_via_conftest (testdir : pytest .Testdir ) -> None :
754
- testdir .makeconftest (
787
+ makeconftest (
788
+ testdir ,
755
789
"""
756
790
from playwright.async_api import expect
757
791
expect.set_options(timeout=1111)
758
- """
792
+ """ ,
759
793
)
760
794
testdir .makepyfile (
761
795
"""
0 commit comments