Skip to content

Commit 3948cdb

Browse files
committed
Comment android_intent.py
1 parent 9aac8d7 commit 3948cdb

File tree

2 files changed

+87
-29
lines changed

2 files changed

+87
-29
lines changed

_hp/hp/tools/crawler/android_config.json

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,16 @@
22
"chrome":{
33
"package_name":"com.android.chrome",
44
"intent":"com.google.android.apps.chrome.Main",
5-
"version":"121.0.6167.180"
6-
},
7-
"opera":{
8-
"package_name":"com.opera.browser",
9-
"intent":"com.opera.android.BrowserActivity",
10-
"version":"80.5.4244.78163"
5+
"version":"121.0.6167.180"
116
},
127
"brave":{
138
"package_name":"com.brave.browser",
14-
"intent":"org.chromium.chrome.browser.ChromeTabbedActivity",
9+
"intent":"org.chromium.chrome.browser.ChromeTabbedActivity",
1510
"version":"1.62.165"
1611
},
1712
"firefox_beta": {
1813
"package_name":"org.mozilla.firefox_beta",
19-
"intent":"org.mozilla.fenix.IntentReceiverActivity",
14+
"intent":"org.mozilla.fenix.IntentReceiverActivity",
2015
"version":"123.0b9"
21-
},
22-
"duckduckgo": {
23-
"package_name":"com.duckduckgo.mobile.android",
24-
"intent":"com.duckduckgo.app.browser.BrowserActivity",
25-
"version":"5.194.0"
26-
},
27-
"torbrowser_alpha": {
28-
"package_name":"org.torproject.torbrowser_alpha",
29-
"intent":"org.mozilla.fenix.IntentReceiverActivity",
30-
"version":"115.2.1-beta"
3116
}
32-
}
17+
}

_hp/hp/tools/crawler/android_intent.py

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import json
22
import argparse
33
import time
4+
import psycopg
5+
import uuid
46
import subprocess
57
import numpy as np
68

7-
89
from hp.tools.crawler.desktop_selenium import CustomErrorTimeout, CustomTimeout
910
from hp.tools.crawler.utils import get_tests, get_or_create_browser
1011
from multiprocessing import Pool
11-
import psycopg
12-
import uuid
1312

1413

1514
# Setup the config
@@ -37,10 +36,23 @@ def __init__(self, package_name, activity_name, test_url):
3736
self.test_url = test_url
3837

3938
def send_url_intent(device_id, package_name, activity_name, url):
39+
"""Send VIEW intent to open a URL in a browser
40+
41+
Args:
42+
device_id (str): ID of an android emulator device
43+
package_name (str): Name of the package to open
44+
activity_name (str): Name of the activity to open
45+
url (str): The test URL to visit
46+
"""
4047
cmd_text = f'adb -s {device_id} shell am start -n {package_name}/{activity_name} -a android.intent.action.VIEW -d "{url}"'
4148
subprocess.run(cmd_text, shell=True)
4249

4350
def get_available_device():
51+
"""Get a list of available Android Emulator devices
52+
53+
Returns:
54+
list[str]: List of devices IDs
55+
"""
4456
cmd_text = f'adb devices'
4557
cmd_output = subprocess.run(cmd_text, shell=True, check=True, capture_output=True, text=True).stdout.strip()
4658
device_id_list = []
@@ -54,6 +66,11 @@ def get_available_device():
5466
return device_id_list
5567

5668
def force_stop_emulators(device_avd_name = None):
69+
"""Force stop one or all Android Emulators
70+
71+
Args:
72+
device_avd_name (str, optional): Name of an avd device. Defaults to None.
73+
"""
5774
print(f'Stop the enumator name: {device_avd_name}')
5875
if device_avd_name:
5976
kill_current_devices_cmd = "kill -9 $(ps aux | grep '[e]mulator/qemu/linux-x86_64/qemu-system-i386-headless @device_"+device_avd_name+" ' | awk '{print $2}')"
@@ -71,19 +88,40 @@ def force_stop_emulators(device_avd_name = None):
7188
time.sleep(2)
7289

