Skip to content

Commit

Permalink
Merge pull request #31 from yahman72/droid_utils
Browse files Browse the repository at this point in the history
Droid Utils: new file/folder handling methods
  • Loading branch information
jollychang committed Feb 12, 2015
2 parents 7b659bd + caa2031 commit 030c169
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/AppiumLibrary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class AppiumLibrary(
_WaitingKeywords,
_TouchKeywords,
_KeyeventKeywords,
_AndroidUtilsKeywords,
):
"""AppiumLibrary is a App testing library for Robot Framework.
Expand Down
2 changes: 2 additions & 0 deletions src/AppiumLibrary/keywords/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from _waiting import _WaitingKeywords
from _touch import _TouchKeywords
from _keyevent import _KeyeventKeywords
from _android_utils import _AndroidUtilsKeywords

__all__ = ["_LoggingKeywords",
"_RunOnFailureKeywords",
Expand All @@ -17,4 +18,5 @@
"_WaitingKeywords",
"_TouchKeywords",
"_KeyeventKeywords",
"_AndroidUtilsKeywords",
]
74 changes: 74 additions & 0 deletions src/AppiumLibrary/keywords/_android_utils.py
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)
60 changes: 60 additions & 0 deletions tests/keywords/test_androidUtils.py
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')
12 changes: 12 additions & 0 deletions tests/keywords/webdriverremotemock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import unittest
import mock
import base64

logger = logging.getLogger()
stream_handler = logging.StreamHandler(sys.stdout)
Expand All @@ -16,6 +17,7 @@ def __init__(self, command_executor='http://127.0.0.1:4444/wd/hub', desired_capa
self._appiumUrl = command_executor
self._desCapa = desired_capabilities
self._dead = False
self._myData = ''
#logger.debug(desired_capabilities)
for key in desired_capabilities:
self.assertNotEqual(desired_capabilities[key], None, 'Null value in desired capabilities')
Expand All @@ -34,6 +36,16 @@ def lock(self):
if self._dead:
raise RuntimeError('Application has been closed')

def pull_file(self, path, decode=False):
theFile = self._myData
if decode:
theFile = base64.b64decode(theFile)
return theFile

def push_file(self, path, data, encode=False):
if encode:
self._myData = base64.b64decode(data)
else:
self._myData = data


0 comments on commit 030c169

Please sign in to comment.