赞
踩
(1)Android Studio:版本是Android Studio Flamingo | 2022.2.1 Patch 2
(2)数据线
(3)手机
(1)打开Android Studio;
(2)点击菜单栏File->New->New Project...,弹出如下对话框,按图中选择并点击Next;
(3)弹出如下对话框,点击Finish;
(4)在E:\Users\AndroidStudioProjects\Tickets\app\src\androidTest\java文件夹下创建TicketsTest.java类文件,TicketsTest为自定义名称;
(5)TicketsTest.java文件内容为:
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
-
- import androidx.test.uiautomator.UiDevice;
- import androidx.test.uiautomator.UiObject;
- import androidx.test.uiautomator.UiObjectNotFoundException;
- import androidx.test.uiautomator.UiSelector;
- import androidx.test.platform.app.InstrumentationRegistry;
- import androidx.test.uiautomator.By;
-
- import android.os.Handler;
- import android.os.Looper;
-
- import java.util.concurrent.CountDownLatch;
-
- import com.example.tickets.R;
-
- import org.junit.Test;
-
- public class TicketsTest {
-
- private Button ticketButton;
- private static final int DELAY_MS = 10; // 100毫秒
- private static final int NUM_CLICKS = 200; // 点击次数
-
- private Handler handler = new Handler(Looper.getMainLooper());
-
- private int clickCount = 0;
-
- private Runnable clickRunnable = new Runnable() {
- @Override
- public void run() {
-
- // 执行点击操作
- try {
- clickButton();
-
- } catch (UiObjectNotFoundException e) {
- throw new RuntimeException(e);
- }
-
- clickCount++;
-
- if (clickCount < NUM_CLICKS) {
- // 继续下一个定时任务
- handler.postDelayed(this, DELAY_MS);
- }
- }
- };
-
- @Test
- public void startAutoClick() {
- // 创建一个 CountDownLatch,设置为 1
- CountDownLatch latch = new CountDownLatch(1);
- handler.postDelayed(clickRunnable, DELAY_MS);
- // 等待定时任务执行
- try {
- latch.await();
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- latch.countDown(); // 定时任务执行完毕,释放锁
- }
- @Test
- public void stopAutoClick() {
- Log.i("结束", "stopAutoClick");
- handler.removeCallbacks(clickRunnable);
- }
- @Test
- public void clickButton() throws UiObjectNotFoundException {
- Log.i("次数", "clickButton: "+clickCount);
- // 在这里添加自动点击按钮的逻辑
- // 创建一个UiSelector来定位按钮元素
- UiSelector buttonSelector = new UiSelector().text("立即购买");
-
- // 在当前屏幕中查找按钮元素
- UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
- UiObject button = uiDevice.findObject(buttonSelector);
- // 如果找到按钮元素,则模拟点击操作
- if (button.exists() && button.isEnabled()) {
- button.click();
- // 进入购票页面,这部分内容可根据自己抢票的页面修改
- // 创建一个UiSelector来定位票档元素
- UiSelector levelSelector = new UiSelector().text("看台1280元");
- // 在当前屏幕中查找按钮元素
- UiObject levelButton = uiDevice.findObject(levelSelector);
- levelButton.click();
-
- // 创建一个UiSelector来提交订单
- UiSelector submitSelector = new UiSelector().text("确定");
- // 在当前屏幕中查找按钮元素
- UiObject submitButton = uiDevice.findObject(submitSelector);
- submitButton.click();
- } else {
- // 处理找不到按钮的情况
- if(clickCount>100){
- stopAutoClick();
- }
- }
- }
- }
(6)数据线连接手机和电脑,手机打开大麦网到抢票页面,点击运行测试程序
(7)Good Luck!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。