Use the Synthetics CLI

edit

@elastic/synthetics

edit

The Synthetics app uses the @elastic/synthetics Node.js library to run synthetic browser tests and report the test results. The library also provides a CLI to help you scaffold, develop/run tests locally, and push tests to Kibana.

npx @elastic/synthetics [options] [files] [dir]

You will not need to use most command line flags — they have been implemented purely to interact with the Synthetics app. However, there are some you may find useful:

--match <string>
run tests with a name or tags that match the given glob pattern.
--tags Array<string>
run tests with the given tags that match the given glob pattern.
--pattern <string>
RegExp pattern to match journey files in the current working directory. Defaults to /*.journey.(ts|js)$/, which matches files ending with .journey.ts or .journey.js.
--params <jsonstring>

JSON object that defines any variables your tests require. Read more in Work with params and secrets.

Params passed will be merged with params defined in your synthetics.config.js file. Params defined via the CLI take precedence.

--playwright-options <jsonstring>

JSON object to pass in custom Playwright options for the agent. For more details on relevant Playwright options, refer to the the configuration docs.

Options passed will be merged with Playwright options defined in your synthetics.config.js file. Options defined via the CLI take precedence.

--screenshots <on|off|only-on-failure>

Control whether or not to capture screenshots at the end of each step. Options include 'on', 'off', or 'only-on-failure'.

This can also be set in the configuration file using monitor.screenshot. The value defined via the CLI will take precedence.

-c, --config <string>
Path to the configuration file. By default, test runner looks for a synthetics.config.(js|ts) file in the current directory. Synthetics configuration provides options to configure how your tests are run and pushed to Kibana. Allowed options are described in the configuration file.
--reporter <json|junit|buildkite-cli|default>
One of json, junit, buildkite-cli, or default. Use the JUnit or Buildkite reporter to provide easily parsed output to CI systems.
--inline
Instead of reading from a file, cat inline scripted journeys and pipe them through stdin. For example, cat path/to/file.js | npx @elastic/synthetics --inline.
--no-throttling

Does not apply throttling.

Throttling can also be disabled in the configuration file using monitor.throttling. The value defined via the CLI will take precedence.

Network throttling for browser based monitors is disabled. See this documention for more details.

--no-headless

Runs with the browser in headful mode.

This is the same as setting Playwright’s headless option to false by running --playwright-options '{"headless": false}'.

Headful mode should only be used locally to see the browser and interact with DOM elements directly for testing purposes. Do not attempt to run in headful mode when running through Elastic’s global managed testing infrastructure or Private Locations as this is not supported.

-h, --help
Shows help for the npx @elastic/synthetics command.

The --pattern, --tags, and --match flags for filtering are only supported when you run synthetic tests locally or push them to Kibana. Filtering is not supported in any other subcommands like init and locations.

For debugging synthetic tests locally, you can set an environment variable, DEBUG=synthetics npx @elastic/synthetics, to capture Synthetics agent logs.

@elastic/synthetics init

edit

Scaffold a new project using Elastic Synthetics.

This will create a template Node.js project that includes the synthetics agent, required dependencies, a synthetics configuration file, and example browser and lightweight monitor files. These files can be edited and then pushed to Kibana to create monitors.

npx @elastic/synthetics init <name-of-project>

Read more about what’s included in a template project in Create a project.

@elastic/synthetics push

edit

Create monitors in Kibana by using your local journeys. By default, running push command will use the project settings field from the synthetics.config.ts file, which is set up using the init command. However, you can override these settings using the CLI flags.

SYNTHETICS_API_KEY=<api-key> npx @elastic/synthetics push --url <kibana-url> --id <id|name>

The push command includes interactive prompts to prevent you from accidentally deleting or duplicating monitors. You will see a prompt when:

  • You push a project that used to contain one or more monitors but either no longer contains previously running monitors or has any monitors. Select yes to delete the monitors associated with the project ID being pushed.
  • You push a project that’s already been pushed using one project ID and then try to push it using a different ID. Select yes to create duplicates of all monitors in the project.

You can set DEBUG=synthetics environment variable to capture the deleted monitors.

If the journey contains external NPM packages other than the @elastic/synthetics, those packages will be bundled along with the journey code when the push command is invoked. However there are some limitations when using external packages:

  • Bundled journeys after compression should not be more than 1500 Kilobytes.
  • Native node modules will not work as expected due to platform inconsistency.
  • Uploading files in journey scripts(via locator.setInputFiles) is not supported.
--auth <string>

API key used for Kibana authentication. You can also set the API key via the SYNTHETICS_API_KEY environment variable.

If you are pushing to a Private Location, you must use an API key generated in 8.4 or higher.

To create an API key, you must be logged into Kibana as a user with the privileges described in Writer role.

--id <string>

A unique id associated with your project. It will be used for logically grouping monitors.

If you used init to create a project, this is the <name-of-project> you specified.

This can also be set in the configuration file using project.id. The value defined via the CLI will take precedence.

--url <string>

The Kibana URL for the deployment to which you want to upload the monitors.

This can also be set in the configuration file using project.url. The value defined via the CLI will take precedence.

--space <string>

The identifier of the target Kibana space for the pushed monitors. Spaces help you organize pushed monitors. Pushes to the "default" space if not specified.

This can also be set in the configuration file using project.space. The value defined via the CLI will take precedence.

--schedule <number>

The interval (in minutes) at which the monitor should run.

This can also be set in the configuration file using monitor.schedule. The value defined via the CLI will take precedence.

--locations Array<SyntheticsLocationsType>

Where to deploy the monitor. Monitors can be deployed in multiple locations so that you can detect differences in availability and response times across those locations.

To list available locations, refer to @elastic/synthetics locations.

This can also be set in the configuration file using monitor.locations in the configuration file. The value defined via the CLI will take precedence.

--private-locations Array<string>

The Private Locations to which the monitors will be deployed. These Private Locations refer to locations hosted and managed by you, whereas locations are hosted by Elastic. You can specify a Private Location using the location’s name.

To list available Private Locations, refer to @elastic/synthetics locations.

This can also be set in the configuration file using monitor.privateLocations in the configuration file. The value defined via the CLI will take precedence.

--fields <string>

A list of key-value pairs that will be sent with each monitor event. The fields are appended to Elasticsearch documents as labels, and those labels are displayed in Kibana in the Monitor details panel in the individual monitor’s Overview tab.

Example: --fields '{ "foo": bar", "team": "synthetics" }'

This can also be set in the configuration file using monitor.fields. The value defined via the CLI will take precedence.

--yes

The push command includes interactive prompts to prevent you from accidentally deleting or duplicating monitors. If running the CLI non-interactively, you can override these prompts using the --yes option. When the --yes option is passed to push:

  • If you push a project that used to contain one or more monitors but no longer contains any monitors, all monitors associated with the project ID being pushed will be deleted.
  • If you push a project that’s already been pushed using one project ID and then try to push it using a different ID, it will create duplicates of all monitors in the project.

Tagging and Filtering monitors

edit

Synthetics journeys can be tagged with one or more tags. Use tags to filter journeys when running tests locally or pushing them to Kibana.

To add tags to a single journey, add the tags parameter to the journey function or use the monitor.use method.

import {journey, monitor} from "@elastic/synthetics";

journey({name: "example journey", tags: ["env:qa"] }, ({ page }) => {
  monitor.use({
    tags: ["env:qa"]
  })
  // Add steps here
});

For lightweight monitors, use the tags field in the yaml configuration file.

name: example monitor
tags:
  - env:qa

To apply tags to all browser and lightweight monitors, configure using monitor.tags field in the synthetics.config.ts file.

Filtering monitors
edit

When running the npx @elastic/synthetics push command, you can filter the monitors that are pushed to Kibana using the following flags:

--tags Array<string>
Push monitors with the given tags that match the glob pattern.
--match <string>
Push monitors with a name or tags that match the glob pattern.
--pattern <string>
RegExp pattern to match the journey files in the current working directory. Defaults to /*.journey.(ts|js)$/, for browser monitors and /.(yml|yaml)$/ for lightweight monitors.

You can combine these techniques and push the monitors to different Kibana clusters/space based on the tags by using multiple configuration files.

npx @elastic/synthetics push --config synthetics.qa.config.ts --tags env:qa
npx @elastic/synthetics push --config synthetics.prod.config.ts --tags env:prod

@elastic/synthetics locations

edit

List all available locations for running synthetics monitors.

npx @elastic/synthetics locations --url <kibana-host> --auth <api-key>

Run npx @elastic/synthetics locations with no flags to list all the available global locations managed by Elastic for running synthetics monitors.

To list both locations on Elastic’s global managed infrastructure and Private Locations, include:

--url <string>
The Kibana URL for the deployment from which to fetch all available public and Private Locations.
--auth <string>
API key used for Kibana authentication.

If an administrator has disabled Elastic managed locations for the role you are assigned and you do not include --url and --auth, all global locations managed by Elastic will be listed. However, you will not be able to push to these locations with your API key and will see an error: You don’t have permission to use Elastic managed global locations. For more details, refer to the troubleshooting docs.