Example #1
0
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

    @teststep
    def rate(self):
        """获取作业数量"""
        rate = self.driver\
            .find_element_by_id("com.vanthink.student.debug:id/rate").text
        return rate

    @teststep
    def time(self):
        """获取作业时间"""
        rate = self.driver\
            .find_element_by_id("com.vanthink.student.debug:id/time").text
        return rate

    @teststep
    def word(self):
        """展示的Word"""
        ele = self.driver \
            .find_elements_by_class_name("android.widget.TextView")
        return ele

    @teststep
    def explain(self):
        """展示的翻译"""
        word = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_explain")
        return word

    # 以下为答案详情页面元素
    @teststep
    def result_voice(self, index):
        """语音按钮"""
        self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/iv_speak")[index] \
            .click()

    @teststep
    def result_answer(self, index):
        """单词"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/tv_answer")[index].text
        return ele

    @teststep
    def result_explain(self, index):
        """解释"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/tv_hint")[index].text
        return ele

    @teststep
    def result_mine(self, index):
        """我的"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/iv_mine")[index].get_attribute("selected")
        return ele

    @teststep
    def back_up_button(self):
        """以“返回按钮”的class name为依据"""
        self.driver \
            .find_element_by_class_name("android.widget.ImageButton") \
            .click()

    @teststeps
    def match_exercise(self):
        """《连连看》 游戏过程"""
        if self.wait_check_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, word, explain, explain_index)

                for k in range(len(word)):  # 具体操作
                    Homework().rate_judge(rate, k + j * 4,
                                          self.rate())  # 测试当前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():
                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
            mine = self.result_mine(i)  # 对错标识
            if mine != 'true':
                print('★★★ Error - 对错标识')
            else:
                print('对错标识:', mine)
            print('-----------------------------------')

            self.result_voice(i)  # 点击发音按钮
        self.back_up_button()  # 返回结果页

    @teststeps
    def study_again(self):
        """再练一遍 操作过程"""
        if self.result.wait_check_result_page():  # 结果页检查点
            self.result.again_button()  # 结果页 再练一遍 按钮
            print('再练一遍:')
            self.match_exercise()  # 游戏过程
class WordSpelling(BasePage):
    """单词拼写"""
    def __init__(self):
        self.result = ResultPage()

    # 以下为 共有元素
    @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

    @teststep
    def rate(self):
        """获取作业数量"""
        rate = self.driver\
            .find_element_by_id("com.vanthink.student.debug:id/rate").text
        return rate

    @teststep
    def time(self):
        """获取作业时间"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/time").text
        return ele

    @teststep
    def click_voice(self):
        """页面内音量按钮"""
        self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/play_voice") \
            .click()

    @teststep
    def word(self):
        """展示的Word"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_word").text
        word = ele[1::2]
        print('word:', word)
        return word

    @teststep
    def explain(self):
        """展示的翻译"""
        word = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_explain").text
        print("解释:",word)
        return word

    @teststep
    def finish_word(self):
        """完成答题 之后 展示的Word 前后含额外字符:aa"""
        word = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_word").text
        return word[1::2]

    # 默写模式 特有元素
    @teststep
    def dictation_word(self):
        """展示的Word"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_word").text
        return ele[::2]

    @teststep
    def dictation_word_judge(self):
        """判断是否展示Word"""
        ele = self.find_element("com.vanthink.student.debug:id/tv_word")
        return ele

    @teststep
    def under_line(self):
        """展示的横线"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/underline")
        return ele

    # 下一步 按钮之后 答案页展示的答案
    @teststep
    def mine_answer(self):
        """展示的Word  前后含额外字符:aa"""
        word = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_word").text
        return word[1::2]

    @teststep
    def question(self):
        """展示的翻译"""
        word = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_explain").text
        return word

    @teststep
    def correct(self):
        """展示的答案"""
        word = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_answer").text
        return word

    @teststep
    def correct_judge(self):
        """判断 答案是否展示"""
        ele = self.find_element("com.vanthink.student.debug:id/tv_answer")
        return ele

    # 默写模式 答案页特有元素
    @teststep
    def dictation_finish_word(self):
        """完成答题 之后 展示的Word  前后不含额外字符:aa"""
        word = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_word").text
        return word[::2]

    @teststep
    def dictation_mine_answer(self):
        """展示的Word  前后不含额外字符:aa"""
        word = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_word").text
        return word[::2]

    # 以下为答案详情页面元素
    @teststep
    def result_voice(self, index):
        """语音按钮"""
        self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/iv_speak")[index] \
            .click()

    @teststep
    def result_answer(self, index):
        """单词"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/tv_answer")[index].text
        return ele

    @teststep
    def result_explain(self, index):
        """解释"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/tv_hint")[index].text
        return ele

    @teststep
    def result_mine(self, index):
        """我的"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/iv_mine")[index].get_attribute("selected")
        return ele

    @teststep
    def back_up_button(self):
        """以“返回按钮”的class name为依据"""
        self.driver \
            .find_element_by_class_name("android.widget.ImageButton") \
            .click()

    @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():  # 页面检查点
            var = []  # 随机消除的字母
            count = []   # 做错的题目
            answer = []   # return值 与结果页内容比对
            timestr = []  # 获取每小题的时间
            rate = self.rate()
            for i in range(int(rate)):
                Homework().rate_judge(rate, i, self.rate())  # 测试当前rate值显示是否正确
                Homework().next_button_operate('false')  # 下一题 按钮 判断加 点击操作

                explain = self.explain()  # 题目
                word = self.word()  # 未缺失的字母
                value = word_spelling_operate(explain)  # 数据字典

                if self.is_chinese(explain):
                    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():  # 页面检查点
            count = []  # 做错的题目
            answer = []  # return值 与结果页内容比对
            timestr = []  # 获取每小题的时间
            rate = self.rate()
            for i in range(int(rate)):
                Homework().rate_judge(rate, i, self.rate())  # 测试当前rate值显示是否正确
                Homework().next_button_operate('false')  # 下一题 按钮 判断加 点击操作

                explain = self.explain()  # 题目
                word = self.word()  # 未缺失的字母
                value = word_spelling_operate(explain)  # 数据字典

                if self.is_chinese(explain):  # 解释内容为汉字
                    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():  # 页面检查点
            count = []
            answer = []  # return值 与结果页内容比对
            timestr = []  # 获取每小题的时间
            rate = self.rate()
            for i in range(int(rate)):
                Homework().rate_judge(rate, i, self.rate())  # 测试当前rate值显示是否正确
                Homework().next_button_operate('false')  # 下一题 按钮 判断 加点击

                explain = self.explain()  # 题目
                value = word_spelling_operate(explain)  # 数据字典

                if self.is_chinese(explain):  # 解释内容为汉字
                    if self.dictation_word_judge():  # 默写模式 - 字母未全部消除
                        print('★★★ Error - 单词拼写 默写模式 - 字母未全部消除')
                        word = self.dictation_word()
                        for j in range(0, len(value)):
                            if word[j] != value[j]:
                                print('缺失的字母:', value[j])
                                self.keyboard_operate(j, value[j])  # 点击键盘 具体操作
                            else:
                                print('未消除的字母:', value[j])
                    else:
                        for j in range(0, 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:
            games_keyboard('capslock')  # 点击键盘 切换到 大写字母
            games_keyboard(value.upper())  # 点击键盘对应 大写字母
            games_keyboard('capslock')  # 点击键盘 切换到 小写字母
        else:
            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('查看答案:')
                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.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()

    @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

    @teststep
    def rate(self):
        """获取作业数量"""
        rate = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/rate").text
        return rate

    @teststep
    def time(self):
        """获取作业时间"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/time").text
        return ele

    @teststep
    def click_voice(self):
        """页面内音量按钮"""
        self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/play_voice") \
            .click()

    @teststep
    def word(self):
        """展示的Word  点击喇叭听写单词"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_word").text
        word = ele.replace(' ', '')  # 删除空格
        return word

    # 下一步 按钮之后 答案页展示的答案
    @teststep
    def mine_answer(self):
        """展示的Word """
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_word").text
        word = ele.replace(' ', '')  # 删除空格
        return word

    @teststep
    def question(self):
        """展示的翻译"""
        word = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_explain").text
        return word

    @teststep
    def correct(self):
        """展示的答案"""
        word = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_answer").text
        print('正确答案:', word)
        return word

    @teststep
    def correct_judge(self):
        """判断 答案是否展示"""
        ele = self.find_element("com.vanthink.student.debug:id/tv_answer")
        return ele

    # 以下为答案详情页面元素
    @teststep
    def result_voice(self, index):
        """语音按钮"""
        self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/iv_speak")[index] \
            .click()

    @teststep
    def result_answer(self, index):
        """单词"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/tv_answer")[index].text
        return ele

    @teststep
    def result_explain(self, index):
        """解释"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/tv_hint")[index].text
        return ele

    @teststep
    def result_mine(self, index):
        """我的"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/iv_mine")[index].get_attribute("selected")
        return ele

    @teststep
    def back_up_button(self):
        """以“返回按钮”的class name为依据"""
        self.driver \
            .find_element_by_class_name("android.widget.ImageButton") \
            .click()

    @teststeps
    def word_dictation(self):
        """《单词听写》 游戏过程"""
        if self.wait_check_page():  # 页面检查点
            count = []
            answer = []  # return值 与结果页内容比对
            timestr = []  # 获取每小题的时间
            rate = self.rate()
            for i in range(int(rate)):
                Homework().rate_judge(rate, i, self.rate())  # 测试当前rate值显示是否正确
                Homework().next_button_operate('false')  # 下一题 按钮 判断 加点击

                self.word()  # 灰字文案:点击喇叭听写单词
                self.click_voice()  # 点击喇叭

                word = VALID_WORD_DICT.word()  # 数据字典
                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)  # 判断游戏界面 计时功能控件 是否在计时
            return rate, answer

    @teststeps
    def keyboard_operate(self, j, value):
        """点击键盘 具体操作"""
        if j == 4:
            games_keyboard('capslock')  # 点击键盘 切换到 大写字母
            games_keyboard(value.upper())  # 点击键盘对应 大写字母
            games_keyboard('capslock')  # 点击键盘 切换到 小写字母
        else:
            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 normalize(self, string):
        for n in string:
            return n[:1].upper() + n[1:].lower()

    @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('查看答案:')
                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.back_up_button()  # 返回结果页

    @teststeps
    def study_again(self):
        """《》操作过程"""
        if self.result.wait_check_result_page():  # 结果页检查点
            self.result.again_button()  # 结果页 再练一遍 按钮
            print('再练一遍:')
            self.word_dictation()  # 游戏过程
class StrengthenSentence(BasePage):
    """强化炼句"""
    def __init__(self):
        self.bounds = ClickBounds()
        self.result = ResultPage()

    # 以下为 共有元素
    @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

    @teststep
    def rate(self):
        """获取作业数量"""
        rate = self.driver\
            .find_element_by_id("com.vanthink.student.debug:id/rate").text
        return rate

    @teststep
    def time(self):
        """获取作业时间"""
        rate = self.driver\
            .find_element_by_id("com.vanthink.student.debug:id/time").text
        return rate

    @teststeps
    def content_desc(self):
        """点击输入框,激活小键盘"""
        content = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/input2").get_attribute('contentDescription')
        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.driver \
            .find_element_by_id("com.vanthink.student.debug:id/input2").get_attribute('contentDescription')
        value = re.match("\\[(.+?)\\]", content)  # answer
        answer = value.group(1).split(',')  # 所有输入框值的列表
        return answer

    @teststep
    def click_voice(self):
        """页面内音量按钮"""
        self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/play_voice") \
            .click()

    @teststeps
    def sentence(self):
        """展示的句子"""
        word = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/input2").text
        return word

    @teststep
    def explain(self):
        """展示的翻译"""
        word = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/explain").text
        return word

    # 每小题回答完,下一步按钮后展示答案的页面
    @teststeps
    def correct_title(self):
        """展示的答案title:正确答案 的ID为依据"""
        locator = (By.ID, "com.vanthink.student.debug:id/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("com.vanthink.student.debug:id/correct").text
        ele = word[:-1]  # 去掉最后的标点符号
        return ele

    # 查看答案页面
    @teststeps
    def result_question(self):
        """展示的题目"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/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("com.vanthink.student.debug:id/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("com.vanthink.student.debug:id/iv_mine")[index].get_attribute('selected')
        return word

    @teststep
    def back_up_button(self):
        """以“返回按钮”的class name为依据"""
        self.driver \
            .find_element_by_class_name("android.widget.ImageButton") \
            .click()

    @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():  # 页面检查点
            answer = []
            timestr = []  # 获取每小题的时间
            rate = self.rate()
            for i in range(int(rate)):
                Homework().rate_judge(rate, i, self.rate())  # 测试当前rate值显示是否正确
                Homework().next_button_operate('false')  # 下一题 按钮 判断加 点击操作

                value = self.edit_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():  # 页面检查点
            answer = []
            timestr = []  # 获取每小题的时间
            rate = self.rate()
            for i in range(int(rate)):
                Homework().rate_judge(rate, i, self.rate())  # 测试当前rate值显示是否正确
                Homework().next_button_operate('false')  # 下一题 按钮 状态判断 点击

                value = self.edit_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():  # 页面检查点
            answer = []
            timestr = []  # 获取每小题的时间
            rate = self.rate()
            for i in range(int(rate)):
                Homework().rate_judge(rate, i, self.rate())  # 测试当前rate值显示是否正确
                Homework().next_button_operate('false')  # 下一题 按钮 状态判断 点击

                value = self.edit_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():  # 页面检查点
            answer = []
            timestr = []  # 获取每小题的时间
            rate = self.rate()
            for i in range(int(rate)):
                Homework().rate_judge(rate, i, self.rate())  # 测试当前rate值显示是否正确
                Homework().next_button_operate('false')  # 下一题 按钮 状态判断 点击

                value = self.edit_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 edit_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()  # 获取输入框坐标
            x = float(item[0][j]) + 80.0
            y = float(item[1][j]) + 320.0
            self.bounds.click_bounds(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:
                    games_keyboard('capslock')  # 点击键盘 切换到 大写字母
                    games_keyboard(word[j][z].upper())  # 点击键盘对应 大写字母
                    games_keyboard('capslock')  # 点击键盘 切换到 小写字母
                else:
                    if j == 2 and word[j][z] == "'":
                        games_keyboard(',')  # 第二小题  点击键盘 逗号
                    else:
                        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

    def replace_string(self, answer, num, replace):
        string2 = ''
        for i in range(len(answer)):
            if i == num:
                string2 += replace
            else:
                string2 += answer[i]
        return string2

    @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.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.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 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

    @teststep
    def rate(self):
        """获取作业数量"""
        rate = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/rate").text
        return rate

    @teststep
    def time(self):
        """获取作业时间"""
        rate = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/time").text
        return rate

    @teststep
    def clear_button(self):
        """页面内清除按钮"""
        self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/bt_clear").click()

    @teststeps
    def clear_button_judge(self):
        """页面内清除按钮"""
        try:
            self.driver \
                .find_element_by_id("com.vanthink.student.debug:id/bt_clear")
            return True
        except:
            return False

    @teststep
    def question_content(self):
        """展示的题目 - 句子"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/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, "com.vanthink.student.debug:id/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("com.vanthink.student.debug:id/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("com.vanthink.student.debug:id/tv_answer").text
        word = ele[2:].split(' ')
        return ele, word

    @teststep
    def back_up_button(self):
        """以“返回按钮”的class name为依据"""
        self.driver \
            .find_element_by_class_name("android.widget.ImageButton") \
            .click()

    # 查看答案页面
    @teststeps
    def result_question(self):
        """展示的题目"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/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("com.vanthink.student.debug:id/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("com.vanthink.student.debug:id/tv_mine")
        words = []
        for i in range(len(ele)):
            words.append(ele[i].text)
        word = words[0].split(' ')
        return words, word

    @teststep
    def result_mine_state(self, index):
        """我的答案对错标识 selected属性"""
        word = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/iv_mine")[index].get_attribute('selected')
        return word

    @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 sentence_transform(self):
        """《句型转换》 游戏过程"""
        if self.wait_check_page():  # 页面检查点
            count = []  # 做题结果
            answer = []
            timestr = []  # 获取每小题的时间
            rate = self.rate()
            for i in range(int(rate)):
                Homework().rate_judge(rate, i, self.rate())  # 测试当前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 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.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.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('========================================')
        print('再练一遍:')
        if self.result.wait_check_result_page():  # 结果页检查点
            self.result.again_button()  # 结果页 再练一遍 按钮
            result = self.sentence_transform()  # 游戏过程
            return '再练一遍按钮', result[0], result[1]
Example #6
0
class FormSentence(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

    @teststep
    def rate(self):
        """获取作业数量"""
        rate = self.driver\
            .find_element_by_id("com.vanthink.student.debug:id/rate").text
        return rate

    @teststep
    def time(self):
        """获取作业时间"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/time").text
        return ele

    @teststep
    def question(self):
        """展示的题目"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_prompt").text
        return ele

    @teststep
    def word(self):
        """展示的 待还原的单词"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/tv_word")

        return ele

    # 每小题回答完,下一步按钮后展示答案的页面特有元素
    @teststep
    def correct_title(self, var):
        """展示的答案 的ID为依据"""
        locator = (By.ID, "com.vanthink.student.debug:id/tv_sentence")
        try:
            WebDriverWait(self.driver, var, 0.5).until(lambda x: x.find_element(*locator))
            return True
        except:
            return False

    @teststep
    def mine_result(self):
        """展示的答题结果"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/tv_word")
        word = []
        for i in range(len(ele)):
            word.append(ele[i].text)
        return word

    @teststep
    def correct_answer(self):
        """点击 下一题 按钮之后展示的答案"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_sentence").text
        word = ele[3:].split(' ')
        return word

    @teststep
    def back_up_button(self):
        """以“返回按钮”的class name为依据"""
        self.driver \
            .find_element_by_class_name("android.widget.ImageButton") \
            .click()

    # 查看答案页面
    @teststep
    def result_question(self):
        """展示的题目"""
        word = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/tv_hint")
        return word

    @teststep
    def result_answer(self):
        """展示的 正确答案"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/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("com.vanthink.student.debug:id/iv_mine")[index].get_attribute('selected')
        return word

    @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 form_sentence_operate(self):
        """《连词成句》 游戏过程"""
        if self.wait_check_page():  # 页面检查点
            question = []  # 题目
            count = []  # 做题结果
            timestr = []  # 获取每小题的时间
            rate = self.rate()
            for i in range(int(rate)):
                Homework().rate_judge(rate, i, self.rate())  # 测试当前rate值显示是否正确
                Homework().next_button_operate('false')  # 下一题 按钮 状态判断 加点击

                content = self.question()  # 展示的题目
                value = form_sentence_operate(content).split(' ')  # 数据字典
                question.append(content)  # return 题目

                for z in range(len(value) - 1, -1, -1):  # 倒序
                    print(value[z])
                    word = self.word()  # 待还原的单词
                    for k in range(len(word)):
                        if word[k].text == value[z] and k != 0:
                            self.drag_operate(word[k], word[0])  # 拖拽到第一个位置
                            if k == 0:
                                Homework().next_button_judge('true')  # 下一题 按钮 判断
                            break

                if not self.correct_title(2):  # 页面检查点
                    Homework().next_button()  # 点击 下一题 按钮

                if self.correct_title(10):
                    result = self.mine_result()  # 做题结果
                    correct = self.correct_answer()  # 正确答案-- 分解成单词
                    for k in range(len(result)):  # 做错 count+1
                        if correct[k] != result[k]:
                            count.append(k)
                            break

                timestr.append(self.time())  # 统计每小题的计时控件time信息

                Homework().next_button_operate('true')  # 下一题 按钮 状态判断 加点击
                print('-------------------------------------')

            Homework().now_time(timestr)  # 判断游戏界面 计时功能控件 是否在计时
            print('===================================================')
            return rate, question, count

    @teststeps
    def drag_operate(self, word2, word):
        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 check_detail_page(self, i, question, count):
        """查看答案页面"""
        if self.result.wait_check_result_page():
            print('查看答案按钮:')
            self.result.check_result_button()
            if self.result.wait_check_detail_page():
                if int(i) <= 16:
                    self.result_operate(count)
                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(count)  # 滑动前页面内最后一个小题- 题目
                        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.back_up_button()
                            break
                        elif item_2[len(item_2) - 1].text == question[len(question) - 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(count, k[0])
                            break
                        else:
                            continue
                    self.screen_swipe_down(0.5, 0.75, 0.35, 1000)
                time.sleep(2)
            self.back_up_button()

    @teststeps
    def result_operate(self, var, index=0):
        """查看答案页面 -- 展示的解释内容验证"""
        explain = self.result_question()  # 题目
        answer = self.result_answer()  # 正确答案

        for i in range(index, len(explain)):
            value = form_sentence_operate(explain[i].text)
            if answer[i] != value:  # 测试 正确答案
                print('★★★ 正确答案展示Error:', answer[i], value)
            else:
                if len(var) != 0:  # 存在做错的题
                    if i in var:
                        if self.result_mine_state(i) != 'false':
                            print('★★★ Error - 对错标识:%s' % self.result_mine_state(i))
                        else:
                            print('对错标识:', self.result_mine_state(i))
                    else:
                        if self.result_mine_state(i) != 'true':
                            print('★★★ Error - 对错标识:%s' % self.result_mine_state(i))
                        else:
                            print('对错标识:', self.result_mine_state(i))
                else:
                    if self.result_mine_state(i) != 'true':
                        print('★★★ Error - 对错标识:%s' % self.result_mine_state(i))
                    else:
                        print('对错标识:', self.result_mine_state(i))
            print('-----------------------------')

        return explain[len(explain) - 1].text

    @teststeps
    def study_again(self):
        """《句型转换》 再练一遍 操作过程"""
        print('再练一遍:')
        if self.result.wait_check_result_page():  # 结果页检查点
            self.result.again_button()  # 结果页 再练一遍 按钮
            result = self.form_sentence_operate()  # 游戏过程
            return '再练一遍按钮', result[0], result[1], result[2]
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

    @teststep
    def click_voice(self):
        """页面内音量按钮"""
        self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/fab_sound") \
            .click()

    @teststep
    def rate(self):
        """获取作业数量"""
        rate = self.driver\
            .find_element_by_id("com.vanthink.student.debug:id/rate").text
        return rate

    @teststep
    def time(self):
        """获取作业时间"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/time").text
        return ele

    @teststep
    def question_content(self):
        """获取题目内容"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_head").text
        return ele

    @teststep
    def option_selected(self, index):
        """获取所有选项 - 四个选项selected属性"""
        time.sleep(1)
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/option")[index].get_attribute('selected')
        return ele

    @teststep
    def option_button(self):
        """获取四个选项"""
        ele = self.driver\
            .find_elements_by_id("com.vanthink.student.debug:id/option")
        return ele

    @teststep
    def back_up_button(self):
        """以“返回按钮”的class name为依据"""
        time.sleep(2)
        self.driver \
            .find_element_by_class_name("android.widget.ImageButton") \
            .click()

    # 听音选词模式 特有元素
    @teststep
    def voice(self):
        """点击喇叭,听音选词后的小喇叭"""
        self.driver\
            .find_element_by_id("com.vanthink.student.debug:id/iv_speak")\
            .click()

    @teststep
    def tips(self):
        """tips:点击喇叭,听音选词"""
        word = self.driver \
            .find_elements_by_xpath("//android.widget.TextView[contains(@index,0)]")[0].text
        print('tips:', word)
        return word

    # 以下为答案详情页面元素
    @teststep
    def result_voice(self, index):
        """语音按钮"""
        self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/iv_speak")[index] \
            .click()

    @teststep
    def result_answer(self, index):
        """单词"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/tv_answer")[index].text
        return ele

    @teststep
    def result_explain(self, index):
        """解释"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/tv_hint")[index].text
        return ele

    @teststep
    def result_mine(self, index):
        """我的"""
        ele = self.driver \
            .find_elements_by_id("com.vanthink.student.debug:id/iv_mine")[index].get_attribute("selected")
        return ele

    @teststeps
    def back_up(self):
        """返回"""
        j = 0
        while j < 2:
            self.back_up_button()  # 结果页 返回按钮
            j += 1

    @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):
        """《词汇选择》 - 选解释模式 游戏过程--普通单词一般做法,符合yb字体规范的特殊处理"""
        if self.wait_check_page():
            count = 0
            questions = []  # 答对的题
            timestr = []  # 获取每小题的时间
            rate = self.rate()
            for i in range(int(rate)):
                Homework().rate_judge(rate, i, self.rate())  # 测试当前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(options, count, questions)  # 选择对错统计

                print('----------------------------------')
                timestr.append(self.time())  # 统计每小题的计时控件time信息
                Homework().next_button_operate('true')  # 下一题 按钮 状态判断 加点击
            print('==============================================')
            return rate, count, questions

    @teststeps
    def vocab_select_choice_word(self):
        """《词汇选择》 - 选单词模式 游戏过程"""
        if self.wait_check_page():
            count = 0
            questions = []  # 答对的题
            timestr = []  # 获取每小题的时间
            rate = self.rate()
            for i in range(int(rate)):
                Homework().rate_judge(rate, i, self.rate())  # 测试当前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(options, count, questions)  # 选择对错统计

                print('----------------------------------')
                timestr.append(self.time())  # 统计每小题的计时控件time信息
                Homework().next_button_operate('true')  # 下一题 按钮 状态判断 加点击
            print('==============================================')
            return rate, count, questions

    @teststeps
    def vocab_select_listen_choice(self):
        """《词汇选择》 - 听音选词模式 游戏过程"""
        if self.wait_check_page():
            count = 0
            questions = []  # 答对的题
            timestr = []  # 获取每小题的时间
            rate = self.rate()
            for i in range(int(rate)):
                Homework().rate_judge(rate, i, self.rate())  # 测试当前rate值显示是否正确
                Homework().next_button_operate('false')  # 下一题 按钮 判断加 点击操作

                self.voice()  # 题目后的听力按钮
                self.click_voice()  # 发音按钮

                options = self.option_button()
                options[random.randint(0, len(options) - 1)].click()  # 随机点击选项
                # self.options_statistic(options, count, questions)  # 选择对错统计

                # print('----------------------------------')
                timestr.append(self.time())  # 统计每小题的计时控件time信息
                Homework().next_button_operate('true')  # 下一题 按钮 状态判断 加点击
            print('==============================================')
            return rate, count, questions

    @teststeps
    def options_statistic(self, options, count, questions):
        """选择对错统计"""
        ele = []  # 四个选项selected属性值为true的个数
        print(len(options))
        for j in range(len(options)):  # 统计答案正确与否
            if self.option_selected(j) == 'true':
                ele.append(j)

        if len(ele) == 1:  # 如果选项的selected属性为true的作业数为1,说明答对了,则+1
            count += 1
            print('回答正确:', options[ele[0]].text)
            questions.append(self.question_content())
        else:  # len(ele) == 2
            print('回答错误:', ele)

    @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('查看答案:')
                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.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)  # 不同模式 对应不同的游戏过程