-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from ergon/custom-pixelpact-hook
Pixelpact Playwright client
- Loading branch information
Showing
34 changed files
with
177 additions
and
1,067 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules/ | ||
.idea | ||
pixelpact/coverage | ||
pixelpact/coverage | ||
.direnv/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@pixelpact/playwright", | ||
"version": "1.0.0", | ||
"description": "Pixelpact Playwright helps to easily hook into Pixelpact Service", | ||
"type": "module", | ||
"author": "Ergon Informatik AG", | ||
"license": "MIT", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { existsSync, mkdirSync } from "fs"; | ||
import fs from "fs/promises"; | ||
|
||
const appDir = process.env.PWD; | ||
const MODE = process.env.PIXELPACT_MODE ?? "verify"; | ||
const SERVER_URL = process.env.PIXELPACT_SERVER_URL ?? "http://localhost:8888"; | ||
const FOLDER_PATH = process.env.PIXELPACT_FOLDER_PATH; | ||
const folderPath = getFolderPath(); | ||
|
||
const expectedFileSuffix = "expected"; | ||
const actualFileSuffix = "actual"; | ||
const diffFileSuffix = "diff"; | ||
|
||
export async function toMatchVisually(page, testInfo, fileNamePrefix) { | ||
if (!existsSync(folderPath)) { | ||
mkdirSync(folderPath); | ||
} | ||
|
||
const session = await page.context().newCDPSession(page); | ||
const mhtml = ( | ||
await session.send("Page.captureSnapshot", { format: "mhtml" }) | ||
).data; | ||
|
||
if (MODE === "record") { | ||
await recordReferenceImage(mhtml, page, fileNamePrefix); | ||
} else if (MODE === "verify") { | ||
await verfiy(page, testInfo, fileNamePrefix, mhtml); | ||
} else { | ||
throw Error("Unknown Mode!"); | ||
} | ||
} | ||
|
||
async function recordReferenceImage(mHtml, page, fileNamePrefix) { | ||
const referenceFileName = composeFileName(fileNamePrefix, "expected"); | ||
const referenceFilePath = folderPath + referenceFileName; | ||
const body = { | ||
actualHtml: mHtml, | ||
viewport: page.viewportSize(), | ||
}; | ||
|
||
const response = await fetch(`${SERVER_URL}/render`, { | ||
method: "post", | ||
body: JSON.stringify(body), | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
|
||
const result = await response.json(); | ||
const referenceImage = Buffer.from(result.actual, "base64"); | ||
await fs.writeFile(referenceFilePath, referenceImage); | ||
} | ||
|
||
async function verfiy(page, testInfo, fileNamePrefix, mhtml) { | ||
const referenceImage = await readReferenceImage(fileNamePrefix); | ||
const body = { | ||
actualHtml: mhtml, | ||
expected: referenceImage.toString("base64"), | ||
viewport: page.viewportSize(), | ||
}; | ||
|
||
const response = await fetch(SERVER_URL + "/check", { | ||
method: "post", | ||
body: JSON.stringify(body), | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
const result = await response.json(); | ||
|
||
attachExpected(testInfo, fileNamePrefix); | ||
await saveResult(result.actual, testInfo, fileNamePrefix, actualFileSuffix); | ||
await saveResult(result.diff, testInfo, fileNamePrefix, diffFileSuffix); | ||
|
||
if (result.numDiffPixels !== 0) { | ||
throw Error( | ||
"Actual Image does not match reference image! Pixeldiff: " + | ||
result.numDiffPixels | ||
); | ||
} | ||
} | ||
|
||
async function saveResult(fileStr, testInfo, fileNamePrefix, fileNameSuffix) { | ||
const fileName = composeFileName(fileNamePrefix, fileNameSuffix); | ||
const filePath = folderPath + fileName; | ||
await fs.writeFile(filePath, Buffer.from(fileStr, "base64")); | ||
attachToTestInfo(testInfo, fileName, filePath); | ||
} | ||
|
||
function attachExpected(testInfo, fileNamePrefix) { | ||
const fileName = composeFileName(fileNamePrefix, expectedFileSuffix); | ||
const filePath = folderPath + fileName; | ||
attachToTestInfo(testInfo, fileName, filePath); | ||
} | ||
|
||
function attachToTestInfo(testInfo, fileName, filePath) { | ||
testInfo.attachments.push({ | ||
name: fileName, | ||
contentType: "image/png", | ||
path: filePath, | ||
}); | ||
} | ||
|
||
async function readReferenceImage(fileNamePrefix) { | ||
const referenceFileName = composeFileName(fileNamePrefix, expectedFileSuffix); | ||
const referenceFilePath = folderPath + referenceFileName; | ||
return await fs.readFile(referenceFilePath); | ||
} | ||
|
||
function getFolderPath() { | ||
if (FOLDER_PATH) { | ||
return FOLDER_PATH.endsWith("/") ? FOLDER_PATH : `${FOLDER_PATH}/`; | ||
} | ||
return `${appDir}/pixelpact/`; | ||
} | ||
|
||
function composeFileName(fileNamePrefix, suffix) { | ||
return `${fileNamePrefix}-${suffix}.png`; | ||
} |
Empty file.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.