7390
def get_emulator_avd_name(device_id):
91+
"""Convert a device_id to avd_name
92+
93+
Args:
94+
device_id (str): ID of an Emulator device
95+
96+
Returns:
97+
str: AVD name of the device
98+
"""
7499
# device id format: emulator-5554
75100
emulator_port = int(device_id.split('-')[-1])
76101
start_port = 5554
77102
device_name = int((emulator_port - start_port)/2 + 1)
78103
return str(device_name)
79104

80105
def get_port_by_emulator_avd_name(device_avd_name):
106+
"""Convert devive_avd_name to the correct port
107+
108+
Args:
109+
device_avd_name (str): name of the avd device
110+
111+
Returns:
112+
int: Corresponding port
113+
"""
81114
port = 5552
82115
for i in range(1,int(device_avd_name) + 1):
83116
port += 2
84117
return port
85118

86119
def start_emulator_by_avd_name(device_avd_name):
120+
"""Start android emulator with a given name
121+
122+
Args:
123+
device_avd_name (str): Name of the avd device to start
124+
"""
87125
print(f'Starting the emulator: {device_avd_name}')
88126
port = get_port_by_emulator_avd_name(device_avd_name)
89127
cmd_text = f'nohup emulator @device_{device_avd_name} -no-snapshot -screen multi-touch -no-window -port {port}&'
@@ -108,6 +146,11 @@ def start_emulator_by_avd_name(device_avd_name):
108146
time.sleep(10)
109147

110148
def start_emulators(num_devices):
149+
"""Start as many emulators as requested
150+
151+
Args:
152+
num_devices (int): Number of emulators to start
153+
"""
111154
port = 5554
112155
for device in range(1,num_devices + 1):
113156
cmd_text = f'nohup emulator @device_{device} -no-snapshot -screen multi-touch -no-window -port {port}&'
@@ -135,13 +178,18 @@ def start_emulators(num_devices):
135178
cmd_text = f'adb -s {device_id} root'
136179
subprocess.run(cmd_text, shell=True)
137180

138-
# run on each emulator
139181
def run_test(parameter):
182+
"""Visit test URLs on a single emulator
183+
184+
Args:
185+
parameter (PARAMATER): Includes app_list (URLs to visit in certain browsers), the emulator device id, and the autorestart setting
186+
"""
140187
device_id = parameter.device_id
141188
device_avd_name = get_emulator_avd_name(device_id)
142189
print(f'Testing: on device ID: {device_id}, with number of URLs: {len(parameter.app_list)}')
143190

144191
run_times = 0
192+
# Visit all test URLs, app_list contains the package_name/intent and the test_url to visit
145193
for app in parameter.app_list:
146194
print(f'Testing: {app.package_name}, on device ID: {device_id}, with URL: {app.test_url}, test run number: {run_times}')
147195
run_times += 1
@@ -157,8 +205,10 @@ def run_test(parameter):
157205
conn.execute("LISTEN page_runner")
158206
gen = conn.notifies()
159207

208+
# Sent the Intent to visit the URL
160209
send_url_intent(device_id, app.package_name, app.activity_name, encoded_test_url)
161210

211+
# Go to the next test as soon as the current one is finished
162212
for notify in gen:
163213
if notify.payload == run_id:
164214
print(notify)
@@ -170,6 +220,7 @@ def run_test(parameter):
170220
print(e)
171221
print('Timeout exception!')
172222

223+
# Restart every 100 intents
173224
if run_times >= 100 and parameter.auto_restart:
174225
run_times = 0
175226
print(f'Restarting the emulator: {device_id}, AVD: {device_avd_name}')
@@ -179,50 +230,70 @@ def run_test(parameter):
179230

180231
force_stop_emulators(device_avd_name)
181232

182-
183233
def main(browser_list, url_dict, repeat_times, num_devices, resp_type, auto_restart, config_dict):
234+
"""Start all emulators and distribute the test_url visits among them
235+
236+
Args:
237+
browser_list (List[str]): List of browsers to test
238+
url_dict (JSON dict/optional): Optional dict of URLs to tests (for the repeat mode)
239+
repeat_times (int): How often each URL should be visited
240+
num_devices (int): How many emulators to start
241+
resp_type (str): One of debug, basic, parsing
242+
auto_restart (bool): Whether to automatically restart emulators every 100 URLs
243+
config_dict (dict): Additional informatino about the browsers
244+
"""
184245
app_list = list()
185246
url_list = list()
186247

