appium 程序下载安装见之前的帖子:https://www.cnblogs.com/gancuimian/p/16536322.html

appium 环境搭建见之前的帖子:https://www.cnblogs.com/gancuimian/p/16557576.html

uiautomator2 常用公共方法封装见之前的帖子:https://www.cnblogs.com/gancuimian/p/16948536.html

selenium 常用公共方法封装见之前的帖子:

在写(UI)自动化测试用例的时候,最常用的就是方法的调用。我们在这里,把公共方法封装到一个文件中,

这样以后需要使用,直接调用这个方法就可以了。

以下为个人常使用到的一些 appium 公共方法的封装。

里面有一些操作是有重复的,这个根据个人情况,如果不需要可以不用。重复的话就多个选择,想用哪个用哪个。

包括个别的方法引用的库/类是 python 2.0版本以上弃用的,不过没关系,照样能用。

首先需要导入/引用到的库

import os,time,re,random,faker
from loguru import logger
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from typing import Dict, NoReturn, Tuple, List, Union
from appium.webdriver.webelement import WebElement as MobileWebElement
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions.pointer_input import PointerInput
from selenium.webdriver.support import expected_conditions

直接上代码:

class App_Page:
def __init__(self,driver):
self.driver = driver def click(self,element):
""" 根据元素点击 """
el = self.driver.find_element(*element)
el.click() def click_long(self,element,duration: int = 1000):
""" 根据元素长按1 """
el = self.driver.find_element(*element)
TouchAction(self.driver).long_press(el,duration=duration).release().perform() def new_click_long(self,element):
""" 根据元素长按2 """
el = self.driver.find_element(*element)
ActionChains(self.driver).click_and_hold(el).perform() def click_tap(self,x,y):
""" 根据坐标点击 """
action = TouchAction(self.driver)
action.tap(x=x,y=y).perform() def click_tap_long(self,x,y,duration: int = 1000):
""" 根据坐标(长按用百分比貌似不行)长按 """
action = TouchAction(self.driver)
action.long_press(x=x,y=y,duration=duration).release().perform() def click_two(self, element, sleepTime=0):
""" 点击,与第一种没太大区别,作为备用 """
if str(element).startswith("com"): # 若开头是com则使用ID定位
self.driver.find_element(*element).click() # 点击定位元素
elif re.findall("//", str(element)): # 若//开头则使用正则表达式匹配后用xpath定位
self.driver.find_element(*element).click() # 点击定位元素
else: # 若以上两种情况都不是,则使用描述定位
self.driver.find_element(*element).click() # 点击定位元素
time.sleep(sleepTime) def pinch_extend(self,kind: str = "out or in"):
""" 放大/缩小,主要用于图片操作 """
action = ActionChains(self.driver)
action.w3c_actions.devices = []
finger1 = action.w3c_actions.add_pointer_input('touch','finger1')
finger2 = action.w3c_actions.add_pointer_input('touch','finger2')
# 获取屏幕尺寸(宽高)
width = self.driver.get_window_size()['width']
height = self.driver.get_window_size()['height']
if kind == "out":
# 两个手指移动到屏幕正中间
finger1.create_pointer_move(x = width * 0.5, y = height * 0.5)
finger2.create_pointer_move(x = width * 0.5, y = height * 0.5)
# 两个手指按下去
finger1.create_pointer_down()
finger2.create_pointer_down()
# 两个手指移动
finger1.create_pointer_move(x = width * 0.5, y = height * 0.6)
finger2.create_pointer_move(x = width * 0.5, y = height * 0.4)
# 两个手指松开
finger1.create_pointer_up(0)
finger2.create_pointer_up(0)
action.perform()
elif kind == "in":
# 两个手指移动到屏幕正中间
finger1.create_pointer_move(x=width * 0.5, y=height * 0.6)
finger2.create_pointer_move(x=width * 0.5, y=height * 0.4)
# 两个手指按下去
finger1.create_pointer_down()
finger2.create_pointer_down()
# 两个手指移动
finger1.create_pointer_move(x=width * 0.5, y=height * 0.5)
finger2.create_pointer_move(x=width * 0.5, y=height * 0.5)
# 两个手指松开
finger1.create_pointer_up(0)
finger2.create_pointer_up(0)
action.perform()
else:
raise Exception("输入kind不能是非in/out") def click_press_move_one(self,x1,y1,x2,y2):
""" 按下之后滑动,长按滑动(这个用法好像不行,有用成功的踢我一脚) """
action = TouchAction(self.driver)
# 按住点(x1, y1),等待1000ms,滑动至点(x2, y2),释放
action.long_press(x=x1,y=y1).wait(500).move_to(x=x2,y=y2).release()
# 执行操作
action.perform() def click_press_move_two(self,x1,y1,x2,y2):
""" 按下之后滑动,(这里是把长按与滑动组合起来了,2步而不是1步) """
action = TouchAction(self.driver)
# 按住(x1, y1),等待xx秒,滑动至点(x2, y2),释放
action.long_press(x=x1,y=y1)
time.sleep(0.5)
# action.release() # 释放
action.perform() # 执行操作
self.driver.swipe(x1,y1,x2,y2)
action.release() # 释放 def input(self,element,value):
""" 输入框当中输入内容 """
el = self.driver.find_element(*element) # 先找到/定位到输入框
el.send_keys(value) # 输入框输入内容 def clear(self, element):
""" 清空输入框中的内容 """
el = self.driver.find_element(*element)
el.clear() def get_toast_text(self):
""" toast弹窗/获取toast文本内容 """
toast = self.driver.find_element("xpath", "//android.widget.Toast")
return toast.text def get_element_text(self,element):
""" 元素定位,获取text文本 """
el = self.driver.find_element(*element)
return el.text def get_screen_size(self) -> Tuple[int, int]:
""" 获取手机屏幕大小 """
x = self.driver.get_window_size()['width'] # 获取app的宽度
y = self.driver.get_window_size()['height'] # 获取app的高度
return x, y def swipe_screen(self, direction: str, duration_ms: int = 800) -> NoReturn:
""" 屏幕滑动操作 """
location = self.get_screen_size()
if direction.lower() == "up":
x = int(location[0] * 0.5)
start_y = int(location[1] * 0.75)
end_y = int(location[1] * 0.25)
self.driver.swipe(x, start_y, x, end_y, duration_ms)
elif direction.lower() == "down":
x = int(location[0] * 0.5)
start_y = int(location[1] * 0.25)
end_y = int(location[1] * 0.75)
self.driver.swipe(x, start_y, x, end_y, duration_ms)
elif direction.lower() == "left":
start_x = int(location[0] * 0.9)
y = int(location[1] * 0.5)
end_x = int(location[0] * 0.1)
self.driver.swipe(start_x, y, end_x, y, duration_ms)
elif direction.lower() == "right":
start_x = int(location[0] * 0.1)
y = int(location[1] * 0.5)
end_x = int(location[0] * 0.9)
self.driver.swipe(start_x, y, end_x, y, duration_ms)
else:
print("请输入正确的方向") def swipe_extend(self,x1,y1,x2,y2):
""" 根据坐标滑动 """
self.driver.swipe(x1,y1,x2,y2) def is_element_exist(self, element: Tuple[str, Union[str, Dict]], wait_seconds: int = 10) -> bool:
""" 判断元素是否存在 """
by = element[0]
value = element[1] try:
if by == "id":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.ID, value)))
elif by == "name":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.NAME, value)))
elif by == "class":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.CLASS_NAME, value)))
elif by == "text":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.LINK_TEXT, value)))
elif by == "partial_text":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.PARTIAL_LINK_TEXT, value)))
elif by == "xpath":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.XPATH, value)))
elif by == "css":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR, value)))
elif by == "tag":
WebDriverWait(self.driver, wait_seconds, 1).until(expected_conditions.presence_of_element_located((By.TAG_NAME, value)))
else:
raise NameError("Please enter the correct targeting elements,'id','name','class','text','xpath','css'.")
except:
return False
return True def find_elements(self, element: Tuple[str, Union[str, Dict]]) -> Union[List[MobileWebElement], List]:
""" 寻找一组元素,可用来断言该元素是否存在 """
by = element[0]
value = element[1]
try:
if self.is_element_exist(element):
if by == "id":
return self.driver.find_elements(By.ID, value)
elif by == "name":
return self.driver.find_elements(By.NAME, value)
elif by == "class":
return self.driver.find_elements(By.CLASS_NAME, value)
elif by == "text":
return self.driver.find_elements(By.LINK_TEXT, value)
elif by == "partial_text":
return self.driver.find_elements(By.PARTIAL_LINK_TEXT, value)
elif by == "xpath":
return self.driver.find_elements(By.XPATH, value)
elif by == "css":
return self.driver.find_elements(By.CSS_SELECTOR, value)
elif by == "tag":
return self.driver.find_elements(By.TAG_NAME, value)
else:
raise NameError("Please enter the correct targeting elements,'id','name','class','text','xpath','css'.")
except Exception as e:
logger.error(">>>>>>>> failed to find elements: %s is %s. Error: %s" % (by, value, e)) def is_text_exist(self, text: str, wait_seconds: int = 10) -> bool:
""" 判断text是否于当前页面存在 """
for i in range(wait_seconds):
if text in self.driver.page_source:
return True
time.sleep(1)
return False def screenshot(self, name):
""" 截图(注释的部分,根据个人需求可增or减) """
# day = time.strftime('%Y-%m-%d', time.localtime(time.time()))
# fp = "..\\Result\\" + day
fp = ".\\images\\" # ".":表示上级; "..":表示上上级 tm = time.strftime('%Y-%m-%d-%H_%M', time.localtime(time.time()))
if os.path.exists(fp):
filename = fp + "\\" + tm + '_' + name + '.png'
else:
os.makedirs(fp)
filename = fp + "\\" + tm + '_' + name + '.png'
self.driver.save_screenshot(filename) def get_screen(self, path):
""" 使用该方法也可截图,路径我没配 """
self.driver.get_screenshot_as_file(path) def screenshot_physical_key(self):
""" 截图:用命令模拟安卓物理按键事件(可能需要有root权限) """
self.driver.shell("sendevent /dev/input/event0 1 114 1")
self.driver.shell("sendevent /dev/input/event0 0 0 0")
self.driver.shell("sendevent /dev/input/event0 1 116 1")
self.driver.shell("sendevent /dev/input/event0 0 0 0")
self.driver.shell("sendevent /dev/input/event0 1 116 0")
self.driver.shell("sendevent /dev/input/event0 0 0 0")
self.driver.shell("sendevent /dev/input/event0 1 114 0")
self.driver.shell("sendevent /dev/input/event0 0 0 0") def randmon_phone(self):
""" 随机生成一个手机号,或者其他想生成的数据 """
while True:
phone = "130"
for i in range(8):
num = random.randint(0, 9)
phone += str(num)
return phone def generate_phone_number(self):
""" 随机生成手机号(与上面的实现方法一致,写法用了列表推导式) """
prefix = "130"
suffix = [random.randint(0, 9) for _ in range(8)]
return f"{prefix}{''.join([str(i) for i in suffix])}" def new_mobile(self):
""" 随机生成手机号,需下载:pip install pytest_facker """
fk = faker.Faker(locale=["zh_CN"])
return fk.phone_number() def get_toast(self):
""" toast 弹窗 """
el= self.driver.find_element(By.XPATH,'//android.widget.Toast')
return el def enter(self):
""" 回车 """
self.driver.press_keycode(MobileKey.KEYCODE_ENTER) def volume_up(self):
""" 音量加 """
self.driver.press_keycode(MobileKey.KEYCODE_VOLUME_UP) def select(self):
""" 搜索 """
self.driver.press_keycode(MobileKey.KEYCODE_SEARCH) if __name__ == '__main__':
pass

