真的感謝@蟲師 這位來自互聯網的老師,讓我這個原本對代碼膽怯且迷惑的人開始學習自動化測試。
一開始搜索自動化測試的時候,蟲師的博客園教程都是在百度的前幾位的,我就跟着蟲師博客園里面的教程學習。后來學seleinum聽說他出書了《python+selenium》的這本我買了也學習了,是這本書帶着我這個代碼小白入門的,其實很多事情不願意開始就一直不會開始,一旦開始了,可能也沒有當初想的那么難。最近想學習下appium的時候發現他還有一個專門針對測試的網頁http://www.testclass.net/,現在這么有用且條理清晰沒有廣告的網站真的很難得。
有關appium幾乎所有的內容都是跟着testclass網站上的教程走下來的,不過踩到了幾個坑,這里就記錄一下這幾個坑。
一、Could not extract PIDs from ps output.
配置好所有的環境后,運行腳本,提示下面的錯誤:
selenium.common.exceptions.WebDriverException: Message: A new session could not be created. (Original error: Could not extract PIDs from ps output. PIDS: [], Procs: ["bad pid 'uiautomator'"])
修改 Appium\node_modules\appium\node_modules\appium-adb\lib\adb.js 文件
添加這行:
this.shell("ps '" + name + "'", function (err, stdout) {
...
var procs = [];
var outlines = stdout.split("\n");
outlines.shift() //添加這行
參考文檔:http://blog.csdn.net/zxz_tsgx/article/details/53204258
二、獲取應用的包名packageName 和 ActivibyName
配置desired_caps 信息時需要獲得appPackage和appActivity
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '7.0'
desired_caps['deviceName'] = 'c1b8cb07'
desired_caps["appPackage"] = 'com.lianchuan.cashloan'
desired_caps['appActivity'] = 'com.lianchuan.cashloan.activity.other.SplashActivity'
如果關於apk的信息未知,可以通過使用logcat方法:
清除logcat內容,使用命令adb logcat -c
啟動logcat,使用命令adb logcat ActivityManager:I *:s
啟動要查看的程序
括號中分別為PackageName和ActivityName
參考文檔:http://blog.csdn.net/jlminghui/article/details/40622103
三、有關開啟android模擬器
創建模擬器時提示 No System images installed for this target,需要到SDK Manager中安裝“ARM EABI v7a System Image”組件。
模擬器打開是很慢的,需要很多耐心,且模擬其中的一些操作和真機還有所不同,下面是模擬器按鍵和鍵盤按鍵的對應。
參考文檔:http://www.cnblogs.com/fnng/p/4560298.html
四、android webdriver appium中的Xpath定位
appium中的定位主要用到的工具是SDK 自帶的uiautomatoviewer.bat,這個文件存放在SDK 目錄的tools文件夾中。
UI automator Viewer工具可以查看錄制下來的頁面的屬性。
參考文檔:https://testerhome.com/topics/7129
五、appium中滑動屏幕的方法
appium中的swipe方法的使用為:swipe(int startx, int starty, int endx, int endy, int duration)
start_x:開始滑動的x坐標。 * start_y:開始滑動的y坐標。 * end_x:結束滑動的x坐標。 * end_y:結束滑動的y坐標。 * duration:持續時間
但是不同大小的手機屏幕,元素所在的位置是不確定的,所以可以用占屏幕的位置比例來定位滑動的位置。
獲取屏幕的寬 width = driver.get_window_size()['width']
獲取屏幕高 height = driver.get_window_size()['height']
獲得屏幕的寬高之后,上滑操作可以理解為,手指從屏幕的3/4高度出移動到1/4高度處。
driver.swipe(1/2*width, 3/4*height, 1/2*with, 1/7*height, 200)
參考文檔:http://blog.csdn.net/u011541946/article/details/77986018
六、在有些手機上每次運行都要提示安裝Appium Settings 和 Unlock的問題
不同的手機設置這個情況也未必都會出現,如果出現這個問題,可以在手機已經安裝好這兩個之后,操作\Appium\node_modules\appium\lib\devices\android目錄下的android.js文件
//this.pushSettingsApp.bind(this),
//this.pushUnlock.bind(this),
//this.unlock.bind(this),
將上面三行注釋再重啟appium。
參考文檔:https://www.cnblogs.com/dreamyu/p/8027050.html
七、Appium中send_keys無法輸入中文 和 Appium中鍵盤輸入不能使用的問題
Appium已支持中文輸入,但是需要用appium自帶輸入法(沒有UI 界面)
在初始化設置中增加一下兩行代碼:
desired_caps["unicodeKeyboard"] = "True"
desired_caps["resetKeyboard"] = "True"
參考文檔:https://testerhome.com/topics/1903
Appium中鍵盤輸入無法使用問題,需要用adb方法先調用一個輸入法,在cmd中輸入
adb shell ime list -s 查看所有可用輸入法
adb shell ime set com.sohu.inputmethod.sogou/.SogouIME 調用搜狗輸入法
將對adb的調用寫入python代碼:
import os
command = 'adb shell ime list -s'
os.system(command)
以上代碼就可以在python中使用adb命令。
參考文檔:https://www.jianshu.com/p/28937a73316e
(有些輸入法依舊不能使用鍵盤press_keycode功能,測試搜狗輸入法可以使用;但是有的輸入法不能直接用send_keys上傳文字,測試搜狗輸入法不能直接上傳文字,這時候,又需要用上面那個方法調出appium自帶的輸入法輸入中文后,再換成搜狗,然后再點擊鍵盤……復雜了我的appium!)