appium页面元素封装(十一)

1、首先封装一个基类

"""
appium的二次封装
"""
from selenium.webdriver.support.wait import WebDriverWait

class Base:
    # 初始化
    def __init__(self, driver):
        self.driver = driver

    # 获取单个元素
    def find_element(self, loc, timeout=10, poll=0.5):
        """
        :param loc: 定位方式+属性值,类似(By.XPATH,"xpath语句") (By.ID, "id属性值")
        :param timeout: 等待时间
        :param poll: 请求间隔
        """
        return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(*loc))
    
    # 获取一组元素
    def find_elements(self, loc, timeout=10, poll=0.5):
        """
        :param loc: 定位方式+属性值,类似(By.XPATH,"xpath语句") (By.ID, "id属性值")
        :param timeout: 等待时间
        :param poll: 请求间隔
        """
        return WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(*loc))

    # 点击
    def click_element(self, loc):
        # 元组类型 (By.XPATH,"xpath语句") (By.ID, "id属性值")
        self.find_element(loc).click()

    # 输入
    def input_element(self, loc, text):
        """
        :param text: 输入内容
        """
        self.find_element(loc).send_keys(text)

2、调用基类来使用

from selenium.webdriver.common.by import By
from init_driver.Init_driver import init_driver
from Base.base import Base
import pytest

class Test_search:
    def setup(self):
        # 实例化driver类
        self.driver = init_driver()
        # 实例化base类
        self.base_obj = Base(self.driver)

    def teardown(self):
        self.driver.quit()

    def test_001(self):
        # 搜索按钮
        search_b = (By.ID, "com.android.settings:id/search")
        # 输入框
        input_t = (By.CLASS_NAME, "android.widget.EditText")
        # 点击搜索按钮
        self.base_obj.click_element(search_b)
        # 输入内容
        self.base_obj.input_element(input_t, "123你好")

if __name__ == '__main__':
    pytest.main(["-s", "test_13.py", "--html=../report/report.html"])
原文地址:https://www.cnblogs.com/zhaoquanmo/p/10819718.html