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"])