Skip to main content

Custom WebDriver Commands by TestingBot

TestingBot has added custom WebDriver commands for Chrome and Microsoft Edge browsers.

With these custom commands, you can retrieve various metrics, throttle network speed and monitor/adjust network requests during your test.

These custom commands can be used with WebDriver's JavascriptExecutor.

Important: to be able to use these commands, you need to pass debugging: true in your desired capabilities.

Throttle Network

Description:

This call allows you to throttle network speed, or even simulate no network connection (offline-mode).

((JavascriptExecutor) driver).executeScript("tb:throttle", "3G");
// or
Map<String, Object> throttleMap = new HashMap<>();
throttleMap.put("downloadSpeed", 10 * 1024);
throttleMap.put("uploadSpeed", 10 * 1024);
throttleMap.put("latency", 0);
throttleMap.put("loss", 0);
((JavascriptExecutor) driver).executeScript("tb:throttle", throttleMap);
driver.execute_script("tb:throttle", "3G")
# or
driver.execute_script("tb:throttle", {
    "downloadSpeed": 10 * 1024,
    "uploadSpeed": 10 * 1024,
    "latency": 0,
    "loss": 0
})
await driver.executeScript('tb:throttle', '3G');
// or
await driver.executeScript('tb:throttle', {
    "downloadSpeed": 10 * 1024,
    "uploadSpeed": 10 * 1024,
    "latency": 0,
    "loss": 0
});
driver.execute_script("tb:throttle", "3G")
# or
driver.execute_script("tb:throttle", {
    "downloadSpeed" => 10 * 1024,
    "uploadSpeed" => 10 * 1024,
    "latency" => 0,
    "loss" => 0
})
$driver->executeScript("tb:throttle", ["3G"]);
// or
$driver->executeScript("tb:throttle", [
    "downloadSpeed" => 10 * 1024,
    "uploadSpeed" => 10 * 1024,
    "latency" => 0,
    "loss" => 0
]);
((IJavaScriptExecutor)driver).ExecuteScript("tb:throttle", "3G");
// or
((IJavaScriptExecutor)driver).ExecuteScript("tb:throttle", new Dictionary<string, object>
{
    ["downloadSpeed"] = 10240,
    ["uploadSpeed"] = 10240,
    ["latency"] = 0,
    ["loss"] = 0
});

Options:

tb:throttle
Specifying a predefined condition
Condition Download Speed (KB/s) Upload Speed (KB/s) Latency (ms)
airplane
Simulate Offline Mode
0 0 0
disable
Default - useful if you want to undo the offline command.
no throttle no throttle no throttle
edge 250 150 300
3G 400 100 100
4G 18000 9000 100
tb:throttle
Specify custom settings
downloadSpeed uploadSpeed latency
... KB/s ... KB/s ... ms

Preset values

Each preset sets the same four values you can pass yourself. Speeds are in kilobytes per second (KB/s, 1 KB = 1024 bytes), latency in milliseconds and loss in percent; the same units apply to your own values. tb:throttle and the throttle_network capability use the same presets, as does network throttling in a Live session.

Preset downloadSpeed (KB/s) uploadSpeed (KB/s) latency (ms) loss (%)
Edge 250 150 300 0
3G 400 100 100 0
4G 18000 9000 100 0
airplane No network connection
disable Removes the throttle

How the values are applied:

  • Traffic is throttled by sending it through a proxy on the test machine or device. Traffic that does not use the system HTTP proxy is not throttled.
  • latency is added once when a new connection opens, not to every request or packet on that connection.
  • loss is the percentage of new connections that are dropped.
  • tb:throttle does not need debugging: true.
  • Throttling is not available for browsers on Linux: the command is accepted, but the network is not changed.

Intercept/Mock

Description:

With these commands, you can intercept and even fake responses during your test.
Blacklist requests, add/modify headers, mock responses, ...

The url parameter supports wildcard statements, for example: https://www.testingbot.com/*.

Redirect a URL

Instructs Chrome/Edge to redirect to redirect upon navigating to url.

