Path: blob/master/AndroidRunner/WebExperiment.py
629 views
import os.path as op1import time2import multiprocessing as mp3from . import Tests4import paths5from .BrowserFactory import BrowserFactory6from .Experiment import Experiment7from .util import makedirs, slugify_dir8from AndroidRunner.PrematureStoppableRun import PrematureStoppableRun91011class WebExperiment(Experiment):12def __init__(self, config, progress, restart):13super(WebExperiment, self).__init__(config, progress, restart)14self.browsers = [BrowserFactory.get_browser(b)() for b in config.get('browsers', ['chrome'])]15Tests.check_dependencies(self.devices, [b.package_name for b in self.browsers])16self.duration = Tests.is_integer(config.get('duration', 0)) / 100017self.config = config1819# Browsers have version specific formatting, allows re-creation if needed20def regenerate_browsers(self, device):21# Regenerate browsers based on device version22self.browsers = [BrowserFactory.get_browser(b)(device) for b in self.config.get('browsers', ['chrome'])]2324def run(self, device, path, run, browser_name):25browser = None26for browserItem in self.browsers:27if browser_name in browserItem.to_string():28browser = browserItem29kwargs = {30'browser': browser,31'app': browser.package_name32}33self.before_run(device, path, run, **kwargs)34self.after_launch(device, path, run, **kwargs)3536self.usb_handler.disable_usb()37self.start_profiling(device, path, run, **kwargs)3839if self.run_stopping_condition_config:40self.queue = mp.Queue()41premature_stoppable_run = PrematureStoppableRun(self.run_stopping_condition_config, self.queue, self.interaction, device, path, run, **kwargs)42premature_stoppable_run.run()43else:44self.interaction(device, path, run, **kwargs)4546self.stop_profiling(device, path, run, **kwargs)47self.usb_handler.enable_usb()4849self.before_close(device, path, run, **kwargs)50self.after_run(device, path, run, **kwargs)5152def last_run_subject(self, current_run):53if self.progress.subject_finished(current_run['device'], current_run['path'], current_run['browser']):54self.after_last_run(self.devices.get_device(current_run['device']), current_run['path'])55self.aggregate_subject()5657def prepare_output_dir(self, current_run):58paths.OUTPUT_DIR = op.join(paths.BASE_OUTPUT_DIR, 'data/', current_run['device'],59slugify_dir(current_run['path']),60current_run['browser'])61makedirs(paths.OUTPUT_DIR)6263def before_run_subject(self, device, path, *args, **kwargs):64super(WebExperiment, self).before_run_subject(device, path, *args, **kwargs)65self.logger.info('URL: %s' % path)6667def before_run(self, device, path, run, *args, **kwargs):68super(WebExperiment, self).before_run(device, path, run, *args, **kwargs)69device.shell('logcat -c')70kwargs['browser'].start(device)71time.sleep(5)7273def before_experiment(self, device, *args, **kwargs):74super().before_experiment(self, device, *args, **kwargs)75self.regenerate_browsers(device)7677def interaction(self, device, path, run, *args, **kwargs):78kwargs['browser'].load_url(device, path)79time.sleep(5)80super(WebExperiment, self).interaction(device, path, run, *args, **kwargs)81# TODO: Fix web experiments running longer than self.duration82time.sleep(self.duration)8384def after_run(self, device, path, run, *args, **kwargs):85kwargs['browser'].stop(device, self.clear_cache)86time.sleep(3)87super(WebExperiment, self).after_run(device, path, run, *args, **kwargs)8889def after_last_run(self, device, path, *args, **kwargs):90super(WebExperiment, self).after_last_run(device, path, *args, **kwargs)9192def cleanup(self, device):93super(WebExperiment, self).cleanup(device)94for browser in self.browsers:95browser.stop(device, clear_data=True)969798