Skip to content

Commit

Permalink
Support getting/setting network conditions in Chrome
Browse files Browse the repository at this point in the history
Related to #3450
  • Loading branch information
p0deje committed Dec 6, 2017
1 parent 028aa74 commit 1103cb7
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 2 deletions.
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/chrome.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

require 'net/http'

require 'selenium/webdriver/chrome/bridge'
require 'selenium/webdriver/chrome/service'
require 'selenium/webdriver/chrome/driver'
require 'selenium/webdriver/chrome/profile'
Expand Down
43 changes: 43 additions & 0 deletions rb/lib/selenium/webdriver/chrome/bridge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

module Selenium
module WebDriver
module Chrome
module Bridge

COMMANDS = {
get_network_conditions: [:get, '/session/:session_id/chromium/network_conditions'.freeze],
set_network_conditions: [:post, '/session/:session_id/chromium/network_conditions'.freeze]
}.freeze

def commands(command)
COMMANDS[command] || super
end

def network_conditions
execute :get_network_conditions
end

def network_conditions=(conditions)
execute :set_network_conditions, {}, {network_conditions: conditions}
end

end # Bridge
end # Chrome
end # WebDriver
end # Selenium
3 changes: 3 additions & 0 deletions rb/lib/selenium/webdriver/chrome/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Chrome
#

class Driver < WebDriver::Driver
include DriverExtensions::HasNetworkConditions
include DriverExtensions::HasTouchScreen
include DriverExtensions::HasWebStorage
include DriverExtensions::TakesScreenshot
Expand All @@ -44,6 +45,8 @@ def initialize(opts = {})

listener = opts.delete(:listener)
@bridge = Remote::Bridge.handshake(opts)
@bridge.extend Bridge

super(@bridge, listener: listener)
end

Expand Down
1 change: 1 addition & 0 deletions rb/lib/selenium/webdriver/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
require 'selenium/webdriver/common/driver_extensions/has_session_id'
require 'selenium/webdriver/common/driver_extensions/has_touch_screen'
require 'selenium/webdriver/common/driver_extensions/has_remote_status'
require 'selenium/webdriver/common/driver_extensions/has_network_conditions'
require 'selenium/webdriver/common/driver_extensions/has_network_connection'
require 'selenium/webdriver/common/driver_extensions/uploads_files'
require 'selenium/webdriver/common/driver_extensions/has_addons'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

module Selenium
module WebDriver
module DriverExtensions
module HasNetworkConditions

#
# Returns network conditions.
#
# @return [Hash]
#

def network_conditions
@bridge.network_conditions
end

#
# Sets network conditions
#
# @param [Hash] conditions
# @option conditions [Integer] :latency
# @option conditions [Integer] :throughput
# @option conditions [Boolean] :offline
#

def network_conditions=(conditions)
@bridge.network_conditions = conditions
end

end # HasNetworkConditions
end # DriverExtensions
end # WebDriver
end # Selenium
14 changes: 12 additions & 2 deletions rb/spec/integration/selenium/webdriver/chrome/driver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Selenium
module WebDriver
module Chrome
describe Driver, only: {driver: :chrome} do
it 'should accept an array of custom command line arguments' do
it 'accepts an array of custom command line arguments' do
create_driver!(args: ['--user-agent=foo;bar']) do |driver|
driver.navigate.to url_for('click_jacker.html')

Expand All @@ -30,9 +30,19 @@ module Chrome
end
end

it 'should raise ArgumentError if :args is not an Array' do
it 'raises ArgumentError if :args is not an Array' do
expect { create_driver!(args: '--foo') }.to raise_error(ArgumentError, ':args must be an Array of Strings')
end

it 'gets and sets network conditions' do
driver.network_conditions = {offline: false, latency: 56, throughput: 789}
expect(driver.network_conditions).to eq(
'offline' => false,
'latency' => 56,
'download_throughput' => 789,
'upload_throughput' => 789
)
end
end
end # Chrome
end # WebDriver
Expand Down

0 comments on commit 1103cb7

Please sign in to comment.