Appium自动化测试(5)—— java-client安装与测试

前置条件

使用 Java语言编写 appium自动化测试脚本,需要安装:

  • Java环境
  • IntelliJ IDEA / Eclipse
  • Maven

Maven 安装Java-client

启动IntelliJ IDEA,创建Maven项目,然后在pom.xml添加Java-client配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
<!--https://github.com/appium/java-client-->
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>6.0.0-BETA4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>24.1-jre</version>
</dependency>

运行第一个Appium测试

启动Android模拟器或连接Android真机

本文以连接红米Note 4X为例,测试小米计算器

启动 Appium

  • 若通过 Node.js安装 Appium
1
> appium
  • 若安装 Appium-Desktop

点击 Start Server v1.7.2 按钮启动服务

  • 若安装 Appium Server

点击右上角三角按钮,注意Appium的启动日志

Appium在启动时默认占用本机的4723端口,即:127.0.0.1:4723

编写第一个Appium测试程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package me.yezhou.appium;

import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;

public class CalculatorTest {

public static void main(String[] args) throws MalformedURLException, InterruptedException {

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Redmi Note 4X");
capabilities.setCapability("automationName", "Appium");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "7.0");
capabilities.setCapability("appPackage", "com.miui.calculator");
capabilities.setCapability("appActivity", ".cal.CalculatorActivity");

AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

driver.findElement(By.name("1")).click();
driver.findElement(By.name("5")).click();
//driver.findElement(By.name("8")).click();
driver.findElement(By.xpath("//android.widget.Button[contains(@text, 8)]")).click();
//driver.findElement(By.name("delete")).click();
driver.findElement(By.id("btn_plus_s")).click();
driver.findElement(By.name("8")).click();
//driver.findElement(By.id("btn_equal_s")).click();
driver.findElement(By.id("btn_equal_s")).click();
Thread.sleep(2000);

String result = driver.findElement(By.id("com.miui.calculator:id/btn_0_s")).getText();
System.out.println("result: " + result);

driver.quit();
}

}

通过 IDEA 运行程序。将会看到 红米Note 4X 的小米计算器运行界面如下:

![Appium测试 - 小米计算器](http://www.yezhou.me/AppBlog/images/测试/Appium测试 - 小米计算器.png)

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :