Skip to content

Commit

Permalink
fix: session UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
kobenguyent committed Sep 16, 2024
1 parent 12f3139 commit a10e23f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
16 changes: 8 additions & 8 deletions test/acceptance/session_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ Scenario('simple session @WebDriverIO @Puppeteer @Playwright', ({ I }) => {

Scenario('screenshots reflect the current page of current session @Puppeteer @Playwright @WebDriver', async ({ I }) => {
I.amOnPage('/')
I.saveScreenshot('session_default_1.png')
I.saveScreenshot('default_session_1.png')

session('john', () => {
I.amOnPage('/info')
I.saveScreenshot('session_john_1.png')
I.saveScreenshot('john_session_1.png')
})

I.saveScreenshot('session_default_2.png')
I.saveScreenshot('default_session_2.png')

session('john', () => {
I.saveScreenshot('session_john_2.png')
I.saveScreenshot('john_session_2.png')
})

const [default1Digest, default2Digest, john1Digest, john2Digest] = await I.getSHA256Digests([
`${output_dir}/session_default_1.png`,
`${output_dir}/session_default_2.png`,
`${output_dir}/john_session_john_1.png`,
`${output_dir}/session_john_2.png`,
`${output_dir}/default_session_1.png`,
`${output_dir}/default_session_2.png`,
`${output_dir}/john_john_session_1.png`,
`${output_dir}/john_session_2.png`,
])

// Assert that screenshots of same page in same session are equal
Expand Down
24 changes: 16 additions & 8 deletions test/support/ScreenshotSessionHelper.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
const Helper = codecept_helper

const crypto = require('crypto')
const fs = require('fs')
const fs = require('fs').promises

class ScreenshotSessionHelper extends Helper {
constructor(config) {
super(config)
this.outputPath = output_dir
}

getSHA256Digests(files = []) {
const digests = []
async getSHA256Digests(files = []) {
if (!Array.isArray(files)) {
throw new TypeError('Expected an array of file paths');
}

for (const file of files) {
const hash = crypto.createHash('sha256')
const data = fs.readFileSync(file)
hash.update(data)
const digests = [];

digests.push(hash.digest('base64'))
for (const file of files) {
try {
const data = await fs.readFile(file)
const hash = crypto.createHash('sha256')
hash.update(data)
digests.push(hash.digest('base64'))
} catch (error) {
console.error(`Error processing file ${file}:`, error.message)
digests.push(null) // Add null or handle it as you need for failed files
}
}

return digests
Expand Down

0 comments on commit a10e23f

Please sign in to comment.