Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Test browser profile isolation after attaching Playwright over CDP

When Playwright attaches to Chrome over CDP, it can see the browser's existing context. A script that opens a new tab there inherits that context's first-party storage for the origin. That can help a task continue in the right signed-in tab. It can also make a test for signed-out behavior pass through screens it should have tested. An AI assistant helped run the experiment and prepare this…

The Playwright browser automation tool can inspect existing browser contexts when attaching to Chrome over the Chrome DevTools Protocol (CDP). A script demonstrates how a new tab opened within an existing context inherits that context's first-party storage for the targeted origin. This behavior can aid testing by ensuring the right signed-in tab is utilized, but it can also cause tests intended for signed-out behavior to pass unintended screens.

In the experiment described, a Cloudbrowser Chrome session was used with two pages on a single origin. By setting specific environment variables and leveraging Playwright's JavaScript API, a script demonstrated how an existing context's context can read and write to localStorage. A unique key was generated for the test, and two new pages were created within the same existing context.

The first page set the key to "present" in localStorage, while the second page retrieved the key's value, resulting in "present" being logged. A third page, created with a fresh context, did not retain the key's value, returning "null" when queried.

This experiment highlights the importance of isolating browser contexts for testing purposes. When continuing an existing flow, it's crucial to locate the expected tab and verify its origin and page-specific landmarks. Relying solely on the first open tab might lead to automation targeting unrelated pages. To simulate a fresh visit, a new context should be explicitly opened before testing begins.

After the test is complete, closing the context is recommended. When parallel tests share the same remote account, separate contexts should be utilized to prevent request collisions with the same service.

In cases where write tests would result in conflicting changes, it's advisable to use separate application records or accounts. Cloudbrowser provides additional details regarding the persistence of browser profiles, including how session state is retained between tasks and scenarios where users may be prompted to sign in again.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

Basic HTML structure of a webpage

The doctype tag ( ) informs the browser on which type of document is expected and this tag seems to be complicated in older versions of HTML whereas in the latest version ( HTML 5 ) , its simple as…

The message that retried hundreds of times: a retry loop with no exit

I'm Eric Gitangu. I work on backend systems for a mobility/fintech platform. This is the story of one text message that was retried hundreds of times, and what it took to teach the code to take no for…

  • Retry loop with no exit led to hundreds of SMS message retries
  • Three small decisions caused the issue: no catch, error thrown, progress saved prematurely
  • Catch per recipient, exponential backoff, and "not before" stamp resolved the problem

My Framework Said Any: How a Pyrefly Error Led Me to Fix Typing in SwiftPY

I build SwiftPY , a Python web framework with an async Active Record ORM. Last week I opened one of my own repositories in VS Code and saw this: class DocRepository : model = Doc async def get_doc (…

  • Pyrefly error caused Any return instead of "Doc" type
  • SwiftPY fixed type chain gaps in version 0.3.8
  • Pyrefly silent import fix added in version 0.3.9

More from Friday 27 November →