APP自动化之pages页面的封装

h5_page.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/2/20 13:05
# @Author  : shisuiyi
# @File    : h5_page.py
# @Software: win10 Tensorflow1.13.1 python3.9
from time import sleep

from common.basepage import BasePage


class H5(BasePage):
    """登录页面"""
    # 通过类属性获取元素的定位方式与元素定位表达式,便于修改与管理。
    company = ('xpath', "//span[text()='评价']")

    def switch_h5(self):
        self.switch_to_webview('切换到h5页面')

    def click_pingjia(self):
        """点击评价"""
        sleep(5)
        self.get_element(self.company, '评价').click()

home_page.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/2/22 19:51
# @Author  : shisuiyi
# @File    : home_page.py
# @Software: win10 Tensorflow1.13.1 python3.9
from pages.nav_page import NavPage


class HomePage(NavPage):
    # 全程班
    quanchengban_locator = ('xpath', "//*[@text='全程班']")

    def click_quanchengban(self):
        # UserPage, 点击头像
        self.click_button(self.quanchengban_locator, "全程班")
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/2/22 19:37
# @Author  : shisuiyi
# @File    : nav_page.py
# @Software: win10 Tensorflow1.13.1 python3.9
from common.basepage import BasePage


class NavPage(BasePage):
    # 首页
    home_locator = ('id', 'com.lemon.lemonban:id/navigation_home')
    # 题库
    tiku_locator = ('id', 'com.lemon.lemonban:id/navigation_tiku')
    # 我的
    my_locator = ('id', 'com.lemon.lemonban:id/navigation_my')

    def click_homepage(self):
        self.click_button(self.home_locator, "主页")

    def click_tiku(self):
        self.click_button(self.tiku_locator, "题库")

    def click_my(self):
        self.click_button(self.my_locator, "我的柠檬")

out_page.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/2/19 14:02
# @Author  : shisuiyi
# @File    : out_page.py
# @Software: win10 Tensorflow1.13.1 python3.9
from time import sleep

from common.basepage import BasePage


class OutPage(BasePage):
    appPackage = 'com.xxzb.fenwoo'
    appActivity = 'com.xxzb.fenwoo.activity.addition.SplashActivity'

    def swift_app_kaoyan(self):
        """跳到另外一个app(前程贷)滑动介绍页"""
        self.application_switching(self.appPackage, self.appActivity, '跳往前程贷app')
        for i in range(3):
            self.sliding_screen('left', '向左滑动')
            sleep(2)

tiku_page.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/2/22 19:42
# @Author  : shisuiyi
# @File    : tiku_page.py
# @Software: win10 Tensorflow1.13.1 python3.9
from time import sleep

from pages.nav_page import NavPage


class TikuPage(NavPage):
    # 选择题库, xpath
    choose_tiku_locator = ('xpath', "//android.widget.TextView[@text='Linux']")
    # 选择题库难度
    tiku_diff_locator = ('id', 'com.lemon.lemonban:id/first_level')
    # 选择题库编号
    tiku_number_locator = ('id', 'com.lemon.lemonban:id/suit_subject_title')
    # 获取编号的
    title_number_locator = ('id', 'com.lemon.lemonban:id/toolbar_textview')
    # 答案开关
    switch_locator = ('id', 'com.lemon.lemonban:id/switch_button')
    # 收藏按钮
    collect_locator = ('id', 'com.lemon.lemonban:id/action_favourite')
    # 答案
    answer_locator = ('id', 'com.lemon.lemonban:id/tvBody')

    def show_tiku(self, swipe_times):
        """展示题库中的题目"""
        self.click_button(self.choose_tiku_locator, '选择题库')
        self.click_button(self.tiku_diff_locator, '选择题库难度')
        self.click_button(self.tiku_number_locator, '选择题库编号')
        for _ in range(swipe_times):
            print("第{}次左滑".format(_+1))
            self.sliding_screen('left', '左滑')
        el = self.wait_element_to_be_visible(self.title_number_locator, '获取编号')
        number = el.text.split('/')[0]
        return number

    def accept_answer(self):
        self.click_button(self.switch_locator, '查看答案打开开关')
        el = self.wait_element_to_be_visible(self.answer_locator, '答案内容')
        answer = el.text
        return answer

    def collect(self):
        self.click_button(self.collect_locator, '收藏按钮')
        for _ in range(3):
            print("第{}次返回".format(_ + 1))
            self.keycode('back', "返回")
            sleep(2)

user_page.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/2/22 19:44
# @Author  : shisuiyi
# @File    : user_page.py
# @Software: win10 Tensorflow1.13.1 python3.9
from common.basepage import BasePage


class UserPage(BasePage):
    """My 和 login 组合,因为login页面简单,所以合并,放到同一个类当中管理"""
    # 头像
    avatar_locator = ('id', 'com.lemon.lemonban:id/fragment_my_lemon_avatar_layout')
    # 手机号码
    mobile_locator = ('id', 'com.lemon.lemonban:id/et_mobile')
    # 密码
    pass_locator = ('id', 'com.lemon.lemonban:id/et_password')
    # 登录按钮
    login_btn_locator = ('id', 'com.lemon.lemonban:id/btn_login')
    # 收藏记录
    collect_locator = ('xpath', "//*[@text='收藏记录']")
    # 收藏列表
    collect_list_locator = ('id', 'com.lemon.lemonban:id/fragment_category_type')

    def login(self, *args):
        """登录"""
        # UserPage, 点击头像
        self.click_button(self.avatar_locator, "头像")

        # UserPage
        self.input_text(args[0], self.mobile_locator, "账号输入框")

        # 输入密码
        self.input_text(args[1], self.pass_locator, '密码输入框')

        # 点击登录
        self.click_button(self.login_btn_locator, "登录按钮")

    def get_login_msg(self):
        """获取登录后的提示信息"""

        el = self.get_toast_msg('登录toast')
        return el

    def cat_collect_list(self):
        self.click_button(self.collect_locator, '收藏记录')
        el = self.wait_element_to_be_visible(self.collect_list_locator, '获取收藏的内容')
        lists = el.text
        return lists