appium 安卓真机学习记录(一)

前提:
1. 安装好appium server,配置好环境变量
2. 下载并配置好adb,有问题先请教百度,再请教安卓的同事

步骤:
3. 通过Usb接口将真机与PC端连接起来
4. 用adb devices命令来查看手机设备,复制下来这个设备号
5. 打开指定测试App的指定Activity,能找到类似下面的一行: adb shell dumpsys activity activities
realActivity=com.maihaoche.bentley.debug/com.maihaoche.bentley.activity.LauncherActivity
com.maihaoche.bentley.debug是appPackage
com.maihaoche.bentley.activity.LauncherActivity是appActivity
6. 找到adb里自带的uiautomatorviewer,作为参考:我的目录是/Users/user/Library/Android/sdk/tools/bin/
7. 启动uiautomatorviewer,手机点到你要抓取元素的那个界面,点击uiautomatorviewer界面上文件夹icon右边的那个按钮
8. 选中你要处理的元素,拷贝出右下角resource-id对应的值,之后靠这个来定位元素

代码如下:

package com.appiumDemo;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.net.URL;

/**
 * Created by douhua on 2018/3/1.
 * 注意:这个类仅针对于安卓机子
 */
public class loginMHC {
    private AppiumDriver appiumDriver;

    @BeforeClass
    public void setup() throws Exception {
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability(CapabilityType.BROWSER_NAME, "");
        cap.setCapability("platformName", "Android"); //指定测试平台
        cap.setCapability("deviceName", "17d6720d"); //指定测试机的ID,通过adb命令`adb devices`获取
        cap.setCapability("platformVersion", "6.0.1");

        //将上面获取到的包名和Activity名设置为值
        cap.setCapability("appPackage", "com.maihaoche.bentley.debug");
        cap.setCapability("appActivity", "com.maihaoche.bentley.activity.LauncherActivity");

        //A new session could not be created的解决方法
        //cap.setCapability("appWaitActivity","com.maihaoche.bentley.activity.LauncherActivity");
        //每次启动时覆盖session,否则第二次后运行会报错不能新建session
        cap.setCapability("sessionOverride", true);

        appiumDriver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
    }

    @Test
    public void plus(){
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //点击登录
        appiumDriver.findElementById("com.maihaoche.bentley.debug:id/txt_login").click();
        //获取手机号的输入框
        appiumDriver.findElementById("com.maihaoche.bentley.debug:id/et_mobile").sendKeys("18267909637");
        //获取密码框
        appiumDriver.findElementById("com.maihaoche.bentley.debug:id/et_pwd").sendKeys("qqqqqq");
        //获取登录按钮
        appiumDriver.findElementById("com.maihaoche.bentley.debug:id/btn_login").click();
    }

    @AfterClass
    public void tearDown() throws Exception {
        appiumDriver.quit();
    }

}

pom.xml


<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.0modelVersion>

    <groupId>com.mhc.testgroupId>
    <artifactId>appium-demoartifactId>
    <version>1.0-SNAPSHOTversion>

    <dependencies>
        <dependency>
            <groupId>org.testnggroupId>
            <artifactId>testngartifactId>
            <version>6.14.2version>
        dependency>
        <dependency>
            <groupId>io.appiumgroupId>
            <artifactId>java-clientartifactId>
            <version>RELEASEversion>
        dependency>
    dependencies>

project>

你可能感兴趣的:(自动化测试)