前提:已部署appnium客户端并启动服务 模拟场景:
- 进入app首页切换题库
- 进入我的页面识别用户名并断言
class Driver:
driver: WebDriver
def start_android(self):
desire_caps = {
'platformName': 'Android',
'platformVersion': '10',
'deviceName': 'XXXXXXXXXXx',
'appPackage': 'XXXXXXXXXXXX',
'appActivity': 'XXXXXXXXXXXXX',
'noReset': True,
'automationName': 'uiautomator2',
}
driver = webdriver.Remote('http://0.0.0.0:4723/wd/hub', desire_caps)
print("app启动成功...")
driver.implicitly_wait(10)
return driver
class BasePage:
def __init__(self,driver:WebDriver):
self.driver = driver
def wait(self,time):
self.driver.implicitly_wait(time)
def get_element(self,*locator):
return self.driver.find_element(*locator)
def send_keys(self,*locator,text):
print('正在输入文本...')
self.wait(3)
self.get_element(*locator).click()
self.get_elemenet(*locator).send_keys(text)
def click_element(self,*locator):
self.wait(3)
self.get_element(*locator).click()
def get_element_attribute(self,attribute_name,*locator):
return self.get_element(*locator).get_attribute(attribute_name)
def tap_element(self,locator):
self.driver.tap([[0,0],locator],500)
class HomePage(King):
change_subject1 = (By.XPATH, 'XXXXXXXXXX[@content-desc="综合笔试"]')
radio_button_me = (By.ID, 'XXXXXXXXXX:id/radio_button_me')
def change_subject(self):
self.get_element(*self.change_subject1).click()
return ChangeSubjectPage(self.driver)
def to_mine(self):
self.get_element(*self.radio_button_me).click()
return MinePage(self.driver)
快速获得手机屏幕坐标的方法: 开启系统设置-开发者选项-指针设置
class ChangeSubjectPage(BasePage):
licedsed = (By.XPATH,'XXXXXXXXXX/android.widget.LinearLayout')
tap_tv_next = [900, 2000]
rb_first = (By.ID,'XXXXXXXXXX:id/rb_first')
def to_comprehensive(self):
self.click_element(*self.licedsed)
self.tap_element(self.tap_tv_next)
return ChangeSubjectPage(self.driver)
def choose_first(self):
self.get_element(*self.rb_first).click()
self.tap_element(self.tap_tv_next)
class MinePage(BasePage):
head = (By.ID,'XXXXXXXXXX:id/img_profile_head')
def to_personal(self):
self.click_element(*self.head)
return PersonalPage(self.driver)
class PersonalPage(BasePage):
name = (By.XPATH,'XXXXXXXXXX="昵称 请选择昵称"]/android.view.View[2]')
dt_back = (By.ID,'XXXXXXXXXX:id/dt_back')
def get_name(self):
return self.get_element_attribute('text',*self.name)
def to_back(self):
self.click_element(*self.dt_back)
return MinePage(self.driver)
class TestDemo:
def setup_class(self):
self.driver= Driver().start_android()
self.homePage = HomePage(self.driver)
def test_changesub(self):
self.homePage.change_subject().to_comprehensive().choose_first()
class TestDemo:
def setup_class(self):
self.driver = Driver().start_android()
self.homePage = HomePage(self.driver)
def test_personal(self):
person = self.homePage.to_mine().to_personal()
assert person.get_name() == 'XXXXXXXXXX'
person.to_back()
封装日志类,将日志以report方式输出
import logging
from config.pathName import PathName
pathName = PathName()
def logger():
'''
Logger 记录器,用于设置日志采集。
Handler 处理器,将日志记录发送至合适的路径。
Filter 过滤器,提供了更好的粒度控制,它可以决定输出哪些日志记录。
Formatter 格式化器,指明了最终输出中日志的格式。
'''
logger = logging.getLogger()
logger.handlers.clear()
logger.setLevel(logging.INFO)
logfile = pathName.get_file_path('log','log.txt')
fh = logging.FileHandler(logfile,mode='a',encoding='utf-8')
fh.setLevel(logging.INFO)
format = logging.Formatter(
f"%(asctime)s | %(levelname)s | %(filename)s - %(funcName)s - [line:%(lineno)d]: %(message)s"
)
fh.setFormatter(format)
logger.addHandler(fh)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(format)
logger.addHandler(ch)
return logger
class Path:
"""获取文件路径"""
def get_file_path(self, path, file=None):
"""获取当前项目的路径"""
if file is None:
path = os.path.join(os.path.dirname(os.path.dirname(__file__)), f"{path}")
else:
path = os.path.join(os.path.dirname(os.path.dirname(__file__)), f"{path}/{file}")
return path
|