def exam_exit_operate(self, exam_type=1): """ 试卷做指定题退出 :param exam_type: 做题类型 1:章节考核 2 真题模考 """ answer_info = {} print('试卷做指定题退出处理\n') print(self.test_content()) url = self.driver.current_url book_id = re.findall(r'\d+', url)[0] if exam_type == 1: # 获取章节考核答案 chapter_name = self.exam_chapter_name() chapter_id = Sql().find_catalog_id_by_name(chapter_name, 0) question_ids = Sql().find_chapter_assessment_questions(book_id, chapter_id[0][0]) else: # 获取真题模考答案 chapter_name = self.exam_title() question_ids = Sql().find_questions_by_exam_name(chapter_name) exam_answer = HandleAnswer().get_exam_answers(question_ids) # 获取本张试卷的全部答案 self.start_exam().click() time.sleep(2) self.result_icon().click() time.sleep(2) for x in [1, len(self.number()) - 2]: if self.wait_check_answer_card_page(): num = self.number()[x].text self.number()[x].click() time.sleep(1) if self.container() == 'dx-container': choices = self.choose_answer() random_index = random.randint(0, len(choices) - 1) mine_answer = choices[random_index].text answer_info[num] = mine_answer choices[random_index].click() time.sleep(0.5) elif self.container() == 'bq-container': input_wraps = self.blank_input() answers = [] for i in input_wraps: i.click() random_letters = ''.join(random.sample(string.ascii_lowercase, 4)) answers.append(random_letters) i.send_keys(random_letters + Keys.ENTER) time.sleep(0.5) answer_info[num] = answers self.result_icon().click() time.sleep(2) if self.wait_check_answer_card_page(): self.exam_process_exit_icon().click() time.sleep(2) self.entry.alert_tip_operate() return answer_info, exam_answer
def find_ques_id(self, answers): """根据题干获取题目id""" # 获取题目的id bank_whole_text = self.blank_question_span() for x in answers: bank_whole_text = bank_whole_text.replace(" {} ".format(x), "[[{}]]".format(x)) unicode_ques = bank_whole_text.encode('unicode-escape').decode('utf-8').replace(r'\u', '_u') question_id = Sql().find_question_id_by_content(unicode_ques) return str(question_id)
class HandleAnswer(BasePage): def __init__(self): super().__init__() self.data = Sql() @teststeps def get_exam_answers(self, question_ids): """获取书籍对应的题型和答案""" question_id_list = [y for x in question_ids for y in x[0].split(',')] question_info = self.data.find_game_type_content( tuple(question_id_list)) select_ques = [x[1] for x in question_info if x[0] == 203] blank_ques = [x[1] for x in question_info if x[0] == 204] data = {'选择': {}, '填空': {}} select_data = [ x.encode('utf-8').decode('unicode_escape') for x in select_ques ] blank_data = [ x.encode('utf-8').decode('unicode_escape') for x in blank_ques ] select_q_a = [{ self.get_select_q_a(x)[0]: self.get_select_q_a(x)[1] } for x in select_data] blank_q_a = [{ self.get_blank_q_a(x)[0]: self.get_blank_q_a(x)[1] } for x in blank_data] for x in select_q_a: for y in x: data['选择'][y] = x[y] for x in blank_q_a: for y in x: data['填空'][y] = x[y] return data @staticmethod def get_blank_q_a(x): x = x.replace('\n', '') question = re.findall(r'.*?"#q":"(.*?)",.*?', x)[0] reform_q = re.sub(r'\[\[(.*?)\]\]', '', question) reform_q = reform_q.replace(' ', ' ').replace('\t', ' ').replace('\n', '') reform_q = reform_q.replace('\\', '').strip() answers = re.findall(r'\[\[(.*?)\]\]', question) return reform_q, answers @staticmethod def get_select_q_a(x): question = re.findall(r'.*?"#q":"(.*?)",.*?', x)[0].strip() question = question.replace('\\', '') answer = re.findall(r'.*?"#a":"(.*?)",.*?', x)[0] return question, answer
class Examination(unittest.TestCase): sql = Sql() sql.start_db() # exam_count = sql.get_all_exam(2)[0][0] @classmethod @setup def setUp(cls): cls.result = unittest.TestResult() cls.base_assert = ExpectingTest(cls, cls.result) cls.login = LoginPage() cls.game = GameStudy() cls.home = PassionHomePage() cls.exam = ExamPage() cls.login.login_status(SCHOOL_CODE, SCHOOL_PASSWORD, STUDENT_ACCOUNT, SCHOOL_PASSWORD) @teardown def tearDown(self): for x in self.base_assert.get_error(): self.result.addFailure(self, x) def run(self, result=None): self.result = result super(Examination, self).run(result) def test_exam_process(self): """试卷做题过程""" SelectCoursePage().select_course_operate('地理', card_index=0, book_index=0) if self.home.wait_check_home_page(): self.home.exam().click() mine_answer_info = {} # 选择一个试卷,并输出试卷信息 if self.exam.wait_check_exam_record_list_page(): exam_list = self.exam.exam_group_ele() random_index = random.randint(0, len(exam_list) - 1) select_exam = exam_list[random_index] exam_name = self.exam.exam_name(select_exam) exam_date = self.exam.latest_exam_date(select_exam) exam_time = self.exam.exam_used_time(select_exam) exam_score = self.exam.exam_score(select_exam) print('试卷名称:', exam_name, '\n', '试卷最新完成时间:', exam_date, '\n', '试卷耗时:', exam_time, '\n', '试卷得分:', exam_score, '\n') start_exam_btn = self.exam.start_exam_by_ele(select_exam) ActionChains(self.exam.driver).click(start_exam_btn).perform() # 中途做对退出,验证分数是否变化 if self.home.wait_check_start_study_page(): print(self.home.start_text()) self.home.click_start_button() self.exam.exam_operate(do_right=True, half_exit=True, mine_answer_info=mine_answer_info) if self.exam.wait_check_exam_record_list_page(): if self.exam.exam_score(select_exam) == exam_score: # 待议 self.base_assert.except_error('已经做对一题, 中途退出以后成绩未发生变化') # 完成所有题 ActionChains(self.exam.driver).click(start_exam_btn).perform() if self.home.wait_check_start_study_page(): print(self.home.start_text()) self.home.click_start_button() mine_answer_info = {} do_right = random.choice([False, True]) self.exam.exam_operate(do_right=do_right, half_exit=False, mine_answer_info=mine_answer_info) self.exam.exit_icon().click() # 查看报告,验证记录答案和页面答案是否一致 if self.exam.wait_check_exam_record_list_page(): check_report_btn = self.exam.check_report(select_exam) ActionChains(self.exam.driver).click(check_report_btn).perform() if self.exam.wait_check_answer_card_page(): exam_result_score = self.exam.exam_result_score() check_score = 100 if do_right else 0 if exam_result_score != check_score: self.base_assert.except_error('试卷得分与计算得分不一致') self.exam.check_answer_operate(do_right, mine_answer_info)
def __init__(self): super().__init__() self.data = Sql()
def __init__(self): self.mysql = Sql()
class SqlHandle(BasePage): def __init__(self): self.mysql = Sql() @teststep def get_stu_id(self): """获取学生id""" stu_id = self.mysql.find_user_id(STUDENT_ACCOUNT) if stu_id: return stu_id[0][0] else: return 0 @teststep def get_user_by_phone(self, phone): """根据手机号获取学生id""" stu_id = self.mysql.find_user_id(phone) if stu_id: return True else: return False @allure.step('删除学生会考记录') @teststeps def delete_student_all_record(self): """删除学生所有会考做题信息""" stu_id = self.get_stu_id() self.mysql.delete_student_assessment_overview(stu_id) self.mysql.delete_student_assessment_record(stu_id) self.mysql.delete_student_wrong_record(stu_id) self.mysql.delete_student_wrong_note(stu_id) @teststep def get_section_total_count(self, section_name): """获取小节总题数""" nums = self.mysql.find_section_item_count(section_name) count_list = [x[0] for x in nums] return sum(count_list) @teststep def update_exam_date_operate(self, exam_name): """更改试卷做题时间""" start_time = datetime.datetime.now() - datetime.timedelta(days=1) end_time = start_time + datetime.timedelta(minutes=1) stu_id = self.get_stu_id() exam_id = self.mysql.find_exam_id(exam_name)[-1][0] self.mysql.update_exam_date(start_time.strftime("%Y-%m-%d %H:%M:%S"), end_time.strftime("%Y-%m-%d %H:%M:%S"), stu_id, exam_id)