Skip to content

Commit

Permalink
Added support for custom browsers
Browse files Browse the repository at this point in the history
Fixed #13
  • Loading branch information
pmeenan committed Mar 23, 2017
1 parent f01133f commit 4121a30
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 38 deletions.
73 changes: 43 additions & 30 deletions internal/android_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,52 @@ def prepare(self, job, task):
"""Prepare the browser and OS"""
self.adb.cleanup_device()
# Download and install the APK if necessary
if 'apk_info' in job and 'packages' in job['apk_info'] and \
self.config['package'] in job['apk_info']['packages']:
apk_info = job['apk_info']['packages'][self.config['package']]
if 'apk_url' in apk_info and 'md5' in apk_info:
local_file = os.path.join(job['persistent_dir'],
self.config['package'] + '.' + apk_info['md5'] + '.apk')
if not os.path.isfile(local_file):
valid = False
md5_hash = hashlib.md5()
if 'apk_url' in self.config and 'md5' in self.config:
if not os.path.isdir(job['persistent_dir']):
os.makedirs(job['persistent_dir'])
last_install_file = os.path.join(job['persistent_dir'],
self.config['package'] + '.md5')
last_md5 = None
if os.path.isfile(last_install_file):
with open(last_install_file, 'rb') as f_in:
last_md5 = f_in.read()
if last_md5 is None or last_md5 != self.config['md5']:
valid = False
tmp_file = os.path.join(job['persistent_dir'],
self.config['package'] + '.apk')
if os.path.isfile(tmp_file):
try:
logging.debug('Downloading browser update: %s',
apk_info['apk_url'])
import requests
if not os.path.isdir(job['persistent_dir']):
os.makedirs(job['persistent_dir'])
request = requests.get(apk_info['apk_url'], stream=True)
if request.status_code == 200:
with open(local_file, 'wb') as f_out:
for chunk in request.iter_content(chunk_size=4096):
f_out.write(chunk)
md5_hash.update(chunk)
md5 = md5_hash.hexdigest().lower()
if md5 == apk_info['md5'].lower():
valid = True
os.remove(tmp_file)
except Exception:
pass
md5_hash = hashlib.md5()
try:
logging.debug('Downloading browser update: %s',
self.config['apk_url'])
import requests
request = requests.get(self.config['apk_url'], stream=True)
if request.status_code == 200:
with open(tmp_file, 'wb') as f_out:
for chunk in request.iter_content(chunk_size=4096):
f_out.write(chunk)
md5_hash.update(chunk)
md5 = md5_hash.hexdigest().lower()
if md5 == self.config['md5']:
valid = True
except Exception:
pass
if os.path.isfile(tmp_file):
if valid:
logging.debug('Installing browser APK')
self.adb.adb(['install', '-rg', tmp_file])
with open(last_install_file, 'wb') as f_out:
f_out.write(md5)
else:
logging.error('Error downloading browser APK')
try:
os.remove(tmp_file)
except Exception:
pass
if os.path.isfile(local_file):
if valid:
logging.debug('Installing browser APK')
self.adb.adb(['install', '-rg', local_file])
else:
logging.error('Error downloading browser APK')
os.remove(local_file)
# kill any running instances
self.adb.shell(['am', 'force-stop', self.config['package']])

Expand Down
2 changes: 2 additions & 0 deletions internal/browsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def get_browser(self, name, job):
"""Return an instance of the browser logic"""
browser = None
if self.options.android:
if 'customBrowser_package' in job:
name = "Chrome"
separator = name.find('-')
if separator >= 0:
name = name[separator + 1:].strip()
Expand Down
38 changes: 30 additions & 8 deletions internal/chrome_android.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,31 @@ class ChromeAndroid(AndroidBrowser, DevtoolsBrowser):
"""Chrome browser on Android"""
def __init__(self, adb, config, options, job):
self.adb = adb
self.config = config
self.options = options
AndroidBrowser.__init__(self, adb, options, job, config)
self.config = dict(config)
# default (overridable) configs
self.config['command_line_file'] = 'chrome-command-line'
self.config['adb_socket'] = 'localabstract:chrome_devtools_remote'
# pull in the APK info for the browser
if 'apk_info' in job and 'packages' in job['apk_info'] and \
self.config['package'] in job['apk_info']['packages']:
apk_info = job['apk_info']['packages'][self.config['package']]
self.config['apk_url'] = apk_info['apk_url']
self.config['md5'] = apk_info['md5'].lower()
# pull in the settings for a custom browser into the config
if 'customBrowser_package' in job:
self.config['package'] = job['customBrowser_package']
if 'customBrowser_activity' in job:
self.config['activity'] = job['customBrowser_activity']
if 'customBrowserUrl' in job:
self.config['apk_url'] = job['customBrowserUrl']
if 'customBrowserMD5' in job:
self.config['md5'] = job['customBrowserMD5'].lower()
if 'customBrowser_socket' in job:
self.config['adb_socket'] = job['customBrowser_socket']
if 'customBrowser_flagsFile' in job:
self.config['command_line_file'] = os.path.basename(job['customBrowser_flagsFile'])
AndroidBrowser.__init__(self, adb, options, job, self.config)
DevtoolsBrowser.__init__(self, options, job, use_devtools_video=False)
self.connected = False

Expand Down Expand Up @@ -105,9 +127,9 @@ def launch(self, job, task):
command_line = 'chrome ' + ' '.join(args)
if 'addCmdLine' in job:
command_line += ' ' + job['addCmdLine']
local_command_line = os.path.join(task['dir'], 'chrome-command-line')
remote_command_line = '/data/local/tmp/chrome-command-line'
root_command_line = '/data/local/chrome-command-line'
local_command_line = os.path.join(task['dir'], self.config['command_line_file'])
remote_command_line = '/data/local/tmp/' + self.config['command_line_file']
root_command_line = '/data/local/' + self.config['command_line_file']
logging.debug(command_line)
with open(local_command_line, 'wb') as f_out:
f_out.write(command_line)
Expand All @@ -121,7 +143,7 @@ def launch(self, job, task):
'android.intent.action.VIEW', '-d', START_PAGE])
# port-forward the devtools interface
if self.adb.adb(['forward', 'tcp:{0}'.format(task['port']),
'localabstract:chrome_devtools_remote']):
self.config['adb_socket']]):
if DevtoolsBrowser.connect(self, task):
self.connected = True
DevtoolsBrowser.prepare_browser(self, task)
Expand All @@ -139,8 +161,8 @@ def stop(self, job, task):
DevtoolsBrowser.disconnect(self)
# kill the browser
self.adb.shell(['am', 'force-stop', self.config['package']])
self.adb.shell(['rm', '/data/local/tmp/chrome-command-line'])
self.adb.su(['rm', '/data/local/chrome-command-line'])
self.adb.shell(['rm', '/data/local/tmp/' + self.config['command_line_file']])
self.adb.su(['rm', '/data/local/' + self.config['command_line_file']])
# grab the netlog if there was one
if 'netlog' in job and job['netlog']:
netlog_file = os.path.join(task['dir'], task['prefix']) + '_netlog.txt'
Expand Down
1 change: 1 addition & 0 deletions wptagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def run_testing(self):
"""Main testing flow"""
import monotonic
start_time = monotonic.monotonic()
browser = None
while not self.must_exit:
try:
if self.browsers.is_ready():
Expand Down

0 comments on commit 4121a30

Please sign in to comment.