--- title: "Scripting" slug: "scripting-1" updated: 2025-10-16T14:37:50Z published: 2025-10-16T14:37:50Z canonical: "docs.catchpoint.com/scripting-1" --- > ## Documentation Index > Fetch the complete documentation index at: https://docs.catchpoint.com/llms.txt > Use this file to discover all available pages before exploring further. # Scripting If you need to log into a site or run arbitrary JavaScript while a test is running, use WebPageTest scripts. A script lets you automate a WebPageTest test. Here’s how it looks: ``` // EXAMPLE: log into a site before measuring a page // Disable logging: we don’t want the login page to appear in test results logData 0 // Visit the login page, enter the username and the password into the fields, and press “Submit” navigate https://my-site.com/login setValue name=email darth@vader.com setValue name=password foooooooooooooooorce click innerText=Sign in // Now, as we’ve signed in, enable logging again logData 1 // And go the page we’re measuring navigate https://my-site.com/dashboard ``` Here’s another example: ``` // EXAMPLE: measure what happens when we interact with the page // Go the page we’re measuring navigate https://my-site.com/dashboard // Give it a bit of time to initialize after loading sleep 5 // Open the support dialog and wait for the page to become idle execAndWait document.querySelector('.chat-dialog > button').click() ``` See more [commands](/wpt/docs/scripting-1#recommended-commands) and [examples](/wpt/docs/scripting-1#sample-scripts) below. ## Recommended Commands For historical reasons, if a script step fails, it does so silently. If something doesn’t work, unfortunately, you won’t get any feedback why. If you’re new to scripting, we recommend using the following commands only. It’s easier to debug them as you can run them manually in the browser. #### `navigate` Navigates the browser to the provided URL and waits for it to complete. Browser Support: IE, Chrome, Firefox, Safari ``` usage: navigate example: navigate http://webmail.aol.com - URL to provide the browser for navigation (same as you would enter into the address bar) ``` #### `exec` Executes JavaScript. Browser Support: IE, Chrome, Firefox ``` usage: exec example: exec window.setInterval('window.scrollBy(0,600)', 1000); ``` #### `execAndWait` Executes JavaScript and waits for the browser to complete any activity generated from the action. Browser Support: IE, Chrome, Firefox ``` usage: execAndWait example: execAndWait window.setInterval('window.scrollBy(0,600)', 1000); ``` ## Full Reference ### Comments A line that starts with `//` is ignored. Use this for comments. ``` // Example comment navigate not-a-comment.com ``` ### Selectors Commands like [`click`](/wpt/docs/scripting-1#click), [`setValue`](/wpt/docs/scripting-1#setvalue), and others operate on DOM elements. To select a DOM element, use the `<attribute>=<value>` selector. This selector selects elements based on their HTML attributes. For the following input: ``` ``` the following selectors will work: - ✅ `type=email` - ✅ `id=emailField` and the following selectors will not: - ❌ `class=input input-email` (spaces inside attribute values are not supported) - ❌ `class="input input-email"` (not even if quoted) Example: ``` // Set the email to darth@vader.com setValue type=email darth@vader.com ``` ### Variable substitutions Some variables are replaced based on the URL provided for the test. #### `%URL%` URL provided for the test. ``` URL: https://wpt.example input: navigate %URL% output: navigate https://wpt.example ``` #### `%HOST%` This will be the Host of the URL provided for the test. This does not include the protocol. ``` URL: https://wpt.example input: setDnsName %HOST% dns.example output: setDnsName wpt.example dns.example ``` #### `%TEST_ID%` The ID Generated by Webpagetest for the given test. ``` TEST_ID: 220927_1B_8 input: addHeader %TEST_ID% output: addHeader 220927_1B_8 ``` #### `%ORIGIN%` The Origin of the URL. This includes the protocol, the host and the port (if it is defined). ``` URL: https://wpt.example/hello input: setCookie %ORIGIN% foo=bar output: setCookie https://wpt.example foo=bar URL: https://wpt.example:8080/hello input: setCookie %ORIGIN% foo=bar output: setCookie https://wpt.example:8080 foo=bar ``` #### `%HOSTR%` Same as [`%HOST%`](/wpt/docs/scripting-1#host) but uses the final host name of the test URL after following any redirects. ``` URL: https://redirect.wpt.example input: setDnsName %HOSTR% dns.example output: setDnsName wpt.example dns.example ``` ### Navigation/DOM Interaction #### `navigate` Navigates the browser to the provided URL and waits for it to complete. Browser Support: IE, Chrome, Firefox, Safari ``` usage: navigate example: navigate http://webmail.aol.com - URL to provide the browser for navigation (same as you would enter into the address bar) ``` #### `click` Triggers a click event for the identified DOM element. This version does not have an implied wait and the script will continue running after the event is submitted (see [`clickAndWait`](/wpt/docs/scripting-1#clickandwait) for the wait version). Browser Support: IE, Chrome, Firefox ``` usage: click example: click title=Delete - DOM element to click on ``` For the list of supported selectors, see [Selectors](/wpt/docs/scripting-1#selectors). #### `clickAndWait` Triggers a click event for the identified DOM element and subsequently waits for browser activity to complete. Browser Support: IE, Chrome, Firefox ``` usage: clickAndWait example: clickAndWait innerText=Send - DOM element to click on ``` For the list of supported selectors, see [Selectors](/wpt/docs/scripting-1#selectors). #### `selectValue` Selects a value from a dropdown list of the given DOM element. Browser Support: IE ``` usage: selectValue example: selectValue id=country usa - DOM element to select the value of - value to use ``` For the list of supported selectors, see [Selectors](/wpt/docs/scripting-1#selectors). #### `sendClick` / `sendClickAndWait` Creates a JavaScript `onclick` event and sends it to the indicated element. Browser Support: IE ``` usage: sendClickAndWait example: sendClickAndWait innerText=Send - DOM element to send the click event to ``` For the list of supported selectors, see [Selectors](/wpt/docs/scripting-1#selectors). For the difference between `sendClick` and `sendClickAndWait`, see [`click`](/wpt/docs/scripting-1#click) and [`clickAndWait`](/wpt/docs/scripting-1#clickandwait). #### `type` / `typeAndWait` Simulate keyboard keypresses for each character in the given string. Browser Support: Chrome ``` usage: type example: type Hello World - String of characters to type into the keyboard. ``` For the difference between `type` and `typeAndWait`, see [`click`](/wpt/docs/scripting-1#click) and [`clickAndWait`](/wpt/docs/scripting-1#clickandwait). #### `mouseClick` / `mouseClickAndWait` Simulate a trusted mouse click event for the indicated element. Browser Support: Chrome ``` usage: mouseClick [