[Android測試] Appium的一些坑問題錯誤解決 與 技巧集錦


轉:https://blog.csdn.net/niubitianping/article/details/52624417

1. error: Failed to start an Appium session, err was: Error: Requested a new session but one was in progress

這里寫圖片描述
之前的會話沒有關閉,然后你又運行了測試實例,也沒有設置覆蓋.
解決:
1. 重新停止appium服務,開啟Appium服務
2. 在Genarel Setting那里設置覆蓋Session,重啟Appium

測試結束在AfterClass加driver.quit()

2. error: Failed to start an Appium session, err was: Error: Command failed: C:\Windows\system32\cmd.exe /s /c “D:\android-sdk-windows\platform-tools\adb.exe -s adb server version (32) doesn’t match this client (36); killing…

wait-for-device”
error: could not install smartsocket listener: cannot bind to 127.0.0.1:5037:

這里寫圖片描述

沒有鏈接上手機或者模擬器,請確認已經連接成功,重新鏈接

3. error: Android devices must be of API level 17 or higher. Please change your device to Selendroid or upgrade Android on your device.

這里寫圖片描述

手機系統低於4.2,appium不支持4.2.2以下的系統,請換一個手機或者模擬器來測試。

4. Error: Permission to start activity denied.

這里寫圖片描述
**activity在清單文件里面沒添加android:exported=”true”的話,你不能直接打開對應的activity,需要從啟動頁activity打開。
exported屬性就是設置是否允許activity被其它程序調用**

5. error: Failed to start an Appium session, err was: Error: Activity used to start app doesn’t exist or cannot ve launched! Make usre it exists and is launchable activity

這里寫圖片描述

要打開的activity不存在,activity路徑錯誤,改為完整正確的activity路徑

6. error: Failed to start an Appium session, err was: Error: ‘java - version’ failed. Error: Command failed: C:\Windows\system32\cmd.exe /s /c “java -version”

這里寫圖片描述
Java版本錯誤,請安裝最新的版本

7.> info: [debug] Error: Command failed: C:\Windows\system32\cmd.exe /s /c “D:\android-sdk-windows\platform-tools\adb.exe -s 8806a0b0 shell “echo ‘ready‘“error: unknown host service

這里寫圖片描述

鏈接手機失敗,重新鏈接手機即可,我就是重新拔插了一下usb

Error: Command failed: C:\Windows\system32\cmd.exe /s /c “D:\android-sdk-windows\platform-tools\adb.exe -s 8806a0b0 shell “echo ‘ping’”“

error: unknown host service

adb被突然占用導致,例如你在運行用例的時候運行了模擬器。

7. UIAutomatorViewer提示: Unable to connect to adb. Check if adb is installed correctly

解決,sdk升級到了25產生的問題。

解決方法:

  1. 將adb.exe 復制一份到uiautomatorviewer.bat 目錄下
  2. 修改uiautomatorviewer.bat文件最后一行(改binddir=%prog_dir%為自己的platform-tools本地路徑)
    image

技巧

1. 每次測試都重新安裝app

為capabilities色設置noReset為true
capabilities.setCapability(“noReset”, true);

2. 中文亂碼

這都是編碼問題:

1.方法1:

Android Studio修改文件編碼的方法,最底部的UTf-8,點擊選GBK就可以了,reload文件。(ps: 先把文件內容全選復制一下再轉換編碼,再粘貼,不然文件內容就變亂碼了)

這里寫圖片描述

2.方法2:

用的是原來的UTF-8編碼,然后在測試module的build.gradle里面添加三行代碼

tasks.withType(JavaCompile){ options.encoding = 'UTF-8' }
  • 這里寫圖片描述

3. 清除編輯框EditText內容

這個問題好像是看手機系統的,我之前的手機就會出現sendKeys的時候沒有全選去掉本來的內容,現在都會自動全選覆蓋了,這個也不算問題了。

    /** * 逐字刪除編輯框中的文字 * @param element 文本框架控件 */ public void clearText(AndroidElement element){ String className = element.getClass().getSimpleName(); if (className.equals("EditText")){ String text = element.getText(); //跳到最后 driver.pressKeyCode(KEYCODE_MOVE_END); for (int i = 0; i < text.length(); i ++){ //循環后退刪除 driver.pressKeyCode(BACKSPACE); } }else { print("不是文本輸入框架,無法刪除文字"); } }

 

4. 點擊輸入法鍵盤的回車搜索

方法1: 切換輸入法

利用adb命令先切換為自己的輸入法,按了搜索再切換為appium的輸入法

查看當前手機的輸入法

cmd執行下面的的代碼

adb shell ime list -s

可以看到類似下面的結果,

C:\Users\LITP>adb shell ime list -s com.baidu.input_mi/.ImeService com.sohu.inputmethod.sogou.xiaomi/.SogouIME io.appium.android.ime/.UnicodeIME 

執行adb命令

先寫好一個執行cmd的方法

    /** * 執行adb命令 * @param s 要執行的命令 */ private void excuteAdbShell(String s) { Runtime runtime=Runtime.getRuntime(); try{ runtime.exec(s); }catch(Exception e){ print("執行命令:"+s+"出錯"); } }

在需要搜索的時候執行下面的代碼,切換的輸入法用自己查看列表的輸入法內容,我這里是搜狗輸入法

        //使用adb shell 切換輸入法-更改為搜狗拼音,這個看你本來用的什么輸入法 excuteAdbShell("adb shell ime set com.sohu.inputmethod.sogou.xiaomi/.SogouIME"); //再次點擊輸入框,調取鍵盤,軟鍵盤被成功調出 clickView(page.getSearch()); //點擊右下角的搜索,即ENTER鍵 pressKeyCode(AndroidKeyCode.ENTER); //再次切回 輸入法鍵盤為Appium unicodeKeyboard excuteAdbShell("adb shell ime set io.appium.android.ime/.UnicodeIME");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM