---
title: 'Custom WebDriver commands: performance, network and mocking.'
description: 'Custom WebDriver Commands by TestingBot: mock responses and monitor
  performance during your tests.'
source_url:
  html: https://testingbot.com/support/web-automate/selenium/network
  md: https://testingbot.com/support/web-automate/selenium/network/index.md
---
# 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).

[Java](https://testingbot.com#)[Python](https://testingbot.com#)[NodeJS](https://testingbot.com#)[Ruby](https://testingbot.com#)[PHP](https://testingbot.com#)[C#](https://testingbot.com#)

    ((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/sec) | Upload Speed (kb/sec) | 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 / sec | ... kb / sec | ... ms |

 |

## 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`.

[Java](https://testingbot.com#)[Python](https://testingbot.com#)[NodeJS](https://testingbot.com#)[Ruby](https://testingbot.com#)[PHP](https://testingbot.com#)[C#](https://testingbot.com#)

    Map<String, Object> interceptMap = new HashMap<>();
    interceptMap.put("redirect", "https://www.google.com");
    interceptMap.put("url", "https://testingbot.com");
    ((JavascriptExecutor) driver).executeScript("tb:intercept", interceptMap);

    driver.execute_script("tb:intercept", {
        "redirect": "https://google.com",
        "url": "https://testingbot.com"
    })

    await driver.executeScript('tb:intercept', {
        "redirect": "https://google.com",
        "url": "https://testingbot.com"
    });

    driver.execute_script("tb:intercept", {
        "redirect" => "https://www.google.com",
        "url" => "https://testingbot.com"
    })

    $driver->executeScript("tb:intercept", [
        "redirect" => "https://google.com",
        "url" => "https://testingbot.com"
    ]);

    ((IJavaScriptExecutor)driver).ExecuteScript("tb:intercept", new Dictionary<string, object>
    {
        ["redirect"] = "https://www.google.com",
        ["url"] = "https://testingbot.com"
    });

### Trigger Failure

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

[Possible errors](https://chromedevtools.github.io/devtools-protocol/tot/Network#type-ErrorReason):

- Failed
- Aborted
- TimedOut
- AccessDenied
- ConnectionClosed
- ConnectionReset
- ConnectionRefused
- ConnectionAborted
- ConnectionFailed
- NameNotResolved
- InternetDisconnected
- AddressUnreachable
- BlockedByClient
- BlockedByResponse

[Java](https://testingbot.com#)[Python](https://testingbot.com#)[NodeJS](https://testingbot.com#)[Ruby](https://testingbot.com#)[PHP](https://testingbot.com#)[C#](https://testingbot.com#)

    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).

[Java](https://testingbot.com#)[Python](https://testingbot.com#)[NodeJS](https://testingbot.com#)[Ruby](https://testingbot.com#)[PHP](https://testingbot.com#)[C#](https://testingbot.com#)

    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.

[Java](https://testingbot.com#)[Python](https://testingbot.com#)[NodeJS](https://testingbot.com#)[Ruby](https://testingbot.com#)[PHP](https://testingbot.com#)[C#](https://testingbot.com#)

    ((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");

### Looking for more help?

Have questions or need more information? Reach out via email or Slack.

[Email us](https://testingbot.com/contact/new)[Slack Join our Slack](https://join.slack.com/t/testingb0t/shared_invite/zt-3bcw9xch-jk19~6XPs_xBrsAgAedkCw)
