Ejemplo n.º 1
0
class VocabularyChoose(BasePage):
    """词汇选择"""
    def __init__(self):
        self.attr = GetAttribute()
        self.homework = Homework()
        self.mysql = MysqlData()
        self.common = DataActionPage()

    @teststeps
    def wait_check_head_page(self):
        """以“词汇选择 -选单词模式”的 发音按钮 为依据"""
        locator = (By.ID, "com.vanthink.student.debug:id/tv_head")
        try:
            WebDriverWait(self.driver, 3,
                          0.5).until(lambda x: x.find_element(*locator))
            return True
        except:
            return False

    @teststeps
    def wait_check_sentence_page(self):
        """以“词汇选择 -句子选单词模式”的 提示按钮 为依据"""
        locator = (
            By.XPATH,
            "//android.widget.TextView[contains(@resource-id,'com.vanthink.student.debug:id/hint')]"
        )
        try:
            WebDriverWait(self.driver, 2,
                          0.5).until(lambda x: x.find_element(*locator))
            return True
        except:
            return False

    @teststeps
    def check_voice_ele(self):
        try:
            self.driver.find_element_by_id(
                "com.vanthink.student.debug:id/fab_sound")
            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 question_content(self):
        """获取题目内容"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_head").text
        return ele

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

    # 听音选词
    @teststep
    def explain(self):
        """选择答案后,出现中文解释"""
        word = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/explain").text
        return word

    # 句子选单词
    @teststep
    def click_hint_button(self):
        """提示按钮"""
        self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/hint").click()
        time.sleep(2)

    @teststep
    def sentence_explain(self):
        """点击 提示按钮后,出现中文解释"""
        explain = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_explain").text
        print('explain:', explain)
        return explain

    @teststep
    def hint_button_judge(self, var):
        """‘提示’按钮 状态判断"""
        item = self.driver.find_element_by_id(
            "com.vanthink.student.debug:id/hint").get_attribute(
                "enabled")  # ‘下一题’按钮
        if item != var:  # 测试 提示 按钮 状态
            print('★★★ 提示按钮 状态Error', item)

    @teststeps
    def vocab_select_choice_explain(self, i):
        """《词汇选择》 - 选解释模式"""
        if i == 0:
            print('\n词汇选择 - 根据单词选解释模式(复习)\n')
        self.homework.next_button_operate('false')  # 下一题 按钮 判断加 点击操作

        self.click_voice()  # 点击发音按钮
        word = self.question_content()  # 题目
        print('题目:', word)

        options = self.option_button()  #遍历选项,点击和word一样的单词
        for j in range(0, len(options)):
            find_word = self.common.get_word_by_explain(options[j].text)
            if word == find_word:
                print('选项解释:', options[j].text)
                options[j].click()
                break

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

    @teststeps
    def vocab_select_choice_word(self, i):
        """《词汇选择》 - 根据解释选单词"""
        if i == 0:
            print('\n词汇选择-选单词模式(复习)\n')

        self.homework.next_button_operate('false')  # 下一题 按钮 判断加 点击操作

        item = self.question_content()  # 题目
        print('题目:', item)
        word = self.common.get_word_by_explain(item)  # 根据解释获取单词

        options = self.option_button()  # 遍历选项,点击和word一样的单词
        for j in range(0, len(options)):
            if options[j].text == word:
                options[j].click()
                time.sleep(2)
                break

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

    @teststeps
    def vocab_select_listen_choice(self, answer, lc, fc, ws, star, familiar):
        """《词汇选择》 - 听音选词模式 具体操作"""
        if lc == 0:
            self.start_familiar_count_compare(fc, ws, star, familiar)
            print('\n词汇选择-听音选词模式(新词)\n')

        self.homework.next_button_operate('false')  # 下一题 按钮 判断加 点击操作

        self.click_voice()  # 点击发音按钮
        options = self.option_button()  # 获取当前页面所有选项

        if len(answer) == 0:  # 若answer为0,则说明上一选项为正确选项,随机选择
            options[random.randint(0, len(options) - 1)].click()
            explain = self.explain()
            word = self.common.get_word_by_explain(explain)
            self.options_statistic(answer, word, explain)  # 选择对错统计

        elif len(answer) == 1:  # 若answer为其他,则说明上一选项为错误选项,这一次需定向选择
            for i in range(0, len(options)):
                if options[i].text == answer[0]:
                    options[i].click()
                    exp = self.explain()
                    print('答案正确:%s\n解释:%s' % (answer[0], exp))
                    break

            del answer[:]
            print('-------------------------------------')
        self.homework.next_button_operate("true")  # 下一题 按钮 状态判断 加点击

    @teststep
    def start_familiar_count_compare(self, fc, ws, star, familiar):
        """标星和标熟个数的判断"""
        # 对上一游戏的标熟和标星进行个数验证
        if len(star) == fc:
            print('\n标星单词:', star)
            print('标星个数与闪卡抄写个数一致')
        else:
            print("★★★ Error--标星个数与闪卡抄写个数不一致!")

        if len(familiar) == ws:
            print('标熟单词', familiar)
            print('标熟个数与单词默写个数一致\n')
        else:
            print('★★★ Error--熟个数与单词默写个数不一致!')
        print('------------------------------------')

    @teststeps
    def vocab_apply(self, i):
        if i == 0:
            #词汇选择分组

            #单词拼写分组
            print('\n 词汇运用 --句子选单词模式(复习)\n')
        self.homework.next_button_operate('false')
        item = self.question_content()  #题目
        print('题目:%s' % item)

        self.click_hint_button()  #点击提示按钮
        self.hint_button_judge('false')  #提示按钮 状态判断

        explain = self.sentence_explain()
        word = self.mysql.find_word_by_sentence_exp(explain)  #根据中文获取缺少单词
        print('word:', word)

        options = self.option_button()  # 四个选项
        for i in range(0, len(options)):
            if options[i].text == word:
                options[i].click()
                break

        self.click_voice()
        self.homework.next_button_operate('true')
        print('----------------------------------')

    @teststeps
    def options_statistic(self, answer, word, explain):
        """选择对错统计"""
        options = self.option_button()  # 四个选项
        for i in range(0, len(options)):
            if self.attr.selected(options[i]) == "true":
                if options[i].text == word:
                    print('选择正确:%s\n解释:%s' % (options[i].text, explain))
                    print('----------------------------------')

                else:
                    answer.append(word)
                    print('选择错误,正确答案为:%s\n' % word)
Ejemplo n.º 2
0
class SpellingWord(BasePage):
    """单词拼写"""
    def __init__(self):
        self.get = GetAttribute()
        self.homework = Homework()
        self.common = DataActionPage()

    @teststeps
    def wait_check_page(self):
        """以“词汇选择 -句子选单词模式”的 提示按钮 为依据"""
        locator = (By.XPATH,
                   "//android.widget.TextView[contains(@resource-id,'com.vanthink.student.debug:id/hint')]")
        try:
            WebDriverWait(self.driver, 2, 0.5).until(lambda x: x.find_element(*locator))
            return True
        except:
            return False

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

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

    @teststep
    def 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 finish_word(self):
        """完成答题 之后 展示的Word 每个字母之间有空格"""
        word = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_word").text
        return word[::2]

    @teststep
    def correct_judge(self):
        """判断 答案是否展示"""
        try:
            self.driver.find_element_by_id("com.vanthink.student.debug:id/tv_answer")
            return True
        except:
            return False

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

    # 默写模式
    @teststep
    def hint_button(self):
        """提示按钮"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/hint")
        return ele

    @teststeps
    def dictation_word_judge(self):
        """判断是否展示Word"""
        try:
            self.driver \
                .find_element_by_id("com.vanthink.student.debug:id/tv_word")
            return True
        except:
            return False

    @teststeps
    def dictation_word(self):
        """展示的Word"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_word").text
        value = ele[::2]
        return value

    @teststeps
    def dictation_pattern_new(self, i):
        """单词默写 新词"""
        if i == 0:
            print('\n单词拼写 - 默写模式(新词)\n')
        self.dictation_pattern_core()

    @teststeps
    def dictation_pattern_recite(self, i,first_game):
        """单词默写 新词"""
        if i == 0:
            level1_count = self.common.get_different_level_words(1)  # 获取需要B轮复习的单词
            if level1_count != 0:
                if first_game[0] != '词汇选择(复习)':
                    print ('★★★ Error-第一个游戏不是B1的词汇选择游戏')
                else:
                    print ("B轮单词存在,首个游戏为 '词汇选择(复习)' 名称正确!")
            else:
                if first_game[0] != '词汇运用(复习)':
                    print ("★★★ Error-第一个游戏不是B2/C1/D1/E1的词汇运用游戏'")
                else:
                    print ("B轮单词已结束,首个游戏为 '词汇运用(复习)' 名称正确!\n")

            print ('\n单词拼写 - 默写模式(新词)\n')
        self.dictation_pattern_core ()


    @teststeps
    def dictation_pattern_mine(self, i,familiar_add):
        """单词默写 我的单词"""
        if i == 0:
            print ("\n单词拼写 - 默写模式(单词详情)\n")
        explain = self.explain ()  # 题目
        value = self.common.get_word_by_explain (explain)
        familiars = self.common.get_familiar_words () + familiar_add
        if i in range(0,5):
            self.dictation_pattern_core()
            if value not in familiars:
                print ('★★★ Error-- 单词未被标熟却出现默写模式')
        else:
            FlashCard().tips_operate()
            for i in familiar_add:
                level = self.common.get_word_level(i)
                if level < 3:
                    print("★★★ Error--提交未成功,单词熟练度未更改")


    def dictation_pattern_core(self):
        """单词拼写 - 《默写模式》游戏过程"""
        explain = self.explain ()  # 题目
        value = self.common.get_word_by_explain (explain)

        self.homework.next_button_operate ('false')  # 下一题 按钮 判断加 点击操作
        if self.dictation_word_judge ():  # 默写模式 - 字母未全部消除
            print ('★★★ Error - 单词拼写 默写模式 - 字母未全部消除')

        hint = self.hint_button ()  # 提示按钮

        if self.get.enabled (hint) == 'true':
            hint.click ()  # 点击 提示按钮
            if self.get.enabled (self.hint_button ()) != 'false':
                print ('★★★ Error - 点击后提示按钮enabled属性错误')

            if self.dictation_word_judge ():  # 出现首字母提示
                word = self.dictation_word ()
                if len (word) == 1:
                    if word == value[0]:
                        print ('点击提示出现首字母提示', word)
                    else:
                        print('点击提示出现首字母提示', word)
                        print ("★★★ Error - 首字母提示错误")
                else:
                    print ('★★★ Error - 提示字母不为一个')
            else:
                print ("★★★ Error - 首字母提示未出现")
        else:
            print ('★★★ Error - 提示按钮enabled属性错误')

        games_keyboard ('backspace')
        for j in range (0, len (value)):
            self.keyboard_operate (j, value[j])  # 点击键盘 具体操作

        answer = self.finish_word ()  # 我的答案
        self.homework.next_button_operate ('true')  # 下一题 按钮 状态判断 加点击
        self.result_operate (answer, self.mine_answer ())  # 下一步按钮后的答案页面 测试
        self.homework.click_voice ()
        self.homework.next_button_operate ('true')  # 下一题 按钮 状态判断 加点击


    @teststeps
    def result_operate(self, answer, mine):
        """下一步按钮后的答案页面"""
        print('我的答案:', answer)
        print('答题结果:', mine)
        if self.correct_judge():
            correct = self.correct()  # 正确答案
            if len(mine) <= len(correct):  # 输入少于或等于单词字母数的字符
                if mine.lower() != answer.lower():  # 展示的 我的答题结果 是否与我填入的一致
                    print('★★★ Error - 字符数少于或等于时:', mine.lower(), answer.lower())
            else:  # 输入过多的字符
                if correct + mine[len(correct):].lower() != correct + answer[len(correct):].lower():  # 展示的 我的答题结果 是否与我填入的一致
                    print('★★★ Error - 字符输入过多时:', correct + mine[len(correct):].lower(),correct + answer[len(correct):].lower())
        else:  # 回答正确
            if mine.lower() != answer.lower():  # 展示的 我的答题结果 是否与我填入的一致
                print('★★★ Error - 展示的答题结果 与我填入的不一致:', mine.lower(), answer.lower())
            else:
                print('回答正确!')
        print('----------------------------------')

    @teststeps
    def keyboard_operate(self, j, value):
        """点击键盘 具体操作"""
        if j == 4:
            games_keyboard('capslock')  # 点击键盘 切换到 大写字母
            games_keyboard(value.upper())  # 点击键盘对应 大写字母
            games_keyboard('capslock')  # 点击键盘 切换到 小写字母
        else:
            games_keyboard(value)  # 点击键盘对应字母
Ejemplo n.º 3
0
class FlashCard(BasePage):
    """单词本 - 闪卡练习"""
    def __init__(self):
        self.homework = Homework()
        self.home = HomePage()
        self.mysql = MysqlData()
        self.common = DataActionPage()

    @teststeps
    def wait_check_study_page(self):
        """以“闪卡练习 -学习模式”的xpath-text为依据"""
        locator = (By.XPATH,
                   "//android.widget.TextView[contains(@text,'设置熟词')]")
        try:
            WebDriverWait(self.driver, 2,
                          0.5).until(lambda x: x.find_element(*locator))
            return True
        except:
            return False

    @teststeps
    def wait_check_copy_page(self):
        """以“闪卡练习 -抄写模式”的xpath-text为依据"""
        locator = (By.ID, "com.vanthink.student.debug:id/english")
        try:
            WebDriverWait(self.driver, 2,
                          0.5).until(lambda x: x.find_element(*locator))
            return True
        except:
            return False

    @teststeps
    def wait_check_sentence_page(self):
        """以“闪卡练习 -抄写模式”的xpath-text为依据"""
        locator = (By.ID, "com.vanthink.student.debug:id/sentence")
        try:
            WebDriverWait(self.driver, 2,
                          0.5).until(lambda x: x.find_element(*locator))
            return True
        except:
            return False

    # 学习模式
    @teststep
    def pattern_switch(self):
        """点击右上角的全英/英汉,切换模式"""
        self.driver\
            .find_element_by_id("com.vanthink.student.debug:id/side")\
            .click()
        time.sleep(2)

    @teststep
    def author(self):
        """例句推荐老师"""
        english = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/author").text
        return english

    @teststep
    def english_study(self):
        """全英模式 页面内展示的word"""
        english = self.driver\
            .find_element_by_id("com.vanthink.student.debug:id/tv_english").text
        return english

    @teststep
    def explain_study(self):
        """英汉模式 页面内展示的word解释"""
        explain = self.driver.find_element_by_id(
            "com.vanthink.student.debug:id/tv_chinese")
        return explain.text

    @teststep
    def sentence_study(self):
        """全英模式 页面内展示的句子"""
        english = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/sentence").text
        return english

    @teststep
    def sentence_explain_study(self):
        """英汉模式 页面内展示的句子解释"""
        explain = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/sentence_explain").text
        return explain

    @teststep
    def explain_copy(self):
        """抄写模式的单词翻译"""
        explain = self.driver.find_element_by_id(
            "com.vanthink.student.debug:id/chinese").text
        return explain

    @teststep
    def star_button(self):
        """星标按钮"""
        ele = self.driver.find_element_by_id(
            "com.vanthink.student.debug:id/iv_star")
        return ele

    @teststep
    def familiar_button(self):
        """熟词按钮"""
        ele = self.driver.find_element_by_id(
            "com.vanthink.student.debug:id/expert")
        return ele

    @teststep
    def click_star(self):
        """闪卡练习页面内五角星按钮"""
        self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/iv_star") \
            .click()

    @teststep
    def click_familiar(self):
        """设置为熟词 按钮"""
        self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/expert").click()

    # 抄写模式
    @teststep
    def word_copy(self):
        """闪卡练习- 抄写模式 内展示的Word"""
        ele = self.driver\
            .find_element_by_id("com.vanthink.student.debug:id/tv_word").text
        return ele

    @teststep
    def english_copy(self):
        """单页面内 答题框填入的Word"""
        word = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/english").text
        return word

    # 提示 页面
    @teststeps
    def wait_check_tips_page(self):
        """以“icon”为依据"""
        locator = (
            By.XPATH,
            "//android.widget.TextView[contains(@resource-id,'com.vanthink.student.debug:id/md_title')]"
        )
        try:
            WebDriverWait(self.driver, 3,
                          0.5).until(lambda x: x.find_element(*locator))
            return True
        except:
            return False

    @teststep
    def tips_title(self):
        """提示title"""
        item = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/md_title").text
        print(item)
        return item

    @teststep
    def tips_content(self):
        """提示 具体内容"""
        item = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/md_content").text
        print(item + "\n")
        time.sleep(2)
        return item

    @teststep
    def commit_button(self):
        """确定 按钮"""
        self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/md_buttonDefaultPositive") \
            .click()
        time.sleep(2)

    @teststep
    def skip_button(self):
        """放弃 按钮"""
        self.driver. \
            find_element_by_id ('com.vanthink.student.debug:id/md_buttonDefaultNeutral') \
            .click ()
        time.sleep(2)

    @teststep
    def cancel_button(self):
        """取消 按钮"""
        self.driver. \
            find_element_by_id ('com.vanthink.student.debug:id/md_buttonDefaultNegative') \
            .click ()

    @teststeps
    def tips_operate(self):
        """温馨提示 页面信息"""
        if self.wait_check_tips_page():  # 提示 页面
            self.tips_title()
            self.tips_content()
            self.commit_button()  # 确定按钮

    @teststeps
    def study_word_core(self):
        """闪卡练习学习模式 主要步骤"""
        word = self.english_study()  # 单词

        self.pattern_switch()  # 切换到 英汉模式
        explain = self.explain_study()  # 解释
        print('单词:%s\n解释:%s' % (word, explain))

        if self.wait_check_sentence_page():  # 判断句子是否存在
            sentence = self.sentence_study()  # 句子
            sen_explain = self.sentence_explain_study()  # 句子解释
            auth = self.author()  # 推荐老师
            print('句子:%s\n句子解释:%s\n推荐老师:%s' % (sentence, sen_explain, auth))

        self.pattern_switch()  # 切换到 全英模式
        self.homework.click_voice()
        return word

    ##学习模式
    @teststeps
    def study_new_word(self, i, star, familiar):
        """学习模式  新词操作"""
        if i == 0:
            print('\n闪卡练习-学习模式(新词)\n')

        word = self.study_word_core()
        if i in range(1, 9, 2):  # 点击star按钮
            self.click_star()
            self.tips_operate()

            if i == 5:
                self.click_star()  #取消标星
            else:
                star.append(word)

        if i in range(0, 10, 2):  # 点击标熟按钮
            self.click_familiar()
            self.tips_operate()
            if i == 4:
                self.click_familiar()  # 取消标熟
            else:
                familiar.append(word)

        self.next_word(i)

    @teststeps
    def study_mine_word(self, i, star_list, familiar_list, star_add,
                        familiar_add):
        """学习模式  单词详情操作"""
        if i == 0:
            print('\n闪卡练习-单词详情(我的单词)\n')

        if i in (range(0, 5)):
            word = self.study_word_core()

            if word in star_list:  #单词是否在标星数组中
                self.judge_word_is_star(i)  #判断单词星标是否被标注
                self.judge_word_is_familiar(familiar_list, word, i,
                                            familiar_add)  #判断单词是否同时被标熟
            else:
                if i == 1 or i == 3:
                    self.click_star()
                    self.tips_operate()
                    star_add.append(word)
                self.judge_word_is_familiar(familiar_list, word, i,
                                            familiar_add)  #判断单词是否被标熟

            self.next_word(i)  #下一单词滑屏还是点击按钮
        else:
            print('标星单词:', star_list + star_add)
            print('标熟单词:', familiar_list + familiar_add)
            print('-------------------------------------')
            self.home.back_up_button()

    @teststeps
    def next_word(self, i):
        """进入下一单词的方式"""
        if i == 1:  # 向左滑屏
            self.screen_swipe_left(0.8, 0.5, 0.1, 1000)
            time.sleep(1)
        else:
            self.homework.next_button_operate('true')
            time.sleep(1)

        print('-------------------------------------')

    @teststep
    def judge_word_is_star(self, i):
        """判断单词是否被标星"""
        if GetAttribute().selected(
                self.star_button()) == 'true':  # 判断但是标星是否被标注
            print('单词已标星')
            if i == 3:
                self.click_star()  # 取消标星
        else:
            print("★★★ Error--此题未被标星")

    @teststep
    def judge_word_is_familiar(self, familiar, word, i, familiar_add):
        """判断单词是否被标熟"""
        if word in familiar:
            if GetAttribute().selected(self.familiar_button()) == 'true':
                print("★★★ Error-- 此题未被标熟")
                self.click_familiar()
                self.tips_operate()
                familiar_add.append(word)
            else:
                print('单词已标熟')
        else:
            if i == 2 or i == 4:
                self.click_familiar()
                self.tips_operate()
                familiar_add.append(word)

    ##抄写模式
    @teststeps
    def copy_new_word(self, i):
        """抄写模式  新词操作"""
        if i == 0:
            print('\n闪卡练习-抄写模式(新词)\n')

        word = self.word_copy()
        self.copy_word_core(word)

    @teststeps
    def copy_mine_word(self, i, star_add):
        """抄写模式  我的单词操作"""
        if i == 0:
            print('\n闪卡练习-抄写模式((单词详情)\n')
        word = self.word_copy()
        if i in (range(0, 5)):
            star_words = self.common.get_star_words()
            self.copy_word_core(word)
            stars = star_words + star_add
            if word not in stars:
                print('★★★ Error-- 单词未被标星却出现抄写模式')
        else:
            self.home.back_up_button()  #若没有点击放弃,且i>=4,则点击 确定按钮
            if self.wait_check_tips_page():
                self.tips_operate()
        print('--------------------------')

    @teststeps
    def copy_word_core(self, word):
        """闪卡练习 抄写模式 主要步骤"""
        self.homework.click_voice()  # 听力按钮
        time.sleep(1)
        word_explain = self.explain_copy()
        print("单词:%s\n解释:%s" % (word, word_explain))
        if len(self.english_copy()) == 0:  # 抄写模式 消除全部字母
            for j in range(0, len(word)):
                if j == 4:
                    games_keyboard('capslock')  # 点击键盘 切换到 大写字母
                    games_keyboard(word[j].upper())  # 点击键盘对应 大写字母
                else:
                    if j == 5:
                        games_keyboard('capslock')  # 点击键盘 切换到 小写字母
                    games_keyboard(word[j].lower())  # 点击键盘对应字母
            time.sleep(3)
        print('--------------------------')
Ejemplo n.º 4
0
class WordDictation(BasePage):
    """单词听写"""
    def __init__(self):
        self.homework = Homework()

    @teststep
    def word(self):
        """展示的Word  点击喇叭听写单词"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_word").text
        word = ele[::2]
        print('我的答案:%s' % word)
        return word

    # 下一步 按钮之后 答案页展示的答案
    @teststep
    def mine_answer(self):
        """展示的Word """
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_word").text
        mine_ans = ele[::2]
        print("提交结果:%s" % mine_ans)
        return mine_ans

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

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

    @teststep
    def correct_judge(self):
        """判断 答案是否展示"""
        locator = (By.ID, "com.vanthink.student.debug:id/tv_answer")
        try:
            WebDriverWait(self.driver, 2,
                          0.5).until(lambda x: x.find_element(*locator))
            return True
        except:
            return False

    @teststep
    def result_explain(self):
        """解释"""
        ele = self.driver \
            .find_element_by_id("com.vanthink.student.debug:id/tv_explain").text
        return ele

    @teststeps
    def word_dictation(self, i, answer):
        """《单词听写》 游戏过程"""
        if i == 0:
            print('\n单词听写模式(新词)\n')

        self.homework.next_button_operate('false')  # 下一题 按钮 判断加 点击操作
        self.homework.click_voice()  #点击播放按钮

        if len(answer) == 0:  #数组为0,说明上一题已回答正确,本题需随机填入字母以获取正确答案
            alpha_list = [chr(i) for i in range(65, 91)]  #生成小写字母表
            self.keyboard_operate(i, alpha_list[random.randint(0,
                                                               25)])  #随机输入一个字母
            self.homework.next_button_operate('true')  # 提交 判断加 点击操作
            self.word()  #输入的答案
            mine_ans = self.mine_answer()  #提交后的答案
            if self.correct_judge():  #判断正确答案是否存在
                correct_ans = self.correct()  #获取正确答案
                self.result_explain()  #解释
                if mine_ans != correct_ans:
                    answer.append(correct_ans)
                    print('答案不正确\n')
            else:
                print("★★★ Error - 未显示正确答案")

        elif len(answer) == 1:  #数组长度为1,说明已获取正确答案,直接输入正确答案即可
            for j in range(0, len(answer[0])):
                self.keyboard_operate(j, answer[0][j])  #
            self.homework.next_button_operate('true')  # 提交 判断加 点击操作
            self.word()  #填入的答案
            self.mine_answer()  #提交后的答案
            if self.correct_judge():  #判断正确答案是否出现
                print("★★★ Error -听写正确却显示正确答案")
            else:
                self.result_explain()  # 解释
                print('回答正确!')
                print('----------------------------------')
            del answer[:]

        self.homework.next_button_operate('true')  #下一题

    @teststeps
    def keyboard_operate(self, j, value):
        """点击键盘 具体操作"""
        if j == 4:
            games_keyboard('capslock')  # 点击键盘 切换到 大写字母
            games_keyboard(value.upper())  # 点击键盘对应 大写字母
            games_keyboard('capslock')  # 点击键盘 切换到 小写字母
        else:
            games_keyboard(value)  # 点击键盘对应字母