Map<String, Object> interceptMap = new HashMap<>();
interceptMap.put("redirect", "https://testingbot.com/pricing");
interceptMap.put("url", "https://testingbot.com");
((JavascriptExecutor) driver).executeScript("tb:intercept", interceptMap);
driver.execute_script("tb:intercept", {
    "redirect": "https://testingbot.com/pricing",
    "url": "https://testingbot.com"
})
await driver.executeScript('tb:intercept', {
    "redirect": "https://testingbot.com/pricing",
    "url": "https://testingbot.com"
});
driver.execute_script("tb:intercept", {
    "redirect" => "https://testingbot.com/pricing",
    "url" => "https://testingbot.com"
})
$driver->executeScript("tb:intercept", [
    "redirect" => "https://testingbot.com/pricing",
    "url" => "https://testingbot.com"
]);
((IJavaScriptExecutor)driver).ExecuteScript("tb:intercept", new Dictionary<string, object>
{
    ["redirect"] = "https://testingbot.com/pricing",
    ["url"] = "https://testingbot.com"
});

Trigger Failure

Allows you to return a specific error for a specific request.

Possible errors:

  • Failed
  • Aborted
  • TimedOut
  • AccessDenied
  • ConnectionClosed
  • ConnectionReset
  • ConnectionRefused
  • ConnectionAborted
  • ConnectionFailed
  • NameNotResolved
  • InternetDisconnected
  • AddressUnreachable
  • BlockedByClient
  • BlockedByResponse
Map<String, Object> interceptMap = new HashMap<>();
interceptMap.put("error", "ConnectionRefused");
interceptMap.put("url", "https://testingbot.com");
((JavascriptExecutor) driver).executeScript("tb:intercept", interceptMap);
driver.execute_script("tb:intercept", {
    "error": "ConnectionRefused",
    "url": "https://testingbot.com"
})
await driver.executeScript('tb:intercept', {
    "error": "ConnectionRefused",
    "url": "https://testingbot.com"
});
driver.execute_script("tb:intercept", {
    "error" => "ConnectionRefused",
    "url" => "https://testingbot.com"
})
$driver->executeScript("tb:intercept", [
    "error" => "ConnectionRefused",
    "url" => "https://testingbot.com"
]);
((IJavaScriptExecutor)driver).ExecuteScript("tb:intercept", new Dictionary<string, object>
{
    ["error"] = "ConnectionRefused",
    ["url"] = "https://testingbot.com"
});

Mock Response

Allows you to return a different response (body or headers).

Map<String, Object> mockMap = new HashMap<>();
Map<String, Object> headersMap = new HashMap<>();
headersMap.put("x-a-header", "Sample");
Map<String, Object> responseMap = new HashMap<>();
responseMap.put("statusCode", 200);
responseMap.put("headers", headersMap);
responseMap.put("body", "Hello World");
mockMap.put("response", responseMap);
mockMap.put("url", "https://testingbot.com");
((JavascriptExecutor) driver).executeScript("tb:intercept", mockMap);
driver.execute_script("tb:intercept", {
    "response": {
        "statusCode": 200,
        "headers": {
            "x-a-header": "Sample"
        },
        "body": "Hello World"
    },
    "url": "https://testingbot.com"
})
await driver.executeScript('tb:intercept', {
    "response": {
        "statusCode": 200,
        "headers": {
            "x-a-header": "Sample"
        },
        "body": "Hello World"
    },
    "url": "https://testingbot.com"
});
driver.execute_script("tb:intercept", {
    "response" => {
        "statusCode" => 200,
        "headers" => {
            "x-a-header" => "Sample"
        },
        "body" => "Hello World"
    },
    "url" => "https://testingbot.com"
})
$driver->executeScript("tb:intercept", [
    "response" => [
        "statusCode" => 200,
        "headers" => [
            "x-a-header" => "Sample"
        ],
        "body" => "Hello World"
    ],
    "url" => "https://testingbot.com"
]);
((IJavaScriptExecutor)driver).ExecuteScript("tb:intercept", new Dictionary<string, object>
{
    ["response"] = new Dictionary<string, object>
    {
        ["statusCode"] = 200,
        ["headers"] = new Dictionary<string, object> { ["x-a-header"] = "Sample" },
        ["body"] = "Hello World"
    },
    ["url"] = "https://testingbot.com"
});

Retrieve Network Log

Description:

With the tb:network command you can fetch a log of network calls from Chrome and Microsoft Edge.

((JavascriptExecutor) driver).executeScript("tb:network");
driver.execute_script("tb:network")
await driver.executeScript('tb:network');
driver.execute_script("tb:network")
$driver->executeScript("tb:network");
((IJavaScriptExecutor)driver).ExecuteScript("tb:network");
Was this page helpful?
Last updated