Appium之开发计算器自动化测试脚本Demo

时间:2024-01-01 11:14:40

1、依赖包

        <!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>6.1.0</version>
</dependency>

2、查看元素信息

打开安卓SDK目录下的uiautomatorviewer查看元素,点击第二个按钮(即“Device SrceenShot”按钮)同步手机屏幕元素到该界面

本机安装在:C:\software\AndroidSDK\sdk\tools\bin\uiautomatorviewer.bat

点击元素后右边显示该元素的信息,如下图:

Appium之开发计算器自动化测试脚本Demo

3、Demo源码

 package MyAppium;

 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; /**
* @Auther:GongXingRui
* @Date:2018/11/1
* @Description: 测试计算器操作
**/
public class CalculatorTest {
public static void main(String[] args) throws MalformedURLException, InterruptedException { DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "Android S4");
capabilities.setCapability("automationName", "Appium");
capabilities.setCapability("platformName", "Android");
// 安卓版本号,保持默认即可
// capabilities.setCapability("platformVersion", "7.0");
//启动包名,使用SDK的uiautomatorviewer查看
capabilities.setCapability("appPackage", "com.sec.android.app.popupcalculator");
capabilities.setCapability("appActivity", ".Calculator"); AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); driver.findElement(By.id("com.sec.android.app.popupcalculator:id/bt_01")).click();
driver.findElement(By.id("com.sec.android.app.popupcalculator:id/bt_add")).click();
driver.findElement(By.id("com.sec.android.app.popupcalculator:id/bt_02")).click();
driver.findElement(By.id("com.sec.android.app.popupcalculator:id/bt_equal")).click();
Thread.sleep(5000); String result = driver.findElement(By.id("com.sec.android.app.popupcalculator:id/txtCalc")).getText();
System.out.println("结果:" + result); driver.quit();
}
}