Best Python code snippet using playwright-python
test_defaultbrowsercontext.py
Source: test_defaultbrowsercontext.py
...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()...
Playwright error connection refused in docker
playwright-python advanced setup
How to select an input according to a parent sibling label
Error when installing Microsoft Playwright
Trouble waiting for changes to complete that are triggered by Python Playwright `select_option`
Capturing and Storing Request Data Using Playwright for Python
Can Playwright be used to launch a browser instance
Trouble in Clicking on Log in Google Button of Pop Up Menu Playwright Python
Scrapy Playwright get date by clicking button
React locator example
I solved my problem. In fact my docker container (frontend) is called "app" which is also domain name of fronend application. My application is running locally on http. Chromium and geko drivers force httpS connection for some domain names one of which is "app". So i have to change name for my docker container wich contains frontend application.
Check out the latest blogs from LambdaTest on this topic:
The sky’s the limit (and even beyond that) when you want to run test automation. Technology has developed so much that you can reduce time and stay more productive than you used to 10 years ago. You needn’t put up with the limitations brought to you by Selenium if that’s your go-to automation testing tool. Instead, you can pick from various test automation frameworks and tools to write effective test cases and run them successfully.
When it comes to web automation testing, there are a number of frameworks like Selenium, Cypress, PlayWright, Puppeteer, etc., that make it to the ‘preferred list’ of frameworks. The choice of test automation framework depends on a range of parameters like type, complexity, scale, along with the framework expertise available within the team. However, it’s no surprise that Selenium is still the most preferred framework among developers and QAs.
Playwright is a framework that I’ve always heard great things about but never had a chance to pick up until earlier this year. And since then, it’s become one of my favorite test automation frameworks to use when building a new automation project. It’s easy to set up, feature-packed, and one of the fastest, most reliable frameworks I’ve worked with.
The speed at which tests are executed and the “dearth of smartness” in testing are the two major problems developers and testers encounter.
With the rapidly evolving technology due to its ever-increasing demand in today’s world, Digital Security has become a major concern for the Software Industry. There are various ways through which Digital Security can be achieved, Captcha being one of them.Captcha is easy for humans to solve but hard for “bots” and other malicious software to figure out. However, Captcha has always been tricky for the testers to automate, as many of them don’t know how to handle captcha in Selenium or using any other test automation framework.
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.
Get 100 minutes of automation test minutes FREE!!