248+
# Create tests for each specified browser
187249
for browser_name in browser_list:
188250
browser_config = config_dict[browser_name]
189251

252+
# Get the browser_id from the database
190253
browser_id = get_or_create_browser(browser_name, browser_config['version'], 'Android 11', 'real', 'intent', '')
191254

255+
# Normal mode: load the URLs from the DB (get_tests)
192256
if not url_dict:
193257
for scheme in ["http", "https"]:
194258
urls = get_tests(resp_type = resp_type, browser_id = browser_id, scheme = scheme, max_popups = 1, browser_modifier = 2)
195259
url_list.extend(urls)
260+
# Repeat mode: visit the URLs of the dict
196261
else:
197262
browser_id_key = str(browser_id)
198263
if browser_id_key in url_dict:
199264
url_list.extend(url_dict[browser_id_key])
200265

266+
# Repeat each URL up to repeat_times
201267
for i in range(0, repeat_times):
202268
for url in url_list:
203269
app_list.append(APP(browser_config['package_name'], browser_config['intent'], url))
204270

205271
print(f'Total number of URLs: {len(app_list)}')
206272

273+
# Stop old emulators
207274
device_ids = get_available_device()
208275
while len(device_ids) > 0:
209276
print('Force stop current emulators ...')
210277
force_stop_emulators()
211278
device_ids = get_available_device()
212279
time.sleep(2)
213280

281+
# Start num_devices new emulators
214282
print('Starting emulators ...')
215283
start_emulators(num_devices)
216284
time.sleep(5)
217285
device_ids = get_available_device()
218286

287+
# Split the URLs that have to be visited in equal chunks
219288
chunked_app_lists = np.array_split(app_list, len(device_ids))
220289
parameters = []
221290

291+
# Each chunk needs to know on which device it should run
222292
for index, working_list in enumerate(chunked_app_lists):
223293
device_id = device_ids[index]
224294
parameters.append(PARAMETER(working_list, device_id, auto_restart))
225295

296+
# Start the work in each emulator
226297
start_time = time.time()
227298
print(start_time)
228299
pool = Pool()
@@ -232,19 +303,21 @@ def main(browser_list, url_dict, repeat_times, num_devices, resp_type, auto_rest
232303
print(f'{(time.time() - start_time)}')
233304

234305
if __name__ == '__main__':
306+
"""Execute all test runs as specified in the lauch arguments
307+
"""
308+
# Load all possible browsers
235309
with open('android_config.json') as file:
236310
config_dict = json.load(file)
237-
238311
browser_list = list(config_dict.keys())
239312
browser_list.append('all')
240313

241314
ap = argparse.ArgumentParser(description='Tester for Android devices')
242315
ap.add_argument('-browsers', '--browsers', dest='browsers', type=str, required=True, nargs='+', choices=browser_list)
243-
ap.add_argument('-url_json', '--url_json', default='', type=str, help='Path to a json list of create_repeat.py tests')
244-
ap.add_argument('-repeat', '--repeat', default=1, type=int, help='Repeat x times')
245-
ap.add_argument('-num_devices', '--num_devices', dest='num_devices', type=int)
316+
ap.add_argument('-url_json', '--url_json', default='', type=str, help='Optional path to a json list of create_repeat.py tests')
317+
ap.add_argument('-repeat', '--repeat', default=1, type=int, help='Repeat each test x times.')
318+
ap.add_argument('-num_devices', '--num_devices', dest='num_devices', type=int, help='How many emulators to start/use')
246319
ap.add_argument('-type', '--resp_type', choices=['basic', 'debug', 'parsing'], default='basic', help='Specify the response type (default: basic)')
247-
ap.add_argument('-auto_restart', action='store_true', help='Auto restart')
320+
ap.add_argument('-auto_restart', action='store_true', help='Automatically restart the emulator every 100 URLs.')
248321

249322
args = ap.parse_args()
250323

0 commit comments

Comments
 (0)