APPIUM+Java+Android Studio 环境搭建及示例总结

一、安装Android Studio环境

请参考其他文章安装

二、安装手机模拟器

请参考其他文章安装

三、安装appium desktop版本

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

四、Android Studio中创建工程

通过“new project”先创建一个工程,然后在project中通过"new module"创建一个模块用例实现测试用例。具体请参考其他文章。

五、selenium 库和appium 库导入AS

有两种导入方式,一种是下载jar包后手动导入,还有一种是在线导入;

第一种,手动下载导入:

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网站进入下载链接: http://appium.io/downloads.html

还有个工具jar包 commons-lang3-3.1.jar 也会用到。

将下载的jar包直接拷入前面创建的new module的libs目录下就可以了:

image

确保module的build.gradle中有如下声明,就会自动找到这两个jar包:

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

第二种在线下载导入:

通过如下菜单选中demoTest模块,点击加号中的“Library Dependency”:

image

然后如下菜单中搜索需要的库,可以通过通配符匹配,注意查询的格式如下标红处,比如查询selenium库(org.seleniumhq.selenium:selenium* )如下:

image

类似输入"io.appium:java-client*"可查找appium的对应库,完成后如下:

image

另外,测试用例还需要 junit:junit:4.12 这个库相同方法在线导入一个,版本号尽量选新点的。

导入成功后会在demoTest的build.gradle中生成如下内容,实际上手动在这里写入如下内容效果也是一样的:

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包。

不过,在线导入的 org.seleniumhq.selenium:selenium-server-standalone:2.53.0 这个库版本太老了不知道为什么搜不到最新的库,我后面的测试代码用的的函数里面没有,所以,除了junit:junit:4.12 这个在线导入,其他两个我还是选择了第一种本地导入的方式。

六、用例实现

用例源码如下:

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();
        // 设备名,这里是模拟器的设备名
        capabilities.setCapability("deviceName", "127.0.0.1:12");
        // 系统平台版本
        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启动相应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);
        // 找到相应元素
        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时发现不会编这个demoTest模块,需要将此模块和app做如下依赖:

image

进入后可以选择 demoTest,这样在build project的时候就同时会去编译demoTest这个依赖模块了。

但是发现,编译后会报类似如下错误:

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

这个主要是导入的各种jar包(包括app和demoTest依赖的各种包)有类重复定义,折腾很久不知怎么去掉,后来发现如果只是跑测试用例不需要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:

运行中查找中文字符时会报查找不到,需要将中文转换成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就可以跑这个用例了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容