java + appium + android emulator app to achieve automation Demo

There are more online tutorials, I wrote again, under the impression deepened.

Environment to build

It is said that many people have been cumbersome to build an environment to scared.

Yes, indeed, complicated.

node.js

Site

cmd input node -v, figure illustrates the emergence of success.

JDK

Site

Baidu, "Configure Java"

cmd enter java -version, appear under the figure illustrates successful.

Android SDK

Site

Baidu, "Android SDK Configuration Environment Variables"

cmd enter adb, figure illustrates the emergence of success.

appium Desktop

Website 1 / URL 2

Maven

Site

Baidu, "maven environment configuration"

cmd enter mvn -version, figure illustrates the emergence of success.

IntelliJ IDEA

Site

Andrews simulator

Site

The next Android SDK directory \ platform-tools copy of adb.exe

Open Nox \ bin directory

Paste replace adb.exe

Open Simulator

cmd enter adb connect 127.0.0.1:62001

Then enter adb devices

Wealth Calculator

Site

After downloading, apk files can be stored in a directory.

The apk files directly drag Ruan Zhuo simulator.

So far, environmental set up is completed, a total of eight installed.

Implementation steps

Open IntelliJ

Create a maven project


Click ok, copy the following code into the pom.xm

  <dependencies>
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.2</version>
        </dependency>
    </dependencies>



Copy and paste the code below

import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;

import java.net.URL;

import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class AppDemo {
    private AppiumDriver driver;

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

        //将上面获取到的包名和Activity名设置为值
        cap.setCapability("appPackage", "com.youdao.calculator");
        cap.setCapability("appActivity", "com.youdao.calculator.activities.MainActivity");

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

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

    @Test
    public void plus() throws Exception {
        Thread.sleep(3000);
        int width = driver.manage().window().getSize().width;
        int height = driver.manage().window().getSize().height;
        int x0 = (int)(width * 0.8);  // 起始x坐标
        int x1 = (int)(height * 0.2);  // 终止x坐标
        int y = (int)(height * 0.5);  // y坐标
        for (int i=0; i<5; i++) {
            driver.swipe(x0, y, x1, y, 500);
            Thread.sleep(1000);
        }

        driver.findElementById("com.youdao.calculator:id/guide_button").click();
        for (int i=0; i<6; i++) {
            driver.findElementByXPath("//android.webkit.WebView[@text='Mathbot Editor']").click();
            Thread.sleep(1000);
        }

        String btn_xpath = "//*[@resource-id='com.youdao.calculator:id/view_pager_keyboard']/android.widget.GridView/android.widget.FrameLayout[%d]/android.widget.FrameLayout";
        driver.findElementByXPath(String.format(btn_xpath, 7)).click();
        driver.findElementByXPath(String.format(btn_xpath, 10)).click();
        driver.findElementByXPath(String.format(btn_xpath, 8)).click();
        Thread.sleep(3000);


    }

    @AfterClass
    public void tearDown() throws Exception {

        driver.quit();

    }
}

Right-run file

Then you can see the script ran

Open the Android emulator

One will see

Calculator app in their move!

Guess you like

Origin www.cnblogs.com/df888/p/10959503.html