microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
66.99k stars 3.67k forks source link

[Question] Regarding CDP Session. #2484

Closed shirshak55 closed 4 years ago

shirshak55 commented 4 years ago

I am converting all my puppeteer code which used to look like this.

https://github.com/shirshak55/scrapper-tools/blob/master/src/fastPage/index.ts#L113

const session = await page.target().createCDPSession()
  await session.send("Page.enable")
  await session.send("Page.setWebLifecycleState", {
    state: "active",
  })

So we went to docs and saw this

const client = await page.context().newCDPSession(page);

But now i am seeing error on typescript like this

Property newCDPSession doesn't exists on type BrowserContext

pavelfeldman commented 4 years ago

^^ @JoelEinbinder a ts types bug?

JoelEinbinder commented 4 years ago

page.context() needs to be casted to ChromiumBrowserContext in order to use CDPSession

shirshak55 commented 4 years ago

@JoelEinbinder yep thanks I wrote

const session = await (page.context() as ChromiumBrowserContext).newCDPSession(page)

and it works but can you let me know why it requires page argument when it is already given page.context() ? I am asking because puppeteer doesn't use to require page argument? I think new syntax is not that ergonomic tbh.

Thanks :)

Niek commented 4 years ago

The page parameter is strange indeed, as is the requirement to cast to ChromiumBrowserContext. Since all 3 supported browsers use CDP, it's unclear to me why the newCDPSession() method is not available in e.g FFBrowserContext.

Edit: I stand corrected, this is not true. See https://github.com/microsoft/playwright/issues/2484#issuecomment-652828709

shirshak55 commented 4 years ago

@Niek Yea casting and additional page argument is not ergonomic . And the api to launch persistent and non persistent is so different i was so shocked :(

See this https://github.com/shirshak55/scrapper-tools/blob/5c216cd7b0894f16f2d07128b20812523e3afb1a/src/fastPage/index.ts#L137

aslushnikov commented 4 years ago

Since all 3 supported browsers use CDP

@Niek this is not actually the case.

Niek commented 4 years ago
  • Juggler

Since all 3 supported browsers use CDP

@Niek this is not actually the case.

  • In Chromium we rely on CDP
  • In WebKit we rely on "Web Inspector Remote Debugging Protocol"
  • In Firefox we rely on "Juggler" protocol

Apologies @aslushnikov, I assumed Playwright (just like Puppeteer) used CDP for FF now that it's built-in (and enabled in at least the nightly builds). But reading https://github.com/microsoft/playwright/issues/1765#issuecomment-613178363 that's not the case until the CDP support in FF is more mature.