Appium 基础教程

Appium 基础教程

Appium inspector的使用:
(1)启动和配置;
(2)定位和操作元素;
(3)录制

定位元素
(1)resource_id: find_element_by_id:
(2)content-desc: find_element_by__accessibility_id
(3)xpath: 结合属性/上下级节点/索引定位
(4)重复元素+索引定位: l = find_elements_by_… l[1].click()
(5)逐级定位: driver.find_element_by_id(…).find_element_by_class_name(…)
(6)使用By及元素定位器
from appium.webdriver.common.mobileby import By
driver.find_element(By.ID, “…”)

uiautomator的原始定位方式

(1)通过id定位

# resourceId
driver.find_element_by_android_uiautomator('new UiSelector().resourceId(“id”)').click()

(2)通过text定位

# Text定位 text
driver.find_element_by_android_uiautomator('new UiSelector().text("textstr")')
# 文字包含 textContains
driver.find_element_by_android_uiautomator('new UiSelector().textContains(textStr)')
# 以哪个字符开始 textStartsWith
driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith(textStr)')

(3)通过class定位

driver.find_element_by_android_uiautomator('new UiSelector().className("class属性")')

(4)多属性定位

driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.TextView").text("Custom View")')

能够用来定位元素的属性主要有以下几种:
😊 UISelector.text
😊 UISelector.textContains
😊 UISelector.textStartsWith
😊 UiSelector.resourceId
😊 UISelector.className
😊 UISelector.textMatches
😊 UISelector.classNameMatches
😊 UiSelector.fromParent
😊 UiSelector.childSelector
😊 UiSelector.resourceIdMatches
😊 UiSelector.description
😊 UiSelector.descriptionStartWith
😊 UiSelector.descriptionMatches

操作元素
★ click()
★ send_keys()
★ clear()
★ is_displayed()/is_enabled()/is_checked()
★ get_attribute(): 获取属性 文本/状态/是否可操作等
get_attribute(“text”)
get_attribute(“resourceId”)
get_attribute(“className”)
★ set_value(): 设置text值
★ text: 元素的文本
★ location: 获取元素坐标值
★ size: 获取元素大小

1. 打开微信应用 ,判断是否存在 登录按钮
2. 如果已经登录,登录按钮不存在 ,就直接进入登录页,打印 通讯录的这些属性值, resourceid,text,className,enabled,clickable,selected,Displayed
c = driver.find...()
print(c.get_attribute("clickable"))
print(c.is_enabled()/c.is_displayed()/c.is_selected())
如果登录按钮存在 ,打印这个登录按钮的上面的属性值resourceid,text,className,enabled,clickable,selected,Displayed, 并点击登录按钮,
	使用set_value())输入用户名密码
	判断登录按钮是否可用,
		可用的话打印可用,并点击登录,判断是否登录成功
		不可用打印不可用
3. 退出应用


try:  # 尝试定位登录按钮
	l = driver.find_element_by_id("")	
except:  # 未定位到
	print(“没有登录按钮”)
	c = driver.find_....()    # 定位 通讯录 标签
	print(c.get_attribute('resource-id')  # 打印各种属性
	print(c.text)
	print(c.get_attribute(class)
	print(c.is_enabled())
	....
else:  # 如果没有异常(定位到了登录按钮)
	print(l.get_attribute("resource-id")   # 打印登录按钮的各种属性
	print(l.text)
	print(l.get_attribute(class)
	print(l.is_enabled())
	...
	l.click()  # 点击登录按钮
	driver.find…().set_value("18010181267")  # 使用set_value输入文字
	driver.find…().set_value("123456")  # 使用set_value输入密码
	l2 = driver.find..()  # 定位 登录 按钮
	if l2.is_enabled() is True:  # 判断 登录按钮 是否可用
		print(“可用”)
		l2.click()  # 点击登录按钮
		try:   # 判断是否登录成功, 登录成功后会有 "通讯录" 这个标签
			c = driver.find_....()   # 尝试定位通讯录这个标签
		except:  # 定位不到打印失败
			print(“登录失败”)
		else:  # 没有异常(定位到) 打印成功
			print(“登录成功”)
	else:
		print("不可用")  # 上面的l2登录按钮不可用打印不可用

按键操作

drvier.keyevent()
driver.press_keycode()

屏幕操作

tap: 触控(点击指定坐标),支持多点触控
swipe: 滑动
flick: 快滑
pinch:
zoom:
get_screenshot_as_file: 截图

获取屏幕宽度
width = driver.get_window_size()[‘width’]
height = driver.get_window_size()[‘height’]

文件操作

driver.pull()
driver.push()

APP操作

autoLaunch=False
driver.is_app_installed()
driver.install_app()
driver.remove_app()
driver.launch_app()
driver.start_activity()
driver.current_activity()
driver.background_app(3)
driver.close_app()

网络操作
driver.network_connection

查找已安装app(无apk包)Package/MainActiviy方法

Copy
adb shell
logcat | grep cmp=

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值