'use strict'
const { getBrowserContext, runServer } = require('@browserless/test')
const sharp = require('sharp')
const { default: test } = require('ava')
const createScreenshot = require('..')
const TIMEOUT = 33000
const STEP_BUDGET = TIMEOUT / 11
const noisePng = () =>
sharp({
create: {
width: 200,
height: 80,
channels: 3,
noise: { type: 'gaussian', mean: 128, sigma: 60 }
}
})
.png()
.toBuffer()
// 24px stays under the 32px pending threshold, so only decode() can hold the shot.
const page = (markup, add) => `
Ready
${markup}
`
const hidden = page(
`
`,
`add(document.getElementById('menu'), '')
add(document.body, 'style="display:block;width:0;height:24px"')
add(document.body, 'style="display:block;width:24px;height:0"')
add(document.body, 'width="24" height="24" style="display:block;visibility:hidden"')
add(document.body, 'width="24" height="24" style="display:block;content-visibility:hidden"')
add(document.body, 'width="200" height="80" style="display:block;visibility:hidden"')
add(document.body, 'width="200" height="80" style="display:block;content-visibility:hidden"')
add(document.body, 'src="/quiet.png" width="200" height="80" style="display:block;content-visibility:hidden"')
add(document.getElementById('slide'), 'width="24" height="24" style="display:block"')
add(document.getElementById('fold'), 'width="24" height="24" style="display:block"')`
)
const shown = page(
'',
'add(document.getElementById(\'shown\'), \'width="24" height="24" style="display:block"\')'
)
const faded = page(
'',
'add(document.getElementById(\'shown\'), \'width="24" height="24" style="display:block;opacity:0"\')'
)
const fadedEdge = page(
'',
'add(document.body, \'style="display:block;opacity:0;position:fixed;left:0;bottom:-100px;width:400px;height:200px"\')'
)
const noVisibilityApi = page(
'',
'add(document.getElementById(\'shown\'), \'width="24" height="24" style="display:block"\')'
)
const noVisibilityApiHidden = page(
'',
'add(document.body, \'width="200" height="80" style="display:block;content-visibility:hidden"\')'
)
const dropCheckVisibility = async page => {
const client = page.mainFrame().client
const names = []
const onContext = event => {
if (event.context.name) names.push(event.context.name)
}
client.on('Runtime.executionContextCreated', onContext)
await client.send('Runtime.enable')
await page.goto('about:blank')
await page
.mainFrame()
.isolatedRealm()
.evaluate(() => 1)
client.off('Runtime.executionContextCreated', onContext)
const worldName = names.find(name => name.startsWith('__puppeteer_utility_world__'))
if (!worldName) throw new Error('missing puppeteer isolated world')
await client.send('Page.addScriptToEvaluateOnNewDocument', {
source: 'delete Element.prototype.checkVisibility',
worldName
})
}
const capture = async (t, html, { hideCheckVisibility = false } = {}) => {
const stalled = await noisePng()
const url = await runServer(t, ({ req, res }) => {
if (req.url === '/stalled.png' || req.url === '/quiet.png') {
res.setHeader('content-type', 'image/png')
res.setHeader('content-length', String(stalled.length))
if (req.url === '/quiet.png') return
res.write(stalled.subarray(0, Math.floor(stalled.length / 2)))
return
}
res.setHeader('content-type', 'text/html')
res.end(html)
})
const browserless = await getBrowserContext(t)
const started = Date.now()
const png = await browserless.withPage((browserPage, goto) => async () => {
if (hideCheckVisibility) await dropCheckVisibility(browserPage)
return createScreenshot({ goto })(browserPage)(url, { adblock: false, timeout: TIMEOUT })
})()
return { png, duration: Date.now() - started }
}
test('an image that draws nothing and never finishes loading does not hold the capture', async t => {
const { png, duration } = await capture(t, hidden)
t.true(png.length > 0)
t.true(duration < STEP_BUDGET, `took ${duration}ms, the step budget is ${STEP_BUDGET}ms`)
})
test('a rendered image that never finishes loading is still decoded', async t => {
const { png, duration } = await capture(t, shown)
t.true(png.length > 0)
t.true(
duration >= STEP_BUDGET,
`took ${duration}ms, decode should spend the step budget of ${STEP_BUDGET}ms`
)
})
test('an opacity-zero image that never finishes loading is still decoded', async t => {
const { png, duration } = await capture(t, faded)
t.true(png.length > 0)
t.true(
duration >= STEP_BUDGET,
`took ${duration}ms, a fade-in should spend the step budget of ${STEP_BUDGET}ms`
)
})
test('a page without checkVisibility still waits on a rendered image', async t => {
const { png, duration } = await capture(t, noVisibilityApi, { hideCheckVisibility: true })
t.true(png.length > 0)
t.true(
duration >= STEP_BUDGET,
`took ${duration}ms, a rendered image should spend the step budget of ${STEP_BUDGET}ms`
)
})
test('a page without checkVisibility still skips an image that draws nothing', async t => {
const { png, duration } = await capture(t, noVisibilityApiHidden, { hideCheckVisibility: true })
t.true(png.length > 0)
t.true(duration < STEP_BUDGET, `took ${duration}ms, the step budget is ${STEP_BUDGET}ms`)
})
test('an opacity-zero image across the viewport edge keeps the settle scan waiting', async t => {
const { png, duration } = await capture(t, fadedEdge)
t.true(png.length > 0)
t.true(
duration >= STEP_BUDGET,
`took ${duration}ms, settle should spend the step budget of ${STEP_BUDGET}ms`
)
})