最后几个虚拟按键的封装,是引用了下面这个类,如果不想引用的话,方法里面的值直接写成对应的数字即可。

class MobileKey(PointerInput):
""" 每个数字代表的含义:https://www.jianshu.com/p/f7ec856ff56f """
KEYCODE_BACK = 4
KEYCODE_CALL = 5
KEYCODE_VOLUME_UP = 24
KEYCODE_VOLUME_DOWN = 25
KEYCODE_ENTER = 66
KEYCODE_SEARCH = 84

以上就是本人 appium自动化常用到的一些公共方法的封装。

最新文章

  1. Unity Animator动画状态机 深入理解(一)
  2. Yii2中多表关联查询(join、joinwith)
  3. [转]linux中强大的screen命令
  4. ubuntu openstack packaages
  5. html笔记04:在html之中导入css两种常见方法
  6. jQuery插件之Form
  7. Reflux 使用教程
  8. 【小分享】Date对象封装,时间格式化函数time()
  9. 用 Vue 全家桶二次开发 V2EX 社区
  10. NYOJ 69 数的长度(数学)
  11. Ecshop商品描述上传中文名图片无法显示解决方法
  12. Java设计模式之职责链设计模式
  13. linux日常服务器部署一些命令使用
  14. HDU-1709 The Balance(生成函数)
  15. spring创建bean的三种方式
  16. android初学
  17. 【Static Program Analysis - Chapter 2】 代码的表征之控制流图
  18. Java与JavaScript之间关于JSON的是非恩怨
  19. IE浏览器url中带中文报错的问题;以及各种兼容以及浏览器问题总结
  20. SpringBoot异步请求

热门文章

  1. 硬件路灯第一期CPU篇
  2. (0724) 格雷码 verilog
  3. centos8使用kubeadm搭建高可用k8s集群
  4. springcloud(二) - 服务调用Feign&openFeign
  5. pytest学习总结
  6. mac os 11 Big Sur 根目录无法写入解决办法
  7. 一、mysql基础
  8. 标量子查询加聚合函数sql改写一
  9. Assembler Instructions with C Expression Operands
  10. PowerShell学习笔记二_变量、Select、Foreach、where、自动变量