forked from bitbar/test-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitbar_android.py
More file actions
180 lines (147 loc) · 7.12 KB
/
bitbar_android.py
File metadata and controls
180 lines (147 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#
# For help on setting up your machine and configuring this TestScript go to
# http://docs.bitbar.com/testing/appium/
#
import os
import unittest
import subprocess
from time import sleep
from appium import webdriver
from device_finder import DeviceFinder
from selenium.common.exceptions import WebDriverException
from bitbar_utils import BitbarUtils
class BitbarAndroid(unittest.TestCase):
def setUp(self):
#
# IMPORTANT: Set the following parameters.
# You can set the parameters outside the script with environment
# variables.
# If env var is not set the string after or is used.
#
bitbar_url = os.environ.get('BITBAR_URL') or \
"https://cloud.bitbar.com"
appium_url = os.environ.get('BITBAR_APPIUM_URL') or \
'https://appium.bitbar.com/wd/hub'
bitbar_apiKey = os.environ.get('BITBAR_APIKEY') or ""
bitbar_project_name = os.environ.get('BITBAR_PROJECT') or \
"Android sample project"
bitbar_testrun_name = os.environ.get('BITBAR_TESTRUN') or \
"My testrun"
bitbar_app = os.environ.get('BITBAR_APP') or ""
app_package = os.environ.get('BITBAR_APP_PACKAGE') or \
'com.bitbar.testdroid'
app_activity = os.environ.get('BITBAR_ACTIVITY') or \
'.BitbarSampleApplicationActivity'
new_command_timeout = os.environ.get('BITBAR_CMD_TIMEOUT') or '60'
bitbar_test_timeout = os.environ.get('BITBAR_TEST_TIMEOUT') or '600'
bitbar_find_device = os.environ.get('BITBAR_FINDDEVICE') or True
self.screenshot_dir = os.environ.get('BITBAR_SCREENSHOTS') or \
os.getcwd() + "/screenshots"
self.screenshot_count = 1
# Options to select device
# 1) Set environment variable BITBAR_DEVICE
# 2) Set device name to this python script
# 3) Do not set #1 and #2 and let DeviceFinder to find free device for
# you
bitbar_device = os.environ.get('BITBAR_DEVICE') or ""
deviceFinder = DeviceFinder(url=bitbar_url)
# Loop will not exit until free device is found
while bitbar_device == "":
bitbar_device = deviceFinder.available_android_device()
if "localhost" in appium_url:
self.api_level = subprocess.check_output(["adb", "shell", "getprop ro.build.version.sdk"])
else:
self.api_level = deviceFinder.device_API_level(bitbar_device)
self.utils = BitbarUtils(self.screenshot_dir)
self.utils.log("Will save screenshots at: " + self.screenshot_dir)
self.utils.log("Device API level is %s" % self.api_level)
self.utils.log("Starting Appium test using device '%s'" % bitbar_device)
desired_capabilities_cloud = {}
desired_capabilities_cloud['bitbar_apiKey'] = bitbar_apiKey
desired_capabilities_cloud['bitbar_target'] = 'android'
desired_capabilities_cloud['automationName'] = 'Appium'
if self.api_level <= 16:
desired_capabilities_cloud['bitbar_target'] = 'selendroid'
desired_capabilities_cloud['automationName'] = 'Selendroid'
desired_capabilities_cloud['bitbar_project'] = bitbar_project_name
desired_capabilities_cloud['bitbar_testrun'] = bitbar_testrun_name
desired_capabilities_cloud['bitbar_device'] = bitbar_device
desired_capabilities_cloud['bitbar_app'] = bitbar_app
desired_capabilities_cloud['platformName'] = 'Android'
desired_capabilities_cloud['deviceName'] = 'Android Phone'
desired_capabilities_cloud['appPackage'] = app_package
desired_capabilities_cloud['appActivity'] = app_activity
desired_capabilities_cloud['fullReset'] = False
desired_capabilities_cloud['noReset'] = True
desired_capabilities_cloud['newCommandTimeout'] = new_command_timeout
desired_capabilities_cloud['bitbar_testTimeout'] = bitbar_test_timeout
desired_capabilities_cloud['bitbar_findDevice'] = bitbar_find_device
# set up webdriver
self.utils.log("WebDriver request initiated. Waiting for response, this typically takes 2-3 mins")
self.driver = webdriver.Remote(appium_url, desired_capabilities_cloud)
self.utils.log("WebDriver response received")
self.utils.update_driver(self.driver)
self.utils.log("Driver session id: " + self.driver.session_id)
def tearDown(self):
self.utils.log("Quitting")
self.driver.quit()
def testSample(self):
self.utils.log(" Getting device screen size")
self.utils.log(" " + str(self.driver.get_window_size()))
isSelendroid = None
if self.api_level < 17:
isSelendroid = True
self.utils.screenshot("app_launch")
self.utils.log(" Typing in name")
elems = self.driver.find_elements_by_class_name('android.widget.EditText')
self.utils.log(" info: EditText:" + str(len(elems)))
self.utils.log(" Filling in name")
elems[0].send_keys("Bitbar User")
sleep(2)
self.utils.screenshot("name_typed")
self.driver.orientation = "LANDSCAPE"
self.utils.screenshot("landscape")
self.driver.orientation = "PORTRAIT"
self.utils.screenshot("portrait")
try:
self.utils.log(" Hiding keyboard")
self.driver.hide_keyboard()
except WebDriverException:
pass # pass exception, if keyboard isn't visible already
self.utils.screenshot("name_typed_keyboard_hidden")
self.utils.log(" Clicking element 'Buy 101 devices'")
if isSelendroid:
elem = self.driver.find_element_by_link_text('Buy 101 devices')
else:
elem = self.driver.find_element_by_android_uiautomator('new UiSelector().text("Buy 101 devices")')
elem.click()
self.utils.screenshot("clicked_button1")
self.utils.log(" Clicking Answer")
if isSelendroid:
elem = self.driver.find_element_by_link_text('Answer')
else:
elem = self.driver.find_element_by_android_uiautomator('new UiSelector().text("Answer")')
elem.click()
self.utils.screenshot("answer")
self.utils.log(" Navigating back to Activity-1")
self.driver.back()
self.utils.screenshot("main_activity")
self.utils.log(" Clicking element 'Use Testdroid Cloud'")
if isSelendroid:
elem = self.driver.find_element_by_link_text('Use Testdroid Cloud')
else:
elem = self.driver.find_element_by_android_uiautomator('new UiSelector().text("Use Testdroid Cloud")')
elem.click()
self.utils.screenshot("clicked_button2")
self.utils.log(" Clicking Answer")
if isSelendroid:
elem = self.driver.find_element_by_link_text('Answer')
else:
elem = self.driver.find_element_by_android_uiautomator('new UiSelector().text("Answer")')
elem.click()
self.utils.screenshot("answer")
def initialize():
return BitbarAndroid
if __name__ == "__main__":
suite = unittest.TestLoader().loadTestsFromTestCase(BitbarAndroid)
unittest.TextTestRunner(verbosity=2).run(suite)