|
| 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] |
0 commit comments