question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature] `addInitScript` context option in `playwright.config.ts`

See original GitHub issue

i’m trying to work around https://github.com/microsoft/playwright/issues/7008#issuecomment-927175899 as that solution no longer works, so i’m trying an init script instead:

test('test', async ({ page, context }) => {
    await context.addInitScript(() => window._playwrightResume())
    await page.goto('https://google.com/')
})

this doesn’t work, i assume because the page was created before the context was given the init script.

this works however, but it’s not ideal because it creates a new Page when @playwright/test has already created one

test('test', async ({ context }) => {
    await context.addInitScript(() => window._playwrightResume())
    const page = await context.newPage()
    await page.goto('https://google.com/')
})

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
aslushnikovcommented, Jan 27, 2022

Nah, so I mean something like this:

// example.spec.ts
import { test as base, expect } from '@playwright/test';

const test = base.extend({
  // override `context` fixture to add init script
  context: async ({ context }, use) => {
    await context.addInitScript(() => window._playwrightResume())
    await use(context);
  },
});

test('my test', async ({ page }) => {
  // `page` will be created off the context, so it'll be initialized
  await page.goto('https://example.com');
});
1reaction
aslushnikovcommented, Jan 24, 2022

@DetachHead you can always override default page fixture with your implementation. Would it work?

Read more comments on GitHub >

github_iconTop Results From Across the Web

BrowserContext
Playwright allows creating "incognito" browser contexts with browser. ... The method adds a function called name on the window object of every frame...
Read more >
Advanced: configuration | Playwright
To make use of this feature, we will declare an "option fixture" for the backend version, and use it in the tests. TypeScript;...
Read more >
Use addInitScript in Playwright Internal With Examples
Learn how to use addInitScript function in Playwright Internal framework for your next JavaScript automation project with LambdaTest Automation Testing ...
Read more >
Playwright before each for all spec files
First I have this in playwright/src/index.ts where I setup all the fixtures for ... addInitScript( { content } ); } await use( context...
Read more >
playwright - test: cleanup proxy and context tests (#6085) - Gitnet
config /playwrightTest'; ... tests/browsercontext-add-init-script.spec.ts ... it('should use proxy', async ({ contextFactory, contextOptions, server }) => {.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found