Skip to content

Commit d7bdd7c

Browse files
daviandejleyba
authored andcommitted
JavaScript bindings for Chrome network emulation (#3624)
1 parent 3c200d7 commit d7bdd7c

1 file changed

Lines changed: 48 additions & 1 deletion

File tree

javascript/node/selenium-webdriver/chrome.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ const CHROMEDRIVER_EXE =
143143
* @enum {string}
144144
*/
145145
const Command = {
146-
LAUNCH_APP: 'launchApp'
146+
LAUNCH_APP: 'launchApp',
147+
GET_NETWORK_CONDITIONS: 'getNetworkConditions',
148+
SET_NETWORK_CONDITIONS: 'setNetworkConditions'
147149
};
148150

149151

@@ -169,6 +171,14 @@ function configureExecutor(executor) {
169171
Command.LAUNCH_APP,
170172
'POST',
171173
'/session/:sessionId/chromium/launch_app');
174+
executor.defineCommand(
175+
Command.GET_NETWORK_CONDITIONS,
176+
'GET',
177+
'/session/:sessionId/chromium/network_conditions');
178+
executor.defineCommand(
179+
Command.SET_NETWORK_CONDITIONS,
180+
'POST',
181+
'/session/:sessionId/chromium/network_conditions');
172182
}
173183

174184

@@ -727,6 +737,43 @@ class Driver extends webdriver.WebDriver {
727737
new command.Command(Command.LAUNCH_APP).setParameter('id', id),
728738
'Driver.launchApp()');
729739
}
740+
741+
/**
742+
* Schedules a command to get Chrome network emulation settings.
743+
* @return {!promise.Thenable<T>} A promise that will be resolved
744+
* when network emulation settings are retrievied.
745+
*/
746+
getNetworkConditions() {
747+
return this.schedule(
748+
new command.Command(Command.GET_NETWORK_CONDITIONS),
749+
'Driver.getNetworkConditions()');
750+
}
751+
752+
/**
753+
* Schedules a command to set Chrome network emulation settings.
754+
*
755+
* __Sample Usage:__
756+
*
757+
* driver.setNetworkConditions({
758+
* offline: false,
759+
* latency: 5, // Additional latency (ms).
760+
* download_throughput: 500 * 1024, // Maximal aggregated download throughput.
761+
* upload_throughput: 500 * 1024 // Maximal aggregated upload throughput.
762+
* });
763+
*
764+
* @param {Object} spec Defines the network conditions to set
765+
* @return {!promise.Thenable<void>} A promise that will be resolved
766+
* when network emulation settings are set.
767+
*/
768+
setNetworkConditions(spec) {
769+
if (!spec || typeof spec !== 'object') {
770+
throw TypeError('setNetworkConditions called with non-network-conditions parameter');
771+
}
772+
773+
return this.schedule(
774+
new command.Command(Command.SET_NETWORK_CONDITIONS).setParameter('network_conditions', spec),
775+
'Driver.setNetworkConditions(' + JSON.stringify(spec) + ')');
776+
}
730777
}
731778

732779

0 commit comments

Comments
 (0)