-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from yahman72/droid_utils
Droid Utils: new file/folder handling methods
- Loading branch information
Showing
5 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# -*- coding: utf-8 -*- | ||
import base64 | ||
|
||
from keywordgroup import KeywordGroup | ||
from appium.webdriver.connectiontype import ConnectionType | ||
|
||
class _AndroidUtilsKeywords(KeywordGroup): | ||
|
||
# Public | ||
def get_network_connection_status(self): | ||
"""Returns an integer bitmask specifying the network connection type. | ||
Android only. | ||
See `set network connection status` for more details. | ||
""" | ||
driver = self._current_application() | ||
return driver.network_connection | ||
|
||
def set_network_connection_status(self, connectionStatus): | ||
"""Sets the network connection Status. | ||
Android only. | ||
Possible values: | ||
Value |(Alias) | Data | Wifi | Airplane Mode | ||
------------------------------------------------- | ||
0 |(None) | 0 | 0 | 0 | ||
1 |(Airplane Mode) | 0 | 0 | 1 | ||
2 |(Wifi only) | 0 | 1 | 0 | ||
4 |(Data only) | 1 | 0 | 0 | ||
6 |(All network on) | 1 | 1 | 0 | ||
""" | ||
driver = self._current_application() | ||
connType = ConnectionType(int(connectionStatus)) | ||
return driver.set_network_connection(connType) | ||
|
||
def pull_file(self, path, decode=False): | ||
"""Retrieves the file at `path` and return it's content. | ||
Android only. | ||
:Args: | ||
- path - the path to the file on the device | ||
- decode - True/False decode the data (base64) before returning it (default=False) | ||
""" | ||
driver = self._current_application() | ||
theFile = driver.pull_file(path) | ||
if decode: | ||
theFile = base64.b64decode(theFile) | ||
return theFile | ||
|
||
def pull_folder(self, path, decode=False): | ||
"""Retrieves a folder at `path`. Returns the folder's contents zipped. | ||
Android only. | ||
:Args: | ||
- path - the path to the folder on the device | ||
- decode - True/False decode the data (base64) before returning it (default=False) | ||
""" | ||
driver = self._current_application() | ||
theFolder = driver.pull_folder(path) | ||
if decode: | ||
theFolder = base64.b64decode(theFolder) | ||
return theFolder | ||
|
||
def push_file(self, path, data, encode=False): | ||
"""Puts the data in the file specified as `path`. | ||
Android only. | ||
:Args: | ||
- path - the path on the device | ||
- data - data to be written to the file | ||
- encode - True/False encode the data as base64 before writing it to the file (default=False) | ||
""" | ||
driver = self._current_application() | ||
if encode: | ||
data = base64.b64encode(data) | ||
driver.push_file(path, data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import logging | ||
import sys | ||
import unittest | ||
import appium | ||
import mock | ||
|
||
from AppiumLibrary.keywords import _ApplicationManagementKeywords | ||
from webdriverremotemock import WebdriverRemoteMock | ||
from AppiumLibrary.keywords import _AndroidUtilsKeywords | ||
|
||
|
||
logger = logging.getLogger() | ||
stream_handler = logging.StreamHandler(sys.stdout) | ||
logger.addHandler(stream_handler) | ||
stream_handler = logging.StreamHandler(sys.stderr) | ||
logger.addHandler(stream_handler) | ||
|
||
class AndroidUtilsTests(unittest.TestCase): | ||
am=None | ||
au=None | ||
|
||
def tearDown(self): | ||
self.am.close_all_applications() | ||
|
||
def setUp(self): | ||
self.am = _ApplicationManagementKeywords() | ||
self.am._debug = mock.Mock() | ||
# Uncomment to use Mock / Comment to test against real appium instance | ||
appium.webdriver.Remote = WebdriverRemoteMock | ||
# log debug from _ApplicationManagementKeywords to console | ||
self.am._debug = logger.debug | ||
self.am.open_application('http://10.1.160.124:4724/wd/hub', alias='MsB1', deviceName='MsB1', udid='d81e91ba', platformVersion='4.4', appPackage='com.android.contacts', platformName='Android', appActivity='.activities.DialtactsActivity') | ||
self.au = _AndroidUtilsKeywords() | ||
self.au._current_application = self.am._current_application | ||
|
||
def test_set_network_connection_status(self): | ||
self.au.set_network_connection_status(4) | ||
|
||
def test_get_network_connection_status(self): | ||
self.au.get_network_connection_status() | ||
|
||
def test_push_pull_file(self): | ||
myFile='VGhpcyBpcyBteUZpbGUgYXMgYmFzZTY0' #'This is myFile as base64' | ||
#logger.debug('Pushing myFile as base64: %s' % (myFile, )) | ||
self.au.push_file('/storage/sdcard0/foo.txt', myFile) | ||
myFile=self.au.pull_file('/storage/sdcard0/foo.txt') | ||
#logger.debug('Pulled myFile as base64: %s' % (myFile, )) | ||
self.assertRegexpMatches(myFile, 'VGhpcyBpcyBteUZpbGUgYXMgYmFzZTY0') | ||
myFile=self.au.pull_file('/storage/sdcard0/foo.txt', decode=True) | ||
#logger.debug('Pulled myFile as Text: %s' % (myFile, )) | ||
self.assertRegexpMatches(myFile, 'as base64') | ||
myFile='This is myFile as Text' | ||
#logger.debug('Pushing myFile as Text: %s' % (myFile, )) | ||
myFile=self.au.push_file('/storage/sdcard0/foo.txt', myFile, encode=True) | ||
myFile=self.au.pull_file('/storage/sdcard0/foo.txt') | ||
#logger.debug('Pulled myFile as base64: %s' % (myFile, )) | ||
self.assertRegexpMatches(myFile, 'VGhpcyBpcyBteUZpbGUgYXMgVGV4dA==') | ||
myFile=self.au.pull_file('/storage/sdcard0/foo.txt', decode=True) | ||
#logger.debug('Pulled myFile as Text: %s' % (myFile, )) | ||
self.assertRegexpMatches(myFile, 'as Text') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters