1. 程式人生 > >[Android測試] Appium的一些坑問題錯誤解決 與 技巧集錦

[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

C:\Users\LITP>

執行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");