appium属性模板
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
class BasePages:
def __init__(self,driver):
self.driver = driver
#元素定位
def locator(self,*loc):
return self.driver.find_element(*loc)
#清空
def clear(self,*loc):
self.locator(*loc).clear()
#输入
def input(self,test,*loc):
self.locator(*loc).send_keys(test)
#点击
def click(self,*loc):
self.locator(*loc).click()
# swip滑动事件 从一个坐标位置滑动到另一个坐标位置,只能是两个点之间的滑动
def swip_huadong(self,x,y,x1,y1):
# 滑动没有持续时间
self.driver.swipe(x, y, x1, y1)
# 滑动持续5秒的时间 X,Y
def swip_huandong_shijian5(self,x,y,x1,y1):
self.driver.swipe(x, y, x1, y1, 5000)
# 手指按操作:模拟手指按下屏幕,按就要对应着离开
def sz_dianji(self,*args):
# 通过元素定位方式按下屏幕
el = self.driver.find_element(*args)
TouchAction(self.driver).press(el).release().perform()
def sz_dianji_zuonbiao(self,x,y):
# 通过坐标方式按下屏幕
TouchAction(self.driver).press(x=x, y=y).release().perform()
# 等待操作
def dengdai(self,x,y):
TouchAction(self.driver).press(x=x, y=y).wait(5000).release().perform()
# 手指长按操作:模拟手机按下屏幕一段时间,按就要对应着离开
def sz_changan(self,x,y):
TouchAction(self.driver).long_press(x=x, y=y).perform()
# 手指移动一次操作
def sz_yidong1(self,x,y,x1,y1):
TouchAction(self.driver).press(x=x, y=y).wait(100).move_to(x=x1, y=y1).release().perform()
# 手指移动二次操作
def sz_yidong2(self,x,y,x1,y1,x2,y2):
TouchAction(self.driver).press(x=x, y=y).wait(100).move_to(x=x1, y=y1)\
.move_to(x=x2, y=y2).release().perform()
# 手指移动三次操作
def sz_yidong3(self,x,y,x1,y1,x2,y2,x3,y3):
TouchAction(self.driver).press(x=x, y=y).wait(100).move_to(x=x1, y=y1)\
.move_to(x=x2, y=y2).move_to(x=x3, y=y3).release().perform()
# 手指移动四次操作
def sz_yidong4(self,x,y,x1,y1,x2,y2,x3,y3,x4,y4):
TouchAction(self.driver).press(x=x, y=y).wait(100).move_to(x=x1, y=y1)\
.move_to(x=x2, y=y2).move_to(x=x3, y=y3).move_to(x=x4, y=y4).release().perform()
# 手指移动五次操作
def sz_yidong5(self,x,y,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5):
TouchAction(self.driver).press(x=x, y=y).wait(100).move_to(x=x1, y=y1)\
.move_to(x=x2, y=y2).move_to(x=x3, y=y3).move_to(x=x4, y=y4)\
.move_to(x=x5, y=y5).release().perform()
# 手指移动六次操作
def sz_yidong6(self,x,y,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6):
TouchAction(self.driver).press(x=x, y=y).wait(100).move_to(x=x1, y=y1)\
.move_to(x=x2, y=y2).move_to(x=x3, y=y3).move_to(x=x4, y=y4)\
.move_to(x=x5, y=y5).move_to(x=x6, y=y6).release().perform()
# 手指移动七次操作
def sz_yidong7(self,x,y,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7):
TouchAction(self.driver).press(x=x, y=y).wait(100).move_to(x=x1, y=y1)\
.move_to(x=x2, y=y2).move_to(x=x3, y=y3).move_to(x=x4, y=y4)\
.move_to(x=x5, y=y5).move_to(x=x6, y=y6).move_to(x=x7, y=y7).release().perform()
# 手指移动八次操作
def sz_yidong8(self,x,y,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8):
TouchAction(self.driver).press(x=x, y=y).wait(100).move_to(x=x1, y=y1)\
.move_to(x=x2, y=y2).move_to(x=x3, y=y3).move_to(x=x4, y=y4)\
.move_to(x=x5, y=y5).move_to(x=x6, y=y6).move_to(x=x7, y=y7)\
.move_to(x=x8, y=y8).release().perform()
# 手指移动九次操作
def sz_yidong9(self,x,y,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,x8,y8,x9,y9):
TouchAction(self.driver).press(x=x, y=y).wait(100).move_to(x=x1, y=y1)\
.move_to(x=x2, y=y2).move_to(x=x3, y=y3).move_to(x=x4, y=y4)\
.move_to(x=x5, y=y5).move_to(x=x6, y=y6).move_to(x=x7, y=y7)\
.move_to(x=x8, y=y8).move_to(x=x9, y=y9).release().perform()
#滑动(上下左右滑动)
def swipe(self,start_x,start_y,end_x,end_y,duration=0):
#获取屏幕的尺寸
window_size = self.driver.get_window_size()
x = window_size["width"]
y = window_size["height"]
self.driver.swipe(start_x=x*start_x,
start_y=y*start_y,
end_x= x*end_x,
end_y=y*end_y,
duration=duration)
yml文件
caps:
deviceName: LDN-TL00
platformName: Android
appActivity: com.yxcorp.gifshow.HomeActivity
appPackage: com.kuaishou.nebula
读取yml文件
import yaml
def readYaml(path):
with open(path,"r",encoding="utf-8") as file:
data = yaml.load(stream=file,Loader=yaml.FullLoader)
return data
想要测试的APP模块
from fu.BasePages import BasePages
from appium.webdriver.common.mobileby import MobileBy #appium 用这个定位
class Baidu(BasePages):
#继承
def __init__(self,driver):
BasePages.__init__(self,driver)
需要测试几个写几个测试模块
导入单元测试,开始测试,每个方法测试一个模块
# This sample code uses the Appium python client
# pip install Appium-Python-Client==1.3.0
# Then you can paste this into a file and simply run with Python
import os,unittest,time
from html.HTMLTestRunner import HTMLTestRunner
from appium import webdriver
from duwenjian.dusj import readYaml
from cs_moban.cs_001 import TaoBao
from cs_moban.cs_002 import BiJiBeng
class TestClass(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
# os.path.dirname(__file__) 选择上个目录 os.path.abspath 转义 / 转为 \
rootpath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
# os.path.join 字符串连接
dd = os.path.join(rootpath,"wenjian\sj.yml")
aa = readYaml(dd)
cls.driver = webdriver.Remote("http://localhost:4723/wd/hub",aa['caps'])
cls.driver.implicitly_wait(30)
def test001(self):
pass
def test002(self):
pass
def test003(self):
pass
@classmethod
def tearDownClass(cls) -> None:
#关闭 移动端 APP
time.sleep(20)
cls.driver.quit()
taojian = unittest.TestSuite()
test_lise = ["test001", "test002", "test003"]
for i in test_lise:
taojian.addTest(TestClass(i))
with open("../report1.html", "wb") as f:
HTMLTestRunner(
stream=f,
title="单元测试",
description="测试一期",
verbosity=2
).run(taojian)