Example #1
0
test('screenshot by selector', async t => {
  const version = await CDP.Version()
  const versionMajor = parseInt(/Chrome\/(\d+)/.exec(version['User-Agent'])[1])
  // clipping will only work on chrome 61+

  const chromeless = new Chromeless({ launchChrome: false })
  const screenshot = await chromeless.goto(testUrl).screenshot('img')

  await chromeless.end()

  const png = await getPngMetaData(screenshot)
  t.is(png.width, versionMajor > 60 ? 512 : 1440)
  t.is(png.height, versionMajor > 60 ? 512 : 900)
})
Example #2
0
  private async setViewport(client: Client) {
    const { viewport = {} } = this.options

    const config: any = {
      deviceScaleFactor: 1,
      mobile: false,
      scale: viewport.scale || 1,
      fitWindow: false, // as we cannot resize the window, `fitWindow: false` is needed in order for the viewport to be resizable
    }

    const { host, port } = this.options.cdp
    const versionResult = await CDP.Version({ host, port })
    const isHeadless = versionResult['User-Agent'].includes('Headless')

    if (viewport.height && viewport.width) {
      config.height = viewport.height
      config.width = viewport.width
    } else if (isHeadless) {
      // just apply default value in headless mode to maintain original browser viewport
      config.height = 900
      config.width = 1440
    } else {
      config.height = await evaluate(
        client,
        (() => window.innerHeight).toString(),
      )
      config.width = await evaluate(
        client,
        (() => window.innerWidth).toString(),
      )
    }

    await client.Emulation.setDeviceMetricsOverride(config)
    await client.Emulation.setVisibleSize({
      width: config.width,
      height: config.height,
    })
  }