appium根据坐标进行操作-以阴阳师辅助脚本为例

package com.fallhat.appium.service;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.MalformedURLException;
import java.net.URL;

@Component
public class RunService {

    @Value("${appium.deviceName}")
    private String deviceName;

    @Value("${appium.platformVersion}")
    private String platformVersion;

    @Value("${appium.platformName}")
    private String platformName;

    @Value("${appium.appPackage}")
    private String appPackage;

    @Value("${appium.appActivity}")
    private String appActivity;

    @Value("${appium.url}")
    private String url;

    public void scroll() {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        //capabilities.setCapability(MobileCapabilityType.APP, "Path to your apk file");
        //capabilities.setCapability("deviceName", "cc77e79e");//deviceID
        capabilities.setCapability("deviceName", deviceName);//deviceID
        //capabilities.setCapability("automationName", "Appium");
        capabilities.setCapability("platformName", platformName);//操作系统
        //capabilities.setCapability("platformVersion", "8.0.0");//系统版本
        capabilities.setCapability("platformVersion", platformVersion);//系统版本
        //capabilities.setCapability("appPackage", "cn.pear.browser");//应用包名
        //com.ss.android.ugc.aweme
        capabilities.setCapability("appPackage", appPackage);//应用包名/data/app/
        //capabilities.setCapability("appActivity", "cn.pear.browser.activities.WelcomeActivity");//启动页面
        capabilities.setCapability("appActivity", appActivity);//启动页面
        //com.ss.android.ugc.aweme/
        capabilities.setCapability("automationName", "UiAutomator2");
        capabilities.setCapability("noReset", true);
        capabilities.setCapability("autoGrantPermissions", "true");
        capabilities.setCapability("uiautomator2ServerLaunchTimeout", "60000");

        AppiumDriver driver = null;
        try {
            driver = new AppiumDriver<>(
                    new URL(url), //if it needs to use locally started server
                    //then the target_ip is 127.0.0.1 or 0.0.0.0
                    //the default port is 4723
                    capabilities);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        TouchAction touchAction = new TouchAction(driver);

        //结束开场动画
        touchAction.press(PointOption.point(774, 347)).perform();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        touchAction.press(PointOption.point(774, 347)).perform();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        //选择不升级
        MobileElement element11 = driver.findElementById("android:id/button1");//
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (null != element11) {
            element11.click();
        }
        try {
            Thread.sleep(40000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //去掉游戏公告
        touchAction.tap(PointOption.point(1295, 163)).perform();
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //点击进入游戏
        touchAction.tap(PointOption.point(734, 582)).perform();
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        AppiumUtils.takeScreenShot(driver,"点击进入游戏之后20秒的画面");

        //点开卷轴
        touchAction.tap(PointOption.point(1322, 576)).perform();
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < 50; i++) {
            AppiumUtils.jiaotu(driver);
        }
        try {
            Thread.sleep(1000 * 10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        driver.quit();
    }
}

package com.fallhat.appium.service;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.offset.PointOption;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AppiumUtils {
    public static void jiaotu(AppiumDriver driver) {
        TouchAction touchAction = new TouchAction(driver);
        //点开组队
        touchAction.tap(PointOption.point(465, 592)).perform();
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("点开组队失败");
        }

        //滑动组队菜单
        touchAction.longPress(PointOption.point(357, 608)).moveTo(PointOption.point(363, 137)).release();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("组队滑动失败");
        }

        //点击妖气封印
        touchAction.tap(PointOption.point(369, 191)).perform();
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("点开妖气封印失败");
        }

        //点击椒图
        touchAction.tap(PointOption.point(592, 344)).perform();
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("点开椒图失败");
        }

        //点击自动匹配
        touchAction.tap(PointOption.point(879, 605)).perform();
        try {
            Thread.sleep(1000 * 30);
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("点开自动匹配失败");
        }
        AppiumUtils.takeScreenShot(driver, "点击自动匹配之后30秒的画面");
        //点击准备
        for (int i = 0; i < 30; i++) {
            touchAction.tap(PointOption.point(1330, 530)).perform();
            try {
                Thread.sleep(1000 * 5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        try {
            Thread.sleep(1000 * 8);
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("点击结束战斗失败");
        }
    }

    public static void takeScreenShot(AppiumDriver driver, String title) {
        File screenShotFile = driver.getScreenshotAs(OutputType.FILE);
        try {
            String fieName = "D:\\autoScreenCapture\\" + "-" + getCurrentDateTime() + title + ".png";
            FileUtils.copyFile(screenShotFile, new File(fieName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getCurrentDateTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd=HHmmss");//设置日期格式
        return df.format(new Date());
    }
}

https://blog.csdn.net/ouyanggengcheng/article/details/89184266

你可能感兴趣的:(appium)