Skip to content
This repository was archived by the owner on Sep 27, 2021. It is now read-only.

Commit 9743cef

Browse files
committed
Stability and test improvements + dev docs
1 parent 6c1b258 commit 9743cef

File tree

6 files changed

+177
-25
lines changed

6 files changed

+177
-25
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,9 @@ parts/
77
src/robotframework_androidlibrary.egg-info/
88
features/
99
develop-eggs/robotframework-androidlibrary.egg-link
10+
.calabash_settings
11+
android-screenshot-*.png
12+
log.html
13+
output.xml
14+
report.html
15+
ApiDemos.apk

DEVELOPMENT.rst

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
========================================
2+
Hacking on robotframework-androidlibrary
3+
========================================
4+
5+
This is a guide on how to work on robotframework-androidlibrary itself, that is -
6+
if you want to use the library to test your own application, please consult
7+
README.rst
8+
9+
10+
Prerequisites
11+
=============
12+
13+
- Install the `Android SDK <http://developer.android.com/sdk/index.html>`_
14+
- Install calabash-android v0.1.0::
15+
16+
gem install --version '= 0.1.0' calabash-android
17+
18+
- Create a debug keystore::
19+
20+
$ANDROID_SDK/tools/android create project -n dummy_project_to_create_debug_keystore -t 8 -p dummy_project_to_create_debug_keystore -k what.ever -a whatever
21+
cd dummy_project_to_create_debug_keystore
22+
ant debug
23+
cd -
24+
25+
Development environment
26+
=======================
27+
28+
To get started, use the following commands::
29+
30+
git clone https://github.com/lovelysystems/robotframework-androidlibrary
31+
cd robotframework-androidlibrary/
32+
python bootstrap.py --distribute
33+
bin/buildout
34+
35+
Running tests
36+
=============
37+
38+
The library itself is tested using robotframework, to run the tests type::
39+
40+
export ANDROID_HOME=path/to/android/sdk
41+
bin/robotframework tests/
42+
43+
Optionally, the following parameters can be specified:
44+
45+
**Highest debug level**::
46+
47+
-L TRACE
48+
49+
**Show the android emulator when running tests**::
50+
51+
-v HEADLESS:False
52+

src/AndroidLibrary/__init__.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def _execute(self, args, **kwargs):
7373
logging.debug("$> %s", ' '.join(args))
7474
output = execute(args, **kwargs)
7575
logging.debug(output)
76+
return output
7677

7778
def start_emulator(self, avd_name, no_window=False):
7879
'''
@@ -81,21 +82,30 @@ def start_emulator(self, avd_name, no_window=False):
8182
`avd_name` Identifier of the Android Virtual Device, for valid values on your machine run "$ANDROID_HOME/tools/android list avd|grep Name`
8283
`no_window` Set to True to start the emulator without GUI, useful for headless environments.
8384
'''
84-
cmd = [self._emulator, '-avd', avd_name]
85+
args = [self._emulator, '-avd', avd_name]
8586

8687
if no_window:
87-
cmd.append('-no-window')
88+
args.append('-no-window')
89+
90+
logging.debug("$> %s", ' '.join(args))
8891

89-
self._emulator_proc = subprocess.Popen(cmd)
92+
self._emulator_proc = subprocess.Popen(args)
9093

9194
def stop_emulator(self):
9295
'''
9396
Halts a previously started Android Emulator.
9497
'''
98+
99+
if not hasattr(self, '_emulator_proc'):
100+
logging.warn("Could not stop Android Emulator: It was not started.")
101+
return
102+
95103
self._emulator_proc.terminate()
96104
self._emulator_proc.kill()
97105
self._emulator_proc.wait()
98106

107+
self._emulator_proc = None
108+
99109
def set_package_name(self, package_name):
100110
self._package_name = package_name
101111

@@ -109,10 +119,16 @@ def install_apk(self, test_apk_path, app_apk_path):
109119
`test_apk_path` Path to the Test.apk, usually at 'features/support/Test.apk'
110120
`app_apk_path` Path the the application you want to test
111121
'''
112-
self._execute([self._adb, "uninstall", "%s.test" % self._package_name])
113-
self._execute([self._adb, "uninstall", self._package_name])
114-
self._execute([self._adb, "install", "-r", test_apk_path])
115-
self._execute([self._adb, "install", "-r", app_apk_path])
122+
123+
def execute_and_output_does_not_contain_error(*args):
124+
output = self._execute(*args)
125+
assert 'Error' not in output, output
126+
return output
127+
128+
execute_and_output_does_not_contain_error([self._adb, "uninstall", "%s.test" % self._package_name])
129+
execute_and_output_does_not_contain_error([self._adb, "uninstall", self._package_name])
130+
execute_and_output_does_not_contain_error([self._adb, "install", "-r", test_apk_path])
131+
execute_and_output_does_not_contain_error([self._adb, "install", "-r", app_apk_path])
116132

117133
def wait_for_device(self):
118134
'''

