class ChoiceWordCloze(BasePage): """选词填空""" def __init__(self): self.bounds = ClickBounds() self.result = ResultPage() self.get = GetAttribute() self.key = Keyboard() @teststeps def wait_check_page(self): """以“title:选词填空”的xpath-index为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@text,'选词填空')]") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def wait_check_play_page(self): """以“rate”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," + self.id_type() + "rate)]") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def rate(self): """获取作业数量""" rate = self.driver\ .find_element_by_id(self.id_type() + "rate").text return rate @teststep def font_middle(self): """第一个Aa""" ele = self.driver \ .find_element_by_id(self.id_type() + "font_middle") return ele @teststep def font_large(self): """第二个Aa""" ele = self.driver \ .find_element_by_id(self.id_type() + "font_large") return ele @teststep def font_great(self): """第三个Aa""" ele = self.driver \ .find_element_by_id(self.id_type() + "font_great") return ele @teststep def prompt(self): """提示词""" self.driver\ .find_element_by_id(self.id_type() + "prompt").click() @teststep def bounds_prompt(self): """提示词按钮""" ele = self.driver\ .find_elements_by_xpath("//android.widget.TextView[contains(@index,0)]")[1] return ele @teststep def prompt_content(self): """提示词内容""" ele = self.driver \ .find_elements_by_xpath("//android.widget.TextView[contains(@index,0)]")[1].text return ele @teststep def click_blank(self): """点击页面 提示词弹框 以外空白处,弹框消失""" self.bounds.click_bounds(67.5, 1119.5) time.sleep(1) @teststep def time(self): """获取作业时间""" ele = self.driver \ .find_element_by_id(self.id_type() + "time").text return ele # 查看答案 页面 @teststeps def wait_check_detail_page(self): """以“answer”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," + self.id_type() + "tv_answer)]") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def content_value(self): """获取整个 外框元素""" ele = self.driver\ .find_element_by_id(self.id_type() + "tb_content") return ele @teststeps def content_desc(self): """点击输入框,激活小键盘""" content = self.get.get_description(self.content_value()) item_x = re.match(".*\[(.*)\].*\[", content) # x值 item_y = re.match(".*\[(.*)\].*", content) # y值 x = item_x.group(1).split(',') # 所有输入框y值的列表 y = item_y.group(1).split(',') # 所有输入框x值的列表 return x, y @teststeps def get_result(self): """点击输入框,激活小键盘""" content = self.get.get_description(self.content_value()) value = re.match("\\[(.+?)\\]", content) # answer answer = value.group(1).split(',') # 所有输入框值的列表 print('正确答案:', answer) self.click_blank() return answer @teststeps def choice_word_filling(self): """《选词填空》 游戏过程""" if self.wait_check_page(): # 页面检查点 if self.wait_check_play_page(): timestr = [] self.prompt() # 右上角 提示词 time.sleep(1) content = self.prompt_content() # 取出提示内容 self.click_blank() # 点击空白处 弹框消失 word_list = content.split(' ') # 取出单词列表 print('待输入的单词:', word_list) self.font_operate() # Aa文字大小切换按钮 切换 及状态统计 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 Homework().next_button_operate('false') # 下一题 按钮 状态判断 加点击 if i == 0: # 获取第一个输入框的坐标,点击激活小键盘 item = self.content_desc() loc = self.get_element_location(self.content_value()) # 文章元素左上角 顶点坐标 x = float(item[0][i]) + loc[0]+55.0 # 55.0为点击输入框中心点的偏移量 y = float(item[1][i]) + loc[1] self.driver.tap([(x, y)]) # 点击激活输入框 else: self.key.games_keyboard('enter') # 点击回车键进入下一题 word = word_list[i] # 单词 print('study_word:', word) if len(word_list) >= int(rate): for index in range(len(word)): if index == 4: self.key.games_keyboard('capslock') # 点击键盘 切换到 大写字母 self.key.games_keyboard(word[index].upper()) # 点击键盘对应 大写字母 self.key.games_keyboard('capslock') # 点击键盘 切换到 小写字母 else: self.key.games_keyboard(word[index]) # 点击键盘对应字母 else: self.key.games_keyboard('a') # 点击键盘对应字母 timestr.append(self.time()) # 统计每小题的计时控件time信息 Homework().next_button_operate('true') # 下一题 按钮 状态判断 加点击 Homework().now_time(timestr) # 判断游戏界面 计时功能控件 是否在计时 print('==================================================') return rate @teststeps def check_detail_page(self, rate, homework_title, game_title): """查看答案页面面""" if self.result.wait_check_result_page(): # 结果页检查点 self.result.check_result_button() # 结果页 查看答案 按钮 if self.result.wait_check_detail_page(): # 页面检查点 print('查看答案页面:') item = self.get_result() print('excel-opeate:') for i in range(len(item)): print('----------------------') ExcelUtil().data_write(rate, homework_title, game_title, item[i]) self.result.back_up_button() @teststeps def font_operate(self): """Aa文字大小切换按钮 状态判断 及 切换操作""" x = [] y = [] middle = self.font_middle() # first large = self.font_large() # second great = self.font_great() # third i = 0 j = 0 while i < 3: bounds = self.content_desc() # 获取输入框坐标 print(self.get.get_checked(middle), self.get.get_checked(large), self.get.get_checked(great)) if self.get.get_checked(middle) == 'false': if self.get.get_checked(large) == 'false': x.insert(2, bounds[0][0]) y.insert(2, bounds[1][0]) print('当前选中的Aa按钮为第3个:', bounds[0][0], bounds[1][0]) j = 3 else: if self.get.get_checked(large) == 'true': x.insert(1, bounds[0][0]) y.insert(1, bounds[1][0]) print('当前选中的Aa按钮为第2个:', bounds[0][0], bounds[1][0]) j = 2 else: x.insert(0, bounds[0][0]) y.insert(0, bounds[1][0]) print('当前选中的Aa按钮为第1个:', bounds[0][0], bounds[1][0]) j = 1 if j == 1: large.click() elif j == 2: great.click() else: middle.click() i += 1 print('--------------------------------------------') time.sleep(2) if not float(y[2]) > float(y[1]) > float(y[0]): print('❌❌❌ Error - Aa文字大小切换按钮:', y) print('==============================================')
class WordSpelling(BasePage): """单词拼写""" def __init__(self): self.result = ResultPage() self.get = GetAttribute() self.key = Keyboard() # 以下为 共有元素 @teststeps def wait_check_page(self): """以“title:单词拼写”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@text,'单词拼写')]") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def wait_check_play_page(self): """以“rate”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," "'{}rate')]".format(self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def rate(self): """获取作业数量""" rate = self.driver\ .find_element_by_id(self.id_type() + "rate").text return rate @teststep def time(self): """获取作业时间""" ele = self.driver \ .find_element_by_id(self.id_type() + "time").text return ele @teststep def click_voice(self): """页面内喇叭量按钮""" self.driver \ .find_element_by_id(self.id_type() + "play_voice") \ .click() @teststeps def word(self): """展示的Word""" ele = self.driver \ .find_element_by_id(self.id_type() + "tv_word").text word = ele[1::2] print('study_word:', word) return word @teststep def explain(self): """展示的翻译""" word = self.driver \ .find_element_by_id(self.id_type() + "tv_explain").text return word @teststep def finish_word(self): """完成答题 之后 展示的Word 前后含额外字符:aa""" word = self.driver \ .find_element_by_id(self.id_type() + "tv_word").text return word[1::2] # 默写模式 特有元素 @teststeps def dictation_word(self): """展示的Word""" ele = self.driver \ .find_element_by_id(self.id_type() + "tv_word").text value = ele[::2] return value @teststeps def dictation_word_judge(self): """判断是否展示Word""" try: self.driver \ .find_element_by_id(self.id_type() + "tv_word") return True except Exception: return False @teststep def under_line(self): """展示的横线""" ele = self.driver \ .find_element_by_id(self.id_type() + "underline") return ele @teststep def hint_button(self): """提示按钮""" ele = self.driver \ .find_element_by_id(self.id_type() + "hint") return ele # 下一步 按钮之后 答案页展示的答案 @teststep def mine_answer(self): """展示的Word 前后含额外字符:aa""" word = self.driver \ .find_element_by_id(self.id_type() + "tv_word").text return word[1::2] @teststep def question(self): """展示的翻译""" word = self.driver \ .find_element_by_id(self.id_type() + "tv_explain").text return word @teststep def correct(self): """展示的答案""" word = self.driver \ .find_element_by_id(self.id_type() + "tv_answer").text return word @teststeps def correct_judge(self): """判断 答案是否展示""" try: self.driver \ .find_element_by_id(self.id_type() + "tv_answer") return True except Exception: return False # 默写模式 答案页特有元素 @teststep def dictation_finish_word(self): """完成答题 之后 展示的Word 前后不含额外字符:aa""" word = self.driver \ .find_element_by_id(self.id_type() + "tv_word").text return word[::2] @teststep def dictation_mine_answer(self): """展示的Word 前后不含额外字符:aa""" word = self.driver \ .find_element_by_id(self.id_type() + "tv_word").text return word[::2] # 以下为答案详情页面元素 @teststeps def wait_check_detail_page(self): """以“answer”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," + self.id_type() + "tv_answer)]") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def result_voice(self, index): """语音按钮""" self.driver \ .find_elements_by_id(self.id_type() + "iv_speak")[index] \ .click() @teststep def result_answer(self, index): """单词""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_answer")[index].text return ele @teststep def result_explain(self, index): """解释""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_hint")[index].text return ele @teststep def result_mine(self, index): """我的""" ele = self.driver \ .find_elements_by_id(self.id_type() + "iv_mine")[index] value = self.get.get_selected(ele) return value @teststeps def diff_type(self, tpe): """选择 不同模式小游戏的 游戏方法""" print(tpe) time.sleep(2) if tpe == '默写模式': answer = self.dictation_pattern() elif tpe == '自定义': answer = self.custom_pattern() else: # 随机模式 answer = self.random_pattern() return answer @teststeps def random_pattern(self): """《单词拼写 随机模式》 游戏过程""" if self.wait_check_page(): # 页面检查点 if self.wait_check_play_page(): var = [] # 随机消除的字母 count = [] # 做错的题目 answer = [] # return值 与结果页内容比对 timestr = [] # 获取每小题的时间 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 Homework().next_button_operate('false') # 下一题 按钮 判断加 点击操作 explain = self.explain() # 解释 word = self.word() # 未缺失的字母 value = word_spelling_operate(explain) # 数据字典 item = word.replace('_', '') if self.is_alphabet(item): # 未缺失的内容为字母 if value != word: # 随机消除的字母消除了 for j in range(len(value)): if value[j] != word[j]: print('缺失的字母:', value[j]) var.append(j) self.keyboard_operate( j, value[j]) # 点击键盘 具体操作 answer.append(self.finish_word()) # 我的答案 Homework().next_button_operate('true') # 下一题 按钮 状态判断加 点击操作 self.result_operate(answer, count, i, timestr, self.mine_answer()) # 下一步按钮后的答案页面 测试 Homework().next_button_operate('true') # 下一题 按钮 判断加 点击操作 print('======================================') Homework().now_time(timestr) # 判断游戏界面 计时功能控件 是否在计时 return rate, answer, var @teststeps def custom_pattern(self): """《单词拼写 自定义模式》 游戏过程""" if self.wait_check_page(): # 页面检查点 if self.wait_check_play_page(): count = [] # 做错的题目 answer = [] # return值 与结果页内容比对 timestr = [] # 获取每小题的时间 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 Homework().next_button_operate('false') # 下一题 按钮 判断加 点击操作 explain = self.explain() # 解释 word = self.word() # 未缺失的字母 value = word_spelling_operate(explain) # 数据字典 item = word.replace('_', '') if self.is_alphabet(item): # 未缺失的内容为字母 if len(word) != 0: if value != word: # 自定义消除的字母消除了 for j in range(len(value)): if value[j] != word[j]: print('缺失的字母:', value[j]) self.keyboard_operate( j, value[j]) # 点击键盘 具体操作 else: print('❌❌❌ Error - 自定义消除的字母未消除', word) for j in range(0, len(value) - 1): if value[j] != word[j]: print('❌❌❌ Error - 未自定义消除的字母%s也消除了' % value[j]) answer.append(self.finish_word()) # 我的答案 Homework().next_button_operate('true') # 下一题 按钮 状态判断 加点击 self.result_operate(answer, count, i, timestr, self.mine_answer()) # 下一步按钮后的答案页面 测试 Homework().next_button_operate('true') # 下一题 按钮 状态判断 加点击 print('======================================') Homework().now_time(timestr) # 判断游戏界面 计时功能控件 是否在计时 return rate, answer @teststeps def dictation_pattern(self): """《单词拼写 默写模式》 游戏过程""" if self.wait_check_page(): # 页面检查点 if self.wait_check_play_page(): count = [] answer = [] # return值 与结果页内容比对 timestr = [] # 获取每小题的时间 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 Homework().next_button_operate('false') # 下一题 按钮 判断 加点击 explain = self.explain() # 解释 value = word_spelling_operate(explain) # 数据字典 if self.dictation_word_judge(): # 默写模式 - 字母未全部消除 print('❌❌❌ Error - 单词拼写 默写模式 - 字母未全部消除') if i in range(2, 5, 2): hint = self.hint_button() # 提示按钮 if self.get.get_enabled(hint) == 'true': hint.click() # 点击 提示按钮 if self.get.get_enabled(hint) != 'false': print('❌❌❌ Error - 点击后提示按钮enabled属性为:', self.get.get_enabled(hint)) if self.dictation_word_judge(): # 出现首字母提示 word = self.dictation_word() if len(word) == 1 and word == value[0]: print('点击提示出现首字母提示', word) else: print('❌❌❌ Error - 点击提示未出现首字母提示') else: print('❌❌❌ Error - 提示按钮enabled属性为:', self.get.get_enabled(hint)) for j in range(len(value)): self.keyboard_operate(j, value[j]) # 点击键盘 具体操作 answer.append(self.dictation_finish_word()) # 我的答案 Homework().next_button_operate('true') # 下一题 按钮 状态判断 加点击 self.result_operate( answer, count, i, timestr, self.dictation_mine_answer()) # 下一步按钮后的答案页面 测试 Homework().next_button_operate('true') # 下一题 按钮 状态判断 加点击 print('======================================') Homework().now_time(timestr) # 判断游戏界面 计时功能控件 是否在计时 return rate, answer @teststeps def keyboard_operate(self, j, value): """点击键盘 具体操作""" if j == 4: self.key.games_keyboard('capslock') # 点击键盘 切换到 大写字母 self.key.games_keyboard(value.upper()) # 点击键盘对应 大写字母 self.key.games_keyboard('capslock') # 点击键盘 切换到 小写字母 else: self.key.games_keyboard(value) # 点击键盘对应字母 @teststeps def result_operate(self, answer, count, i, timestr, mine): """下一步按钮后的答案页面""" # mine 为 答案页面展示的 我的答题结果 print('----------------------') result = answer[len(answer) - 1] print('我的答案:', result) print('我的答题结果:', mine) if self.correct_judge(): # 展示的答案元素存在说明回答错误 correct = self.correct() # 正确答案 if len(mine) <= len(correct): # 输入少于或等于单词字母数的字符 if mine.lower() != result.lower(): # 展示的 我的答题结果 是否与我填入的一致 print('Error - 字符数少于或等于时:', mine.lower(), result.lower()) else: # 输入过多的字符 if correct + mine[len(correct):].lower() != correct + result[ len(correct):].lower(): # 展示的 我的答题结果 是否与我填入的一致 print('Error - 字符输入过多时:', correct + mine[len(correct):].lower(), correct + result[len(correct):].lower()) for k in range(len(correct)): # 测试 答案判断是否正确 if result[k] not in correct: count.append(i) # 做错的题目 break else: # 回答正确 if mine.lower() != result.lower(): # 展示的 我的答题结果 是否与我填入的一致 print('Error - 展示的答题结果 与我填入的不一致:', mine.lower(), result.lower()) if i == 1: # 第2题 j = 0 print('多次点击发音按钮:') while j < 4: print(j) self.click_voice() # 多次点击发音按钮 j += 1 time.sleep(1) else: self.click_voice() # 点击 发音按钮 timestr.append(self.time()) # 统计每小题的计时控件time信息 @teststeps def result_detail_page(self, rate): """查看答案 操作过程""" if self.result.wait_check_result_page(): # 结果页检查点 self.result.check_result_button() # 结果页 查看答案 按钮 if self.result.wait_check_detail_page(): print('======================================') print('查看答案:') self.error_sum(rate) time.sleep(2) print('==============================================') @teststeps def error_sum(self, rate): """查看答案 - 点击答错的题 对应的 听力按钮""" print('题数:', int(rate)) for i in range(0, int(rate)): print('解释:', self.result_explain(i)) # 解释 print('单词:', self.result_answer(i)) # 正确word print('对错标识:', self.result_mine(i)) # 对错标识 print('-----------------------------------') self.result_voice(i) # 点击发音按钮 self.result.back_up_button() # 返回结果页 @teststeps def study_again(self, tpe): """再练一遍 操作过程""" if self.result.wait_check_result_page(): # 结果页检查点 self.result.again_button() # 结果页 再练一遍 按钮 print('再练一遍:') self.diff_type(tpe) # 不同模式 对应不同的游戏过程
class CompleteArticle(BasePage): """补全文章""" def __init__(self): self.result = ResultPage() self.get = GetAttribute() @teststeps def wait_check_page(self): """以“title:补全文章”的xpath-index为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@text,'补全文章')]") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def wait_check_play_page(self): """以“rate”的ID为依据""" locator = ( By.XPATH, "//android.widget.TextView[contains(@resource-id,'{}rate)]".format( self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def rate(self): """获取作业数量""" rate = self.driver \ .find_element_by_id(self.id_type() + "rate").text return rate @teststep def font_middle(self): """第一个Aa""" ele = self.driver \ .find_element_by_id(self.id_type() + "font_middle") return ele @teststep def font_large(self): """第二个Aa""" ele = self.driver \ .find_element_by_id(self.id_type() + "font_large") return ele @teststep def font_great(self): """第三个Aa""" ele = self.driver \ .find_element_by_id(self.id_type() + "font_great") return ele @teststep def dragger_button(self): """拖动按钮""" ele = self.driver \ .find_element_by_id(self.id_type() + "dragger") return ele @teststep def option_button(self): """选项ABCD""" ele = self.driver \ .find_elements_by_id(self.id_type() + 'tv_char') return ele @teststep def option_content(self, index): """选项 内容""" ele = self.driver \ .find_elements_by_id(self.id_type() + 'tv_item')[index].text print('选项内容:', ele) return ele def options_view_size(self): """获取整个选项页面大小""" num = self.driver.find_element_by_id(self.id_type() + "option") var = num.size return var['height'] @teststep def time(self): """获取作业时间""" ele = self.driver \ .find_element_by_id(self.id_type() + "time").text return ele @teststep def input_text(self): """输入框""" ele = self.driver \ .find_element_by_id(self.id_type() + "ss_view") return ele @teststeps def get_input_bounds(self): """获取 输入框坐标""" ele = self.input_text() # 输入框 content = self.get.get_description(ele) item_x = re.match(".*\[(.*)\].*\[", content) # x值 item_y = re.match(".*\[(.*)\].*", content) # y值 x = item_x.group(1).split(',') # 所有输入框y值的列表 y = item_y.group(1).split(',') # 所有输入框x值的列表 return x, y # 查看答案 页面 @teststeps def wait_check_detail_page(self): """以“answer”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," "'{}tb_content)']".format(self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def get_result(self): """获取 输入框 的结果""" ele = self.input_text() # 输入框 content = self.get.get_description(ele) value = re.match("\\[(.+?)\\]", content) # answer answer = value.group(1).split(',') # 所有输入框值的列表 return answer @teststeps def complete_article_operate(self): """《补全文章》 游戏过程""" if self.wait_check_page(): if self.wait_check_play_page(): content = [] timestr = [] # 获取每小题的时间 self.font_operate() # Aa文字大小切换按钮 切换 及状态统计 drag = self.dragger_button() # 拖拽 拖动按钮 loc = self.get_element_bounds(drag) # 获取按钮坐标 size = self.options_view_size() # 获取整个选项页面大小 self.driver.swipe(loc[2], loc[3], loc[2], loc[3] - size, 1000) # 向上拖拽 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 Homework().next_button_operate('false') # 下一题 按钮 判断加 点击操作 options = self.option_button() # 选项ABCD if len(options) < i + 1: options = self.option_button() # 选项ABCD self.screen_swipe_up(0.5, 0.9, 0.6, 1000) # 滑屏 text = [options[i].text] options[i].click() # 依次点击选项 content.append(self.get_result()[i]) # 测试 是否答案已填入文章中 time.sleep(1) if content[i] == ' ': print('❌❌❌ Error - 答案未填入文章中') else: print('第%s题:' % (i + 1)) print(text, content[i]) timestr.append(self.time()) # 统计每小题的计时控件time信息 print('-------------------------') if i == int(rate) - 1: drag = self.dragger_button() # 拖拽 拖动按钮 loc = self.get_element_bounds(drag) # 获取按钮坐标 size = self.options_view_size() # 获取整个选项页面大小 self.driver.swipe(loc[2], loc[3], loc[2], loc[3] + size - 10, 1000) # 向下拖拽按钮 Homework().next_button_operate('true') # 下一题 按钮 状态判断 加点击 Homework().now_time(timestr) # 判断游戏界面 计时功能控件 是否在计时 print('============================================') return rate @teststeps def result_detail_page(self): """查看答案页面""" if self.result.wait_check_result_page(): # 结果页检查点 self.result.check_result_button() # 结果页 查看答案 按钮 if self.result.wait_check_detail_page(): # 页面检查点 if self.wait_check_detail_page(): print('查看答案页面:') item = self.get_result() print("正确答案:", item) self.result.back_up_button() # 返回结果页 @teststeps def font_operate(self): """Aa文字大小切换按钮 状态判断 及 切换操作""" x = [] y = [] middle = self.font_middle() # first large = self.font_large() # second great = self.font_great() # third i = 0 j = 0 while i < 3: bounds = self.get_input_bounds() # 获取输入框坐标 print(self.get.get_checked(middle), self.get.get_checked(large), self.get.get_checked(great)) if self.get.get_checked(middle) == 'false': if self.get.get_checked(large) == 'false': x.insert(2, bounds[0][0]) y.insert(2, bounds[1][0]) print('当前选中的Aa按钮为第3个:', bounds[0][0], bounds[1][0]) j = 3 else: if self.get.get_checked(large) == 'true': x.insert(1, bounds[0][0]) y.insert(1, bounds[1][0]) print('当前选中的Aa按钮为第2个:', bounds[0][0], bounds[1][0]) j = 2 else: x.insert(0, bounds[0][0]) y.insert(0, bounds[1][0]) print('当前选中的Aa按钮为第1个:', bounds[0][0], bounds[1][0]) j = 1 if j == 1: large.click() elif j == 2: great.click() elif j == 3: middle.click() i += 1 print('--------------------------------------------') time.sleep(2) if not float(y[2]) > float(y[1]) > float(y[0]): print('❌❌❌ Error - Aa文字大小切换按钮:', y) print('==============================================')
class VocabularyChoice(BasePage): """词汇选择""" def __init__(self): self.result = ResultPage() @teststeps def wait_check_page(self): """以“title:词汇选择”的xpath-index为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@text,'词汇选择')]") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def wait_check_play_page(self): """以“rate”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," "'{}rate')]".format(self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def click_voice(self): """页面内音量按钮""" self.driver \ .find_element_by_id(self.id_type() + "fab_sound") \ .click() @teststep def rate(self): """获取作业数量""" rate = self.driver\ .find_element_by_id(self.id_type() + "rate").text return rate @teststep def time(self): """获取作业时间""" ele = self.driver \ .find_element_by_id(self.id_type() + "time").text return ele @teststep def question_content(self): """获取题目内容""" ele = self.driver \ .find_element_by_id(self.id_type() + "tv_head").text return ele @teststep def option_button(self): """获取四个选项""" ele = self.driver\ .find_elements_by_id(self.id_type() + "option") return ele # 听音选词模式 特有元素 @teststep def voice(self): """点击喇叭,听音选词后的小喇叭""" self.driver\ .find_element_by_id(self.id_type() + "iv_speak")\ .click() @teststeps def tips(self): """tips:点击喇叭,听音选词""" word = self.driver \ .find_elements_by_xpath("//android.widget.TextView[contains(@index,0)]")[0].text print('tips:', word) @teststeps def explain(self): """选择答案后,出现中文解释""" word = self.driver \ .find_element_by_id(self.id_type() + "explain").text print(word) # 以下为答案详情页面元素 @teststeps def wait_check_detail_page(self): """以“answer”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," "'{}tv_answer')]".format(self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def result_voice(self, index): """语音按钮""" self.driver \ .find_elements_by_id(self.id_type() + "iv_speak")[index] \ .click() @teststep def result_answer(self, index): """单词""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_answer")[index].text return ele @teststep def result_explain(self, index): """解释""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_hint")[index].text return ele @teststep def result_mine(self, index): """我的""" ele = self.driver \ .find_elements_by_id(self.id_type() + "iv_mine")[index] value = GetAttribute().get_selected(ele) return value @teststeps def back_up(self): """返回""" if self.result.wait_check_result_page(): self.result.back_up_button() # 结果页 返回按钮 @teststeps def diff_type(self, tpe): """选择 不同模式小游戏的 游戏方法""" print(tpe) if tpe == '选单词': result = self.vocab_select_choice_word() return result elif tpe == '选解释': result = self.vocab_select_choice_explain() return result elif tpe == '听音选词': # 听音选词模式 result = self.vocab_select_listen_choice() return result # print('听音选词模式') @teststeps def vocab_select_choice_explain(self): """《词汇选择》 - 选解释模式 游戏过程""" if self.wait_check_page(): if self.wait_check_play_page(): count = 0 # questions = [] # 答对的题 timestr = [] # 获取每小题的时间 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 Homework().next_button_operate('false') # 下一题 按钮 判断加 点击操作 self.click_voice() # 点击发音按钮 item = self.question_content() # 题目 print('题目:', item) options = self.option_button() options[random.randint(0, len(options) - 1)].click() # 随机点击选项 self.options_statistic(count) # 选择对错统计 print('----------------------------------') timestr.append(self.time()) # 统计每小题的计时控件time信息 Homework().next_button_operate('true') # 下一题 按钮 状态判断 加点击 print('==============================================') return rate, count @teststeps def vocab_select_choice_word(self): """《词汇选择》 - 选单词模式 游戏过程""" if self.wait_check_page(): if self.wait_check_play_page(): count = 0 # questions = [] # 答对的题 timestr = [] # 获取每小题的时间 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 Homework().next_button_operate('false') # 下一题 按钮 判断加 点击操作 item = self.question_content() # 题目 print('题目:', item) options = self.option_button() options[random.randint(0, len(options) - 1)].click() # 随机点击选项 self.options_statistic(count) # 选择对错统计 print('----------------------------------') timestr.append(self.time()) # 统计每小题的计时控件time信息 Homework().next_button_operate('true') # 下一题 按钮 状态判断 加点击 print('==============================================') return rate, count @teststeps def vocab_select_listen_choice(self): """《词汇选择》 - 听音选词模式 游戏过程""" if self.wait_check_page(): if self.wait_check_play_page(): count = 0 # questions = [] # 答对的题 timestr = [] # 获取每小题的时间 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 Homework().next_button_operate('false') # 下一题 按钮 判断加 点击操作 self.voice() # 题目后的听力按钮 self.click_voice() # 发音按钮 options = self.option_button() options[random.randint(0, len(options) - 1)].click() # 随机点击选项 self.explain() # 中文解释 self.options_statistic(count) # 选择对错统计 print('----------------------------------') timestr.append(self.time()) # 统计每小题的计时控件time信息 Homework().next_button_operate('true') # 下一题 按钮 状态判断 加点击 print('==============================================') return rate, count @teststeps def options_statistic(self, count): """选择对错统计""" time.sleep(1) ele = [] # 四个选项selected属性值为true的个数 options = self.option_button() # 四个选项 for j in range(len(options)): # 统计答案正确与否 if GetAttribute().get_selected(options[j]) == 'true': # 错误答案 ele.append(j) if GetAttribute().get_description(options[j]) == 'true': # 正确答案 ele.append(j) if ele[0] == ele[1]: # 如果选项的selected属性为true的作业数为1,说明答对了,则+1 count += 1 print('回答正确:', options[ele[0]].text) else: # ele[0] != ele[1] print('回答错误T/F:%s、%s' % (options[ele[1]].text, options[ele[0]].text)) # questions.append(self.question_content()) @teststeps def result_detail_page(self, rate): """《词汇选择》 查看答案 操作过程""" if self.result.wait_check_result_page(): # 结果页检查点 self.result.check_result_button() # 结果页 查看答案 按钮 if self.result.wait_check_detail_page(): if self.wait_check_detail_page(): print('查看答案:') self.error_sum(rate) time.sleep(2) print('==============================================') @teststeps def error_sum(self, rate): """查看答案 - 点击答错的题 对应的 听力按钮""" print('题数:', int(rate)) for i in range(0, int(rate)): print('解释:', self.result_explain(i)) # 解释 print('单词:', self.result_answer(i)) # 正确word print('对错标识:', self.result_mine(i)) # 对错标识 print('-----------------------------------') self.result_voice(i) # 点击发音按钮 self.result.back_up_button() # 返回结果页 @teststeps def study_again(self, tpe): """《词汇选择》 选解释&听音选词模式 操作过程""" if self.result.wait_check_result_page(): # 结果页检查点 self.result.again_button() # 结果页 错题再练/再练一遍 按钮 print('错题再练:') self.diff_type(tpe) # 不同模式 对应不同的游戏过程
class WordDictation(BasePage): """单词听写""" def __init__(self): self.result = ResultPage() self.key = Keyboard() @teststeps def wait_check_page(self): """以“title:单词听写”的xpath-index为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@text,'单词听写')]") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def wait_check_play_page(self): """以“rate”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," "'{}rate')]".format(self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def rate(self): """获取作业数量""" rate = self.driver \ .find_element_by_id(self.id_type() + "rate").text return rate @teststep def time(self): """获取作业时间""" ele = self.driver \ .find_element_by_id(self.id_type() + "time").text return ele @teststep def click_voice(self): """页面内音量按钮""" self.driver \ .find_element_by_id(self.id_type() + "play_voice") \ .click() @teststeps def word(self): """展示的Word 点击喇叭听写单词""" ele = self.driver \ .find_element_by_id(self.id_type() + "tv_word").text word = ele.replace(' ', '') # 删除空格 return word # 下一步 按钮之后 答案页展示的答案 @teststeps def mine_answer(self): """展示的Word """ ele = self.driver \ .find_element_by_id(self.id_type() + "tv_word").text word = ele.replace(' ', '') # 删除空格 return word @teststep def question(self): """展示的翻译""" word = self.driver \ .find_element_by_id(self.id_type() + "tv_explain").text return word @teststeps def correct(self): """展示的答案""" word = self.driver \ .find_element_by_id(self.id_type() + "tv_answer").text print('正确答案:', word) return word @teststep def correct_judge(self): """判断 答案是否展示""" ele = self.find_element(self.id_type() + "tv_answer") return ele # 以下为答案详情页面元素 @teststeps def wait_check_detail_page(self): """以“answer”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," "'{}tv_answer')]".format(self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def result_voice(self, index): """语音按钮""" self.driver \ .find_elements_by_id(self.id_type() + "iv_speak")[index] \ .click() @teststep def result_answer(self, index): """单词""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_answer")[index].text return ele @teststep def result_explain(self, index): """解释""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_hint")[index].text return ele @teststep def result_mine(self, index): """我的""" ele = self.driver \ .find_elements_by_id(self.id_type() + "iv_mine")[index].get_attribute("selected") return ele @teststeps def word_dictation(self): """《单词听写》 游戏过程""" if self.wait_check_page(): # 页面检查点 if self.wait_check_play_page(): count = [] answer = [] # return值 与结果页内容比对 timestr = [] # 获取每小题的时间 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 Homework().next_button_operate('false') # 下一题 按钮 判断 加点击 self.word() # 灰字文案:点击喇叭听写单词 self.click_voice() # 点击喇叭 word = dictation_operate(i) # 数据字典 if self.is_alphabet(word): # 解释内容为word for j in range(0, len(word)): self.keyboard_operate(j, word[j]) # 点击键盘 具体操作 answer.append(self.word()) # 我的答案 Homework().next_button_operate('true') # 下一题 按钮 状态判断 加点击 self.result_operate(answer, count, i, timestr, self.mine_answer()) # 下一步按钮后的答案页面 测试 Homework().next_button_operate('true') # 下一题 按钮 状态判断 加点击 print('--------------------------------------') Homework().now_time(timestr) # 判断游戏界面 计时功能控件 是否在计时 print('==============================================') return rate, answer @teststeps def keyboard_operate(self, j, value): """点击键盘 具体操作""" if j == 4: self.key.games_keyboard('capslock') # 点击键盘 切换到 大写字母 self.key.games_keyboard(value.upper()) # 点击键盘对应 大写字母 self.key.games_keyboard('capslock') # 点击键盘 切换到 小写字母 else: self.key.games_keyboard(value) # 点击键盘对应字母 Homework().next_button_judge('true') # 测试 下一步 按钮的状态 @teststeps def result_operate(self, answer, count, i, timestr, mine): """下一步按钮后的答案页面""" # mine 为 答案页面展示的 我的答题结果 result = answer[len(answer) - 1] print('我的答案:', result) print('我的答题结果:', mine) if self.correct_judge(): # 展示的答案元素存在说明回答错误 correct = self.correct() # 正确答案 if mine.lower() != result.lower(): # 展示的答题结果与我填入的答案不一致 print('❌❌❌ Error - 展示的答题结果:%s 与我填入的答案:%s 不一致' % (mine, result)) for k in range(len(correct)): # 测试 答案判断是否正确 if result[k] not in correct: count.append(i) # 做错的题目 break else: # 回答正确 if mine.lower() != result.lower(): # 展示的 我的答题结果 是否与我填入的一致 print('❌❌❌ Error - 展示的答题结果 与我填入的不一致:', mine, result) if i == 1: # 第2题 j = 0 print('多次点击发音按钮:') while j < 4: print(j) self.click_voice() # 多次点击发音按钮 j += 1 time.sleep(1) else: self.click_voice() # 点击 发音按钮 timestr.append(self.time()) # 统计每小题的计时控件time信息 @teststeps def result_detail_page(self, rate): """《单词听写》 查看答案 操作过程""" if self.result.wait_check_result_page(): # 结果页检查点 self.result.check_result_button() # 结果页 查看答案 按钮 if self.result.wait_check_detail_page(): if self.wait_check_detail_page(): print('查看答案:') print('题数:', int(rate)) for i in range(int(rate)): print('-----------------------------------') print('解释:', self.result_explain(i)) # 解释 print('单词:', self.result_answer(i)) # 正确word print('对错标识:', self.result_mine(i)) # 对错标识 self.result_voice(i) # 点击发音按钮 self.result.back_up_button() # 返回结果页 time.sleep(2) print('==============================================') @teststeps def study_again(self): """再练一遍 操作过程""" if self.result.wait_check_result_page(): # 结果页检查点 self.result.again_button() # 结果页 再练一遍 按钮 print('再练一遍:') self.word_dictation() # 游戏过程
class PictureDictation(BasePage): """听音选图""" def __init__(self): self.result = ResultPage() @teststeps def wait_check_page(self): """以“title:听音选图”的xpath-index为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@text,'听音选图')]") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def wait_check_play_page(self): """以“rate”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," "'{}rate')]".format(self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def rate(self): """获取作业数量""" rate = self.driver \ .find_element_by_id(self.id_type() + "rate").text return rate @teststep def time(self): """获取作业时间""" ele = self.driver \ .find_element_by_id(self.id_type() + "time").text return ele @teststeps def img_options(self): """展示的图片""" ele = self.driver \ .find_elements_by_id(self.id_type() + "img") return ele @teststeps def sentence(self): """展示的句子 点击喇叭听写单词""" ele = self.driver \ .find_element_by_id(self.id_type() + "sentence").text return ele @teststep def judge_next_button(self): """‘下一题’按钮 状态判断""" try: self.driver \ .find_element_by_id(self.id_type() + "fab_next") # ‘下一题’按钮 return True except: return False # 以下为答案详情页面元素 @teststeps def wait_check_detail_page(self): """以“progress”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," "'{}tv_progress')]".format(self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def result_voice(self): """语音按钮""" self.driver \ .find_element_by_id(self.id_type() + "iv_play") \ .click() @teststep def result_progress(self): """进度""" ele = self.driver \ .find_element_by_id(self.id_type() + "tv_progress").text return ele @teststep def result_sentence(self): """句子""" ele = self.driver \ .find_elements_by_id(self.id_type() + "text") return ele @teststep def result_img(self): """图片 选项""" ele = self.driver \ .find_elements_by_xpath("//android.support.v7.widget.RecyclerView/*") # 包括 句子+选项 count = [] option = [] for i in range(len(ele)): if GetAttribute().get_class_name( ele[i]) == 'android.widget.LinearLayout': count.append(i) count.append(len(ele)) for j in range(len(count) - 1): option.append(count[j + 1] - count[j] - 1) return option @teststeps def get_first_num(self): """获取 当前页面第一个题号""" item = self.result_sentence()[0].text.split(".")[0] return item @teststeps def get_last_element(self): """页面内最后一个class name为android.widget.TextView的元素""" ele = self.driver \ .find_elements_by_class_name("android.widget.TextView") return ele[-1].text @teststep def question_judge(self, var): """页面中最后一个题目是否有选项""" ele = self.driver \ .find_elements_by_xpath( "//android.widget.TextView[contains(@text, '%s')]" "/parent::android.widget.LinearLayout/following-sibling::android.widget.RelativeLayout" % var) if len(ele) == 0: return True else: return False @teststeps def picture_dictation(self): """《听音选图》 游戏过程""" if self.wait_check_page(): # 页面检查点 if self.wait_check_play_page(): answer = [] # return值 与结果页内容比对 timestr = [] # 获取每小题的时间 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 question = self.sentence() # 句子 print(i + 1, '.', question) options = self.img_options() # 图片选项 print('选项:', len(options)) if i == int(rate) - 1: self.screen_swipe_right(0.1, 0.5, 0.9, 1000) # 向右滑屏进入上一题 self.screen_swipe_left(0.9, 0.5, 0.1, 1000) # 返回最后一题 timestr.append(self.time()) # 统计每小题的计时控件time信息 options[random.randint(0, len(options)) - 1].click() print('---------------------------------------') time.sleep(3) while True: if self.judge_next_button(): Homework().now_time(timestr) # 判断游戏界面 计时功能控件 是否在计时 Homework().next_button_operate( 'true') # 下一题 按钮 状态判断 加点击 if Toast().find_toast('请听完音频检查无误,再提交答案'): print('请听完音频检查无误,再提交答案') break else: time.sleep(3) print('==============================================') return rate, answer @teststeps def result_detail_page(self, rate): """《听音选图》 查看答案 操作过程""" if self.result.wait_check_result_page(): # 结果页检查点 self.result.check_result_button() # 结果页 查看答案 按钮 if self.result.wait_check_detail_page(): if self.wait_check_detail_page(): print('查看答案:') print('题数:', int(rate)) self.result_voice() # 点击发音按钮 self.error_sum(int(rate)) # 具体操作 progress = re.sub("\D", "", self.result_progress()) # 时间进度 if int(progress) == 00000000: print('❌❌❌ Error- 听力时间进度', int(progress)) self.result.back_up_button() # 返回结果页 time.sleep(2) print('==============================================') @teststeps def error_sum(self, rate): """查看答案 滑屏 获取所有题目内容""" ques_last_index = 0 # 每个页面最后操作过的题号 for i in range(rate): quesnum = self.result_sentence() # 句子 ques_first_index = int(quesnum[0].text.split(".")[0]) if ques_first_index - ques_last_index > 1: # 判断页面是否滑过,若当前题比上一页做的题不大于1,则下拉直至题目等于上一题的加1 for step in range(0, 10): self.screen_swipe_down(0.5, 0.5, 0.62, 1000) if int(self.get_first_num()) == ques_last_index + 1: # 正好 quesnum = self.result_sentence() break elif int(self.get_first_num() ) < ques_last_index + 1: # 下拉拉过了 self.screen_swipe_up(0.5, 0.6, 0.27, 1000) # 滑屏 if int(self.get_first_num() ) == ques_last_index + 1: # 正好 quesnum = self.result_sentence() break last_one = self.get_last_element() # 最后一个题目 if int(rate) == ques_last_index: # 最后一题 break else: if self.question_judge(last_one): # 判断最后一项为题目 options = self.result_img() # 选项 for j in range(len(quesnum) - 1): print('-----------------------------') current_index = int(quesnum[j].text.split(".")[0]) if current_index > ques_last_index: print(quesnum[j].text) print('选项:', options[j]) ques_last_index = int(quesnum[-2].text.split(".")[0]) else: # 判断最后一题为选项 options = self.result_img() # 选项 for k in range(len(quesnum)): print('-----------------------------') if k < len(quesnum) - 1: # 前面的题目照常点击 current_index = int(quesnum[k].text.split(".")[0]) if current_index > ques_last_index: print(quesnum[k].text) print('选项:', options[k]) if k == len(quesnum) - 2: ques_last_index = int( quesnum[-2].text.split(".")[0]) elif k == len(quesnum) - 1: # 最后一个题目上滑一部分再进行选择 self.screen_swipe_up(0.5, 0.7, 0.4, 1000) quesnum = self.result_sentence() options = self.result_img() # 选项 for z in range(len(quesnum)): current_index = int( quesnum[z].text.split(".")[0]) if current_index > ques_last_index: print(quesnum[z].text) print('选项:', options[z]) ques_last_index = int( quesnum[z].text.split(".")[0]) break if i != rate - 1: self.screen_swipe_up(0.5, 0.9, 0.27, 1000) # 滑屏 @teststeps def study_again(self): """再练一遍 操作过程""" if self.result.wait_check_result_page(): # 结果页检查点 self.result.again_button() # 结果页 再练一遍 按钮 print('再练一遍:') self.picture_dictation() # 游戏过程 if self.result.wait_check_result_page(): # 结果页检查点 HomePage().click_back_up_button()
class StrengthenSentence(BasePage): """强化炼句""" def __init__(self): self.bounds = ClickBounds() self.result = ResultPage() self.key = Keyboard() self.get = GetAttribute() # 以下为 共有元素 @teststeps def wait_check_page(self): """以“title:强化炼句”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@text,'强化炼句')]") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def wait_check_play_page(self): """以“rate”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," "'{}rate')]".format(self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def rate(self): """获取作业数量""" rate = self.driver\ .find_element_by_id(self.id_type() + "rate").text return rate @teststep def time(self): """获取作业时间""" rate = self.driver\ .find_element_by_id(self.id_type() + "time").text return rate @teststep def content_value(self): """获取整个 外框元素""" ele = self.driver \ .find_element_by_id(self.id_type() + "input2") return ele @teststeps def content_desc(self): """点击输入框,激活小键盘""" content = self.get.get_description(self.content_value()) item_x = re.match(".*\[(.*)\].*\[", content) # x值 item_y = re.match(".*\[(.*)\].*", content) # y值 x = item_x.group(1).split(',') # 所有输入框y值的列表 y = item_y.group(1).split(',') # 所有输入框x值的列表 return x, y @teststeps def get_result(self): """点击输入框,激活小键盘""" content = self.get.get_description(self.content_value()) value = re.match("\\[(.+?)\\]", content) # answer answer = value.group(1).split(',') # 所有输入框值的列表 return answer @teststep def click_voice(self): """页面内音量按钮""" self.driver \ .find_element_by_id(self.id_type() + "play_voice") \ .click() @teststeps def sentence(self): """展示的句子""" word = self.driver \ .find_element_by_id(self.id_type() + "input2").text return word @teststep def explain(self): """展示的翻译""" word = self.driver \ .find_element_by_id(self.id_type() + "explain").text return word # 每小题回答完,下一步按钮后展示答案的页面 @teststeps def correct_title(self): """展示的答案title:正确答案 的ID为依据""" locator = (By.ID, self.id_type() + "correct_title") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def correct(self): """展示的答案""" word = self.driver \ .find_element_by_id(self.id_type() + "correct").text ele = word[:-1] # 去掉最后的标点符号 return ele # 查看答案页面 @teststeps def result_question(self): """展示的题目""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_hint") word = [] for i in range(len(ele)): word.append(ele[i].text) return word @teststeps def result_answer(self): """展示的 正确答案""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_answer") word = [] for i in range(len(ele)): word.append(ele[i].text) return word @teststep def result_mine_state(self, index): """我的答案对错标识 selected属性""" word = self.driver \ .find_elements_by_id(self.id_type() + "iv_mine")[index].get_attribute('selected') return word @teststeps def diff_type(self, tpe): """选择 不同模式小游戏的 游戏方法""" print(tpe) if tpe == '默写模式': answer = self.dictation_pattern() elif tpe == '自定义模式': answer = self.custom_pattern() elif tpe == '简单模式': answer = self.easy_pattern() else: # 复杂模式 answer = self.random_pattern() return answer @teststeps def random_pattern(self): """《强化炼句 复杂模式》 游戏过程""" if self.wait_check_page(): # 页面检查点 if self.wait_check_play_page(): answer = [] timestr = [] # 获取每小题的时间 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 Homework().next_button_operate('false') # 下一题 按钮 判断加 点击操作 value = self.input_text() # 激活输入框并进行输入 Homework().next_button_operate('true') # 下一题 按钮 状态判断加 点击操作 ,进入答案页面 self.correct_judge(value, i) # 判断答题是否正确 timestr.append(self.time()) # 统计每小题的计时控件time信息 Homework().next_button_operate('true') # 下一题 按钮 状态判断加 点击操作 print('-------------------------------') Homework().now_time(timestr) # 判断游戏界面 计时功能控件 是否在计时 print('======================================================') return rate, answer @teststeps def custom_pattern(self): """《强化炼句 自定义模式》 游戏过程""" if self.wait_check_page(): # 页面检查点 if self.wait_check_play_page(): answer = [] timestr = [] # 获取每小题的时间 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 Homework().next_button_operate('false') # 下一题 按钮 状态判断 点击 value = self.input_text() # 激活输入框并进行输入 Homework().next_button_operate('true') # 下一题 按钮 状态判断 点击 if self.correct_title(): result = self.correct_judge(value, i) # 判断答题是否正确 answer.append(result[0]) timestr.append(self.time()) # 统计每小题的计时控件time信息 Homework().next_button_operate('true') # 下一题 按钮 状态判断 点击 print('-------------------------------') Homework().now_time(timestr) # 判断游戏界面 计时功能控件 是否在计时 print('======================================================') return rate, answer @teststeps def easy_pattern(self): """《强化炼句 简单模式》 游戏过程""" if self.wait_check_page(): # 页面检查点 if self.wait_check_play_page(): answer = [] timestr = [] # 获取每小题的时间 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 Homework().next_button_operate('false') # 下一题 按钮 状态判断 点击 value = self.input_text() # 激活输入框并进行输入 Homework().next_button_operate('true') # 下一题 按钮 状态判断 点击,进入答案页面 result = self.correct_judge(value, i) # 判断答题是否正确 answer.append(result[0]) timestr.append(self.time()) # 统计每小题的计时控件time信息 Homework().next_button_operate('true') # 下一题 按钮 状态判断 点击 print('-------------------------------') Homework().now_time(timestr) # 判断游戏界面 计时功能控件 是否在计时 print('======================================================') return rate, answer @teststeps def dictation_pattern(self): """《强化炼句 默写模式》 游戏过程""" if self.wait_check_page(): # 页面检查点 if self.wait_check_play_page(): answer = [] timestr = [] # 获取每小题的时间 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 Homework().next_button_operate('false') # 下一题 按钮 状态判断 点击 value = self.input_text() # 激活输入框并进行输入 Homework().next_button_operate('true') # 下一题 按钮 状态判断 点击,进入答案页面 result = self.correct_judge(value, i) # 判断答题是否正确 answer.append(result[0]) timestr.append(self.time()) # 统计每小题的计时控件time信息 Homework().next_button_operate('true') # 下一题 按钮 状态判断 点击 print('-----------------------------') Homework().now_time(timestr) # 判断游戏界面 计时功能控件 是否在计时 print('==================================================') return rate, answer @teststeps def input_text(self): """激活输入框 并 输入内容""" explain = self.explain() # 解释的内容 sentence = self.sentence().split(' ') # 句子 print('题目:', self.sentence()) value = strength_sentence_operate(explain) # 数据字典 words = value.split(' ') # 将句子分割成单词 word = [] for i in range(len(words)): if (self.is_alphabet(words[i])) and (words[i] not in sentence): word.append(words[i]) for j in range(len(word)): item = self.content_desc() # 获取输入框坐标 loc = self.get_element_location(self.content_value()) # 文章元素左上角 顶点坐标 x = float(item[0][j]) + loc[0]+55.0 # 55.0为点击输入框中心点的偏移量 y = float(item[1][j]) + loc[1] self.driver.tap([(x, y)]) # 点击激活输入框 for z in range(len(word[j])): if j == len(word)-1: if word[j][z] in ['.', '?', '!']: # 去掉最后的标点符号 break if z == 4 and z != len(word[j])-1: self.key.games_keyboard('capslock') # 点击键盘 切换到 大写字母 self.key.games_keyboard(word[j][z].upper()) # 点击键盘对应 大写字母 self.key.games_keyboard('capslock') # 点击键盘 切换到 小写字母 else: if j == 2 and word[j][z] == "'": self.key.games_keyboard(',') # 第二小题 点击键盘 逗号 else: self.key.games_keyboard(word[j][z]) # 点击键盘对应字母 return value @teststeps def correct_judge(self, value, i): """每小题回答完,下一步按钮后展示答案的页面""" if self.correct_title(): # 展示的答案title元素是否存在 result = self.get_result() # content-desc的值 answer = self.sentence() # 展示的本人的答案 for j in range(len(result)): for k in range(len(answer)): if answer[k] == '{': if len(answer) != 2: if k == 0: # 展示的本人的答案 result[j] answer = result[j].strip() + answer[k + 2:] elif k + 1 == len(answer) - 1: if ' ' not in result[j]: answer = answer[:k - 1] + ' ' + result[j] else: answer = answer[:k - 1] + result[j] else: if ' ' not in result[j]: answer = answer[:k - 1] + ' ' + result[j] + answer[k + 2:] else: answer = answer[:k - 1] + result[j] + answer[k + 2:] break if answer[len(answer)-1] == ' ': answer = answer[:-1] correct = self.correct() # 展示的正确答案 print('我的答案:', answer) if correct == value: # 测试展示的答案是否正确 if answer == correct: print('回答正确') else: print('回答错误') return answer, i @teststeps def study_again(self, tpe): """《强化炼句》 再练一遍 操作过程""" print('再练一遍 操作过程:') if self.result.wait_check_result_page(): # 结果页检查点 self.result.again_button() # 结果页 再练一遍 按钮 result = self.diff_type(tpe) # 强化炼句 - 游戏过程 return '再练一遍按钮', result[0], result[1] @teststeps def check_detail_page(self, i, answer): """查看答案页面""" if self.result.wait_check_result_page(): # 结果页检查点 self.result.check_result_button() # 查看答案 按钮 if self.result.wait_check_detail_page(): print('结果页 - 查看答案 按钮:') if int(i) <= 16: self.result_operate(answer) else: item = self.result_question() if int(i) % len(item) == 0: page = int(int(i) / len(item)) else: page = int(int(i) / len(item)) + 1 print('页数:', page) for j in range(page): last_one = self.result_operate(answer) # 滑动前页面内最后一个小题- 做题结果 self.screen_swipe_up(0.5, 0.75, 0.35, 1000) item_2 = self.result_question() # 滑动后页面内的题目 的数量 if item_2[len(item_2) - 1].text == last_one: print('到底啦', last_one) self.result.back_up_button() break elif item_2[len(item_2) - 1].text == answer[len(answer) - 1]: # 滑动后到底,因为普通情况下最多只有两页,滑动一次即可到底 print('滑动后到底', last_one) k = [] for i in range(len(item_2) - 1, -1, -1): # 倒序 if item_2[i].text == last_one: k.append(i + 1) break self.result_operate(answer, k[0]) break else: continue self.screen_swipe_down(0.5, 0.75, 0.35, 1000) time.sleep(2) self.result.back_up_button() @teststeps def result_operate(self, var, index=0): """查看答案页面 -- 展示的解释内容验证""" explain = self.result_question() # 题目 answer = self.result_answer() # 正确答案 print(answer, var) for i in range(index, len(explain)): count = [] value = strength_sentence_operate(explain[i]).split(' ') if answer[i] == var[i]: # 测试结果页 我的答案展示是否正确 if answer[i] == value: # 测试 正确答案 for j in range(len(var)): # 我的答案 与 正确答案 比较 if var[j] != value[j]: # 答案不正确 count+1 count.append(j) if self.result_mine_state() != 'false': print('❌❌❌ Error - 我的答案:%s 与 正确答案:%s 对错标识:%s' % (var[j], value[j], 'true')) else: if self.result_mine_state() != 'true': print('❌❌❌ Error - 我的答案:%s 与 正确答案:%s 对错标识:%s' % (var[j], value[j], 'false')) break else: print('❌❌❌ Error - 正确答案:', answer[i], value) print('------------------------------------') return var[1][len(var[1]) - 1]
class SingleChoice(BasePage): """单项选择""" def __init__(self): self.result = ResultPage() @teststeps def wait_check_page(self): """以“title:单项选择”的xpath-index为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@text,'单项选择')]") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def wait_check_play_page(self): """以“rate”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," "'{}rate')]".format(self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def rate(self): """获取作业数量""" rate = self.driver\ .find_element_by_id(self.id_type() + "rate").text return rate @teststep def time(self): """获取作业时间""" ele = self.driver\ .find_element_by_id(self.id_type() + "time").text return ele @teststep def question_content(self): """获取题目内容""" ele = self.driver \ .find_element_by_id(self.id_type() + "question").text return ele @teststep def option_button(self): """获取所有选项 - 四个选项""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_char") return ele @teststeps def option_selected(self, index): """获取所有选项 - 四个选项selected属性""" time.sleep(1) ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_char")[index] value = GetAttribute().get_selected(ele) return value @teststep def option_content(self, index): """获取所有选项 - 四个选项内容""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_char")[index].text return ele # 查看答案 页面 @teststeps def wait_check_detail_page(self): """以“answer”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," + self.id_type() + "tv_answer)]") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def single_choice_operate(self): """《单项选择》 游戏过程 返回当前大题的题数,正确数,正确题目内容, 最终完成时间""" if self.wait_check_page(): if self.wait_check_play_page(): count = 0 timestr = [] # 获取每小题的时间 questions = [] # 答对的题 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 Homework().next_button_operate('false') # 下一题 按钮 判断加 点击操作 item = self.question_content() # 题目 print('题目:', item) options = self.option_button() # 四个选项 options[random.randint(0, len(options) - 1)].click() # 随机点击选项 ele = [] # 四个选项selected属性值为true的个数 for j in range(len(options)): # 统计答案正确与否 if self.option_selected(j) == 'true': ele.append(j) if len(ele) == 1: # 如果选项的selected属性为true的作业数为1,说明答对了,则+1 index = ele[0] print('回答正确:', self.option_content(index)) questions.append(self.question_content()) count += 1 else: print('回答错误:', ele) timestr.append(self.time()) # 统计每小题的计时控件time信息 Homework().next_button_operate('true') # 下一题 按钮 状态判断 加点击 print('--------------------') Homework().now_time(timestr) # 判断游戏界面 计时功能控件 是否在计时 final_time = ResultPage().get_time(timestr[len(timestr) - 1]) # 最后一个小题的时间 print('=======================================') return rate, count, questions, final_time @teststeps def check_detail_page(self): """《单项选择》 查看答案 操作过程""" if self.result.wait_check_result_page(): # 结果页检查点 self.result.check_result_button() # 结果页 查看答案 按钮 if self.result.wait_check_detail_page(): print('查看答案页面:') for i in range(2): self.screen_swipe_up(0.5, 0.75, 0.25, 1000) self.screen_swipe_down(0.5, 0.25, 0.75, 1000) self.result.back_up_button() # 返回结果页 @teststeps def study_again(self): """《单项选择》 错题再练 操作过程""" print('==================================================') if self.result.wait_check_result_page(): # 结果页检查点 self.result.error_again_button() # 结果页 错题再练 按钮 print('错题再练:') result = self.single_choice_operate() # 单项选择 - 游戏过程 return '错题再练按钮', result[0], result[2], result[3]
class SentenceTrans(BasePage): """句型转换""" # 以下为 共有元素 def __init__(self): self.result = ResultPage() @teststeps def wait_check_page(self): """以“句型转换”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@text,'句型转换')]") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def wait_check_play_page(self): """以“rate”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," "'{}rate')]".format(self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def rate(self): """获取作业数量""" rate = self.driver \ .find_element_by_id(self.id_type() + "rate").text return rate @teststep def time(self): """获取作业时间""" rate = self.driver \ .find_element_by_id(self.id_type() + "time").text return rate @teststep def clear_button(self): """页面内清除按钮""" self.driver \ .find_element_by_id(self.id_type() + "bt_clear").click() @teststeps def clear_button_judge(self): """页面内清除按钮""" try: self.driver \ .find_element_by_id(self.id_type() + "bt_clear") return True except: return False @teststep def question_content(self): """展示的题目 - 句子""" ele = self.driver \ .find_element_by_id(self.id_type() + "tv_question").text return ele @teststeps def mine_answer(self): """展示的 我的答案""" ele = self.driver \ .find_elements_by_xpath("//android.widget.RelativeLayout[contains(@index,1)]/descendant::android.widget.TextView") words = [] for i in range(len(ele)): words.append(ele[i].text) print('我的答案:', words) return ele, words @teststep def word(self): """展示的 待还原的单词""" ele = self.driver \ .find_elements_by_xpath("//android.widget.RelativeLayout[contains(@index,2)]/descendant::android.widget.TextView") return ele # 每小题回答完,下一步按钮后展示答案的页面 @teststeps def correct_title(self): """展示的答案 的ID为依据""" locator = (By.ID, self.id_type() + "tv_answer") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def mine_result(self): """展示的答题结果""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_text") word = [] for i in range(len(ele)): word.append(ele[i].text) print('我的答题结果:', word) return word @teststep def correct_answer(self): """点击 下一题 按钮之后展示的答案""" ele = self.driver \ .find_element_by_id(self.id_type() + "tv_answer").text word = ele[2:].split(' ') return ele, word # 查看答案页面 @teststeps def wait_check_detail_page(self): """以“answer”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," "'{}tv_answer')]".format(self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def result_question(self): """展示的题目""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_question") word = [] for i in range(len(ele)): word.append(ele[i].text) return word @teststeps def result_answer(self): """展示的 正确答案""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_answer") word = [] for i in range(len(ele)): word.append(ele[i].text) return word @teststeps def result_mine(self): """我的答案""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_mine") words = [] for i in range(len(ele)): words.append(ele[i].text) word = words[0].split(' ') return words, word @teststeps def result_mine_state(self, index): """我的答案对错标识 selected属性""" word = self.driver \ .find_elements_by_id(self.id_type() + "iv_mine")[index] value = GetAttribute().get_selected(word) return value @teststeps def sentence_transform(self): """《句型转换》 游戏过程""" if self.wait_check_page(): # 页面检查点 if self.wait_check_play_page(): count = [] # 做题结果 answer = [] timestr = [] # 获取每小题的时间 rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 if not self.clear_button_judge(): # 判断清除按钮存在 print('❌❌❌ Error - 清除按钮不存在!!') question = self.question_content() # 展示的题目内容 value = sentence_transform_operate(question).split(' ') self.restore_word(value) # 填入单词 具体过程 var = self.mine_answer() # 到目前为止我填入的答案 if i == 0: print('第%s题 - 点击框中单词,移出框中' % (i + 1)) self.remove_word(var) # 点击框中单词,是否可以移出框中 elif i == 1: print('第%s题 - 点击清除按钮' % (i + 1)) self.clear_button() # 点击清除按钮 self.restore_word(value) # 填入单词 具体过程 print('-------------') timestr.append(self.time()) # 统计每小题的计时控件time信息 Homework().next_button_operate('true') # 下一题 按钮 状态判断 加点击 if self.correct_title(): # 页面检查点 result = self.mine_result() # 做题结果 correct = self.correct_answer()[1] # 正确答案-- 分解成单词 for k in range(len(result)): # 做错 count+1 if correct[k] != result[k]: count.append(k) break answer.append(result) # 做题结果 Homework().next_button_operate('true') # 下一题 按钮 状态判断 加点击 print('--------------------------------') Homework().now_time(timestr) # 判断游戏界面 计时功能控件 是否在计时 print('========================================') return rate, answer @teststeps def restore_word(self, value): """填入单词 具体过程""" time.sleep(2) for j in range(len(value)): words = self.word() # 待还原的单词 for k in range(len(words)): if words[k].text == value[j]: Homework().next_button_operate('false') # 下一题 按钮 状态判断 加点击 words[k].click() break @teststeps def remove_word(self, var): """点击框中单词,是否可以移出框中""" word = [] for i in range(len(var[0])): var[0][i].click() # 移出框中 word.append(var[1][i]) self.restore_word(word) # 将移出框中的单词重新填入 @teststeps def check_detail_page(self, i, answer): """查看答案页面""" if self.result.wait_check_result_page(): print('查看答案页面:') self.result.check_result_button() if self.result.wait_check_detail_page(): if self.wait_check_detail_page(): if int(i) <= 16: self.result_operate(answer) else: item = self.result_question() if int(i) % len(item) == 0: page = int(int(i) / len(item)) else: page = int(int(i) / len(item)) + 1 print('页数:', page) for j in range(page): last_one = self.result_operate( answer) # 滑动前页面内最后一个小题- 做题结果 self.screen_swipe_up(0.5, 0.75, 0.35, 1000) item_2 = self.result_question() # 滑动后页面内的题目 的数量 if item_2[len(item_2) - 1].text == last_one: print('到底啦', last_one) self.result.back_up_button() break elif item_2[len(item_2) - 1].text == answer[len(answer) - 1]: # 滑动后到底,因为普通情况下最多只有两页,滑动一次即可到底 print('滑动后到底', last_one) k = [] for i in range(len(item_2) - 1, -1, -1): # 倒序 if item_2[i].text == last_one: k.append(i + 1) break self.result_operate(answer, k[0]) break else: continue self.screen_swipe_down(0.5, 0.75, 0.35, 1000) time.sleep(2) self.result.back_up_button() # 返回结果页 @teststeps def result_operate(self, var, index=0): """查看答案页面 -- 展示的解释内容验证""" explain = self.result_question() # 题目 word = self.result_mine() # 我的答案 answer = self.result_answer() # 正确答案 for i in range(index, len(explain)): count = [] value = sentence_transform_operate(explain[i]).split(' ') if answer[i] == var[i]: # 测试结果页 我的答案展示是否正确 if answer[i] == value: # 测试 正确答案 for j in range(len(word[1])): # 我的答案 与 正确答案 比较 if word[1][j] != value[j]: # 答案不正确 count+1 count.append(j) break if count == 0: if self.result_mine_state() != 'true': print('❌❌❌ Error - 我的答案:%s 与 正确答案:%s 对错标识:%s' % (word[0], value, 'false')) else: if self.result_mine_state() != 'false': print('❌❌❌ Error - 我的答案:%s 与 正确答案:%s 对错标识:%s' % (word[0], value, 'true')) else: print('❌❌❌ Error - 正确答案:', answer[i], value) print('--------------------------------') return word[1][len(word[1]) - 1] @teststeps def study_again(self): """《句型转换》 再练一遍 操作过程""" print('========================================') if self.result.wait_check_result_page(): # 结果页检查点 self.result.again_button() # 结果页 再练一遍 按钮 print('再练一遍:') result = self.sentence_transform() # 游戏过程 return '再练一遍按钮', result[0], result[1]
class MatchingExercises(BasePage): """连连看""" def __init__(self): self.result = ResultPage() @teststeps def wait_check_page(self): """以“title:连连看”的xpath-index为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@text,'连连看')]") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def wait_check_play_page(self): """以“rate”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," "'{}rate')]".format(self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def rate(self): """获取作业数量""" rate = self.driver\ .find_element_by_id(self.id_type() + "rate").text return rate @teststep def time(self): """获取作业时间""" rate = self.driver\ .find_element_by_id(self.id_type() + "time").text return rate @teststep def word(self): """展示的Word""" ele = self.driver \ .find_elements_by_class_name("android.widget.TextView") return ele # 以下为答案详情页面元素 @teststeps def wait_check_detail_page(self): """以“answer”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," "'{}tv_answer')]".format(self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def result_voice(self, index): """语音按钮""" self.driver \ .find_elements_by_id(self.id_type() + "iv_speak")[index] \ .click() @teststep def result_answer(self, index): """单词""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_answer")[index].text return ele @teststep def result_explain(self, index): """解释""" ele = self.driver \ .find_elements_by_id(self.id_type() + "tv_hint")[index].text return ele @teststep def result_mine(self, index): """我的""" ele = self.driver \ .find_elements_by_id(self.id_type() + "iv_mine")[index] value = GetAttribute().get_selected(ele) return value @teststeps def match_exercise(self): """《连连看》 游戏过程""" if self.wait_check_page(): # 页面检查点 if self.wait_check_play_page(): timestr = [] # 获取每小题的时间 rate = self.rate() if int(rate) % 5 == 0: page = int(int(rate)/5) else: page = int(int(rate) / 5) + 1 print('页数:', page) for j in range(page): # 然后在不同页面做对应的题目 print('第%s页:' % (j+1)) word = [] # 单词list word_index = [] # 单词在所有button中的索引 explain = [] # 解释list explain_index = [] # 解释在所有button中的索引 ele = self.word() # 所有button for i in range(3, len(ele)): if self.is_alphabet(ele[i].text[0]): # 如果是字母 word.append(ele[i].text) word_index.append(i) else: # 如果是汉字 explain.append(ele[i].text) explain_index.append(i) # print(word_index, study_word, explain, explain_index) for k in range(len(word)): # 具体操作 Homework().rate_judge(rate, k+j*4) # 测试当前rate值显示是否正确 value = match_operate(word[k]) # 数据字典 ele[word_index[k]].click() # 点击解释 for z in range(len(explain)): if explain[z] == value: timestr.append(self.time()) # 统计每小题的计时控件time信息 ele[explain_index[z]].click() # 点击对应word # # if k == 1: # 测试 配对成功后,不可再次点击 # ele[word_index[k]].click() print('--------------------------') break time.sleep(1) print('=================================') Homework().now_time(timestr) # 判断游戏界面 计时功能控件 是否在计时 return rate @teststeps def result_detail_page(self, rate): """《连连看》 查看答案 操作过程""" if self.result.wait_check_result_page(): # 结果页检查点 self.result.check_result_button() # 结果页 查看答案 按钮 if self.result.wait_check_detail_page(): if self.wait_check_detail_page(): print('==============================================') print('查看答案:') self.error_sum(rate) time.sleep(2) @teststeps def error_sum(self, rate): """查看答案 - 点击答错的题 对应的 听力按钮""" print('题数:', int(rate)) for i in range(0, int(rate)): print('解释:', self.result_explain(i)) # 解释 print('单词:', self.result_answer(i)) # 正确word mine = self.result_mine(i) # 对错标识 if mine != 'true': print('❌❌❌ Error - 对错标识') else: print('对错标识:', mine) print('-----------------------------------') self.result_voice(i) # 点击发音按钮 self.result.back_up_button() # 返回结果页 @teststeps def study_again(self): """再练一遍 操作过程""" if self.result.wait_check_result_page(): # 结果页检查点 self.result.again_button() # 结果页 再练一遍 按钮 print('==============================================') print('再练一遍:') self.match_exercise() # 游戏过程
class RestoreWord(BasePage): """还原单词""" def __init__(self): self.result = ResultPage() @teststeps def wait_check_page(self): """以“还原单词”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@text,'还原单词')]") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststeps def wait_check_play_page(self): """以“rate”的ID为依据""" locator = (By.XPATH, "//android.widget.TextView[contains(@resource-id," "'{}rate')]".format(self.id_type())) try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def rate(self): """获取作业数量""" rate = self.driver\ .find_element_by_id(self.id_type() + "rate").text return rate @teststep def time(self): """获取作业时间""" rate = self.driver\ .find_element_by_id(self.id_type() + "time").text return rate @teststep def click_voice(self): """页面内音量按钮""" self.driver \ .find_element_by_id(self.id_type() + "fab_sound") \ .click() time.sleep(1) @teststep def prompt(self): """展示的提示词""" ele = self.driver \ .find_element_by_id(self.id_type() + "tv_prompt").text return ele @teststep def word(self): """展示的 待还原的单词""" word = self.driver \ .find_elements_by_id(self.id_type() + "tv_word") return word # 查看答案页面 @teststeps def wait_check_detail_page(self): """以“answer”的ID为依据""" locator = (By.ID, self.id_type() + "tv_answer") try: WebDriverWait(self.driver, 20, 0.5).until(lambda x: x.find_element(*locator)) return True except: return False @teststep def answer(self): """展示的提示词""" word = self.driver \ .find_elements_by_id(self.id_type() + "tv_answer") return word @teststep def hint(self): """展示的 解释""" word = self.driver \ .find_elements_by_id(self.id_type() + "tv_hint") return word @teststep def voice_button(self, index): """页面内音量按钮""" self.driver \ .find_elements_by_id(self.id_type() + "iv_speak")[index] \ .click() @teststep def result_mine(self, index): """我的""" ele = self.driver \ .find_elements_by_id(self.id_type() + "iv_mine")[index] value = GetAttribute().get_selected(ele) return value @teststep def button_swipe(self, from_x, from_y, to_x, to_y, steps=1000): """拖动单词button""" self.driver.swipe(from_x, from_y, to_x, to_y, steps) @teststeps def restore_word(self): """《还原单词》 游戏过程""" if self.wait_check_page(): # 页面检查点 if self.wait_check_play_page(): timestr = [] # 获取每小题的时间 answer = [] rate = self.rate() for i in range(int(rate)): Homework().rate_judge(rate, i) # 测试当前rate值显示是否正确 Homework().next_button_operate('false') # 下一题 按钮 判断加 点击操作 self.voice_operate(i) # 听力按钮 explain = self.prompt() # 展示的提示词 value = restore_word_operate(explain) word = self.word() # 展示的单词 if value == 'aa': self.drag_operate(word[1], word[0]) # 拖拽到第一个位置 else: for z in range(len(value) - 1, -1, -1): # 倒序 words = self.word() for k in range(len(words)): letter = words[k].text if letter[0] == value[z] and k != 0: self.drag_operate(words[k], words[0]) # 拖拽到第一个位置 break timestr.append(self.time()) # 统计每小题的计时控件time信息 var = self.word() # 元素 tv_word # if int(self.rate())+1 == int(rate) - i and len(var) == len(study_word)+1: # todo 判断 是否进入答案页 text = [] # 元素 tv_word的text for z in range(len(var)): text.append(var[z].text) print('我的答案:', text[1:]) print('正确答案:', text[0]) if i != int(rate) - 1: Homework().next_button_operate('true') # 下一题 按钮 判断 answer.append(text[0]) print('----------------------------------------') time.sleep(2) Homework().now_time(timestr) # 判断游戏界面 计时功能控件 是否在计时 print('===========================================') return rate, answer @teststeps def drag_operate(self, word2, word): """获取单词button坐标 及拖拽""" loc = self.get_element_location(word2) y2 = self.get_element_location(word)[1] - 40 self.button_swipe(loc[0], loc[1], loc[0], y2, 1000) time.sleep(1) @teststeps def voice_operate(self, i): """听力按钮 操作""" if i == 2: # 第3题 j = 0 print('多次点击发音按钮:') while j < 4: print(j) self.click_voice() # 多次点击发音按钮 j += 1 time.sleep(1) else: self.click_voice() # 点击 发音按钮 @teststeps def check_detail_page(self, i, answer): """查看答案页面""" if self.result.wait_check_result_page(): print('查看答案页面:') self.result.check_result_button() # 点击查看答案按钮 if self.result.wait_check_detail_page(): if self.wait_check_detail_page(): print('判断是否滑动:', i) if int(i) <= 16: self.result_operate() else: item = self.hint() if int(i) % len(item) == 0: page = int(int(i) / len(item)) else: page = int(int(i) / len(item)) + 1 print('页数:', page) for j in range(page): last_one = self.result_operate( ) # 滑动前页面内最后一个小游戏title self.screen_swipe_up(0.5, 0.75, 0.35, 1000) item_2 = self.hint() # 滑动后页面内的解释 的数量 if item_2[len(item_2) - 1].text == last_one: print('到底啦', last_one) self.result.back_up_button() break elif item_2[len(item_2) - 1].text == answer[len(answer) - 1]: # 滑动后到底,因为普通情况下最多只有两页,滑动一次即可到底 print('滑动后到底', last_one) k = [] for i in range(len(item_2) - 1, -1, -1): # 倒序 if item_2[i].text == last_one: k.append(i + 1) break self.result_operate(k[0]) break else: continue self.screen_swipe_down(0.5, 0.75, 0.35, 1000) self.result.back_up_button() # 返回结果页 time.sleep(2) @teststeps def result_operate(self, index=0): """查看答案页面 -- 展示的解释内容验证""" explain = self.hint() # 解释 word = self.answer() # 题目 for i in range(index, len(explain)): key = explain[i].text value = restore_word_operate(key) if word[i].text == value: print('对错标识:', self.result_mine(i)) # 对错标识 self.voice_button(i) # 结果页 - 听力按钮 else: print('❌❌❌ Error -解释内容与题中不一致:', key, word[i].text, value) print('----------------------------------') return explain[-1].text