Appium自动化测试(5)—— java-client安装与测试
前置条件
使用 Java语言编写 appium自动化测试脚本,需要安装:
- Java环境
- IntelliJ IDEA / Eclipse
- Maven
Maven 安装Java-client
启动IntelliJ IDEA,创建Maven项目,然后在pom.xml添加Java-client配置:
<!--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
> appium
- 若安装 Appium-Desktop
点击 Start Server v1.7.2
按钮启动服务
- 若安装 Appium Server
点击右上角三角按钮,注意Appium的启动日志
Appium在启动时默认占用本机的4723端口,即:127.0.0.1:4723
编写第一个Appium测试程序
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)
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/28/appium-automation-testing-java-client-installation-testing/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
共有 0 条评论