Custom WebDriver Commands by TestingBot
TestingBot has added custom WebDriver commands for Chrome browsers, version 62 and higher.
We are working on making these commands available for other browsers as well.
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).
Ruby
Python
PHP
Java
Node
C#
driver . execute_script ( "tb:throttle" , { "condition" : "GPRS" })
# or
driver . execute_script ( "tb:throttle" , {
"condition" : {
"download" : 500 ,
"upload" : 100 ,
"latency" : 30
}
})
Map < String , Object > throttleMap = new HashMap <>();
throttleMap . put ( "condition" , "GPRS" );
(( JavascriptExecutor ) driver ). executeScript ( "tb:throttle" , throttleMap );
// or
Map < String , Object > conditionMap = new HashMap <>();
conditionMap . put ( "download" , 500 );
conditionMap . put ( "upload" , 100 );
conditionMap . put ( "latency" , 30 );
Map < String , Object > throttleMap = new HashMap <>();
throttleMap . put ( "condition" , conditionMap );
(( JavascriptExecutor ) driver ). executeScript ( "tb:throttle" , throttleMap );
$driver -> executeScript ( "tb:throttle" , [ "condition" => "GRPS" ]);
// or
$driver -> executeScript ( "tb:throttle" , [ "condition" => [
"download" => 500 ,
"upload" => 100 ,
"latency" => 30
]
]);
driver . execute_script ( "tb:throttle" , { "condition" : "GPRS" })
# or
driver . execute_script ( "tb:throttle" , {
"condition" : {
"download" : 500 ,
"upload" : 100 ,
"latency" : 30
}
})
browser . execute ( ' tb:throttle ' , { condition : ' GPRS ' })
// or
browser . execute ( ' tb:throttle ' , {
condition : {
" download " : 500 ,
" upload " : 100 ,
" latency " : 30
}
})
(( IJavaScriptExecutor ) driver ). ExecuteScript ( "tb:throttle" , "{\"condition\":\"GPRS\"}" );
# or
(( IJavaScriptExecutor ) driver ). ExecuteScript ( "tb:throttle" , "{\"condition\":{\"download\":500, \"upload\": 100, \"latency\": 30}}" );
Options:
tb:throttle
Specifying a predefined condition
Condition
Download Speed (kb/sec)
Upload Speed (kb/sec)
Latency (ms)
offline
Simulate Offline Mode
0
0
0
online
Default - useful if you want to undo the offline
command.
no throttle
no throttle
no throttle
GPRS
50
20
500
Regular 2G
250
50
300
Good 2G
450
150
150
Regular 3G
750
250
100
Good 3G
1Mb / sec
750
20
Regular 4G
4Mb / sec
3Mb / sec
20
DSL
2Mb / sec
1Mb / sec
5
WiFi
30Mb / sec
15Mb / sec
2
tb:throttle
Specify custom settings
download
upload
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 to redirect to redirect
upon navigating to url
.
Ruby
Python
PHP
Java
Node
C#
driver . execute_script ( "tb:intercept" , {
"redirect" : "https://www.google.com" ,
"url" : "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 -> executeScript ( "tb:intercept" , [
"redirect" => "https://google.com" ,
"url" => "https://testingbot.com" ,
]
]);
driver . execute_script ( "tb:intercept" , {
"redirect" : "https://google.com" ,
"url" : "https://testingbot.com"
})
browser . execute ( ' tb:intercept ' , {
" redirect " : " https://google.com " ,
" url " : " https://testingbot.com "
})
(( IJavaScriptExecutor ) driver ). ExecuteScript ( "tb:intercept" , "{\"redirect\":\"https://www.google.com\", \"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
Ruby
Python
PHP
Java
Node
C#
driver . execute_script ( "tb:intercept" , {
"redirect" : "https://www.google.com" ,
"url" : "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 -> executeScript ( "tb:intercept" , [
"redirect" => "https://www.google.com" ,
"url" => "https://testingbot.com" ,
]
]);
driver . execute_script ( "tb:intercept" , {
"redirect" : "https://www.google.com" ,
"url" : "https://testingbot.com"
})
browser . execute ( ' tb:intercept ' , {
" redirect " : " https://www.google.com " ,
" url " : " https://testingbot.com "
})
(( IJavaScriptExecutor ) driver ). ExecuteScript ( "tb:intercept" , "{\"redirect\":\"https://www.google.com\", \"url\": \"https://testingbot.com\"}}" );
Mock Response
Allows you to return a different response (body or headers).
Ruby
Python
PHP
Java
Node
C#
driver . execute_script ( "tb:intercept" , {
"response" : {
"statusCode" : 200 ,
"headers" : {
"x-a-header" : "Sample"
},
"body" : "Hello World"
},
"url" : "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 -> 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"
})
browser . execute_script ( " tb:intercept " , {
" response " : {
" statusCode " : 200 ,
" headers " : {
" x-a-header " : " Sample "
},
" body " : " Hello World "
},
" url " : " https://testingbot.com "
})
(( IJavaScriptExecutor ) driver ). ExecuteScript ( "tb:intercept" , "{\"response\":{\"statusCode\": 200, \"headers\": { \"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.
Ruby
Python
PHP
Java
Node
C#
driver . execute_script ( "tb:network" )
(( JavascriptExecutor ) driver ). executeScript ( "tb:network" );
$driver -> executeScript ( "tb:network" );
driver . execute_script ( "tb:network" )
browser . execute ( ' tb:network ' )
(( IJavaScriptExecutor ) driver ). ExecuteScript ( "tb:network" );