Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for playwright #16

Open
tws2000 opened this issue Apr 2, 2020 · 2 comments
Open

Support for playwright #16

tws2000 opened this issue Apr 2, 2020 · 2 comments

Comments

@tws2000
Copy link

tws2000 commented Apr 2, 2020

What do you think about adding support for playwright?

Maybe just another option to start(). Like start({path:.., playwright: true})

Then:

if (playwright) {
 this.client = await this.page.context().newCDPSession(this.page);
} else {
 this.client = await this.page.target().createCDPSession();
}
@maciejmaciejewski
Copy link

Hey,
I have commited #17 but it seems that microsoft has changed chrome browser API and it is not compatible with the new version

@maciejmaciejewski
Copy link

Hello again @tws2000, I managed to make it work without puppeteer-har but with chrome-har
Here is a code snippet

 if (this.browserType === 'chromium') {
    const client = await context.newCDPSession(page)

    await client.send('Page.enable')
    await client.send('Network.enable')

    this.addResponseBodyPromises = []
    this.events = []
    const observe = [
      'Page.loadEventFired',
      'Page.domContentEventFired',
      'Page.frameStartedLoading',
      'Page.frameAttached',
      'Page.frameScheduledNavigation',
      'Network.requestWillBeSent',
      'Network.requestServedFromCache',
      'Network.dataReceived',
      'Network.responseReceived',
      'Network.resourceChangedPriority',
      'Network.loadingFinished',
      'Network.loadingFailed',
      'Network.getResponseBody'
    ]

    observe.forEach(method => {
      client.on(method, params => {
        const harEvent = { method, params }
        this.events.push(harEvent)
        if (method === 'Network.responseReceived') {
          const response = harEvent.params.response
          const requestId = harEvent.params.requestId
          // Response body is unavailable for redirects, no-content, image, audio and video responses
          if (response.status !== 204 &&
            response.headers.location == null &&
            !response.mimeType.includes('image') &&
            !response.mimeType.includes('audio') &&
            !response.mimeType.includes('video')
          ) {
            const addResponseBodyPromise = client.send('Network.getResponseBody', { requestId }).then((responseBody) => {
              // Set the response so chrome-har can add it to the HAR file
              harEvent.params.response = {
                ...response,
                body: Buffer.from(responseBody.body, responseBody.base64Encoded ? 'base64' : undefined).toString()
              }
            }, (reason) => { })
            this.addResponseBodyPromises.push(addResponseBodyPromise)
          }
        }
      })
    })
  }

and then at the end of your test


  if (this.browserType === 'chromium') {
    await Promise.all(this.addResponseBodyPromises)
    const { harFromMessages } = require('chrome-har')
    const { writeFileSync } = require('fs')
    const harPath = `./tracing/${process.env.ENVIRONMENT}-${dashify(scenario.pickle.name)}.har`
    const harObject = harFromMessages(this.events, { includeTextFromResponseBody: true })
    writeFileSync(harPath, JSON.stringify(harObject))
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants