Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions appium/webdriver/applicationstate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python

# Licensed 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.


class ApplicationState(object):
NOT_INSTALLED = 0
NOT_RUNNING = 1
RUNNING_IN_BACKGROUND_SUSPENDED = 2
RUNNING_IN_BACKGROUND = 3
RUNNING_IN_FOREGROUND = 4
3 changes: 3 additions & 0 deletions appium/webdriver/mobilecommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class MobileCommand(object):
IS_APP_INSTALLED = 'isAppInstalled'
INSTALL_APP = 'installApp'
REMOVE_APP = 'removeApp'
TERMINATE_APP = 'terminateApp'
ACTIVATE_APP = 'activateApp'
QUERY_APP_STATE = 'queryAppState'
LAUNCH_APP = 'launchApp'
CLOSE_APP = 'closeApp'
END_TEST_COVERAGE = 'endTestCoverage'
Expand Down
80 changes: 78 additions & 2 deletions appium/webdriver/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,27 +708,49 @@ def is_app_installed(self, bundle_id):
}
return self.execute(Command.IS_APP_INSTALLED, data)['value']

def install_app(self, app_path):
def install_app(self, app_path, **options):
"""Install the application found at `app_path` on the device.

:Args:
- app_path - the local or remote path to the application to install
- options - the possible installation options.
The following options are available for Android:
`replace`: whether to reinstall/upgrade the package if it is
already present on the device under test. True by default
`timeout`: how much time to wait for the installation to complete.
60000ms by default.
`allowTestPackages`: whether to allow installation of packages marked
as test in the manifest. False by default
`useSdcard`: whether to use the SD card to install the app. False
by default
`grantPermissions`: whether to automatically grant application permissions
on Android 6+ after the installation completes. False by default
"""
data = {
'appPath': app_path,
}
if options:
data.update({'options': options})
self.execute(Command.INSTALL_APP, data)
return self

def remove_app(self, app_id):
def remove_app(self, app_id, **options):
"""Remove the specified application from the device.

:Args:
- app_id - the application id to be removed
- options - the possible removal options.
The following options are available for Android:
`keepData`: whether to keep application data and caches after it is uninstalled.
False by default
`timeout`: how much time to wait for the uninstall to complete.
20000ms by default.
"""
data = {
'appId': app_id,
}
if options:
data.update({'options': options})
self.execute(Command.REMOVE_APP, data)
return self

Expand All @@ -745,6 +767,54 @@ def close_app(self):
self.execute(Command.CLOSE_APP)
return self

def terminate_app(self, app_id, **options):
"""Terminates the application if it is running.

:Args:
- app_id - the application id to be terminates
- options - the possible termination options.
The following options are available for Android:
`timeout`: how much time to wait for the uninstall to complete.
500ms by default.

:return: True if the app has been successfully terminated
"""
data = {
'appId': app_id,
}
if options:
data.update({'options': options})
return self.execute(Command.TERMINATE_APP, data)['value']

def activate_app(self, app_id):
"""Activates the application if it is not running
or is running in the background.

:Args:
- app_id - the application id to be activated

:return: self instance for chaining
"""
data = {
'appId': app_id,
}
self.execute(Command.ACTIVATE_APP, data)
return self

def query_app_state(self, app_id):
"""Queries the state of the application.

:Args:
- app_id - the application id to be queried

:return: One of possible application state constants. See ApplicationState
class for more details.
"""
data = {
'appId': app_id,
}
return self.execute(Command.QUERY_APP_STATE, data)['value']

def start_activity(self, app_package, app_activity, **opts):
"""Opens an arbitrary activity during a test. If the activity belongs to
another application, that application is started and the activity is opened.
Expand Down Expand Up @@ -1094,6 +1164,12 @@ def _addCommands(self):
('POST', '/session/$sessionId/appium/device/install_app')
self.command_executor._commands[Command.REMOVE_APP] = \
('POST', '/session/$sessionId/appium/device/remove_app')
self.command_executor._commands[Command.TERMINATE_APP] = \
('POST', '/session/$sessionId/appium/device/terminate_app')
self.command_executor._commands[Command.ACTIVATE_APP] = \
('POST', '/session/$sessionId/appium/device/activate_app')
self.command_executor._commands[Command.QUERY_APP_STATE] = \
('GET', '/session/$sessionId/appium/device/app_state')
self.command_executor._commands[Command.START_ACTIVITY] = \
('POST', '/session/$sessionId/appium/device/start_activity')
self.command_executor._commands[Command.LAUNCH_APP] = \
Expand Down
12 changes: 12 additions & 0 deletions test/functional/android/appium_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from time import sleep
from dateutil.parser import parse

from webdriver.applicationstate import ApplicationState
from selenium.common.exceptions import NoSuchElementException

from appium import webdriver
Expand Down Expand Up @@ -49,6 +50,17 @@ def test_screen_record(self):
result = self.driver.stop_recording_screen()
self.assertTrue(len(result) > 0)

def test_app_management(self):
app_id = self.driver.current_package
self.assertEqual(self.driver.query_app_state(app_id),
ApplicationState.RUNNING_IN_FOREGROUND)
self.driver.background_app(-1)
self.assertTrue(self.driver.query_app_state(app_id) <
ApplicationState.RUNNING_IN_FOREGROUND)
self.driver.activate_app(app_id)
self.assertEqual(self.driver.query_app_state(app_id),
ApplicationState.RUNNING_IN_FOREGROUND)

def test_lock(self):
self.driver.lock(-1)
try:
Expand Down
15 changes: 15 additions & 0 deletions test/functional/ios/appium_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import unittest
from time import sleep

from webdriver.applicationstate import ApplicationState
from appium import webdriver
import desired_capabilities

Expand All @@ -41,6 +42,20 @@ def test_screen_record(self):
result = self.driver.stop_recording_screen()
self.assertTrue(len(result) > 0)

def test_app_management(self):
# this only works in Xcode9+
if float(desired_capabilities.get_desired_capabilities(
desired_capabilities.BUNDLE_ID)['platformVersion']) < 11:
return
self.assertEqual(self.driver.query_app_state(desired_capabilities.BUNDLE_ID),
ApplicationState.RUNNING_IN_FOREGROUND)
self.driver.background_app(-1)
self.assertTrue(self.driver.query_app_state(desired_capabilities.BUNDLE_ID) <
ApplicationState.RUNNING_IN_FOREGROUND)
self.driver.activate_app(desired_capabilities.BUNDLE_ID)
self.assertEqual(self.driver.query_app_state(desired_capabilities.BUNDLE_ID),
ApplicationState.RUNNING_IN_FOREGROUND)

def test_shake(self):
# what can we assert about this?
self.driver.shake()
Expand Down
1 change: 1 addition & 0 deletions test/functional/ios/desired_capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
os.path.join(os.path.dirname(__file__), p)
)

BUNDLE_ID = 'com.example.apple-samplecode.UICatalog'

def get_desired_capabilities(app):
desired_caps = {
Expand Down