Skip to content

Commit 0a48aee

Browse files
committed
Merge pull request #37 from appium/isaac-networkconnection
Add network connection methods
2 parents 0fdaabf + 0de2906 commit 0a48aee

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

appium/webdriver/mobilecommand.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ class MobileCommand(object):
4444
SHAKE = 'shake'
4545
RESET = 'reset'
4646
HIDE_KEYBOARD = 'hideKeyboard'
47+
GET_NETWORK_CONNECTION = 'getNetworkConnection'
48+
SET_NETWORK_CONNECTION = 'setNetworkConnection'

appium/webdriver/webdriver.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from selenium import webdriver
1616

17+
from .connectiontype import ConnectionType
1718
from .mobilecommand import MobileCommand as Command
1819
from .errorhandler import MobileErrorHandler
1920
from .switch_to import MobileSwitchTo
@@ -569,6 +570,36 @@ def open_notifications(self):
569570
self.execute(Command.OPEN_NOTIFICATIONS, {})
570571
return self
571572

573+
@property
574+
def network_connection(self):
575+
"""Returns an integer bitmask specifying the network connection type.
576+
Android only.
577+
Possible values are available through the enumeration `appium.webdriver.ConnectionType`
578+
"""
579+
return self.execute(Command.GET_NETWORK_CONNECTION, {})['value']
580+
581+
def set_network_connection(self, connectionType):
582+
"""Sets the network connection type. Android only.
583+
Possible values:
584+
Value (Alias) | Data | Wifi | Airplane Mode
585+
-------------------------------------------------
586+
0 (None) | 0 | 0 | 0
587+
1 (Airplane Mode) | 0 | 0 | 1
588+
2 (Wifi only) | 0 | 1 | 0
589+
4 (Data only) | 1 | 0 | 0
590+
6 (All network on) | 1 | 1 | 0
591+
These are available through the enumeration `appium.webdriver.ConnectionType`
592+
593+
:Args:
594+
- connectionType - a member of the enum appium.webdriver.ConnectionType
595+
"""
596+
data = {
597+
'parameters': {
598+
'type': connectionType.value
599+
}
600+
}
601+
return self.execute(Command.SET_NETWORK_CONNECTION, data)['value']
602+
572603
def _addCommands(self):
573604
self.command_executor._commands[Command.CONTEXTS] = \
574605
('GET', '/session/$sessionId/contexts')
@@ -625,6 +656,10 @@ def _addCommands(self):
625656
('POST', '/session/$sessionId/appium/device/hide_keyboard')
626657
self.command_executor._commands[Command.OPEN_NOTIFICATIONS] = \
627658
('POST', '/session/$sessionId/appium/device/open_notifications')
659+
self.command_executor._commands[Command.GET_NETWORK_CONNECTION] = \
660+
('GET', '/session/$sessionId/network_connection')
661+
self.command_executor._commands[Command.SET_NETWORK_CONNECTION] = \
662+
('POST', '/session/$sessionId/network_connection')
628663

629664

630665
# monkeypatched method for WebElement

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@
5050
'Topic :: Software Development :: Quality Assurance',
5151
'Topic :: Software Development :: Testing'
5252
],
53-
install_requires=['selenium>=2.41.0']
53+
install_requires=['selenium>=2.41.0', 'enum34']
5454
)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
import unittest
17+
18+
19+
from appium import webdriver
20+
from appium.webdriver.connectiontype import ConnectionType
21+
import desired_capabilities
22+
23+
24+
# the emulator is sometimes slow and needs time to think
25+
SLEEPY_TIME = 1
26+
27+
28+
class NetworkConnectionTests(unittest.TestCase):
29+
def setUp(self):
30+
desired_caps = desired_capabilities.get_desired_capabilities('ApiDemos-debug.apk')
31+
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
32+
33+
def tearDown(self):
34+
self.driver.quit()
35+
36+
37+
def test_get_network_connection(self):
38+
nc = self.driver.network_connection
39+
self.assertIsInstance(nc, int)
40+
41+
def test_set_network_connection(self):
42+
nc = self.driver.set_network_connection(ConnectionType.DATA_ONLY)
43+
self.assertIsInstance(nc, int)
44+
self.assertEqual(nc, ConnectionType.DATA_ONLY.value)
45+
46+
47+
if __name__ == "__main__":
48+
suite = unittest.TestLoader().loadTestsFromTestCase(NetworkConnectionTests)
49+
unittest.TextTestRunner(verbosity=2).run(suite)

0 commit comments

Comments
 (0)