Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
fix: visual regressions
Browse files Browse the repository at this point in the history
  • Loading branch information
CanRau committed Oct 6, 2018
1 parent b0a98ae commit 8829a0a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 22 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 62 additions & 22 deletions src/__tests__/e2e/visual-regressions.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
/**
* lazy loading tricks from
* https://www.screenshotbin.com/blog/handling-lazy-loaded-webpages-puppeteer
*/
const puppeteer = require(`puppeteer`)
const { toMatchImageSnapshot } = require(`jest-image-snapshot`)
expect.extend({ toMatchImageSnapshot })

jest.setTimeout(15000)

function wait(ms) {
return new Promise(resolve => setTimeout(() => resolve(), ms))
}

const [width, height] = [1240, 530]
let page
let browser

const testIf = process.env.MODE && process.env.MODE === `skipsnapshots`
? test.skip
: process.env.SKIP && process.env.SKIP === `e2e`
const testIf =
process.env.MODE && process.env.MODE === `skipsnapshots`
? test.skip
: test
: process.env.SKIP && process.env.SKIP === `e2e`
? test.skip
: test

beforeAll(async () => {
browser = await puppeteer.launch()
browser = await puppeteer.launch({
args: [`--window-size=${width},${height}`],
})
page = await browser.newPage()
await page.setViewport({ width, height })
// await page.setViewport({ width, height })
// await page.goto(`http://localhost:8000`)
})
afterAll(() => {
browser.close()
})

// const URL = `http://localhost:8000/`
const URL = `http://localhost:3333/`
const URL = `http://localhost:8000/en/`

const pagesToTest = [
{
Expand All @@ -43,10 +55,10 @@ const pagesToTest = [
title: `Blog`,
url: `${URL}blog`,
},
{
title: `Gallery`,
url: `${URL}gallery`,
},
// {
// title: `Gallery`,
// url: `${URL}gallery`,
// },
{
title: `Contact`,
url: `${URL}contact`,
Expand All @@ -55,15 +67,43 @@ const pagesToTest = [

describe(`Visual regressions`, () => {
pagesToTest.forEach(x =>
testIf(x.title, async done => {
await page.goto(x.url)
await page.evaluate(() => {
/* eslint-disable-next-line no-undef */
window.scrollBy(0, window.innerHeight)
})
const screenshot = await page.screenshot({ fullPage: true })
expect(screenshot).toMatchImageSnapshot()
done()
}, 20000)
testIf(
x.title,
async done => {
await page.goto(x.url)
// await page.evaluate(() => {
// /* eslint-disable-next-line no-undef */
// window.scrollBy(0, window.innerHeight)
// })
// Get the height of the rendered page
const bodyHandle = await page.$(`body`)
const { height } = await bodyHandle.boundingBox()
await bodyHandle.dispose()

// Scroll one viewport at a time, pausing to let content load
const viewportHeight = page.viewport().height
let viewportIncr = 0
while (viewportIncr + viewportHeight < height) {
await page.evaluate(_viewportHeight => {
window.scrollBy(0, _viewportHeight) // eslint-disable-line
}, viewportHeight)
await wait(20)
viewportIncr = viewportIncr + viewportHeight
}

// Scroll back to top
await page.evaluate(_ => {
window.scrollTo(0, 0) // eslint-disable-line
})

// Some extra delay to let images load
await wait(100)

const screenshot = await page.screenshot({ fullPage: true })
expect(screenshot).toMatchImageSnapshot()
done()
},
20000
)
)
})
})

0 comments on commit 8829a0a

Please sign in to comment.