Skip to content

Commit 7845131

Browse files
authored
chore: selenium sample (GoogleCloudPlatform#130)
1 parent 3bf3f1a commit 7845131

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// [START monitoring_synthetic_monitoring_custom_selenium_script]
16+
const {
17+
instantiateAutoInstrumentation,
18+
runSyntheticHandler,
19+
} = require('@google-cloud/synthetics-sdk-api');
20+
// Run instantiateAutoInstrumentation before any other code runs, to get automatic logs and traces
21+
instantiateAutoInstrumentation();
22+
const functions = require('@google-cloud/functions-framework');
23+
const assert = require('node:assert');
24+
25+
const { Builder, Browser, By } = require('selenium-webdriver');
26+
const chrome = require('selenium-webdriver/chrome');
27+
28+
/*
29+
* This function executes the synthetic code for testing purposes.
30+
* If the code runs without errors, the synthetic test is considered successful.
31+
* If an error is thrown during execution, the synthetic test is considered failed.
32+
*/
33+
functions.http(
34+
'CustomSeleniumSynthetic',
35+
runSyntheticHandler(async ({ logger, executionId }) => {
36+
/*
37+
* Construct chrome options
38+
* Note: `setChromeBinaryPath` must be set to '/srv/bin/chromium' when running in
39+
* GCF (but will need to be changed if running on local machine).
40+
*/
41+
const options = new chrome.Options();
42+
options.setChromeBinaryPath('/srv/bin/chromium');
43+
options.addArguments('--headless', '--disable-gpu', '--no-sandbox');
44+
45+
// Launch headless chrome webdriver with options
46+
const driver = await new Builder()
47+
.forBrowser(Browser.CHROME)
48+
.setChromeOptions(options)
49+
.build();
50+
51+
// Navigate to the target URL
52+
await driver.get('https://example.com');
53+
54+
// Retrieve title and `a` tag of page
55+
const title = await driver.getTitle();
56+
const aTag = await driver.findElement(By.css('a')).getText();
57+
58+
// assert title is as expected and print to console
59+
await assert.equal(title, 'Example Domain');
60+
logger.info(`My URL title is: ${title} ` + executionId);
61+
62+
await driver.quit();
63+
})
64+
);
65+
66+
// [END monitoring_synthetic_monitoring_custom_selenium_script]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "generic-selenium-nodejs",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"dependencies": {
7+
"@google-cloud/functions-framework": "^3.1.2",
8+
"@google-cloud/synthetics-sdk-api": "^0.6.0",
9+
"selenium-webdriver": "^4.20.0"
10+
},
11+
"license": "Apache-2.0",
12+
"author": "Google Inc."
13+
}

0 commit comments

Comments
 (0)