App测试-开发者头条
项目结构
Base包Base.py
1 from selenium.webdriver.support.wait import WebDriverWait 2 3 4 class Base: 5 def __init__(self, driver): 6 self.driver = driver 7 8 # 元素定位方法 9 def find_ele(self, locate, value, timeout=10, poll=0.5): 10 """ 11 :param poll: 12 :param timeout: 13 :param locate: 定位发誓 14 :param value: 定位路径 15 :return: 16 """ 17 return WebDriverWait(self.driver, timeout, poll)\ 18 .until(lambda x: x.find_element(locate, value)) 19 20 # 元素点击方法 21 def click_ele(self, location, value): 22 self.find_ele(location, value).click() 23 24 # 文本输入方法 25 def input_ele(self, location, value, text): 26 ele = self.find_ele(location, value) 27 ele.clear() 28 ele.send_keys(text)
Page包__init__.py (初始化)
1 from selenium.webdriver.common.by import By 2 3 """ 4 开发者头条 5 """ 6 # 新建文章按钮 7 dev_add_title = (By.ID, 'io.manong.developerdaily:id/tab_bar_plus') 8 # 选择邮箱登录 9 dev_login_email = (By.ID, 'io.manong.developerdaily:id/btn_email') 10 # 邮箱输入框 11 dev_input_email = (By.ID, 'io.manong.developerdaily:id/edt_email') 12 # 密码输入框 13 dev_input_pwd = (By.ID, 'io.manong.developerdaily:id/edt_password') 14 # 登录按钮 15 dev_login_button = (By.ID, 'io.manong.developerdaily:id/btn_login')
Page包dev_title.py
1 from Page_Object.Base.Base import Base 2 import Page_Object.Page as P 3 4 5 class Dev_Title(Base): 6 def __init__(self, driver): 7 # 继承Base类,初始化父类,直接使用Base类的方法 8 Base.__init__(self, driver) 9 10 # 页面类的方法,可以创建多个页面的py文件,再提取各页面有用的元素 11 def click_add(self): 12 self.click_ele(P.dev_add_title[0], P.dev_add_title[1]) 13 14 def change_login(self): 15 self.click_ele(P.dev_login_email[0], P.dev_login_email[1]) 16 17 def login(self, email, pwd): 18 self.input_ele(P.dev_input_email[0], P.dev_input_email[1], email) 19 self.input_ele(P.dev_input_pwd[0], P.dev_input_pwd[1], pwd) 20 self.click_ele(P.dev_login_button[0], P.dev_login_button[1])
cmd查app包名和启动名(需手机打开app):adb shell dumpsys window windows |findstr mFocusedApp
scripts包(专门放test文件) test_dev_login.py
1 import pytest 2 from Page_Object.Page.dev_title import Dev_Title 3 from appium import webdriver 4 5 6 class Test_Dev_login: 7 def setup_class(self): 8 desired_caps = { 9 # 设备信息 10 'platformName': 'Android', 11 'platformVersion': '5.1', 12 'deviceName': '555', 13 # app信息 14 'appPackage': 'io.manong.developerdaily', 15 'appActivity': 'io.toutiao.android.ui.activity.LaunchActivity', 16 # 中文输入允许 17 'unicodeKeyboard': True, 18 'resetKeyboard': True, 19 # 不重置应用状态 20 'noReset': True, 21 'newCommandTimeout': 6000 22 } 23 # 声明driver对象 24 self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) 25 self.dev_obj = Dev_Title(self.driver) 26 27 @pytest.fixture(scope='class') 28 def start_step(self): 29 # 点击加号 30 self.dev_obj.click_add() 31 # 点击邮箱登录 32 self.dev_obj.change_login() 33 34 def teardown_class(self): 35 self.driver.quit() 36 37 @pytest.mark.usefixtures('start_step') 38 @pytest.mark.parametrize("email, pwd", [('111', '222'), 39 ('benbenxiong567@163.com', '139432500')]) 40 def test_login(self, email, pwd): 41 self.dev_obj.login(email, pwd) 42 self.driver.get_screenshot_as_file(f'dev_{email}.png') 43 44