Fortinet white logo
Fortinet white logo

User Guide

24.4.0

Set up a JavaScript synthetic check environment

Set up a JavaScript synthetic check environment

This article describes how to set up an environment for JavaScript synthetic checks in FortiMonitor.

Prerequisites

  1. Install Node.js and Node Package Manager (npm).

  2. Enter the following command to install the Selenium WebDriver package:

npm install selenium-webdriver

3. Install the Chrome browser driver for development and testing. Note that the browser driver must be the same version as the browser you are using (Chrome browser 80 must be paired with Chrome driver 80). The driver must be located somewhere in your system path so that it can be executed by the WebDriver.

4. Download the FortiMonitor synthetics module and save it to your local directory.

Test template

You can use the following template to get started:

const {init_panopta_synthetics} = require('./panopta-synthetics');
 
(async function example() {
let driver = await init_panopta_synthetics('chrome');
try {
 
driver.ps_step('Opening initial page');
 
await driver.get('https://google.com');
var result = await driver.findElement(By.name('q'));
 
await result.sendKeys('webdriver', Key.RETURN);
driver.ps_pass_test()
} catch (err) {
driver.ps_fail_test(err);
} finally {
await driver.quit();
}
 
})();

Use this template to run tests in Chrome. You can also use no-browser for tests that do not require a browser to perform tests.

In addition to the normal Selenium WebDriver function, the following additional functions are available in the driver object:

Function

Description

ps_step(name)

Marks the beginning of a step in the test. The name of the step will be displayed in the final output as well as a description of the overall test failure if it fails at this step. This function also begins the timer for measuring the execution time of the test step.

ps_annotate(message)

Adds additional logging information to the test step.

ps_pass_test()

Called to indicate that the test has successfully completed.

ps_fail_test(err)

Called to indicate that the test has failed. Takes an optional JavaScript exception object which, if provided, is used to build the description of why the test failed.

When running a browser-based test, additional functions expose more detailed browser timing metrics for the most recent request:

Method

Description

ps_get_dns_time()

The time needed to resolve the hostname of the request.

ps_get_connect_time()

The time needed to make the initial socket connection.

ps_get_connect_time_ssl()

The time needed to perform the SSL/TLS connection.

ps_get_request_time()

Total time to make the HTTP request.

ps_get_response_time()

Total time needed to download the HTTP response.

ps_get_ttfb()

Time from the initial request until the first byte of the response was returned.

ps_get_total_time()

Total time to process the most recent request.

Full test example

The following is an example JavaScript synthetic check code snippet. This simple test runs a Google query.

const {By, Key, until} = require('selenium-webdriver');
const {init_panopta_synthetics} = require('./panopta-synthetics');
(async function example() {
let driver = await init_panopta_synthetics('chrome');
try {
driver.ps_step('Opening initial page');
await driver.get('https://google.com');
var result = await driver.findElement(By.name('q'));
driver.ps_annotate('Running sample Google query');
await result.sendKeys('panopta', Key.RETURN);
driver.ps_pass_test()
} catch (err) {
driver.ps_fail_test(err);
} finally {
await driver.quit();
}
})();

Test execution

Tests can be run using node test.js --local. This will start the browser and execute the test, displaying information in the terminal about each step that is performed. Upon completion, a JSON block that the FortiMonitor monitoring probe will interpret in production use will be displayed. If you would like to turn this off, add --hide-results to the end of the command.

Additional node modules

The test scripts are run from FortiMonitor's monitoring probes in a limited sandbox environment for security reasons. The following modules are supported:

Additional modules can be for tests, but they need to be whitelisted in our environment in order to be imported. Please contact our support team to discuss any modules that your tests require.

Related documentation

  • Selenium WebDriver - The basic documentation for the JavaScript version of the Selenium web driver covers the basic usage and configuration support for different browsers.

  • Full Javascript API documentation - The most important components are:

    • WebDriver - the main object that represents the driver that Selenium is managing

    • WebElement - used for accessing information about specific DOM elements

    • By - the mechanism for locating elements on the page via CSS, XPath, element name, id, etc.

  • This article describes various strategies on the best way to write repeatable JavaScript tests.

