-
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.
- Loading branch information
Mike Loydd
committed
Jun 29, 2023
1 parent
1871b63
commit c40c079
Showing
2 changed files
with
93 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { readFileSync, existsSync } from "fs"; | ||
import { dirname, resolve } from "path"; | ||
|
||
const appDir = process.env.PWD; | ||
const data = readFileSync(`${appDir}/pixelpact.config.json`, { | ||
encoding: "utf8", | ||
flag: "r", | ||
}); | ||
|
||
const mimeType = "image/png"; | ||
const fallbackFolderPath = `${appDir}/pixelpact/`; | ||
|
||
export async function toMatchVisually(page, testInfo, fileNamePrefix) { | ||
const serverUrl = data.serverUrl; | ||
const folderPath = getFolderPath(); | ||
|
||
if (!existsSync(folderPath)) { | ||
mkdirSync(folderPath); | ||
} | ||
|
||
const session = await page.context().newCDPSession(page); | ||
const mhtml = await session.send("Page.captureSnapshot", { format: "mhtml" }); | ||
|
||
const body = { | ||
actualHtml: mhtml, | ||
expected: referenceImage.toString("base64"), | ||
viewport: { width: 1920, height: 1024 }, | ||
context: context.toString("base64"), | ||
}; | ||
|
||
const response = await fetch(serverUrl, { | ||
method: "post", | ||
body: JSON.stringify(body), | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
const result = await response.json(); | ||
|
||
const expectedFileName = composeFileName(fileNamePrefix, "expected"); | ||
const expectedFilePath = folderPath + actualFileName; | ||
await fs.writeFile(expectedFilePath, Buffer.from(result.expected, "base64")); | ||
testInfo.attachments.push({ | ||
name: expectedFileName, | ||
contentType: mimeType, | ||
path: expectedFileName, | ||
}); | ||
|
||
const actualFileName = composeFileName(fileNamePrefix, "actual"); | ||
const actualFilePath = folderPath + actualFileName; | ||
await fs.writeFile(actualFilePath, Buffer.from(result.actual, "base64")); | ||
testInfo.attachments.push({ | ||
name: actualFileName, | ||
contentType: mimeType, | ||
path: actualFilePath, | ||
}); | ||
|
||
const diffFileName = composeFileName(fileNamePrefix, "diff"); | ||
const diffFilePath = folderPath + diffFileName; | ||
await fs.writeFile(diffFilePath, Buffer.from(result.diff, "base64")); | ||
testInfo.attachments.push({ | ||
name: diffFileName, | ||
contentType: mimeType, | ||
path: diffFilePath, | ||
}); | ||
} | ||
|
||
function getFolderPath() { | ||
if (data.folderPath) { | ||
return data.folderPath.endsWith("/") | ||
? data.folderPath | ||
: `${data.folderPath}/`; | ||
} | ||
return fallbackFolderPath; | ||
} | ||
|
||
function composeFileName(fileNamePrefix, suffix) { | ||
return `${fileNamePrefix}_${suffix}.png`; | ||
} |
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,16 @@ | ||
{ | ||
"name": "@pixelpact-hook/playwright-js", | ||
"version": "1.0.0", | ||
"description": "Pixelpact Playwright-JS helps to easily hook into Pixelpact Service", | ||
"type": "module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "pixelpact" | ||
}, | ||
"author": "", | ||
"license": "ISC" | ||
} |