Skip to content

Commit e4b33f3

Browse files
jimvmandreastt
authored andcommitted
rb: add HasNetworkConnection driver extension
This is related to this issue: https://code.google.com/p/selenium/issues/detail?id=7315 Signed-off-by: Andreas Tolfsen <[email protected]>
1 parent 7b1c21e commit e4b33f3

File tree

7 files changed

+112
-0
lines changed

7 files changed

+112
-0
lines changed

rb/lib/selenium/webdriver/android/bridge.rb

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def driver_extensions
3434
DriverExtensions::HasInputDevices,
3535
DriverExtensions::HasWebStorage,
3636
DriverExtensions::HasLocation,
37+
DriverExtensions::HasNetworkConnection,
3738
DriverExtensions::HasTouchScreen
3839
]
3940
end

rb/lib/selenium/webdriver/common.rb

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
require 'selenium/webdriver/common/driver_extensions/has_session_id'
3535
require 'selenium/webdriver/common/driver_extensions/has_touch_screen'
3636
require 'selenium/webdriver/common/driver_extensions/has_remote_status'
37+
require 'selenium/webdriver/common/driver_extensions/has_network_connection'
3738
require 'selenium/webdriver/common/driver_extensions/uploads_files'
3839
require 'selenium/webdriver/common/keys'
3940
require 'selenium/webdriver/common/bridge_helper'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module Selenium
2+
module WebDriver
3+
module DriverExtensions
4+
module HasNetworkConnection
5+
def network_connection_type
6+
connection_value = @bridge.getNetworkConnection
7+
8+
# Convert connection value to type. In case the connection type is
9+
# not recognized return the connection value.
10+
case connection_value
11+
when 1
12+
:airplane_mode
13+
when 2
14+
:wifi
15+
when 4
16+
:data
17+
when 6
18+
:all
19+
when 0
20+
:none
21+
else
22+
connection_value
23+
end
24+
end
25+
26+
def network_connection_type=(connection_type)
27+
# convert connection type to value
28+
connection_value = case connection_type
29+
when :airplane_mode
30+
1
31+
when :wifi
32+
2
33+
when :data
34+
4
35+
when :all
36+
6
37+
when :none
38+
0
39+
end
40+
41+
@bridge.setNetworkConnection connection_value
42+
end
43+
end
44+
end
45+
end
46+
end

rb/lib/selenium/webdriver/remote/bridge.rb

+8
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,14 @@ def setLocation(lat, lon, alt)
321321
execute :setLocation, {}, :location => loc
322322
end
323323

324+
def getNetworkConnection
325+
execute :getNetworkConnection
326+
end
327+
328+
def setNetworkConnection(type)
329+
execute :setNetworkConnection, {}, :type => type
330+
end
331+
324332
#
325333
# javascript execution
326334
#

rb/lib/selenium/webdriver/remote/commands.rb

+3
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ class Selenium::WebDriver::Remote::Bridge
143143
command :getAppCacheStatus, :get, "session/:session_id/application_cache/status"
144144
command :clearAppCache, :delete, "session/:session_id/application_cache/clear"
145145

146+
command :getNetworkConnection, :get, "session/:session_id/network_connection"
147+
command :setNetworkConnection, :post, "session/:session_id/network_connection"
148+
146149
command :getLocalStorageItem, :get, "session/:session_id/local_storage/key/:key"
147150
command :removeLocalStorageItem, :delete, "session/:session_id/local_storage/key/:key"
148151
command :getLocalStorageKeys, :get, "session/:session_id/local_storage"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require File.expand_path("../spec_helper", __FILE__)
2+
3+
module Selenium::WebDriver::DriverExtensions
4+
describe HasNetworkConnection do
5+
compliant_on :browser => nil do
6+
it "can return the network connection type" do
7+
expect(driver.network_connection_type).to eq :all
8+
end
9+
10+
it "can set the network connection type" do
11+
expect { driver.network_connection_type = :airplane_mode }.to
12+
change { driver.network_connection_type }.from(:all).to(:airplane_mode)
13+
end
14+
end
15+
end
16+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require File.expand_path("../../../spec_helper", __FILE__)
2+
3+
module Selenium
4+
module WebDriver
5+
module DriverExtensions
6+
describe HasNetworkConnection do
7+
class FakeDriver
8+
include HasNetworkConnection
9+
end
10+
11+
let(:driver) { FakeDriver.new }
12+
13+
describe "#network_connection" do
14+
it "returns the correct connection type" do
15+
@bridge.stub(:getNetworkConnection) { 1 }
16+
17+
expect(driver.network_connection_type).to eq :airplane_mode
18+
end
19+
20+
it "returns an unknown connection value" do
21+
@bridge.stub(:getNetworkConnection) { 5 }
22+
23+
expect(driver.network_connection_type).to eq 5
24+
end
25+
end
26+
27+
describe "#network_connection=" do
28+
it "sends out the correct connection value" do
29+
expect(@bridge).to receive(:setNetworkConnection).with(1)
30+
31+
driver.network_connection_type = :airplane_mode
32+
end
33+
end
34+
end
35+
end
36+
end
37+
end

0 commit comments

Comments
 (0)