开始appium的第一个脚本
作者:互联网
设置DesiredCapabilities
存在于以下库中:
org.openqa.selenium.remote.DesiredCapabilities
Desired Capabilities告诉Appium Server需要哪种ssession。以hash键值对的形式存储。
常用(android和ios共有的)
Capability | Description | Values |
---|---|---|
automationName | Which automation engine to use | Appium (default) or Selendroid |
platformName | Which mobile OS platform to use | iOS, Android, or FirefoxOS |
platformVersion | Mobile OS version | e.g., 7.1, 4.4 |
deviceName | The kind of mobile device or emulator to use | iPhone Simulator, iPad Simulator, iPhone Retina 4-inch, Android Emulator, Galaxy S4, etc… |
Android专用
Capability | Description | Values |
---|---|---|
appActivity | Activity name for the Android activity you want to launch from your package | MainActivity, .Settings |
appPackage | Java package of the Android app you want to run | com.example.android.myApp, com.android.settings |
appWaitActivity | Activity name for the Android activity you want to wait for | SplashActivity |
appWaitPackage | Java package of the Android app you want to wait for | com.example.android.myApp, com.android.settings |
获取Android手机型号与设备信息
- 在命令行中输入“adb shell”
- 进入shell之后,再输入
cat /system/build.prop | grep "product"
输出:
shell@HS8916QC:/ $ cat /system/build.prop | grep "product"
ro.product.model=Hisense I635T
ro.product.brand=Hisense
ro.product.name=I635T
ro.product.device=HS8916QC
ro.product.board=msm8916
ro.product.cpu.abi=armeabi-v7a
ro.product.cpu.abi2=armeabi
ro.product.manufacturer=Hisense
ro.product.locale.language=zh
ro.product.locale.region=CN
# ro.build.product is obsolete; use ro.product.device
ro.build.product=I635T
ro.hmct.product.fullname=Hisense I635T
#add for product operator,this value is ct/cu/cmcc
ro.hmct.product.operator=ct
ro.hmct.smsregister.product=HS-Hisense I635T
ro.hmct.product.ua=Mozilla/5.0 (Linux; U; Android 4.4.4; zh-cn; HS-Hisense I635T
Build/KTU84L)
ro.hmct.product.ua.suffix=AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Mob
ile Safari/537.36
型号:Hisense I635T
查看具体的参数:
getprop 查看机器的全部信息参数
getprop ro.serialno 查看机器的SN号
getprop ro.carrier 查看机器的CID号
getprop ro.hardware 查看机器板子代号
getprop ro.bootloader 查看SPL(Hboot)版本号
设备通过wifi链接PC
官方教程
Appium有很多良好的特性,其中一个是通过wifi连接设备。
适用于多个设备连接一个server的情况。
前提:
Android SDK应该安装在机器上。
应该安装Android SDK包。
验证设备已经属于调试模式
adb devices
启动设备tcp/ip端口
首先用USB连接你的Android设备,然后在终端运行命令,它可以启动设备的5555端口使其在网络上可以连接
在TCP/IP模式下重启adb守护进程,然后监听5555端口(adb的默认端口)
adb tcpip 5555
查看设备ip地址
现在断开USB连接, 确保设备和你的电脑连接同一个无线网络
查看手机ip,设置—wifi—你连接的wifi—IP地址
连接
使用电脑上的adb服务连接设备上的IP地址(使用默认的5555端口)
adb connect 10.70.108.70:5555
测试是否连接成功
C:\Users>adb devices
List of devices attached
10.70.108.70:5555 device
通过wifi连接的特别不稳定,还是usb的方便点。
找到apk的appPackage和appActivity
通过命令行
准备步骤:
- 在设备上打开目标app
- 保证设备连上了电脑
- Andoid SDK已经安装在系统上
步骤1:打开命令行,输入adb devices
查看设备是否连接
步骤2:输入adb shell
进入与设备交互状态
步骤3:打开目标app如facebook输入以下命令
dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'
输出:
mCurrentFocus=Window{2d31b9ee u0 com.facebook.katana/com.facebook.katana.LoginActivity}
mFocusedApp=AppWindowToken{370ebfde token=Token{ad82319 ActivityRecord{7067260 u0 com.facebook.katana/.LoginActivity t3624}}}
appPackage是:com.facebook.katana
aapActivity是:com.facebook.katana.LoginActivity
第一行斜杠前后的那一串。
注意:执行命令,要保证app没有锁屏,打开了目标app。
退出交互模式输入:exit回车。
其他方法:通过Appium client自动识别,或者,通过第三方识别设备信息的app如SnapDeal。
使用UIAutomatorviewer
UIAutomatorviewer在Android sdk和tools文件夹下。用于查看布局层次结构。
uiautomatorviewer
第一个appium测试脚本
运行appium
打开cmd,输入命令appium
连接手机
查看是否连接上,cmd输入adb devices
查看设备连接列表。
编写python测试脚本
1 安装python-client-appium库:
pip install Appium-Python-Client
2 启动uiautomatorviewer
cmd输入命令uiautomatorviewer
启动UI。
UIAutomatorviewer和Android sdk打包在一起,在tools目录下。用于检查UI的层级关系,记以及视图与控件相关联的属性。可以查看到元素的查看element的package,属性和id。
3 编写python脚本
from appium import webdriver
import os
# Returns abs path relative to this file and not cwd
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
)
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4.4'
desired_caps['deviceName'] = 'Hisense I635T'
desired_caps['appPackage'] = 'io.appium.android.apis'
desired_caps['appActivity'] = 'io.appium.android.apis.ApiDemos'
desired_caps['app'] = PATH(
'./apps/ApiDemos.apk'
)
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
arr = driver.find_element_by_accessibility_id("App")
assert arr is not None
driver.quit()
遇到的错误
错误 1: python运行报错:urllib2.URLError: <urlopen error [Errno 10061] >
appium没有启动完全,启动要等一下运行py。
错误2:未解决
Telnet command got response: a[K[Dav[K[D[Davd[K[D[D[Davd [K[D[D[D[Davd n[K[D[D[D[D[Davd na[K[D[D[D[D[D[Davd nam[K[D[D[D[D[D[D[Davd name[K
错误3:
Original error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity
appactivity写错了,去掉前面的包名称,试试。
错误4:Original error: Permission to start activity。
没有权限,解决方法是让开发在AndroidManifest.xml文件中将Activity设置成允许调用:Android:exported=”true”。
再次运行,搞定。
测试模拟器
以上步骤也可以在模拟器上面进行测试。步骤如下:
进入Android安装目录,双击AVD Manager.exe
启动。创建一个模拟器并start。
错误:HAXM未安装,到SDK Manager中安装下,如果还是提示未安装,则到android sdk目录下的\extras\intel\Hardware_Accelerated_Execution_Manager双击下intelhaxm-android.exe进行安装。重新启动模拟器应该就OK了。
Device Name填写的是模拟器的名称,启动模拟器,appium后,再启动Inspector就能Reflesh启动App,来进行操作。可是这存在一个问题:模拟器比较慢,而且多少和真机不一样,比如说模拟器不能调出手机键盘等;所以如果我们要做自动化测试的时候,最好还是用真机来运行app,然后进行定位。
注:
能用真机,别用模拟器。模拟器慢的呀。
appium测试内嵌webview
Appium 通过 Chromedriver 内建混合应用支持。Appium 也可以使用 Selendroid 做为 4.4 之前的设备对 webview 支持的背部引擎。(你需要在 desired capability 里指定 "device": "selendroid")。
Appium的核心原则之一是你不应该改变你的应用程序来测试它。可以使用 Selenium 测试传统 web 应用的方法来测试混合 web 应用。这里会有一些技术性的复杂,Appium 需要知道你是想测试原生部分呢还是web部分。
测试步骤:
- 导航进入webview部分(可通过uiautomatorviewer找到对应的webview的名称)
- 调用
GET session/:sessionId/contexts
- 查看所有context:返回一个可以使用的contexts列表, 如 'NATIVE_APP' or 'WEBVIEW_1'
# 假设已经初始化了driver对象
# 切换到webview
webview = driver.contexts.last
driver.switch_to.context(webview)
# do some webby stuff
driver.find_element(:css, ".green_button").click
# 切换回native view
driver.switch_to.context(driver.contexts.first)
# do more native testing if we want
driver.quit()
错误集锦
手机跑过appium脚本后弹不出键盘
跑appium脚本的时候,会自动安装一个appium输入法到手机上,而且设置为默认输入法。所以当在输入框手动输入文字的时候,弹不出平常用的搜狗键盘来。
解决办法就是,到手机设置→语言和输入法→默认输入法选择为搜狗输入法即可。
问题2
selenium.common.exceptions.WebDriverException: Message: A new session
could not be created. Details: Problem getting session data for driver
type AndroidDriver; does it implement 'get driverData'?
adb.exe -P 5037 -s I635T shell getprop ro.build.version.release' exited with code 1{"stdout":"","stderr":"error: unknown host service\r\n","code":1}
错误显示某个程序占了5037端口,根据端口号查找对应的进程号:
netstat -ano | findstr 5037
结果:
C:\Users\Administrator>netstat -ano | findstr 5037
TCP 127.0.0.1:5037 0.0.0.0:0 LISTENING 368
TCP 127.0.0.1:5037 127.0.0.1:50000 ESTABLISHED 368
TCP 127.0.0.1:5037 127.0.0.1:50031 ESTABLISHED 368
TCP 127.0.0.1:5037 127.0.0.1:50809 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50810 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50811 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50812 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50814 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50815 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50844 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50845 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50856 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50857 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50896 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50897 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50927 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50928 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50968 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50969 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50971 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50972 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50979 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50980 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50982 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50983 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50984 TIME_WAIT 0
TCP 127.0.0.1:5037 127.0.0.1:50985 TIME_WAIT 0
TCP 127.0.0.1:50000 127.0.0.1:5037 ESTABLISHED 148
TCP 127.0.0.1:50031 127.0.0.1:5037 ESTABLISHED 148
发现 5037 端口被 PID(进程号)为 368 的进程占用。
据进程号寻找进程名称:
tasklist | findstr 368
C:\Users\Administrator>tasklist|findstr 368
360MobileLoader.exe 368 Console 1 5,844 K
从任务管理器中查找该程序,手动杀死即可。
在任务管理器中找不到该程序,从命令行杀死该程序了
taskkill -PID <进程号> -F //强制关闭某个进程
标签:脚本,appium,127.0,第一个,0.1,5037,TCP,TIME,WAIT 来源: https://www.cnblogs.com/for-you/p/12762471.html