APPIUM+Java+Android Studio 環(huán)境搭建及示例總結(jié)

一、安裝Android Studio環(huán)境

請參考其他文章安裝

二、安裝手機模擬器

請參考其他文章安裝

三、安裝appium desktop版本

appium desktop版本下載地址:https://github.com/appium/appium-desktop/releases/

四、Android Studio中創(chuàng)建工程

通過“new project”先創(chuàng)建一個工程,然后在project中通過"new module"創(chuàng)建一個模塊用例實現(xiàn)測試用例。具體請參考其他文章。

五、selenium 庫和appium 庫導(dǎo)入AS

有兩種導(dǎo)入方式,一種是下載jar包后手動導(dǎo)入,還有一種是在線導(dǎo)入;

第一種,手動下載導(dǎo)入:

selenium 下載地址:http://selenium-release.storage.googleapis.com/index.html
appium 下載地址:https://search.maven.org/search?q=g:io.appium%20AND%20a:java-client

image

?也可以從appium網(wǎng)站進入下載鏈接: http://appium.io/downloads.html

還有個工具jar包 commons-lang3-3.1.jar 也會用到。

將下載的jar包直接拷入前面創(chuàng)建的new module的libs目錄下就可以了:

image

確保module的build.gradle中有如下聲明,就會自動找到這兩個jar包:

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
}

第二種在線下載導(dǎo)入:

通過如下菜單選中demoTest模塊,點擊加號中的“Library Dependency”:

image

然后如下菜單中搜索需要的庫,可以通過通配符匹配,注意查詢的格式如下標紅處,比如查詢selenium庫(org.seleniumhq.selenium:selenium* )如下:

image

類似輸入"io.appium:java-client*"可查找appium的對應(yīng)庫,完成后如下:

image

另外,測試用例還需要 junit:junit:4.12 這個庫相同方法在線導(dǎo)入一個,版本號盡量選新點的。

導(dǎo)入成功后會在demoTest的build.gradle中生成如下內(nèi)容,實際上手動在這里寫入如下內(nèi)容效果也是一樣的:

dependencies {
    implementation 'junit:junit:4.12'
    implementation 'org.seleniumhq.selenium:selenium-server-standalone:2.53.0'
    implementation 'io.appium:java-client:7.3.0'
}

同時,工程的"External libraries"中會增加一堆jar包。

不過,在線導(dǎo)入的 org.seleniumhq.selenium:selenium-server-standalone:2.53.0 這個庫版本太老了不知道為什么搜不到最新的庫,我后面的測試代碼用的的函數(shù)里面沒有,所以,除了junit:junit:4.12 這個在線導(dǎo)入,其他兩個我還是選擇了第一種本地導(dǎo)入的方式。

六、用例實現(xiàn)

用例源碼如下:

package com.example.demotest;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;

public class MyClass {
    private AndroidDriver<AndroidElement> driver;

    @Before
    public void init() {

        File classpathRoot = new File(System.getProperty("user.dir"));
        // 獲取apps文件
        File appDir = new File(classpathRoot, "/src/main/java/apps/");
        // 獲取apk文件
        File app = new File(appDir, "ContactManager.apk");

        DesiredCapabilities capabilities = new DesiredCapabilities();
        // 設(shè)備名,這里是模擬器的設(shè)備名
        capabilities.setCapability("deviceName", "127.0.0.1:12");
        // 系統(tǒng)平臺版本
        capabilities.setCapability("platformVersion", "7.1.2");

        capabilities.setCapability("app", app.getAbsolutePath());

        capabilities.setCapability("appPackage", "com.example.android.contactmanager");
        // app的入口啟動activity
        capabilities.setCapability("appActivity", ".ContactManager");
        // 連接appium啟動相應(yīng)app
        try {
            driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        System.out.println("App is launched!");//
    }

    @Test
    public void start() throws InterruptedException {
        Thread.sleep(100);
        // 找到相應(yīng)元素
        WebElement el = driver.findElement(By.xpath(".//*[@text='Add Contact']"));
        el.click();// 模擬點擊
        // 找到eidit控件
        List<AndroidElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");
        textFieldsList.get(0).sendKeys("New Contact Name");

        Thread.sleep(100);
        WebElement spin = driver.findElement(By.id("com.example.android.contactmanager:id/contactPhoneTypeSpinner"));
        spin.click();
        Thread.sleep(100);
        WebElement t = driver.findElement(By.xpath(".//*[@resource-id='android:id/text1']"));
        t.click();
        Thread.sleep(100);

        textFieldsList.get(2).sendKeys("Some@example.com");
        Thread.sleep(500);
        WebElement spin2 = driver.findElement(By.id("com.example.android.contactmanager:id/contactEmailTypeSpinner"));
        spin2.click();
        Thread.sleep(500);
        WebElement tt = driver.findElement(By.xpath(".//*[@text='\u5176\u4ED6']"));
        System.out.println();
        tt.click();
        Thread.sleep(100);
        System.out.println("App is done!");
    }

    @After
    public void end() {

        driver.quit();
    }
}

七、編譯及運行

典型錯誤1:

build project時發(fā)現(xiàn)不會編這個demoTest模塊,需要將此模塊和app做如下依賴:

image

進入后可以選擇 demoTest,這樣在build project的時候就同時會去編譯demoTest這個依賴模塊了。

但是發(fā)現(xiàn),編譯后會報類似如下錯誤:

Duplicate class org.openqa.selenium.SearchContext found in modules java-client-7.3.0.jar (java-client-7.3.0.jar) and selenium-server-standalone-3.141.59.jar (selenium-server-standalone-3.141.59.jar)

image

這個主要是導(dǎo)入的各種jar包(包括app和demoTest依賴的各種包)有類重復(fù)定義,折騰很久不知怎么去掉,后來發(fā)現(xiàn)如果只是跑測試用例不需要build整個project,只要Make Module 這個demoTest就可以了:

image

典型錯誤2:

運行中報如下錯誤:

org.openqa.selenium.InvalidSelectorException: Locator Strategy 'name' is not supported for this session
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/invalid_selector_exception.html

是由于新版的appium默認不支持用name來查詢控件元素,可以考慮改成其他方式查詢,查看appium配置默認可以支持如下幾種:

path:  Appium\resources\app\node_modules\appium\node_modules\appium-android-driver\build\lib\driver.js

class AndroidDriver extends _appiumBaseDriver.BaseDriver { 
...
    this.locatorStrategies = ['xpath', 'id', 'class name', 'accessibility id', '-android uiautomator'];
    this.desiredCapConstraints = _desiredCaps.default;
...
  }

比如,如下代碼:

WebElement el = driver.findElement(By.name("Add Contact"));
el.click();// 模擬點擊

可以改成:

WebElement el = driver.findElement(By.xpath(".//*[@text='Add Contact']"));
el.click();// 模擬點擊

或者:

List<AndroidElement> el = driver.findElementsById("com.example.android.contactmanager:id/addContactButton");
el.get(0).click();// 模擬點擊

典型錯誤3:

運行中查找中文字符時會報查找不到,需要將中文轉(zhuǎn)換成unicode碼,例如:

WebElement tt = driver.findElement(By.xpath(".//*[@text='其他']"));

改成如下,注意unicode碼的表示方法:

WebElement tt = driver.findElement(By.xpath(".//*[@text='\u5176\u4ED6']"));

典型錯誤4:
報錯“錯誤: 編碼GBK的不可映射字符”,如下:


image.png

在項目下的build.gradle中添加以下代碼即可解決:

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}

此方法同時可以解決典型錯誤3中的問題。

最后,通過run test就可以跑這個用例了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容