tests/apidemos/__init__.txt

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
*** Settings ***
2+
3+
Resource variables.txt
4+
5+
Library AndroidLibrary
6+
Library OperatingSystem
7+
8+
Suite Setup Setup Suite
9+
Suite Teardown Stop Emulator
10+
11+
Documentation Tests the ApiDemos.apk that is included in the Android SDK
12+
13+
*** Keywords ***
14+
15+
Execute [Arguments] ${command}
16+
${rc} ${output} = Run And Return Rc And Output ${command}
17+
Should Be Equal As Integers ${rc} 0
18+
19+
Setup Suite
20+
21+
Android SDK Should Exist
22+
Update Android SDK
23+
Create Android Virtual Device
24+
25+
${HEADLESS_BOOL}= Convert To Boolean ${HEADLESS}
26+
Start Emulator ${EMULATOR_NAME} no_window=${HEADLESS_BOOL}
27+
28+
Pull ApiDemos.apk from Device
29+
Re-Sign ApiDemos.apk with Debug Keystore
30+
Build Instrumentation App
31+
32+
Wait Until Keyword Succeeds 1m 5s Install App
33+
34+
Stop Emulator
35+
36+
Android SDK Should Exist
37+
[Documentation] simple sanity check to see if %{ANDROID_HOME} was set correctly
38+
File Should Exist %{ANDROID_HOME}/tools/android
39+
40+
Update Android SDK
41+
Execute %{ANDROID_HOME}/tools/android update sdk -t android-${API_LEVEL} --no-ui
42+
43+
Create Android Virtual Device
44+
Execute echo "no" | %{ANDROID_HOME}/tools/android --silent create avd --name ${EMULATOR_NAME} --force -t android-${API_LEVEL}
45+
46+
Pull ApiDemos.apk from Device
47+
[Timeout] 120s
48+
Wait For Device
49+
Remove File ApiDemos.apk
50+
Execute %{ANDROID_HOME}/platform-tools/adb pull /data/app/ApiDemos.apk
51+
52+
Re-Sign ApiDemos.apk with Debug Keystore
53+
File Should Exist ApiDemos.apk
54+
Execute zip -d ApiDemos.apk META-INF/*
55+
Execute echo "android" | jarsigner -verbose -keystore $HOME/.android/debug.keystore ApiDemos.apk androiddebugkey
56+
57+
Build Instrumentation App
58+
Remove File .calabash_settings
59+
${settings}= Catenate
60+
... {
61+
... "activity_name": "com.example.android.apis.ApiDemos",
62+
... "api_level": "${API_LEVEL}",
63+
... "app_path": "%{PWD}/ApiDemos.apk",
64+
... "keystore_alias": "androiddebugkey",
65+
... "keystore_alias_password": "debug",
66+
... "keystore_location": "%{HOME}/.android/debug.keystore",
67+
... "keystore_password": "android",
68+
... "package_name": "com.example.android.apis"
69+
... }
70+
Set Package Name com.example.android.apis
71+
Create File .calabash_settings ${settings}
72+
Execute calabash-android build
73+
74+
Install App
75+
[Timeout] 120s
76+
Wait for Device
77+
78+
Install APK
79+
... ${EXECDIR}/features/support/Test.apk
80+
... ${EXECDIR}/ApiDemos.apk
81+

tests/apidemos/apidemos.txt

+8-18
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
11
*** Settings ***
22

3-
Library AndroidLibrary ${ANDROID_HOME}
3+
Resource variables.txt
44

5-
Suite Setup Setup Emulator
6-
Suite Teardown Stop Emulator
5+
Library AndroidLibrary
76

7+
Suite Setup Setup Suite
88
Test Setup Setup Test
99

10-
Documentation Tests the ApiDemos.apk that is included in the Android SDK
11-
...
12-
... See DEVELOPMENT.rst on the prerequisites of how to run this
10+
Test Timeout 2 minutes
1311

1412
*** Keywords ***
1513

16-
Setup Emulator
17-
Start Emulator my_funky_emulator
18-
19-
Set Package Name com.example.android.apis
20-
14+
Setup Suite
15+
${HEADLESS_BOOL}= Convert To Boolean ${HEADLESS}
16+
Start Emulator ${EMULATOR_NAME} no_window=${HEADLESS_BOOL}
2117
Wait For Device
22-
Install APK
23-
... ${APIDEMOS_PATH}/features/support/Test.apk
24-
... ${APIDEMOS_PATH}/ApiDemos.apk
25-
18+
Press Menu Button
2619

2720
Setup Test
28-
Wait For Device
29-
Press Menu Button
3021
Start Testserver
31-
3222
Wait Until Keyword Succeeds
3323
... 1min
3424
... 5sec

tests/apidemos/variables.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*** Variables ***
2+
3+
${EMULATOR_NAME}= robotframework-androidlibrary-emulator
4+
${API_LEVEL}= 8
5+
${SKIP_SETUP}= False
6+
${HEADLESS}= True
7+

0 commit comments

Comments
 (0)