How to use launch_persistent method in Playwright Python

Best Python code snippet using playwright-python

test_defaultbrowsercontext.py

Source:test_defaultbrowsercontext.py Github

copy

Full Screen

...16import pytest17from flaky import flaky18from playwright._impl._api_types import Error19@pytest.fixture()20async def launch_persistent(tmpdir, launch_arguments, browser_type):21 context = None22 async def _launch(**options):23 nonlocal context24 if context:25 raise ValueError("can only launch one persitent context")26 context = await browser_type.launch_persistent_context(27 str(tmpdir), **{**launch_arguments, **options}28 )29 return (context.pages[0], context)30 yield _launch31 await context.close()32async def test_context_cookies_should_work(server, launch_persistent, is_firefox):33 (page, context) = await launch_persistent()34 await page.goto(server.EMPTY_PAGE)35 document_cookie = await page.evaluate(36 """() => {37 document.cookie = 'username=John Doe';38 return document.cookie;39 }"""40 )41 assert document_cookie == "username=John Doe"42 assert await page.context.cookies() == [43 {44 "name": "username",45 "value": "John Doe",46 "domain": "localhost",47 "path": "/",48 "expires": -1,49 "httpOnly": False,50 "secure": False,51 "sameSite": "None",52 }53 ]54async def test_context_add_cookies_should_work(server, launch_persistent):55 (page, context) = await launch_persistent()56 await page.goto(server.EMPTY_PAGE)57 await page.context.add_cookies(58 [{"url": server.EMPTY_PAGE, "name": "username", "value": "John Doe"}]59 )60 assert await page.evaluate("() => document.cookie") == "username=John Doe"61 assert await page.context.cookies() == [62 {63 "name": "username",64 "value": "John Doe",65 "domain": "localhost",66 "path": "/",67 "expires": -1,68 "httpOnly": False,69 "secure": False,70 "sameSite": "None",71 }72 ]73async def test_context_clear_cookies_should_work(server, launch_persistent):74 (page, context) = await launch_persistent()75 await page.goto(server.EMPTY_PAGE)76 await page.context.add_cookies(77 [78 {"url": server.EMPTY_PAGE, "name": "cookie1", "value": "1"},79 {"url": server.EMPTY_PAGE, "name": "cookie2", "value": "2"},80 ]81 )82 assert await page.evaluate("document.cookie") == "cookie1=1; cookie2=2"83 await page.context.clear_cookies()84 await page.reload()85 assert await page.context.cookies([]) == []86 assert await page.evaluate("document.cookie") == ""87async def test_should_not_block_third_party_cookies(88 server, launch_persistent, is_chromium, is_firefox89):90 (page, context) = await launch_persistent()91 await page.goto(server.EMPTY_PAGE)92 await page.evaluate(93 """src => {94 let fulfill;95 const promise = new Promise(x => fulfill = x);96 const iframe = document.createElement('iframe');97 document.body.appendChild(iframe);98 iframe.onload = fulfill;99 iframe.src = src;100 return promise;101 }""",102 server.CROSS_PROCESS_PREFIX + "/grid.html",103 )104 document_cookie = await page.frames[1].evaluate(105 """() => {106 document.cookie = 'username=John Doe';107 return document.cookie;108 }"""109 )110 await page.wait_for_timeout(2000)111 allows_third_party = is_chromium or is_firefox112 assert document_cookie == ("username=John Doe" if allows_third_party else "")113 cookies = await context.cookies(server.CROSS_PROCESS_PREFIX + "/grid.html")114 if allows_third_party:115 assert cookies == [116 {117 "domain": "127.0.0.1",118 "expires": -1,119 "httpOnly": False,120 "name": "username",121 "path": "/",122 "sameSite": "None",123 "secure": False,124 "value": "John Doe",125 }126 ]127 else:128 assert cookies == []129async def test_should_support_viewport_option(launch_persistent, utils):130 (page, context) = await launch_persistent(viewport={"width": 456, "height": 789})131 await utils.verify_viewport(page, 456, 789)132 page2 = await context.new_page()133 await utils.verify_viewport(page2, 456, 789)134async def test_should_support_device_scale_factor_option(launch_persistent, utils):135 (page, context) = await launch_persistent(device_scale_factor=3)136 assert await page.evaluate("window.devicePixelRatio") == 3137async def test_should_support_user_agent_option(launch_persistent, server):138 (page, context) = await launch_persistent(user_agent="foobar")139 assert await page.evaluate("() => navigator.userAgent") == "foobar"140 [request, _] = await asyncio.gather(141 server.wait_for_request("/empty.html"),142 page.goto(server.EMPTY_PAGE),143 )144 assert request.getHeader("user-agent") == "foobar"145async def test_should_support_bypass_csp_option(launch_persistent, server):146 (page, context) = await launch_persistent(bypass_csp=True)147 await page.goto(server.PREFIX + "/csp.html")148 await page.add_script_tag(content="window.__injected = 42;")149 assert await page.evaluate("() => window.__injected") == 42150async def test_should_support_javascript_enabled_option(launch_persistent, is_webkit):151 (page, context) = await launch_persistent(java_script_enabled=False)152 await page.goto('data:text/html, <script>var something = "forbidden"</script>')153 with pytest.raises(Error) as exc:154 await page.evaluate("something")155 if is_webkit:156 assert "Can't find variable: something" in exc.value.message157 else:158 assert "something is not defined" in exc.value.message159async def test_should_support_http_credentials_option(server, launch_persistent):160 (page, context) = await launch_persistent(161 http_credentials={"username": "user", "password": "pass"}162 )163 server.set_auth("/playground.html", b"user", b"pass")164 response = await page.goto(server.PREFIX + "/playground.html")165 assert response.status == 200166async def test_should_support_offline_option(server, launch_persistent):167 (page, context) = await launch_persistent(offline=True)168 with pytest.raises(Error):169 await page.goto(server.EMPTY_PAGE)170async def test_should_support_has_touch_option(server, launch_persistent):171 (page, context) = await launch_persistent(has_touch=True)172 await page.goto(server.PREFIX + "/mobile.html")173 assert await page.evaluate('() => "ontouchstart" in window')174@pytest.mark.skip_browser("firefox")175async def test_should_work_in_persistent_context(server, launch_persistent):176 # Firefox does not support mobile.177 (page, context) = await launch_persistent(178 viewport={"width": 320, "height": 480}, is_mobile=True179 )180 await page.goto(server.PREFIX + "/empty.html")181 assert await page.evaluate("() => window.innerWidth") == 980182async def test_should_support_color_scheme_option(server, launch_persistent):183 (page, context) = await launch_persistent(color_scheme="dark")184 assert (185 await page.evaluate('() => matchMedia("(prefers-color-scheme: light)").matches')186 is False187 )188 assert await page.evaluate(189 '() => matchMedia("(prefers-color-scheme: dark)").matches'190 )191async def test_should_support_timezone_id_option(launch_persistent):192 (page, context) = await launch_persistent(timezone_id="America/Jamaica")193 assert (194 await page.evaluate("() => new Date(1479579154987).toString()")195 == "Sat Nov 19 2016 13:12:34 GMT-0500 (Eastern Standard Time)"196 )197async def test_should_support_locale_option(launch_persistent):198 (page, context) = await launch_persistent(locale="fr-CH")199 assert await page.evaluate("() => navigator.language") == "fr-CH"200async def test_should_support_geolocation_and_permission_option(201 server, launch_persistent202):203 (page, context) = await launch_persistent(204 geolocation={"longitude": 10, "latitude": 10}, permissions=["geolocation"]205 )206 await page.goto(server.EMPTY_PAGE)207 geolocation = await page.evaluate(208 """() => new Promise(resolve => navigator.geolocation.getCurrentPosition(position => {209 resolve({latitude: position.coords.latitude, longitude: position.coords.longitude});210 }))"""211 )212 assert geolocation == {"latitude": 10, "longitude": 10}213async def test_should_support_ignore_https_errors_option(214 https_server, launch_persistent215):216 (page, context) = await launch_persistent(ignore_https_errors=True)217 response = await page.goto(https_server.EMPTY_PAGE)218 assert response.ok219async def test_should_support_extra_http_headers_option(server, launch_persistent):220 (page, context) = await launch_persistent(extra_http_headers={"foo": "bar"})221 [request, _] = await asyncio.gather(222 server.wait_for_request("/empty.html"),223 page.goto(server.EMPTY_PAGE),224 )225 assert request.getHeader("foo") == "bar"226async def test_should_accept_user_data_dir(server, tmpdir, launch_persistent):227 (page, context) = await launch_persistent()228 # Note: we need an open page to make sure its functional.229 assert len(os.listdir(tmpdir)) > 0230 await context.close()231 assert len(os.listdir(tmpdir)) > 0232@flaky233async def test_should_restore_state_from_userDataDir(234 browser_type, launch_arguments, server, tmp_path_factory235):236 user_data_dir1 = tmp_path_factory.mktemp("test")237 browser_context = await browser_type.launch_persistent_context(238 user_data_dir1, **launch_arguments239 )240 page = await browser_context.new_page()241 await page.goto(server.EMPTY_PAGE)242 await page.evaluate('() => localStorage.hey = "hello"')243 await browser_context.close()244 browser_context2 = await browser_type.launch_persistent_context(245 user_data_dir1, **launch_arguments246 )247 page2 = await browser_context2.new_page()248 await page2.goto(server.EMPTY_PAGE)249 assert await page2.evaluate("() => localStorage.hey") == "hello"250 await browser_context2.close()251 user_data_dir2 = tmp_path_factory.mktemp("test")252 browser_context3 = await browser_type.launch_persistent_context(253 user_data_dir2, **launch_arguments254 )255 page3 = await browser_context3.new_page()256 await page3.goto(server.EMPTY_PAGE)257 assert await page3.evaluate("() => localStorage.hey") != "hello"258 await browser_context3.close()259async def test_should_restore_cookies_from_userDataDir(260 browser_type,261 launch_arguments,262 tmp_path_factory,263 server,264 is_chromium,265 is_win,266 is_mac,267):268 if is_chromium and (is_win or is_mac):269 pytest.skip()270 userDataDir = tmp_path_factory.mktemp("1")271 browser_context = await browser_type.launch_persistent_context(272 userDataDir, **launch_arguments273 )274 page = await browser_context.new_page()275 await page.goto(server.EMPTY_PAGE)276 document_cookie = await page.evaluate(277 """() => {278 document.cookie = 'doSomethingOnlyOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT';279 return document.cookie;280 }"""281 )282 assert document_cookie == "doSomethingOnlyOnce=true"283 await browser_context.close()284 browser_context2 = await browser_type.launch_persistent_context(285 userDataDir, **launch_arguments286 )287 page2 = await browser_context2.new_page()288 await page2.goto(server.EMPTY_PAGE)289 assert await page2.evaluate("() => document.cookie") == "doSomethingOnlyOnce=true"290 await browser_context2.close()291 userDataDir2 = tmp_path_factory.mktemp("2")292 browser_context3 = await browser_type.launch_persistent_context(293 userDataDir2, **launch_arguments294 )295 page3 = await browser_context3.new_page()296 await page3.goto(server.EMPTY_PAGE)297 assert await page3.evaluate("() => document.cookie") != "doSomethingOnlyOnce=true"298 await browser_context3.close()299async def test_should_have_default_url_when_launching_browser(launch_persistent):300 (page, context) = await launch_persistent()301 urls = list(map(lambda p: p.url, context.pages))302 assert urls == ["about:blank"]303@pytest.mark.skip_browser("firefox")304async def test_should_throw_if_page_argument_is_passed(305 browser_type, server, tmpdir, launch_arguments306):307 options = {**launch_arguments, "args": [server.EMPTY_PAGE]}308 with pytest.raises(Error) as exc:309 await browser_type.launch_persistent_context(tmpdir, **options)310 assert "can not specify page" in exc.value.message311async def test_should_fire_close_event_for_a_persistent_context(launch_persistent):312 (page, context) = await launch_persistent()313 fired_event = asyncio.Future()314 context.on("close", lambda: fired_event.set_result(True))315 await context.close()...

Full Screen

Full Screen

Playwright tutorial

LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful