Skip to content

Commit d68020b

Browse files
feat(synthetics): node playwright 1.0 and python selenium 4.1 runtime (#32245)
### Issue # (if applicable) None ### Reason for this change AWS Synthetics begins supporting the NodeJS Playwright runtime. https://aws.amazon.com/about-aws/whats-new/2024/11/amazon-cloudwatch-synthetics-playwright-runtime-canaries-nodejs/ And Python Selenium runtime v4.1 is also released. https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Synthetics_Library_python_selenium.html#CloudWatch_Synthetics_runtimeversion-syn-python-selenium-4.1 ### Description of changes Add two runtimes to `Runtime` class - SYNTHETICS_PYTHON_SELENIUM_4_1 - SYNTHETICS_NODEJS_PLAYWRIGHT_1_0 ### Description of how you validated changes Execute describe-runtime AWS CLI. ```sh aws synthetics describe-runtime-versions --region us-east-1 | grep VersionName "VersionName": "syn-python-selenium-4.1", ..., "VersionName": "syn-nodejs-playwright-1.0", ... ``` ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1b3c22d commit d68020b

File tree

38 files changed

+27485
-21943
lines changed

38 files changed

+27485
-21943
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { URL } from 'url';
2+
import { synthetics } from '@amzn/synthetics-playwright';
3+
4+
const loadBlueprints = async function () {
5+
const urls = [process.env.URL];
6+
7+
const browser = await synthetics.launch();
8+
const browserContext = await browser.newContext();
9+
let page = await synthetics.newPage(browserContext);
10+
11+
for (const url of urls) {
12+
await loadUrl(page, url);
13+
}
14+
15+
// Ensure browser is closed
16+
await synthetics.close();
17+
};
18+
19+
// Reset the page in-between
20+
const resetPage = async function(page) {
21+
try {
22+
// Set page.goto timeout to 30 seconds, adjust as needed
23+
// See https://playwright.dev/docs/api/class-page for page.goto options
24+
await page.goto('about:blank', { waitUntil: 'load', timeout: 30000 });
25+
} catch (e) {
26+
console.error('Unable to open a blank page. ', e);
27+
}
28+
};
29+
30+
const loadUrl = async function (page, url) {
31+
let stepName = null;
32+
let domcontentloaded = false;
33+
34+
try {
35+
stepName = new URL(url).hostname;
36+
} catch (e) {
37+
const errorString = `Error parsing url: ${url}. ${e}`;
38+
log.error(errorString);
39+
/* If we fail to parse the URL, don't emit a metric with a stepName based on it.
40+
It may not be a legal CloudWatch metric dimension name and we may not have an alarms
41+
setup on the malformed URL stepName. Instead, fail this step which will
42+
show up in the logs and will fail the overall canary and alarm on the overall canary
43+
success rate.
44+
*/
45+
throw e;
46+
};
47+
48+
await synthetics.executeStep(stepName, async function () {
49+
try {
50+
/* You can customize the wait condition here.
51+
'domcontentloaded' - consider operation to be finished when the DOMContentLoaded event is fired.
52+
'load' - consider operation to be finished when the load event is fired.
53+
'networkidle' - DISCOURAGED consider operation to be finished when there are no network connections for at least 500 ms. Don't use this method for testing, rely on web assertions to assess readiness instead.
54+
'commit' - consider operation to be finished when network response is received and the document started loading.
55+
56+
Set page.goto timeout to 30 seconds, adjust as needed
57+
See https://playwright.dev/docs/api/class-page for page.goto options
58+
*/
59+
const response = await page.goto(url, { waitUntil: 'load', timeout: 30000 });
60+
if (response) {
61+
domcontentloaded = true;
62+
const status = response.status();
63+
console.log(`Response status: ${status}`);
64+
65+
// If the response status code is not a 2xx success code
66+
if (status < 200 || status > 299) {
67+
console.error(`Failed to load url: ${url}, status code: ${status}`);
68+
throw new Error('Failed');
69+
}
70+
} else {
71+
console.error(`No response returned for url: ${url}`);
72+
throw new Error(logNoResponseString);
73+
}
74+
} catch (e) {
75+
const errorString = `Error navigating to url: ${url}. ${e}`;
76+
console.error(errorString);
77+
throw e;
78+
}
79+
});
80+
81+
// Reset page
82+
await resetPage(page);
83+
};
84+
85+
export const handler = async (event, context) => {
86+
return await loadBlueprints();
87+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { URL } from 'url';
2+
import { synthetics } from '@amzn/synthetics-playwright';
3+
4+
const loadBlueprints = async function () {
5+
const urls = [process.env.URL];
6+
7+
const browser = await synthetics.launch();
8+
const browserContext = await browser.newContext();
9+
let page = await synthetics.newPage(browserContext);
10+
11+
for (const url of urls) {
12+
await loadUrl(page, url);
13+
}
14+
15+
// Ensure browser is closed
16+
await synthetics.close();
17+
};
18+
19+
// Reset the page in-between
20+
const resetPage = async function(page) {
21+
try {
22+
// Set page.goto timeout to 30 seconds, adjust as needed
23+
// See https://playwright.dev/docs/api/class-page for page.goto options
24+
await page.goto('about:blank', { waitUntil: 'load', timeout: 30000 });
25+
} catch (e) {
26+
console.error('Unable to open a blank page. ', e);
27+
}
28+
};
29+
30+
const loadUrl = async function (page, url) {
31+
let stepName = null;
32+
let domcontentloaded = false;
33+
34+
try {
35+
stepName = new URL(url).hostname;
36+
} catch (e) {
37+
const errorString = `Error parsing url: ${url}. ${e}`;
38+
log.error(errorString);
39+
/* If we fail to parse the URL, don't emit a metric with a stepName based on it.
40+
It may not be a legal CloudWatch metric dimension name and we may not have an alarms
41+
setup on the malformed URL stepName. Instead, fail this step which will
42+
show up in the logs and will fail the overall canary and alarm on the overall canary
43+
success rate.
44+
*/
45+
throw e;
46+
};
47+
48+
await synthetics.executeStep(stepName, async function () {
49+
try {
50+
/* You can customize the wait condition here.
51+
'domcontentloaded' - consider operation to be finished when the DOMContentLoaded event is fired.
52+
'load' - consider operation to be finished when the load event is fired.
53+
'networkidle' - DISCOURAGED consider operation to be finished when there are no network connections for at least 500 ms. Don't use this method for testing, rely on web assertions to assess readiness instead.
54+
'commit' - consider operation to be finished when network response is received and the document started loading.
55+
56+
Set page.goto timeout to 30 seconds, adjust as needed
57+
See https://playwright.dev/docs/api/class-page for page.goto options
58+
*/
59+
const response = await page.goto(url, { waitUntil: 'load', timeout: 30000 });
60+
if (response) {
61+
domcontentloaded = true;
62+
const status = response.status();
63+
console.log(`Response status: ${status}`);
64+
65+
// If the response status code is not a 2xx success code
66+
if (status < 200 || status > 299) {
67+
console.error(`Failed to load url: ${url}, status code: ${status}`);
68+
throw new Error('Failed');
69+
}
70+
} else {
71+
console.error(`No response returned for url: ${url}`);
72+
throw new Error(logNoResponseString);
73+
}
74+
} catch (e) {
75+
const errorString = `Error navigating to url: ${url}. ${e}`;
76+
console.error(errorString);
77+
throw e;
78+
}
79+
});
80+
81+
// Reset page
82+
await resetPage(page);
83+
};
84+
85+
export const handler = async (event, context) => {
86+
return await loadBlueprints();
87+
};

packages/@aws-cdk-testing/framework-integ/test/aws-synthetics/test/integ.canary-artifact-s3-encryption.js.snapshot/IntegCanaryTestDefaultTestDeployAssert3AD5A094.assets.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-synthetics/test/integ.canary-artifact-s3-encryption.js.snapshot/asset.5178413cfe8db00b2d5dcfa9be417e934c64601d0da3031d88c145c8293bc27f/canary.mjs

+87
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-synthetics/test/integ.canary-artifact-s3-encryption.js.snapshot/asset.5178413cfe8db00b2d5dcfa9be417e934c64601d0da3031d88c145c8293bc27f/playwright/canary.mjs

+87
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)