代码改变世界

3、第一个Appium测试

2018-04-10 18:43  软件测试汪  阅读(239)  评论(0编辑  收藏  举报

运行脚本前环境准备:

1、IDE,推荐使用IJ

2、安装jdk环境,推荐>1.8

3、准备一台真机或者模拟器

4、SDK

5、maven环境

 

项目目录:

CalculatorTest.java文件代码:
package example;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
import java.net.MalformedURLException;
import java.net.URL;
public class CalculatorTest {
    public static AndroidDriver driver;
    public static void main(String[] args) throws MalformedURLException,
            InterruptedException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "msm8953_64");
        capabilities.setCapability("automationName", "Appium");
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("platformVersion", "6.0");
        capabilities.setCapability("appPackage", "com.android.calculator2");
        capabilities.setCapability("appActivity", ".Calculator");
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),
                capabilities);
        driver.findElementByAndroidUIAutomator("text(\"1\")").click();
        driver.findElementByAndroidUIAutomator("text(\"+\")")  .click();
        driver.findElementByAndroidUIAutomator("text(\"6\")").click();
        driver.findElementByAndroidUIAutomator("text(\"=\")").click();
        Thread.sleep(2000);
        String result = driver.findElement(By.id("com.android.calculator2:id/result"))
                .getText();
        System.out.println(result);
        driver.quit();
    }
}

 

pom.xml文件(配置java-client):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test1.cn</groupId>
    <artifactId>testC</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>3.2.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>

 

CalculatorTest.java代码较为简单,启动计算机,计算1+6,然后打印结果,pom.xml主要 配置java-client  下面我们对CalculatorTest.java代码进行详细分析

deviceName:启动哪种设备,是真机还是模拟器? iPhone SimulatoriPad SimulatoriPhone Retina 4-inch
Android EmulatorGalaxy S4...
automationName: 使用哪种自动化引擎。 appium(默认) 还是 Selendroid
platformName: 使用哪种移动平台。 iOS, Android, orFirefoxOS
platformVersion: 指定平台的系统版本。 例如指的 Android 平台, 版本为 5.1
appActivity: 待测试的 app Activity 名字。 比如 MainActivity.Settings。 注意, 原生 App 的话要在activity 前加个"."
appPackage: 待测试的 app Java package。 比如 com.example.android.myAppcom.android.settings

元素定位:

driver.findElementByAndroidUIAutomator("text(\"1\")").click();