1
0
mirror of https://git.sr.ht/~cadence/bibliogram synced 2024-11-22 08:07:30 +00:00

Add screenshot tests for user, post, and gallery

This commit is contained in:
Cadence Ember 2020-07-20 19:15:58 +12:00
parent f25b2891ac
commit 5baae5167d
No known key found for this signature in database
GPG Key ID: 128B99B1B74A6412
4 changed files with 62 additions and 44 deletions

View File

@ -8,7 +8,9 @@
"start": "cd src/site && node server.js", "start": "cd src/site && node server.js",
"assistant": "cd src/site && node assistant.js", "assistant": "cd src/site && node assistant.js",
"build-lang": "cd src/lang/utils && node build_base.js", "build-lang": "cd src/lang/utils && node build_base.js",
"test": "tap" "test": "tap",
"upload-test-files": "rsync -a src/lib/utils/saved_requests/files/ debib:meta/saved_requests; rsync -a test/screenshots/final/ debib:meta/screenshots",
"download-test-files": "bash -c 'mkdir -p test/screenshots/{diff,final,staging} && rm test/screenshots/final/* && wget -q -r -nd -np -P test/screenshots/final -R html -R tmp https://meta.bibliogram.art/screenshots/'"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",

View File

@ -60,7 +60,7 @@ class SavedRequestManager {
const base = pj(folder, row.path) const base = pj(folder, row.path)
try { try {
fs.accessSync(base) fs.accessSync(base)
console.log("Found, using saved request for "+row.path) // console.log("Found, using saved request for "+row.path)
return new Saved(base) return new Saved(base)
} catch (e) { } catch (e) {
console.log("Known, but not downloaded for "+row.path) console.log("Known, but not downloaded for "+row.path)
@ -85,7 +85,7 @@ class SavedRequestManager {
} }
} else { } else {
const name = generateName() const name = generateName()
console.log("Not found, saving now as "+name) console.log(`Not found, saving now:\n ${this.url}\n -> ${name}`)
const internalRequest = new NodeFetch(this.url) const internalRequest = new NodeFetch(this.url)
return new DelayedBackend( return new DelayedBackend(

View File

@ -1,5 +1,6 @@
const tap = require("tap") const tap = require("tap")
const se = require("selenium-webdriver") const se = require("selenium-webdriver")
const firefox = require("selenium-webdriver/firefox")
const fs = require("fs").promises const fs = require("fs").promises
const Jimp = require("jimp") const Jimp = require("jimp")
const commands = require("./screenshots/commands") const commands = require("./screenshots/commands")
@ -24,6 +25,9 @@ const dimensions = new Map([
const browserDimensions = dimensions.get(browser) const browserDimensions = dimensions.get(browser)
const constants = require("../src/lib/constants")
constants.request_backend = "saved" // predictable request results
process.chdir("src/site") process.chdir("src/site")
const server = require("../src/site/server") const server = require("../src/site/server")
@ -39,8 +43,10 @@ function exec(command) {
await fs.mkdir("../../test/screenshots/diff", {recursive: true}) await fs.mkdir("../../test/screenshots/diff", {recursive: true})
await fs.mkdir("../../test/screenshots/staging", {recursive: true}) await fs.mkdir("../../test/screenshots/staging", {recursive: true})
const options = new firefox.Options()
// options.addArguments("-headless")
const [driver] = await Promise.all([ const [driver] = await Promise.all([
new se.Builder().forBrowser(browser).build(), new se.Builder().forBrowser(browser).setFirefoxOptions(options).build(),
server.waitForFirstCompile() server.waitForFirstCompile()
]) ])
@ -56,10 +62,6 @@ function exec(command) {
await driver.get(origin) await driver.get(origin)
for (const command of commands) { for (const command of commands) {
function screenPath(dir) {
return `../../test/screenshots/${dir}/${command.filename}.png`
}
await Promise.all(Object.keys(command.cookies).map(cookieName => await Promise.all(Object.keys(command.cookies).map(cookieName =>
driver.manage().addCookie({ driver.manage().addCookie({
name: cookieName, name: cookieName,
@ -67,23 +69,31 @@ function exec(command) {
}) })
)) ))
await Promise.all([ await setSize(command.size) // complete this before driver.get so that srcset doesn't get confused
setSize(command.size), await driver.get(origin + command.url)
driver.get(origin + command.url)
]) for (let scrollNumber = 1; scrollNumber <= command.scrolls; scrollNumber++) {
const filenameWithScroll = `${command.filename}-${scrollNumber}`
function screenPath(dir) {
return `../../test/screenshots/${dir}/${filenameWithScroll}.png`
}
if (scrollNumber > 1) {
await driver.executeScript(`window.scrollByPages(1)`)
}
const finalExists = await fs.access(screenPath("final")).then(() => true).catch(() => false) const finalExists = await fs.access(screenPath("final")).then(() => true).catch(() => false)
const screenshot = Buffer.from(await driver.takeScreenshot(), "base64") const screenshot = Buffer.from(await driver.takeScreenshot(), "base64")
const message = `equal screens: ${command.filename}` const message = `equal screens: ${filenameWithScroll}`
tap.test(message, async childTest => { tap.test(message, async childTest => {
const image = await Jimp.read(screenshot) const image = await Jimp.read(screenshot)
image.crop(0, 0, command.size.width, command.size.height) // crop out page scrollbar image.crop(0, 0, command.size.width, command.size.height) // crop out page scrollbar
if (finalExists) { if (finalExists) {
await image.writeAsync(screenPath("staging")) await image.writeAsync(screenPath("staging"))
const message = `screen: ${command.filename}` const message = `screen: ${filenameWithScroll}`
const result = await exec(`compare -metric AE ${screenPath("staging")} ${screenPath("final")} ${screenPath("diff")}`) const result = await exec(`compare -metric AE ${screenPath("staging")} ${screenPath("final")} ${screenPath("diff")}`)
const diff = +result.stderr const diff = +result.stderr
childTest.ok(diff === 0, message) childTest.ok(diff === 0, message)
@ -93,10 +103,11 @@ function exec(command) {
} }
} else { } else {
image.writeAsync(screenPath("final")) image.writeAsync(screenPath("final"))
console.log(`note: creating new screenshot ${command.filename}`) console.log(`note: creating new screenshot ${filenameWithScroll}`)
} }
}) })
} }
}
tap.teardown(() => { tap.teardown(() => {
driver.close() driver.close()

View File

@ -18,7 +18,7 @@ const cookies = {
} }
} }
function generateSetup(pageName, url, sizeName, cookiesName, scrolls) { function generateSetup(pageName, url, sizeName, cookiesName, scrolls = 1) {
return { return {
url: url, url: url,
filename: `${pageName}-${sizeName}-${cookiesName}`, filename: `${pageName}-${sizeName}-${cookiesName}`,
@ -28,13 +28,18 @@ function generateSetup(pageName, url, sizeName, cookiesName, scrolls) {
} }
} }
module.exports = [ function generateAllSetups(pageName, url, scrolls) {
generateSetup("home", "/", "laptop", "default"), return Object.keys(cookies).map(cookieKey =>
generateSetup("settings", "/settings", "laptop", "default"), Object.keys(sizes).map(sizeKey =>
generateSetup("home", "/", "phone", "default"), generateSetup(pageName, url, sizeKey, cookieKey, scrolls)
generateSetup("settings", "/settings", "phone", "default"), )
).flat()
}
generateSetup("home", "/", "laptop", "ptc"), module.exports = [
generateSetup("settings", "/settings", "laptop", "ptc"), ...generateAllSetups("home", "/"),
generateSetup("home", "/", "phone", "ptc"), ...generateAllSetups("settings", "/settings"),
...generateAllSetups("anti__reality", "/u/anti__reality?page=3", 2),
...generateAllSetups("post", "/p/CCyko7oJ-ta"),
...generateAllSetups("gallery", "/p/CCbVsCMpizf", 2)
] ]