基于Appium Python构建POM框架:入门步骤与文件关联问题求助
Appium Python POM框架入门步骤与代码结构示例
项目基础结构
先明确标准POM分层结构,各模块职责清晰,便于维护:
appium_pom_demo/ ├── conftest.py # pytest夹具,统一管理Appium驱动的初始化/销毁 ├── locators/ # 页面元素定位器集合,与业务操作解耦 │ ├── login_locators.py │ └── home_locators.py ├── actions/ # 单个页面的原子操作封装 │ ├── login_actions.py │ └── home_actions.py ├── flows/ # 跨页面业务流程组合 │ └── login_flow.py ├── test_suites/ # 测试用例集合 │ └── test_login.py └── config.py # 可选配置文件,存放设备/服务信息
1. 编写conftest.py(驱动夹具)
作为pytest的全局夹具,负责初始化Appium驱动,所有测试用例可直接依赖该驱动实例:
# conftest.py import pytest from appium import webdriver from config import APPIUM_SERVER, DESIRED_CAPS @pytest.fixture(scope="session") def driver(): # 初始化Appium驱动 driver = webdriver.Remote(APPIUM_SERVER, DESIRED_CAPS) yield driver # 测试会话结束后销毁驱动 driver.quit()
配套的config.py(可选,抽离配置便于修改):
# config.py APPIUM_SERVER = "http://localhost:4723/wd/hub" DESIRED_CAPS = { "platformName": "Android", "platformVersion": "11", "deviceName": "emulator-5554", "appPackage": "com.example.testapp", "appActivity": ".MainActivity", "automationName": "UiAutomator2", "noReset": True }
2. 封装元素定位器(locators目录)
每个页面的元素定位单独存为一个类,避免硬编码,修改定位只需改这里:
# locators/login_locators.py class LoginLocators: USERNAME_INPUT = ("id", "com.example.testapp:id/username") PASSWORD_INPUT = ("id", "com.example.testapp:id/password") LOGIN_BUTTON = ("id", "com.example.testapp:id/login_btn") ERROR_TIP = ("xpath", "//android.widget.TextView[@text='用户名或密码错误']")
3. 封装页面原子操作(actions目录)
针对单个页面的操作(输入、点击、获取文本等)进行封装,每个操作对应一个方法:
# actions/login_actions.py from locators.login_locators import LoginLocators class LoginActions: def __init__(self, driver): self.driver = driver def input_username(self, username): self.driver.find_element(*LoginLocators.USERNAME_INPUT).send_keys(username) def input_password(self, password): self.driver.find_element(*LoginLocators.PASSWORD_INPUT).send_keys(password) def click_login_btn(self): self.driver.find_element(*LoginLocators.LOGIN_BUTTON).click() def get_error_tip(self): return self.driver.find_element(*LoginLocators.ERROR_TIP).text
4. 封装业务流程(flows目录)
将多个页面的操作组合成完整业务流程,比如登录流程包含输入账号、密码、点击登录、校验结果:
# flows/login_flow.py from actions.login_actions import LoginActions from actions.home_actions import HomeActions class LoginFlow: def __init__(self, driver): self.login_actions = LoginActions(driver) self.home_actions = HomeActions(driver) def execute_login(self, username, password): self.login_actions.input_username(username) self.login_actions.input_password(password) self.login_actions.click_login_btn() def check_login_success(self): # 假设首页有欢迎文本元素 return self.home_actions.get_welcome_text() def check_login_failure(self): return self.login_actions.get_error_tip()
5. 编写测试用例(test_suites目录)
用pytest编写测试用例,直接调用flows层的业务流程,无需关注底层操作细节:
# test_suites/test_login.py from flows.login_flow import LoginFlow def test_valid_login(driver): login_flow = LoginFlow(driver) login_flow.execute_login("test_user", "test_pass123") welcome_text = login_flow.check_login_success() assert welcome_text == "欢迎回来,test_user!" def test_invalid_login(driver): login_flow = LoginFlow(driver) login_flow.execute_login("wrong_user", "wrong_pass") error_msg = login_flow.check_login_failure() assert error_msg == "用户名或密码错误"
内容的提问来源于stack exchange,提问作者MurodJonQA
相关产品推荐
相关产品推荐

