Created
March 24, 2023 15:11
-
-
Save observethenoyes/c125d1499a3413ea029b75fc062a1be5 to your computer and use it in GitHub Desktop.
A Checkly Playwright/Test script to upload a screenshot to S3.
This file contains hidden or 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
| 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); | |
| }) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You will need to add your
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEYto your Checkly environment variables.Please amend the
S3_BUCKET_NAME,S3_OBJECT_KEYandAWS_REGIONas needed.