本文章转载于搜狗测试
前几篇Appium的文章讲解了Appium的安装和运行,以及在安装运行过程中的报错及解决方案。从本章起,小编带领大家一起用Appium做一点事情。
要想点击操作一个app,我们需要获取到这个app的控件,定位元素的方法较多,有Appium Inspector,UI Automation Viewer等,我这里用的是UI Automation Viewer。
1、获取控件的工具
SDK/tools/uiautomatorviewer.bat
双击打开,如下:
注意:这个工具只能是在Android4.2以上的系统上使用。
读出的信息有:
resource-id
text
index
class
package
...
2、实现一些基本操作的代码
以下代码实现了启动搜狗浏览器,点击“菜单”,然后再点击“设置”,进入设置页面后,滑动到页面最下方,之后点击“下载路径更改”这样的一系列操作。
#coding=utf-8
from appium import webdriver
import os
import unittest
from appium import webdriver
from time import sleep
import time
import urllib2
# Returns abs path relative to this file and notcwd
#PATH = lambda p: os.path.abspath(
# os.path.join(os.path.dirname(__file__),p)
#)
#点击设置中“下载路径更改”,验证直接进入路径设置界面,不会弹出路径选择框
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps["platformVersion"] = '6.0.1'
desired_caps["deviceName"] ='0815f8085adc2b04'
#desired_caps["deviceName"] ='4d005802acc22185'
#desired_caps["deviceName"] ='0123456789'
desired_caps["appPackage"] ='sogou.mobile.explorer'
#desired_caps['appActivity'] = '.BrowserActivity'
desired_caps['appActivity'] = '.NoDisplayActivity'
driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
time.sleep(10)
#btnSkip =driver.find_element_by_id('sogou.mobile.explorer:id/btn_skip')
#print len(btnSkip)
#if len(btnSkip) > 0:
#driver.find_element_by_id('sogou.mobile.explorer:id/btn_skip').click()
driver.find_element_by_id('sogou.mobile.explorer:id/toolbar_menu').click()
time.sleep(5)
driver.find_element_by_name('设置').click()
time.sleep(5)
#滑动到底,找到下载路径更改sogou.mobile.explorer:id/set_download_path
driver.swipe(700, 2435, 700, 200,1000)
time.sleep(5)
driver.find_element_by_id('sogou.mobile.explorer:id/set_download_path').click()
time.sleep(5)
以下是一个Appium Python API 中文版,这里为大家整理了Appium一些基本的操作,方便读者实现自己想实现的功能:
https://testerhome.com/topics/3711
3、点击物理按键
有时候我们遇到一些需要点击手机物理返回按键,或者home键等操作,总结如下:
adb shell input keyevent
点击home键:
adb shell input keyevent 3
点击back键:
adb shell input keyevent 4
具体按键参考:Android Keycode详解
http://blog.csdn.net/huiguixian/article/details/8550170
4、运行过程中报错整理
【报错一】
Appium的log上:
python工具上:
C:\Python27\python.exeD:/python/appium/download/downloadmodule/downloadpath.py
Traceback (most recent call last):
File"D:/python/appium/download/downloadmodule/downloadpath.py", line 24,in
driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
File "build\bdist.win32\egg\appium\webdriver\webdriver.py",line 36, in __init__
File"C:\Python27\lib\site-packages\selenium-2.50.0-py2.7.egg\selenium\webdriver\remote\webdriver.py",line 87, in __init__
self.start_session(desired_capabilities, browser_profile)
File"C:\Python27\lib\site-packages\selenium-2.50.0-py2.7.egg\selenium\webdriver\remote\webdriver.py",line 141, in start_session
'desiredCapabilities': desired_capabilities,
File "C:\Python27\lib\site-packages\selenium-2.50.0-py2.7.egg\selenium\webdriver\remote\webdriver.py",line 201, in execute
self.error_handler.check_response(response)
File"C:\Python27\lib\site-packages\selenium-2.50.0-py2.7.egg\selenium\webdriver\remote\errorhandler.py",line 188, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: A new session could notbe created. (Original error: sogou.mobile.explorer/.NoDisplayActivity neverstarted. Current: sogou.mobile.explorer/.BrowserActivity)
Process finished with exit code 1
解决方案:
(1)将设备信息和系统信息修改正确即可,由于我信息填写的还是之前用的手机,后期换了手机,忘记修改相应的信息,故其中的手机设置信息很重要。
(2)手机的控件没有刷新,需要用UI AutomatorViewer刷新一下
(3)退出浏览器,且重启Appium
【报错二】
python报错:
C:\Python27\python.exe D:/python/appium/download/downloadmodule/downloadpath.py
Traceback (most recent call last):
File"D:/python/appium/download/downloadmodule/downloadpath.py", line 27,in
driver.find_element_by_id('sogou.mobile.explorer:id/btn_skip')
File "C:\Python27\lib\site-packages\selenium-2.50.0-py2.7.egg\selenium\webdriver\remote\webdriver.py",line 234, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File"C:\Python27\lib\site-packages\selenium-2.50.0-py2.7.egg\selenium\webdriver\remote\webdriver.py",line 712, in find_element
{'using': by, 'value': value})['value']
File"C:\Python27\lib\site-packages\selenium-2.50.0-py2.7.egg\selenium\webdriver\remote\webdriver.py",line 201, in execute
self.error_handler.check_response(response)
File "build\bdist.win32\egg\appium\webdriver\errorhandler.py",line 29, in check_response
selenium.common.exceptions.NoSuchElementException:Message: An element could not be located on the page using the given searchparameters.
解决方案:
这是由于
driver.find_element_by_id('sogou.mobile.explorer:id/btn_skip').click()
driver.find_element_by_id('sogou.mobile.explorer:id/toolbar_menu').click()
点击过程,有一个控件没有找到,写脚本要保证相应的控件存在。