Skip to content

Commit 74f599d

Browse files
authored
chore: Fix iOS app management functional tests (appium#575)
* Added sleep to wait the app has gone * Upgrade AndroidSDK to 30 from 27 * Added sleep to ios tc * Fix android activities test * Revert android sdk ver * Used timer instead of fixed wait time * Created wait_for * Update test/functional/test_helper.py * review comments * review comments * Extend callable type * fix * review comment * review comment * review comment * fix comment
1 parent 70048fc commit 74f599d

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

test/functional/android/activities_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def test_start_activity_other_app(self) -> None:
3232
self.driver.start_activity(APIDEMO_PKG_NAME, ".ApiDemos")
3333
self._assert_activity_contains('Demos')
3434

35-
self.driver.start_activity("com.android.calculator2", ".Calculator")
36-
self._assert_activity_contains('Calculator')
35+
self.driver.start_activity("com.google.android.deskclock", "com.android.deskclock.DeskClock")
36+
self._assert_activity_contains('Clock')
3737

3838
def _assert_activity_contains(self, activity: str) -> None:
3939
current = self.driver.current_activity

test/functional/ios/applications_tests.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from appium.webdriver.applicationstate import ApplicationState
1616
from test.functional.ios.helper.test_helper import BaseTestCase
17+
from test.functional.test_helper import wait_for_condition
1718

1819
from .helper import desired_capabilities
1920

@@ -25,6 +26,8 @@ def test_app_management(self) -> None:
2526
return
2627
assert self.driver.query_app_state(desired_capabilities.BUNDLE_ID) == ApplicationState.RUNNING_IN_FOREGROUND
2728
self.driver.background_app(-1)
28-
assert self.driver.query_app_state(desired_capabilities.BUNDLE_ID) < ApplicationState.RUNNING_IN_FOREGROUND
29+
assert wait_for_condition(
30+
lambda: self.driver.query_app_state(desired_capabilities.BUNDLE_ID) < ApplicationState.RUNNING_IN_FOREGROUND
31+
), "The app didn't go to background."
2932
self.driver.activate_app(desired_capabilities.BUNDLE_ID)
3033
assert self.driver.query_app_state(desired_capabilities.BUNDLE_ID) == ApplicationState.RUNNING_IN_FOREGROUND

test/functional/ios/webdriver_tests.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from appium import webdriver
2121
from appium.webdriver.applicationstate import ApplicationState
2222
from test.functional.ios.helper.test_helper import BaseTestCase
23-
from test.functional.test_helper import get_available_from_port_range
23+
from test.functional.test_helper import get_available_from_port_range, wait_for_condition
2424

2525
from ..test_helper import is_ci
2626
from .helper import desired_capabilities
@@ -60,7 +60,11 @@ def test_app_management(self) -> None:
6060
return
6161
assert self.driver.query_app_state(desired_capabilities.BUNDLE_ID) == ApplicationState.RUNNING_IN_FOREGROUND
6262
self.driver.background_app(-1)
63-
assert self.driver.query_app_state(desired_capabilities.BUNDLE_ID) < ApplicationState.RUNNING_IN_FOREGROUND
63+
print(self.driver.query_app_state(desired_capabilities.BUNDLE_ID) < ApplicationState.RUNNING_IN_FOREGROUND)
64+
assert wait_for_condition(
65+
lambda: self.driver.query_app_state(desired_capabilities.BUNDLE_ID)
66+
< ApplicationState.RUNNING_IN_FOREGROUND,
67+
), "The app didn't go to background."
6468
self.driver.activate_app(desired_capabilities.BUNDLE_ID)
6569
assert self.driver.query_app_state(desired_capabilities.BUNDLE_ID) == ApplicationState.RUNNING_IN_FOREGROUND
6670

test/functional/test_helper.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import os
22
import socket
3+
import time
4+
from time import sleep
5+
from typing import Any, Callable
36

47

58
class NoAvailablePortError(Exception):
@@ -37,3 +40,32 @@ def is_ci() -> bool:
3740
`True` if current executions is on CI
3841
"""
3942
return os.getenv('CI', 'false') == 'true'
43+
44+
45+
def wait_for_condition(method: Callable, timeout_sec: float = 5, interval_sec: float = 1) -> Any:
46+
"""Wait while `method` returns the built-in objects considered false
47+
48+
https://docs.python.org/3/library/stdtypes.html#truth-value-testing
49+
50+
Args:
51+
method: The target method to be waited
52+
timeout: The timeout to be waited (sec.)
53+
interval_sec: The interval for wait (sec.)
54+
55+
Returns:
56+
Any: value which `method` returns
57+
58+
Raises:
59+
ValueError: When interval isn't more than 0
60+
61+
"""
62+
if interval_sec < 0:
63+
raise ValueError('interval_sec needs to be not less than 0')
64+
65+
started = time.time()
66+
while time.time() - started <= timeout_sec:
67+
result = method()
68+
if result:
69+
break
70+
sleep(interval_sec)
71+
return result

0 commit comments

Comments
 (0)