本文章轉載于搜狗測試
前幾篇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()
點擊過程,有一個控件沒有找到,寫腳本要保證相應的控件存在。