本文章轉(zhuǎn)載于搜狗測(cè)試
前幾篇Appium的文章講解了Appium的安裝和運(yùn)行腥放,以及在安裝運(yùn)行過(guò)程中的報(bào)錯(cuò)及解決方案。從本章起,小編帶領(lǐng)大家一起用Appium做一點(diǎn)事情。
要想點(diǎn)擊操作一個(gè)app熏纯,我們需要獲取到這個(gè)app的控件,定位元素的方法較多粤策,有Appium Inspector,UI Automation Viewer等误窖,我這里用的是UI Automation Viewer叮盘。
1、獲取控件的工具
SDK/tools/uiautomatorviewer.bat
雙擊打開(kāi)霹俺,如下:
注意:這個(gè)工具只能是在Android4.2以上的系統(tǒng)上使用柔吼。
讀出的信息有:
resource-id
text
index
class
package
...
2、實(shí)現(xiàn)一些基本操作的代碼
以下代碼實(shí)現(xiàn)了啟動(dòng)搜狗瀏覽器丙唧,點(diǎn)擊“菜單”愈魏,然后再點(diǎn)擊“設(shè)置”,進(jìn)入設(shè)置頁(yè)面后,滑動(dòng)到頁(yè)面最下方培漏,之后點(diǎn)擊“下載路徑更改”這樣的一系列操作溪厘。
#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)
#)
#點(diǎn)擊設(shè)置中“下載路徑更改”,驗(yàn)證直接進(jìn)入路徑設(shè)置界面牌柄,不會(huì)彈出路徑選擇框
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('設(shè)置').click()
time.sleep(5)
#滑動(dòng)到底畸悬,找到下載路徑更改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)
以下是一個(gè)Appium Python API 中文版,這里為大家整理了Appium一些基本的操作珊佣,方便讀者實(shí)現(xiàn)自己想實(shí)現(xiàn)的功能:
https://testerhome.com/topics/3711
3蹋宦、點(diǎn)擊物理按鍵
有時(shí)候我們遇到一些需要點(diǎn)擊手機(jī)物理返回按鍵,或者h(yuǎn)ome鍵等操作咒锻,總結(jié)如下:
adb shell input keyevent
點(diǎn)擊home鍵:
adb shell input keyevent 3
點(diǎn)擊back鍵:
adb shell input keyevent 4
具體按鍵參考:Android Keycode詳解
http://blog.csdn.net/huiguixian/article/details/8550170
4冷冗、運(yùn)行過(guò)程中報(bào)錯(cuò)整理
【報(bào)錯(cuò)一】
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)將設(shè)備信息和系統(tǒng)信息修改正確即可,由于我信息填寫(xiě)的還是之前用的手機(jī)惑艇,后期換了手機(jī)贾惦,忘記修改相應(yīng)的信息,故其中的手機(jī)設(shè)置信息很重要敦捧。
(2)手機(jī)的控件沒(méi)有刷新须板,需要用UI AutomatorViewer刷新一下
(3)退出瀏覽器,且重啟Appium
【報(bào)錯(cuò)二】
python報(bào)錯(cuò):
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()
點(diǎn)擊過(guò)程兢卵,有一個(gè)控件沒(méi)有找到习瑰,寫(xiě)腳本要保證相應(yīng)的控件存在。