Set up a JavaScript synthetic check environment

Set up a JavaScript synthetic check environment

This article describes how to set up an environment for JavaScript synthetic checks in FortiMonitor.

Prerequisites

  1. Install Node.js and Node Package Manager (npm).

  2. Enter the following command to install the Selenium WebDriver package:

npm install selenium-webdriver

3. Install the Chrome browser driver for development and testing. Note that the browser driver must be the same version as the browser you are using (Chrome browser 80 must be paired with Chrome driver 80). The driver must be located somewhere in your system path so that it can be executed by the WebDriver.

4. Download the FortiMonitor synthetics module and save it to your local directory.

Test template

You can use the following template to get started:

const {init_panopta_synthetics} = require('./panopta-synthetics');
 
(async function example() {
let driver = await init_panopta_synthetics('chrome');
try {
 
driver.ps_step('Opening initial page');
 
await driver.get('https://google.com');
var result = await driver.findElement(By.name('q'));
 
await result.sendKeys('webdriver', Key.RETURN);
driver.ps_pass_test()
} catch (err) {
driver.ps_fail_test(err);
} finally {
await driver.quit();
}
 
})();

Use this template to run tests in Chrome. You can also use no-browser for tests that do not require a browser to perform tests.

In addition to the normal Selenium WebDriver function, the following additional functions are available in the driver object:

Function

Description

ps_step(name)

Marks the beginning of a step in the test. The name of the step will be displayed in the final output as well as a description of the overall test failure if it fails at this step. This function also begins the timer for measuring the execution time of the test step.

ps_annotate(message)

Adds additional logging information to the test step.

ps_pass_test()

Called to indicate that the test has successfully completed.

ps_fail_test(err)

Called to indicate that the test has failed. Takes an optional JavaScript exception object which, if provided, is used to build the description of why the test failed.

When running a browser-based test, additional functions expose more detailed browser timing metrics for the most recent request:

Method

Description

ps_get_dns_time()

The time needed to resolve the hostname of the request.

ps_get_connect_time()

The time needed to make the initial socket connection.

ps_get_connect_time_ssl()

The time needed to perform the SSL/TLS connection.

ps_get_request_time()

Total time to make the HTTP request.

ps_get_response_time()

Total time needed to download the HTTP response.

ps_get_ttfb()

Time from the initial request until the first byte of the response was returned.

ps_get_total_time()

Total time to process the most recent request.

Full test example

The following is an example JavaScript synthetic check code snippet. This simple test runs a Google query.

const {By, Key, until} = require('selenium-webdriver');
const {init_panopta_synthetics} = require('./panopta-synthetics');
(async function example() {
let driver = await init_panopta_synthetics('chrome');
try {
driver.ps_step('Opening initial page');
await driver.get('https://google.com');
var result = await driver.findElement(By.name('q'));
driver.ps_annotate('Running sample Google query');
await result.sendKeys('panopta', Key.RETURN);
driver.ps_pass_test()
} catch (err) {
driver.ps_fail_test(err);
} finally {
await driver.quit();
}
})();

Test execution

Tests can be run using node test.js --local. This will start the browser and execute the test, displaying information in the terminal about each step that is performed. Upon completion, a JSON block that the FortiMonitor monitoring probe will interpret in production use will be displayed. If you would like to turn this off, add --hide-results to the end of the command.

Additional node modules

The test scripts are run from FortiMonitor's monitoring probes in a limited sandbox environment for security reasons. The following modules are supported:

Additional modules can be for tests, but they need to be whitelisted in our environment in order to be imported. Please contact our support team to discuss any modules that your tests require.

Related documentation

  • Selenium WebDriver - The basic documentation for the JavaScript version of the Selenium web driver covers the basic usage and configuration support for different browsers.

  • Full Javascript API documentation - The most important components are:

    • WebDriver - the main object that represents the driver that Selenium is managing

    • WebElement - used for accessing information about specific DOM elements

    • By - the mechanism for locating elements on the page via CSS, XPath, element name, id, etc.

  • This article describes various strategies on the best way to write repeatable JavaScript tests.