def setUp(self): try: self.base_page = BasePage() self.log = MyLog().get_log().logger except Exception as e: self.log.error(e) raise Exception("出现异常!")
def setUp(self): try: config = ReadConfig() self.base_page = BasePage() self.log = MyLog().get_log().logger self.user_name = config.get_app_param("user_name") self.password = config.get_app_param("password") except Exception as e: self.log.error(e) raise Exception("出现异常!")
class AppExams(unittest.TestCase): """考试""" def setUp(self): try: self.common = Common() self.base_page = BasePage() self.log = MyLog().get_log().logger except Exception as e: self.log.error(e) raise Exception("出现异常!") def test_app_exams(self): """app考试""" try: # 用例开始,输入负责人姓名,必须 self.base_page.case_start("李彬") if not self.base_page.home_page_to("考试"): # 判断待办事项中是否有考试 if AppExams.enter(self) is False: # 首页进入考试 # 如果没有可以考试的试卷就结束用例 self.base_page.case_pass() return # 开始答题 AppExams.answer(self) # 用例成功,必须 self.base_page.case_pass() except Exception as e: self.log.error(e) # 用例失败,必须 self.base_page.case_failed() raise Exception("出现异常!") finally: self.base_page.back_to() def answer(self): """答题""" exam_name = ("css_selector", "h2.exam-title.font-large.text-center.ng-binding") # 试卷名称 start_answer_button = ("xpath", "//a[contains(text(), '开始答题')]") re_answer_button = ("xpath", "//a[contains(text(), '重新答题')]") exam_time_elem = ("css_selector", "span.countdown.ng-binding") # 考试时长 count_num_elem = ("css_selector", "span.item-num.ng-binding" ) # 题号和试题数量 option_elem = ("css_selector", "li.option-list-li") # 选项 next_elem = ("css_selector", "div.col.col-33.col-center.text-right" ) # 下一题 submit_elem = ("css_selector", "a.button.button-clear.button-calm" ) # 提交 result_element = ("css_selector", "div.title.ng-binding") # 成绩单title answer_sheet_elem = ("css_selector", "button.button.button-calm.exam-button" ) # 答题页面的右上角的“答题卡” submit_button = ("css_selector", "button.button.button-block.button-calm") # 提交按钮 # 登录成功提示,有未答题提示,自动提交试卷提示 popup_title = ("css_selector", "h3.popup-title.ng-binding") # 提示框title try: self.base_page.screen_shot() self.assertIs(self.base_page.displayed(exam_name), True, "进入试卷异常!") self.base_page.get_text(exam_name, text="试卷名称:") if self.base_page.displayed(start_answer_button): self.log.debug("点击开始答题按钮") self.base_page.click_elem_tag(start_answer_button) elif self.base_page.displayed(re_answer_button): self.log.debug("点击重新答题按钮") self.base_page.click_elem_tag(re_answer_button) # 提示框 self.base_page.popup() # 用答题页面的“答题卡”元素断言 self.assertIs(self.base_page.displayed(answer_sheet_elem), True, "进入答题页异常!") # 获取考试时常 self.base_page.get_text(exam_time_elem, text="剩余考试时长:") # 获取题目总数 num = self.base_page.get_text(count_num_elem) count_num_str = num.split('/')[1:][0] # 考题总数 current_num_str = num.split('/')[:1][0] # 当前题号 self.log.debug("开始答题") count_num = int(count_num_str) current_num = int(current_num_str) # 自动提交试卷的提示框和总题数 try: while not self.base_page.displayed( popup_title) and current_num <= count_num: self.log.debug("第%s题" % current_num) count_option = len( self.base_page.find_elements(option_elem)) # 选项数量 i = random.randint(0, count_option - 1) self.base_page.click_elem_tag(option_elem, tag=i) self.base_page.screen_shot() # 点击下一题按钮 if current_num < count_num: self.base_page.click_elem_tag(next_elem) current_num += 1 if current_num >= 6: self.log.debug("中断答题") self.base_page.screen_shot() self.base_page.click_elem_tag(answer_sheet_elem) self.base_page.screen_shot() if not self.base_page.displayed(popup_title): self.log.debug("进入答题卡页面提交试卷!") # 点击提交按钮 self.base_page.click_elem_tag(submit_button, roll=True) self.base_page.screen_shot() break else: break else: self.log.debug("置于后台") self.base_page.app_driver.background_app(0) # 手动提交试卷 if current_num == count_num and not self.base_page.displayed( popup_title): self.log.debug("答题完毕后点击'提交'按钮,手动提交试卷!") self.base_page.click_elem_tag(submit_elem, tag=1) self.base_page.screen_shot() self.assertIs(self.base_page.displayed(submit_button), True, "答题完毕后提交失败,未进入答题卡页面!") # 答题卡页面点击提交按钮 self.base_page.click_elem_tag(submit_button, roll=True) # 答题卡页点击提交按钮后的提示框 self.assertIs(self.base_page.displayed(popup_title), True, "答题卡页面点击提交按钮后未出现提示框!") # 提示框 self.base_page.popup() # 自动提交试卷(时间到了/超过离开次数) elif self.base_page.displayed(popup_title): self.base_page.popup() # 用成绩单页面的“成绩单”元素断言 self.assertIs(self.base_page.displayed(result_element), True, "考试结果提交异常!") self.log.debug("提交成功!") except Exception as e: # 过程中出现弹框(自动提交弹框) if self.base_page.displayed(popup_title): self.base_page.popup() # 用成绩单页面的“成绩单”元素断言 self.assertIs(self.base_page.displayed(result_element), True, "考试结果提交异常!") self.log.debug("提交成功!") else: self.log.error(e) raise Exception("出现异常!") except Exception as e: self.log.error(e) raise Exception("出现异常!") def enter(self): """进入试卷""" try: exams_module = ("css_selector", "img[src='img/exam.png']" ) # 首页“考试”menu exam_state = ("css_selector", "span.tag.ng-binding.tag-warning.tag-right" ) # “未考试”标签 prompt_nothing = ("css_selector", "div.nw-nothing-text" ) # “暂无内容”提示 exams_type = ("css_selector", "div.exam-tabs.text-center.font-small.ng-binding" ) # “未考试/已交卷”menu self.assertIs(self.base_page.displayed(exams_module), True, "首页异常!") self.log.debug("从首页进入考试模块") self.base_page.click_elem_tag(exams_module) self.base_page.screen_shot() self.assertIs(self.base_page.displayed(exams_type), True, "进入考试模块异常!") if self.base_page.displayed(exam_state): self.log.debug("进入试卷") self.base_page.click_elem_tag(exam_state) return True else: if self.base_page.displayed(prompt_nothing): self.log.debug("未考试中暂无内容!") else: self.log.debug("暂无未考试的试卷!") self.log.debug("进入已交卷中查看") self.base_page.click_elem_tag(exams_type, tag=1) self.base_page.screen_shot() if self.base_page.displayed(prompt_nothing): self.log.debug("已交卷中暂无内容!") return False else: return AppExams.find_the_paper(self) except Exception as e: self.log.error(e) raise Exception("出现异常!") def find_the_paper(self): """已交卷中查找可以重新答题的试卷""" exams_type = ("css_selector", "div.exam-tabs.text-center.font-small.ng-binding" ) # 已交卷menu exam_state = ("css_selector", "span.tag.ng-binding.tag-success.tag-right") # "已考试"标签 exam_name = ("css_selector", "h2.exam-title.font-large.text-center.ng-binding") # 试卷名称 re_answer_button = ("xpath", "//a[contains(text(), '重新答题')]") # 重新答题按钮 try: count = len(self.base_page.find_elements(exam_state)) # "已考试"标签总数 # self.log.debug("[已考试]标签数量:%s" % str(count)) i = 0 j = 1 n = 0 if count != 0: while j <= count: self.base_page.click_elem_tag(exams_type, tag=1) self.log.debug("进入试卷") self.base_page.click_elem_tag(exam_state, tag=i, roll=True) if self.base_page.displayed(re_answer_button): n += 1 self.log.debug("该试卷可以重新答题") return True self.base_page.get_text("试卷名称:", exam_name) self.base_page.screen_shot() self.log.debug("该试卷不能重新答题,继续查看下一个试卷") self.base_page.back() i += 2 j += 1 if j % 10 == 0: self.log.debug("向上滑动屏幕!") self.base_page.swipe_up() self.base_page.screen_shot() elements = self.base_page.find_elements(exam_state) count = len(elements) if n == 0: self.log.debug("已交卷中没有可以重新答题的试卷!") return False except Exception as e: self.log.error(e) raise Exception("出现异常!") def tearDown(self): # 用例结束,必须 self.base_page.case_end()
class AppPractice(unittest.TestCase): """练习""" def setUp(self): try: self.base_page = BasePage() self.log = MyLog().get_log().logger except Exception as e: self.log.error(e) raise Exception("出现异常!") def test_app_practice(self): """app练习""" try: # 用例开始,输入负责人姓名,必须 self.base_page.case_start("李彬") if not self.base_page.home_page_to("练习"): # 判断待办事项中是否有练习 if AppPractice.enter(self) is False: # 首页进入考试 # 如果没有练习试卷就结束用例 self.base_page.case_pass() return # 开始答题 AppPractice.answer(self) # 用例成功,必须 self.base_page.case_pass() except Exception as e: self.log.error(e) # 用例失败,必须 self.base_page.case_failed() raise Exception("出现异常!") finally: self.base_page.back_to() def enter(self): """进入练习""" practice_module = ("css_selector", "img[src='img/practice-icon.png']" ) # 首页“练习”menu practice_title = ("css_selector", "div.title.ng-binding") # “练习”title practice_list = ( "css_selector", "ion-item.item-remove-animate.item-text-wrap.item.item-complex" ) # “练习”试卷 practice_state = ("css_selector", "span.tag.ng-binding.tag-orange.tag-right" ) # “未练习”标签 try: self.assertIs(self.base_page.displayed(practice_module), True, "首页异常!") self.log.debug("从首页进入练习模块") self.base_page.click_elem_tag(practice_module) self.base_page.screen_shot() self.assertIs(self.base_page.displayed(practice_title), True, "进入练习模块异常!") if self.base_page.displayed(practice_list): if self.base_page.displayed(practice_state): self.log.debug("进入练习试卷") self.base_page.click_elem_tag(practice_state) else: self.log.debug("暂无未练习的试卷,进入已完成试卷中重新练习!") practice_count = len( self.base_page.find_elements(practice_list)) i = random.randint(0, practice_count - 1) self.base_page.click_elem_tag(practice_list, tag=i, roll=True) return True else: self.log.debug("暂无内容!") return False except Exception as e: self.log.error(e) raise Exception("出现异常!") def answer(self): """答题""" title = ("css_selector", "div.title.ng-binding") # “练习”title exam_name = ("css_selector", "h2.exam-title.font-large.text-center.ng-binding") # 试卷名称 start_answer_button = ("xpath", "//a[contains(text(), '开始练习')]") continue_answer_button = ("xpath", "//a[contains(text(), '继续练习')]") re_answer_button = ("xpath", "//a[contains(text(), '重新练习')]") count_num_elem = ("css_selector", "span.item-num.ng-binding" ) # 题号和试题数量 option_elem = ("css_selector", "li.option-list-li") # 选项 next_elem = ("css_selector", "div.col.col-33.col-center.text-right" ) # 下一题 submit_elem = ("css_selector", "a.button.button-clear.button-calm" ) # 提交 result_element = ("css_selector", "div.title.ng-binding") # 练习结果title answer_sheet_elem = ("css_selector", "button.button.button-calm.exam-button" ) # 答题页面的右上角的“答题卡” submit_button = ("css_selector", "button.button.button-block.button-calm") # 提交按钮 # 登录成功提示,有未答题提示,自动提交试卷提示 popup_title = ("css_selector", "h3.popup-title.ng-binding") # 提示框title try: self.base_page.screen_shot() self.assertIs(self.base_page.displayed(title), True, "进入试卷异常!") self.base_page.get_text(exam_name, text="试卷名称:") if self.base_page.displayed(re_answer_button): self.log.debug("点击[重新练习]按钮") self.base_page.click_elem_tag(re_answer_button) elif self.base_page.displayed(start_answer_button): self.log.debug("点击[开始练习]按钮") self.base_page.click_elem_tag(start_answer_button) elif self.base_page.displayed(continue_answer_button): self.log.debug("点击[继续练习]按钮") self.base_page.click_elem_tag(continue_answer_button) # 用答题页面的“答题卡”元素断言 self.assertIs(self.base_page.displayed(answer_sheet_elem), True, "进入答题页异常!") # 获取题目总数 num = self.base_page.get_text(count_num_elem) count_num_str = num.split('/')[1:][0] # 考题总数 current_num_str = num.split('/')[:1][0] # 当前题号 self.log.debug("开始答题") count_num = int(count_num_str) current_num = int(current_num_str) while current_num <= count_num: self.log.debug("第%s题" % current_num) count_option = len( self.base_page.find_elements(option_elem)) # 选项数量 i = random.randint(0, count_option - 1) self.base_page.click_elem_tag(option_elem, tag=i) self.base_page.screen_shot() # 点击下一题按钮 if current_num < count_num: self.base_page.click_elem_tag(next_elem) current_num += 1 else: break if current_num >= 6: self.log.debug("中断答题") self.base_page.screen_shot() self.base_page.click_elem_tag(answer_sheet_elem) self.base_page.screen_shot() if not self.base_page.displayed(popup_title): self.log.debug("进入答题卡页面提交试卷!") # 点击提交按钮 self.base_page.click_elem_tag(submit_button, roll=True) self.base_page.screen_shot() break else: break # 手动提交试卷 if current_num == count_num and not self.base_page.displayed( popup_title): self.log.debug("答题完毕后点击'提交'按钮,手动提交试卷!") self.base_page.click_elem_tag(submit_elem, tag=1) self.base_page.screen_shot() # 自动提交试卷(中断答题/超过离开次数) elif self.base_page.displayed(popup_title): self.base_page.popup() # 用成绩单页面的“成绩单”元素断言 self.assertIs(self.base_page.displayed(result_element), True, "考试结果提交异常!") self.log.debug("练习完成!") except Exception as e: self.log.error(e) raise Exception("出现异常!") def tearDown(self): self.base_page.case_end()
class AppSurvey(unittest.TestCase): """问卷调查""" def setUp(self): try: self.base_page = BasePage() self.log = MyLog().get_log().logger except Exception as e: self.log.error(e) raise Exception("出现异常!") def test_app_survey(self): """app问卷调查""" try: # 用例开始,输入负责人姓名,必须 self.base_page.case_start("李彬") if AppSurvey.enter(self) is False: # 首页进入问卷调查 # 如果没有有效的问卷调查就结束用例 self.base_page.case_pass() return # 开始答题 AppSurvey.answer(self) # 用例成功,必须 self.base_page.case_pass() except Exception as e: self.log.error(e) # 用例失败,必须 self.base_page.case_failed() raise Exception("出现异常!") finally: self.base_page.back_to() def enter(self): """进入问卷调查""" survey_module = ("css_selector", "img[src='img/survey.png']" ) # 首页“问卷调查”menu survey_title = ("css_selector", "div.title.ng-binding") # “问卷调查”title survey_list = ( "css_selector", "ion-item.item-remove-animate.item-text-wrap.item.item-complex" ) # “问卷调查”问卷 survey_state = ("css_selector", "span.tag.ng-binding.tag-warning.tag-right") # “未填写”标签 try: self.assertIs(self.base_page.displayed(survey_module), True, "首页异常!") self.log.debug("从首页进入问卷调查模块") self.base_page.click_elem_tag(survey_module) self.base_page.screen_shot() self.assertIs(self.base_page.displayed(survey_title), True, "进入问卷调查模块异常!") if self.base_page.displayed(survey_list): if self.base_page.displayed(survey_state): self.log.debug("进入问卷") self.base_page.click_elem_tag(survey_state) return True else: self.log.debug("暂无未填写的问卷!") return False else: self.log.debug("暂无内容!") return False except Exception as e: self.log.error(e) raise Exception("出现异常!") def answer(self): """答题""" survey_name = ("css_selector", "h2.exam-title.font-large.text-center.ng-binding" ) # 问卷名称 start_answer_button = ("css_selector", "a.button.button-block.button-calm") # “开始填写”按钮 problem_name = ("css_selector", "div.paper-title.font-big.ng-binding" ) # 问题名称 submit_button = ( "css_selector", "button.button.button-full.button-calm.lesson-footer-button" ) # 提交按钮 text = u"大幅度dddz12345,,kds。,.,m@!~" * 5 try: self.base_page.screen_shot() self.assertIs(self.base_page.displayed(survey_name), True, "进入问卷异常!") self.base_page.get_text(survey_name, text="问卷名称:") if self.base_page.displayed(start_answer_button): self.log.debug("点击[开始填写]按钮") self.base_page.click_elem_tag(start_answer_button) self.base_page.screen_shot() # 用答题页面的“提交”按钮断言 self.assertIs(self.base_page.displayed(submit_button), True, "进入答题页异常!") # 问题数量 problem_count = len(self.base_page.find_elements(problem_name)) self.log.debug("问题数量:%s" % problem_count) i = 1 while i <= problem_count: problem_type_elem = ( "xpath", "//div[@class='survey-content'][%s]//span[2]" % i ) # 问题类型 problem_type = self.base_page.get_text(problem_type_elem, text="第%s题:" % i) if "单选题" in problem_type or "评分题" in problem_type: radio = ("xpath", "//div[@class='survey-content'][%s]//label" % i ) # 单选题和评分题选项 radio_count = len(self.base_page.find_elements(radio)) a = random.randint(0, radio_count - 1) self.base_page.click_elem_tag(radio, tag=a) elif "多选题" in problem_type: multiple = ( "xpath", "//div[@class='survey-content'][%s]//div[@ng-repeat='option in question.questionOptionList']" % i) # 多选题选项 multiple_count = len( self.base_page.find_elements(multiple)) b = random.randint(0, multiple_count - 1) self.base_page.click_elem_tag(multiple, tag=b) c = b while c == b: c = random.randint(0, multiple_count - 1) self.base_page.click_elem_tag(multiple, tag=c) elif "简答题" in problem_type: textarea = ( "xpath", "//div[@class='survey-content'][%s]//textarea[@class='no-resize ng-pristine ng-untouched ng-invalid ng-invalid-required']" % i) # 问答题输入框 self.base_page.input_tag(textarea, text=text) i += 1 self.base_page.screen_shot() self.base_page.click_elem_tag(submit_button) self.base_page.screen_shot() self.base_page.popup() except Exception as e: self.log.error(e) raise Exception("出现异常!") def tearDown(self): self.base_page.case_end() self.base_page.quit()
class AppCurriculum(unittest.TestCase): """课程""" def setUp(self): try: self.base_page = BasePage() self.log = MyLog().get_log().logger except Exception as e: self.log.error(e) raise Exception("出现异常!") def test_app_curriculum(self): """app课程""" try: # 用例开始,输入负责人姓名,必须 self.base_page.case_start("李彬") if not self.base_page.home_page_to("课程"): # 判断待办事项中是否有课程 if AppCurriculum.enter(self) is False: # 首页进入课程 # 如果没有课程就结束用例 self.base_page.case_pass() return # 开始学习 AppCurriculum.study(self) # 用例成功,必须 self.base_page.case_pass() except Exception as e: self.log.error(e) # 用例失败,必须 self.base_page.case_failed() raise Exception("出现异常!") finally: self.base_page.back_to() def enter(self): """进入课程""" module = ("css_selector", "img[src='img/lesson.png']") # 首页“课程”menu title = ("css_selector", "div.title.ng-binding") # “课程”title curriculum_list = ( "css_selector", "ion-item.item-remove-animate.item-text-wrap.item.item-complex" ) # “课程”list state = ("css_selector", "span.tag.ng-binding.tag-orange.tag-right" ) # “未学习”标签 try: self.assertIs(self.base_page.displayed(module), True, "首页异常!") self.log.debug("从首页进入课程模块") self.base_page.click_elem_tag(module) self.base_page.screen_shot() self.assertIs(self.base_page.displayed(title), True, "进入课程模块异常!") if self.base_page.displayed(curriculum_list): if self.base_page.displayed(state): self.log.debug("进入课程") self.base_page.click_elem_tag(state, roll=True) else: self.log.debug("暂无未学习的课程,进入已学习的课程中重新学习!") count = len( self.base_page.find_elements(curriculum_list)) # 课程数量 i = random.randint(0, count - 1) self.base_page.click_elem_tag(curriculum_list, tag=i, roll=True) return True else: self.log.debug("暂无内容!") return False except Exception as e: self.log.error(e) raise Exception("出现异常!") def study(self): """学习""" curriculum_name = ("css_selector", "h1.exam-title.font-large.text-center.ng-binding" ) # 课程名称 curriculum_exam = ( "css_selector", "button.button.button-full.button-calm.lesson-footer-button" ) # 随堂考试 curriculum_list = ( "css_selector", "ion-item.item-remove-animate.item-complex.item-text-wrap.item" ) # 课件list # state = ("css_selector", "span.tag.ng-binding.tag-orange.tag-right") # “未学习”标签 courseware_name = ("css_selector", "h2.exam-title.font-large.text-center.ng-binding" ) # 课件名称 start_answer_button = ("xpath", "//a[contains(text(), '开始学习')]") try: self.base_page.screen_shot() self.assertIs(self.base_page.displayed(curriculum_name), True, "进入课程异常!") self.base_page.get_text(curriculum_name, text="课程名称:") # 随堂考试(只能放课件学习的前面,不然会有句柄切换问题) if self.base_page.displayed(curriculum_exam): AppCurriculum.answer(self) self.base_page.back_to(curriculum_name) curriculum_count = len( self.base_page.find_elements(curriculum_list)) if curriculum_count != 0: self.base_page.switch_handle(curriculum_list) i = random.randint(0, curriculum_count - 1) self.base_page.click_elem_tag(curriculum_list, tag=i, roll=True) self.assertIs(self.base_page.displayed(courseware_name), True, "进入课件异常!") self.base_page.get_text(courseware_name, text="进入课件:") self.base_page.screen_shot() if self.base_page.displayed(start_answer_button): self.log.debug("点击开始学习按钮") self.base_page.click_elem_tag(start_answer_button, t=2) self.base_page.screen_shot() self.base_page.back_to(curriculum_name) self.log.debug("学习完成!") else: self.log.debug("暂无内容!") except Exception as e: self.log.error(e) raise Exception("出现异常!") def answer(self): """随堂考试""" curriculum_exam = ( "css_selector", "button.button.button-full.button-calm.lesson-footer-button" ) # 随堂考试 count_num_elem = ("css_selector", "span.item-num.ng-binding" ) # 题号和试题数量 option_elem = ("css_selector", "li.option-list-li") # 选项 next_elem = ("css_selector", "div.col.col-33.col-center.text-right" ) # 下一题 submit_elem = ("css_selector", "a.button.button-clear.button-calm" ) # 提交 result_element = ("css_selector", "div.title.ng-binding") # 练习结果title answer_sheet_elem = ("css_selector", "button.button.button-calm.exam-button" ) # 答题页面的右上角的“答题卡” submit_button = ("css_selector", "button.button.button-block.button-calm") # 提交按钮 # 登录成功提示,有未答题提示,自动提交试卷提示 popup_title = ("css_selector", "h3.popup-title.ng-binding") # 提示框title try: self.log.debug("进入随堂考试") self.base_page.click_elem_tag(curriculum_exam) self.base_page.screen_shot() # 用答题页面的“答题卡”元素断言 self.assertIs(self.base_page.displayed(answer_sheet_elem), True, "进入答题页异常!") # 获取题目总数 num = self.base_page.get_text(count_num_elem) count_num_str = num.split('/')[1:][0] # 考题总数 current_num_str = num.split('/')[:1][0] # 当前题号 self.log.debug("开始答题") count_num = int(count_num_str) current_num = int(current_num_str) while current_num <= count_num: self.log.debug("第%s题" % current_num) count_option = len( self.base_page.find_elements(option_elem)) # 选项数量 i = random.randint(0, count_option - 1) self.base_page.click_elem_tag(option_elem, tag=i) self.base_page.screen_shot() # 点击下一题按钮 if current_num < count_num: self.base_page.click_elem_tag(next_elem) current_num += 1 else: break if current_num >= 6: self.log.debug("中断答题") self.base_page.screen_shot() self.base_page.click_elem_tag(answer_sheet_elem) self.base_page.screen_shot() if not self.base_page.displayed(popup_title): self.log.debug("进入答题卡页面提交试卷!") # 点击提交按钮 self.base_page.click_elem_tag(submit_button, roll=True) self.base_page.screen_shot() break else: break # 手动提交试卷 if current_num == count_num and not self.base_page.displayed( popup_title): self.log.debug("答题完毕后点击'提交'按钮,手动提交试卷!") self.base_page.click_elem_tag(submit_elem, tag=1) self.base_page.screen_shot() # 自动提交试卷(中断答题/超过离开次数) elif self.base_page.displayed(popup_title): self.base_page.popup() # 用成绩单页面的“成绩单”元素断言 self.assertIs(self.base_page.displayed(result_element), True, "考试结果提交异常!") self.log.debug("提交成功!") except Exception as e: self.log.error(e) raise Exception("出现异常!") def tearDown(self): self.base_page.case_end()
class APITestCases(unittest.TestCase): """api测试用例""" def setUp(self): try: self.request = SendRequest() self.base_page = BasePage() self.log = MyLog.get_log().logger except Exception as e: self.log.error(e) raise Exception("出现异常!") def execute_case(self, origin, case_params, case_name, case_num): """api请求实现""" try: principal = case_params["principal"] # 从用例中提取负责人姓名 self.base_page.case_start(principal, case_name, case_num) # 用例开始头部信息 msg, code = "", "" # 期望返回的文本内容和状态码 self.log.debug("用例参数:%s" % case_params) for param_key, param_value in case_params.items(): if param_key == "msg": if isinstance(param_value, str): msg = param_value else: raise Exception("api用例中msg类型错误!") elif param_key == "code": if isinstance(param_value, int): code = int(param_value) else: raise Exception("api用例中code类型错误!") response = self.request.send_request(origin, case_params) # 发送请求 self.log.debug("是否请求成功:%s" % response.ok) self.log.debug("返回状态码:%s" % response.status_code) self.log.debug("接口响应时长:%s s" % response.elapsed.total_seconds()) self.log.debug("编码格式:%s" % response.encoding) self.log.debug("请求的url:%s" % response.url) self.log.debug("请求头:%s" % response.request.headers) self.log.debug("响应头:%s" % response.headers) self.log.debug("请求返回json:%s" % response.json()) # 断言 tip = response.json()["tip"] # 返回文本内容 actual_code = response.json()["ret"] # 返回状态码 self.assertEqual(msg, tip, "msg期望值:%s,实际值:%s" % (msg, tip)) self.assertEqual(code, actual_code, "code期望值:%s,实际值:%s" % (code, actual_code)) response.raise_for_status() # 状态码不是200时抛出异常 self.base_page.case_pass() except Exception as e: self.log.error(e) self.base_page.case_failed() raise Exception("出现异常!") finally: self.base_page.case_end() # 定位标记 def test_case(self): """用例描述""" case_params = {} origin = 'null' case_name = 'null' case_num = 0 self.execute_case(origin, case_params, case_name, case_num)
class AppOperation(unittest.TestCase): """技术操作""" def setUp(self): try: self.base_page = BasePage() self.log = MyLog().get_log().logger except Exception as e: self.log.error(e) raise Exception("出现异常!") def test_app_operation(self): """app技术操作""" bottom_menu = ("css_selector", "span.tab-title.ng-binding") # 底部menu operation_tool_menu = ("css_selector", "i.iconfont.nw-tool.mine-icon") # 操作工具 operation_exam_menu = ("css_selector", "a.item.item-icon-right") # 技术操作考核 prompt_nothing = ("css_selector", "div.nw-nothing-text") # “暂无内容”提示 operation_list = ("css_selector", "ion-item.item-remove-animate.item-text-wrap.item") # 试卷list operation_name = ("css_selector", "h1.operate-title.text-center.font-large.ng-binding") # 试卷名称 scan_button = ("css_selector", "button.button.button-block.button-calm") # 扫码考核按钮 popup_title = ("css_selector", "h3.popup-title.ng-binding") # 提示框title deduction_button = ("xpath", "//android.view.View[@content-desc='扣分']") # 扣分按钮 remark = ("class", "android.widget.EditText") # 扣分备注 deduction_value = ("xpath", "//android.view.View[@content-desc='0.5分']") # 扣分值 determine_button = ("xpath", "//android.view.View[@content-desc='确认']") # 确认扣分 remark_value = u"agaa vjvvabaz v测试,./!,。?!1234567890" * 10 submit_button = ("css_selector", "button.button.button-calm.exam-button") # 判分零和提交按钮 try: # 用例开始,输入负责人姓名,必须 self.base_page.case_start("李彬") self.assertIs(self.base_page.displayed(bottom_menu), True, "首页异常!") self.log.debug("进入[我的]") self.base_page.click_elem_tag(bottom_menu, tag=-1) self.base_page.screen_shot() if self.base_page.displayed(operation_tool_menu): self.log.debug("进入[操作工具]") self.base_page.click_elem_tag(operation_tool_menu) self.base_page.screen_shot() if self.base_page.displayed(prompt_nothing): self.log.debug("暂无内容!") self.base_page.case_pass() return else: self.log.debug("没有[操作工具]选项,结束测试!") self.base_page.case_pass() return self.log.debug("进入[技术操作列表]") self.base_page.click_elem_tag(operation_exam_menu) self.base_page.screen_shot() self.assertIs(self.base_page.displayed(operation_list), True, "进入[技术操作列表]异常!") self.log.debug("进入[技术操作详情]") operation_count = len(self.base_page.find_elements(operation_list)) # 技术操作试卷数量 i = random.randint(0, operation_count - 1) self.base_page.click_elem_tag(operation_list, tag=i, roll=True) self.base_page.screen_shot() self.assertIs(self.base_page.displayed(scan_button), True, "进入[技术操作详情]异常!") self.base_page.get_text(operation_name, text="考核表名称:") self.log.debug("请扫描考生的二维码!") self.base_page.click_elem_tag(scan_button) i = 0 while self.base_page.displayed(scan_button): self.log.debug("等待扫码!") time.sleep(2) # 开始考核/重新考核/非本院学员/扫码失败 if self.base_page.displayed(popup_title): self.base_page.popup() break i += 1 if i == 30: raise Exception("扫码超时!") self.assertIs(self.base_page.displayed(submit_button), True, "进入答题页面异常!") self.log.debug("扫码完成,进入答题页面!") self.base_page.switch_context(tag=0) elements = len(self.base_page.find_elements(deduction_button)) self.log.debug("总题数:%s" % str(elements)) if elements != 0: i = 0 while i < elements: self.log.debug("点击第%s题的扣分按钮" % (i+1)) self.base_page.click_elem_tag(deduction_button, tag=i, roll=True) self.base_page.screen_shot() self.log.debug("选择扣分") self.assertIs(self.base_page.displayed(deduction_value), True, "点击扣分按钮时异常!") tag_id = len(self.base_page.find_elements(deduction_value)) self.base_page.click_elem_tag(deduction_value, tag=(tag_id - 1)) self.base_page.get_text(deduction_value, text="第%s题扣除:" % (i+1), tag=(tag_id-1)) self.base_page.screen_shot() self.base_page.click_elem_tag(determine_button) # 确认扣分 self.base_page.screen_shot() if self.base_page.displayed(remark, tag=i): self.log.debug("输入第%s题的扣分备注" % (i+1)) self.base_page.input_tag(remark, remark_value, tag=i, roll=True) self.base_page.screen_shot() i += 1 if i >= 5: self.log.debug("中断答题") self.base_page.screen_shot() break self.base_page.switch_context() self.log.debug("点击[提交]按钮,提交本次考核成绩!") self.base_page.click_elem_tag(submit_button, tag=1) self.assertIs(self.base_page.displayed(popup_title), True, "点击[提交]按钮后未出现确定提交的提示框!") self.base_page.popup() self.assertIs(self.base_page.displayed(popup_title), True, "点击[确定]按钮后未出现提交成功的提示框!") self.base_page.popup() self.assertIs(self.base_page.displayed(scan_button), True, "提交考试结果后未回到技术操作详情页面!") self.log.debug("本次考核成绩已提交!") # 用例成功,必须 self.base_page.case_pass() else: self.base_page.screen_shot() raise Exception("页面显示异常!") except Exception as e: self.log.error(e) # 用例失败,必须 self.base_page.case_failed() raise Exception("出现异常!") finally: self.base_page.back_to() def tearDown(self): self.base_page.case_end()
class AppLogin(unittest.TestCase): """登录""" def setUp(self): try: config = ReadConfig() self.base_page = BasePage() self.log = MyLog().get_log().logger self.user_name = config.get_app_param("user_name") self.password = config.get_app_param("password") except Exception as e: self.log.error(e) raise Exception("出现异常!") def test_app_login(self): """app登录""" username_input = ("css_selector", "input[type='text']") # 用户名输入框定位信息 password_input = ("css_selector", "input[type='password']" ) # 密码输入框定位信息 login_button = ("css_selector", "button.button.button-block.button-calm") # 登录按钮定位信息 bottom_menu = ("css_selector", "span.tab-title.ng-binding") # 底部menu answer_sheet_elem = ("css_selector", "button.button.button-calm.exam-button" ) # 答题页面的“答题卡”按钮 submit_button = ("css_selector", "button.button.button-block.button-calm") # 提交按钮 # 登录成功提示,有未答题提示,自动提交试卷提示 popup_title = ("css_selector", "h3.popup-title.ng-binding") # 提示框title try: # 用例开始,输入负责人姓名,必须 self.base_page.case_start("李彬") self.log.debug("打开app") self.base_page.open_app() for i in range(1, 6, +1): # 未登录状态 if self.base_page.displayed(login_button): self.log.debug("输入用户名") self.base_page.input_tag(username_input, self.user_name) self.log.debug("输入密码") self.base_page.input_tag(password_input, self.password) self.base_page.screen_shot() self.log.debug("点击登录按钮") self.base_page.click_elem_tag(login_button) self.base_page.screen_shot() # 用弹框的标题断言 self.assertEqual(self.base_page.displayed(login_button), False, "登录失败!") # 打印弹框上的信息 self.base_page.popup() self.log.debug("进入首页!") self.base_page.case_pass() break # 有提示框(登录成功提示,自动交卷提示) elif self.base_page.displayed(popup_title): # 打印弹框上的信息 self.base_page.popup() self.base_page.back_to() # 返回首页 self.log.debug("返回首页!") self.base_page.case_pass() break # 处于答题页面 elif self.base_page.displayed(answer_sheet_elem): self.log.debug("进入app后正处于答题页面,进入答题卡页面提交试卷,结束该次考试!") self.base_page.screen_shot() self.base_page.click_elem_tag(answer_sheet_elem) # 点击提交按钮 self.base_page.click_elem_tag(submit_button) # 未答题的提示框 self.base_page.popup() self.base_page.back_to() # 返回首页 self.log.debug("返回首页!") self.base_page.case_pass() break # 进入首页 elif self.base_page.displayed(bottom_menu): self.base_page.screen_shot() self.log.debug("进入首页!") self.base_page.case_pass() break else: raise Exception("打开app时异常!") except Exception as e: self.log.error(e) # 用例失败,必须 self.base_page.case_failed() raise Exception("出现异常!") def tearDown(self): self.base_page.case_end()
class AppCourseware(unittest.TestCase): """课件""" def setUp(self): try: self.base_page = BasePage() self.log = MyLog().get_log().logger except Exception as e: self.log.error(e) raise Exception("出现异常!") def test_app_courseware(self): """app课件""" top_menu = ("xpath", "//div[@class='nw-tab']/div") # 课件类型menu search_menu = ("css_selector", "button.button.button-calm.exam-button") # 搜索按钮 search_input = ("css_selector", "input[type='text']") # 搜索内容 search_button = ("css_selector", "button.button.button-small.button-clear.button-dark.search-button.font-small") # 搜索按钮 global text text = "" try: # 用例开始,输入负责人姓名,必须 self.base_page.case_start("李彬") if not self.base_page.home_page_to("课件"): # 判断待办事项中是否有课件 if AppCourseware.enter(self) is False: # 首页进入课件 # 如果没有课件就结束用例 self.base_page.case_pass() return # 开始学习 self.log.debug("进入全部:") AppCourseware.study(self) type_count = len(self.base_page.find_elements(top_menu)) # 课件类型数量 i = 1 while i < type_count: self.base_page.get_text(top_menu, tag=i, text="进入:") self.base_page.click_elem_tag(top_menu, tag=i) AppCourseware.study(self, tag=i) i += 1 self.log.debug("进入搜索:") self.base_page.click_elem_tag(search_menu) self.base_page.input_tag(search_input, text) self.base_page.screen_shot() self.base_page.switch_handle(search_button) self.base_page.click_elem_tag(search_button) self.base_page.screen_shot() self.base_page.switch_handle(search_button) AppCourseware.study(self) # 用例成功,必须 self.base_page.case_pass() except Exception as e: self.log.error(e) # 用例失败,必须 self.base_page.case_failed() raise Exception("出现异常!") finally: self.base_page.back_to() def enter(self): """进入课件""" module = ("css_selector", "img[src='img/courseware.png']") # 首页“课件”menu title = ("css_selector", "div.title.ng-binding") # “课件”title all_list = ("css_selector", "ion-item.item-remove-animate.item-text-wrap.item.item-complex") # “课件”list try: self.assertIs(self.base_page.displayed(module), True, "首页异常!") self.log.debug("从首页进入课件模块") self.base_page.click_elem_tag(module) self.base_page.screen_shot() self.base_page.switch_handle(title) self.assertIs(self.base_page.displayed(title), True, "进入课件模块异常!") if self.base_page.displayed(all_list): return True else: self.log.debug("暂无内容!") return False except Exception as e: self.log.error(e) raise Exception("出现异常!") def study(self, tag=0): """学习课件""" top_menu = ("xpath", "//div[@class='nw-tab']/div") # 课件类型menu courseware_list = ("css_selector", "ion-item.item-remove-animate.item-complex.item-text-wrap.item") # 课件list courseware_name = ("css_selector", "h2.exam-title.font-large.text-center.ng-binding") # 课件名称 start_answer_button = ("xpath", "//a[contains(text(), '开始学习')]") try: courseware_count = len(self.base_page.find_elements(courseware_list)) if courseware_count != 0: self.base_page.click_elem_tag(courseware_list) self.assertIs(self.base_page.displayed(courseware_name), True, "进入课件列表异常!") name = self.base_page.get_text(courseware_name, text="进入课件:") self.base_page.screen_shot() global text text = name self.base_page.switch_handle(start_answer_button) self.assertIs(self.base_page.displayed(start_answer_button), True, "进入课件主页异常!") self.log.debug("点击开始学习按钮") self.base_page.click_elem_tag(start_answer_button, t=2) self.base_page.screen_shot() self.base_page.back_to(top_menu) self.base_page.click_elem_tag(top_menu, tag=tag) self.log.debug("学习完成!") else: self.log.debug("暂无内容!") except Exception as e: self.log.error(e) raise Exception("出现异常!") def tearDown(self): self.base_page.case_end()