From bf25fa71be1ae33f0e050e11cccac8c6d6a78ffd Mon Sep 17 00:00:00 2001 From: daviande Date: Wed, 8 Mar 2017 14:34:14 -0800 Subject: [PATCH 1/2] JavaScript bindings for Chrome network emulation Update WebDriverJS to support the Chrome debugging protocol functionality for getting and setting network emulation settings. This functionality was added to ChromeDriver in March of 2015 (https://chromium.googlesource.com/chromium/src.git/+/12a55ff7369b35adb29cd28f5e3dd4ef923e3e6c), but hasn't been implemented in the JavaScript bindings. Example script using this functionality: var webdriver = require('selenium-webdriver'); var driver = new webdriver.Builder() .forBrowser('chrome') .build(); var latency = 5; var throughput = 500 * 1024; driver.setNetworkConditions(latency, throughput, throughput) .then(() => driver.getNetworkConditions()) .then(network => { console.log(network); driver.get('https://fast.com/'); }) // once selenium disconnects from remote debugging protocol, // emulation settings are discarded .then(() => driver.sleep(60000)); --- javascript/node/selenium-webdriver/chrome.js | 43 +++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/javascript/node/selenium-webdriver/chrome.js b/javascript/node/selenium-webdriver/chrome.js index 2dbc93351e049..a9013bcf9948e 100644 --- a/javascript/node/selenium-webdriver/chrome.js +++ b/javascript/node/selenium-webdriver/chrome.js @@ -143,7 +143,9 @@ const CHROMEDRIVER_EXE = * @enum {string} */ const Command = { - LAUNCH_APP: 'launchApp' + LAUNCH_APP: 'launchApp', + GET_NETWORK_CONDITIONS: 'getNetworkConditions', + SET_NETWORK_CONDITIONS: 'setNetworkConditions' }; @@ -169,6 +171,14 @@ function configureExecutor(executor) { Command.LAUNCH_APP, 'POST', '/session/:sessionId/chromium/launch_app'); + executor.defineCommand( + Command.GET_NETWORK_CONDITIONS, + 'GET', + '/session/:sessionId/chromium/network_conditions'); + executor.defineCommand( + Command.SET_NETWORK_CONDITIONS, + 'POST', + '/session/:sessionId/chromium/network_conditions'); } @@ -727,6 +737,37 @@ class Driver extends webdriver.WebDriver { new command.Command(Command.LAUNCH_APP).setParameter('id', id), 'Driver.launchApp()'); } + + /** + * Schedules a command to get Chrome network emulation settings. + * @return {!promise.Thenable} A promise that will be resolved + * when network emulation settings are retrievied. + */ + getNetworkConditions() { + return this.schedule( + new command.Command(Command.GET_NETWORK_CONDITIONS), + 'Driver.getNetworkConditions()'); + } + + /** + * Schedules a command to set Chrome network emulation settings. + * @param {number} latency Additional latency (ms). + * @param {number} download_throughput Maximal aggregated download throughput. + * @param {number} upload_throughput Maximal aggregated upload throughput. + * @return {!promise.Thenable} A promise that will be resolved + * when network emulation settings are set. + */ + setNetworkConditions(latency, download_throughput, upload_throughput) { + const params = { + 'offline': false, + 'latency': latency, + 'download_throughput': download_throughput, + 'upload_throughput': upload_throughput + }; + return this.schedule( + new command.Command(Command.SET_NETWORK_CONDITIONS).setParameter('network_conditions', params), + 'Driver.setNetworkConditions(' + latency + ', ' + download_throughput + ', ' + upload_throughput + ')'); + } } From 5711c9c19e6e28128c9b78abf6678e834a1014b3 Mon Sep 17 00:00:00 2001 From: daviande Date: Mon, 13 Mar 2017 14:45:53 -0700 Subject: [PATCH 2/2] Change setNetworkConditions to take a config object Change setNetworkConditions to take a config object, so that users don't need to remember the order of parameters. --- javascript/node/selenium-webdriver/chrome.js | 30 ++++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/javascript/node/selenium-webdriver/chrome.js b/javascript/node/selenium-webdriver/chrome.js index a9013bcf9948e..b1e8a29e4e752 100644 --- a/javascript/node/selenium-webdriver/chrome.js +++ b/javascript/node/selenium-webdriver/chrome.js @@ -751,22 +751,28 @@ class Driver extends webdriver.WebDriver { /** * Schedules a command to set Chrome network emulation settings. - * @param {number} latency Additional latency (ms). - * @param {number} download_throughput Maximal aggregated download throughput. - * @param {number} upload_throughput Maximal aggregated upload throughput. + * + * __Sample Usage:__ + * + * driver.setNetworkConditions({ + * offline: false, + * latency: 5, // Additional latency (ms). + * download_throughput: 500 * 1024, // Maximal aggregated download throughput. + * upload_throughput: 500 * 1024 // Maximal aggregated upload throughput. + * }); + * + * @param {Object} spec Defines the network conditions to set * @return {!promise.Thenable} A promise that will be resolved * when network emulation settings are set. */ - setNetworkConditions(latency, download_throughput, upload_throughput) { - const params = { - 'offline': false, - 'latency': latency, - 'download_throughput': download_throughput, - 'upload_throughput': upload_throughput - }; + setNetworkConditions(spec) { + if (!spec || typeof spec !== 'object') { + throw TypeError('setNetworkConditions called with non-network-conditions parameter'); + } + return this.schedule( - new command.Command(Command.SET_NETWORK_CONDITIONS).setParameter('network_conditions', params), - 'Driver.setNetworkConditions(' + latency + ', ' + download_throughput + ', ' + upload_throughput + ')'); + new command.Command(Command.SET_NETWORK_CONDITIONS).setParameter('network_conditions', spec), + 'Driver.setNetworkConditions(' + JSON.stringify(spec) + ')'); } }