Skip to content

Instantly share code, notes, and snippets.

@checklyalex
Created March 24, 2023 15:11
Show Gist options
  • Save checklyalex/c125d1499a3413ea029b75fc062a1be5 to your computer and use it in GitHub Desktop.
Save checklyalex/c125d1499a3413ea029b75fc062a1be5 to your computer and use it in GitHub Desktop.
A Checkly Playwright/Test script to upload a screenshot to S3.
const { expect, test } = require('@playwright/test')
const aws4 = require('aws4')
const axios = require('axios').default
const S3_BUCKET_NAME = 'xxx';
const S3_OBJECT_KEY = '/image.jpg'; // The name you want to give to the uploaded image file
const AWS_REGION = 'eu-west-2'
test('visit page and take screenshot', async ({ page }) => {
const response = await page.goto(process.env.ENVIRONMENT_URL || 'https://checklyhq.com')
const screenshot = await page.screenshot({ path: 'screenshot.jpg' })
const opts = aws4.sign({
host: `${S3_BUCKET_NAME}.s3.${AWS_REGION}.amazonaws.com`,
method: 'PUT',
path: S3_OBJECT_KEY,
body: screenshot,
headers: {
'Content-Type': 'image/jpeg',
'X-Amz-Content-Sha256': 'UNSIGNED-PAYLOAD'
},
extraHeadersToIgnore: {
'content-length': true
}
});
async function request(opts) {
await axios({
method: opts.method || 'GET',
url: `https://${opts.host}${opts.path}`,
headers: opts.headers || {},
data: opts.body || '',
});
}
await request(opts);
})
@checklyalex
Copy link
Author

checklyalex commented Mar 24, 2023

You will need to add your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to your Checkly environment variables.
Please amend the S3_BUCKET_NAME, S3_OBJECT_KEY and AWS_REGION as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment