def __init__(self, from_json: str = None, **kwargs): if from_json: data = json.loads(from_json) assert data["command"] == "take_exam" self.questions = pickle.loads( base64.b64decode(data["questions"].encode("utf-8"))) self.exam_configuration = pickle.loads( base64.b64decode(data["exam_configuration"].encode("utf-8"))) self.settings = Settings(from_dict=data["settings"]) self.workarounds = Workarounds(from_dict=data["workarounds"]) else: data = kwargs self.questions = kwargs["questions"] self.exam_configuration = kwargs["exam_configuration"] self.settings = kwargs["settings"] self.workarounds = kwargs["workarounds"] self.ilias_url = data["ilias_url"] self.verify_ssl = data["verify_ssl"] self.ilias_version = tuple(data["ilias_version"]) self.machine = data["machine"] self.machine_index = data["machine_index"] self.username = data["username"] self.password = data["password"] self.test_id = data["test_id"] self.test_url = data["test_url"] self.wait_time = data["wait_time"] self.admin_lang = data["admin_lang"] self.n_deterministic_machines = int( self.settings.num_deterministic_machines)
def get(self): if self.state.batch: settings = self.state.batch.settings workarounds = self.state.batch.workarounds else: # use defaults settings = Settings() workarounds = Workarounds() fixed_workarounds = Workarounds.get_solved( self.state.get_ilias_version_tuple()) self.write( json.dumps( dict(settings=settings.get_catalog(), workarounds=workarounds.get_catalog( exclude=fixed_workarounds)))) self.finish()
def post(self): data = json.loads(self.request.body) workarounds_dict = data["workarounds"] Workarounds.disable_solved(workarounds_dict, self.state.get_ilias_version_tuple()) settings = Settings(from_dict=data["settings"]) workarounds = Workarounds(from_dict=workarounds_dict) test_id = data["test"] wait_time = 0 batch_id = self.state.start_batch(PackagedTest(test_id), settings, workarounds, wait_time) if batch_id is None: self.write("error") else: self.write(batch_id) self.flush()
class TakeExamCommand: def __init__(self, from_json: str = None, **kwargs): if from_json: data = json.loads(from_json) assert data["command"] == "take_exam" self.questions = pickle.loads( base64.b64decode(data["questions"].encode("utf-8"))) self.exam_configuration = pickle.loads( base64.b64decode(data["exam_configuration"].encode("utf-8"))) self.settings = Settings(from_dict=data["settings"]) self.workarounds = Workarounds(from_dict=data["workarounds"]) else: data = kwargs self.questions = kwargs["questions"] self.exam_configuration = kwargs["exam_configuration"] self.settings = kwargs["settings"] self.workarounds = kwargs["workarounds"] self.ilias_url = data["ilias_url"] self.verify_ssl = data["verify_ssl"] self.ilias_version = tuple(data["ilias_version"]) self.machine = data["machine"] self.machine_index = data["machine_index"] self.username = data["username"] self.password = data["password"] self.test_id = data["test_id"] self.test_url = data["test_url"] self.wait_time = data["wait_time"] self.admin_lang = data["admin_lang"] self.n_deterministic_machines = int( self.settings.num_deterministic_machines) def to_json(self): return json.dumps( dict(command="take_exam", ilias_url=self.ilias_url, verify_ssl=self.verify_ssl, ilias_version=self.ilias_version, machine=self.machine, machine_index=self.machine_index, username=self.username, password=self.password, test_id=self.test_id, test_url=self.test_url, questions=base64.b64encode( pickle.dumps(self.questions, pickle.HIGHEST_PROTOCOL)).decode("utf-8"), exam_configuration=base64.b64encode( pickle.dumps(self.exam_configuration, pickle.HIGHEST_PROTOCOL)).decode("utf-8"), settings=self.settings.to_dict(), workarounds=self.workarounds.to_dict(), wait_time=self.wait_time, admin_lang=self.admin_lang)) def _create_result_with_details(self, driver: selenium.webdriver.Remote, report: Callable[[str], None], e: Exception, trace: str): files = dict() files['error/trace.txt'] = trace.encode('utf8') url, html, alert = get_driver_error_details(driver) if alert: files['error/alert.txt'] = alert.encode('utf8') if html: files['error/page.html'] = html.encode('utf8') try: files['error/screen.png'] = base64.b64decode( driver.get_screenshot_as_base64()) except: traceback.print_exc() filenames = map(lambda s: '%s_%s' % (self.username, s), files.keys()) error = 'test failed on url %s. for details, see %s.' % ( url, ', '.join(filenames)) report(error) return Result.from_error(Origin.recorded, e.get_error_domain(), error, files) def run(self, browser, master_report: Callable[[str], None]): driver = browser.driver machine_info = "running test on machine #%s (%s)." % ( self.machine_index, self.machine) master_report(machine_info) try: with run_interaction(): user_driver = UserDriver(driver, self.ilias_url, self.ilias_version, master_report, verify_ssl=self.verify_ssl) with user_driver.login(self.username, self.password): test_driver = user_driver.create_test_driver( PackagedTest(self.test_id)) test_driver.goto(self.test_url) if self.machine_index <= self.n_deterministic_machines: # some machines can operate deterministically as a well-defined baseline regression test context = RegressionContext( self.machine_index * 73939133, self.questions, self.settings, self.workarounds, self.admin_lang, self.ilias_version) else: context = RandomContext(self.questions, self.settings, self.workarounds, self.admin_lang, self.ilias_version) exam_driver = test_driver.start(self.username, context, self.questions, self.exam_configuration) try: exam_driver.add_protocol(machine_info) def report(s): master_report(s) exam_driver.add_protocol(s) robot = ExamRobot(exam_driver, context, report, self.questions, self.settings) robot.run(self.settings.test_passes) except TiltrException as e: traceback.print_exc() r = self._create_result_with_details( driver, master_report, e, traceback.format_exc()) exam_driver.add_protocol_to_result(r) return r exam_driver.close() result = exam_driver.get_expected_result( self.workarounds, self.admin_lang) result.attach_coverage(context.coverage) except TiltrException as e: traceback.print_exc() return self._create_result_with_details(driver, master_report, e, traceback.format_exc()) except WebDriverException as webdriver_error: e = InteractionException(str(webdriver_error)) traceback.print_exc() master_report("test aborted: %s" % traceback.format_exc()) return Result.from_error(Origin.recorded, e.get_error_domain(), traceback.format_exc()) except: traceback.print_exc() master_report("test aborted with an unexpected error: %s" % traceback.format_exc()) return None master_report("done running test.") return result