Browse Source

test

master
kavil 1 year ago
parent
commit
4dc8ba2463
  1. 1
      .gitignore
  2. 5
      buildConfig/webpack.dev.js
  3. 5308
      package-lock.json
  4. 2
      package.json
  5. 248
      src/base/index.ts
  6. 27
      src/index.ts
  7. 21
      src/lib/utils.ts

1
.gitignore

@ -5,3 +5,4 @@ regex.md
index.dts
*.code-workspace
pnpm-lock.yaml
dist

5
buildConfig/webpack.dev.js

@ -5,9 +5,12 @@ const defaultConfig = require("./webpack.config");
module.exports = merge(defaultConfig, {
// 压缩代码
optimization: {
minimize: false
minimize: true
},
// 实时编译
watch: true,
// 指定入口文件
entry: "./src/index.ts",

5308
package-lock.json

File diff suppressed because it is too large

2
package.json

@ -13,6 +13,8 @@
"devDependencies": {
"@babel/core": "^7.18.0",
"@babel/plugin-proposal-object-rest-spread": "^7.18.9",
"@babel/plugin-proposal-private-methods": "^7.18.6",
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"@babel/plugin-transform-runtime": "^7.18.10",
"@babel/preset-env": "^7.18.10",
"@typescript-eslint/eslint-plugin": "^5.4.0",

248
src/base/base.ts → src/base/index.ts

@ -1,118 +1,130 @@
import { DPEvent } from './event';
type DPStarterEvents = {
over: () => DPTask;
exit: () => void;
};
export abstract class DPStarter extends DPEvent<DPStarterEvents> {
private _tasks: DPTask[] = [];
get tasks() {
return this._tasks;
}
init() {
console.log('init');
this.doInit();
}
abstract doInit(): void;
start() {
console.log('start');
this.on('over', (task: DPTask) => {
const index = this._tasks.indexOf(task);
if (index < this._tasks.length - 1) {
this._tasks[index + 1].start();
}
if (index === this._tasks.length - 1) {
this.emit('exit');
}
task.removeAllListeners();
this.removeTask(task);
});
}
addTask(task: DPTask) {
this._tasks.push(task);
}
removeTask(task: DPTask) {
const index = this._tasks.indexOf(task);
if (index >= 0) {
this._tasks.splice(index, 1);
}
}
clearTasks() {
this._tasks = [];
}
}
export enum TaskStatus {
Init = 'init',
Running = 'running',
Paused = 'paused',
Stopped = 'stopped',
Finished = 'finished'
}
export class DPTask extends DPEvent {
// 任务状态
private _status: TaskStatus = TaskStatus.Init;
private owner: DPStarter;
get status() {
return this._status;
}
set status(status: TaskStatus) {
this._status = status;
(status === TaskStatus.Finished || status === TaskStatus.Stopped) && this.owner.emit('over', this);
}
// 任务名称
private _name = '';
get name() {
return this._name;
}
set name(name: string) {
this._name = name;
}
constructor(owner: DPStarter) {
super();
this.owner = owner;
}
init() {
console.log('init');
this.status = TaskStatus.Init;
}
start() {
console.log('start');
this.status = TaskStatus.Running;
}
pause() {
console.log('pause');
this.status = TaskStatus.Paused;
}
stop() {
console.log('stop');
this.status = TaskStatus.Stopped;
}
restart() {
console.log('restart');
this.status = TaskStatus.Running;
}
}
import { clickForce } from '../lib/utils';
import { DPEvent } from './event';
type DPStarterEvents = {
over: () => DPTask;
exit: () => void;
};
export abstract class DPStarter extends DPEvent<DPStarterEvents> {
private _tasks: DPTask[] = [];
get tasks() {
return this._tasks;
}
init() {
console.log('init');
const texts = className('android.widget.TextView')
.find()
.map((item) => item.text());
console.log(texts);
if (texts.includes('启动应用')) {
console.log('启动应用');
clickForce(id('button1').findOne());
}
this.doInit();
}
abstract doInit(): void;
start() {
console.log('start');
this.init();
this.on('over', (task: DPTask) => {
const index = this._tasks.indexOf(task);
if (index < this._tasks.length - 1) {
this._tasks[index + 1].start();
}
if (index === this._tasks.length - 1) {
this.emit('exit');
}
task.removeAllListeners();
this.removeTask(task);
});
}
addTask(task: DPTask) {
this._tasks.push(task);
}
removeTask(task: DPTask) {
const index = this._tasks.indexOf(task);
if (index >= 0) {
this._tasks.splice(index, 1);
}
}
clearTasks() {
this._tasks = [];
}
}
export enum TaskStatus {
Init = 'init',
Running = 'running',
Paused = 'paused',
Stopped = 'stopped',
Finished = 'finished'
}
export class DPTask extends DPEvent {
// 任务状态
private _status: TaskStatus = TaskStatus.Init;
private owner: DPStarter;
get status() {
return this._status;
}
set status(status: TaskStatus) {
this._status = status;
(status === TaskStatus.Finished || status === TaskStatus.Stopped) && this.owner.emit('over', this);
}
// 任务名称
private _name = '';
get name() {
return this._name;
}
set name(name: string) {
this._name = name;
}
constructor(owner: DPStarter) {
super();
this.owner = owner;
}
init() {
console.log('init');
this.status = TaskStatus.Init;
}
start() {
console.log('start');
this.status = TaskStatus.Running;
}
pause() {
console.log('pause');
this.status = TaskStatus.Paused;
}
stop() {
console.log('stop');
this.status = TaskStatus.Stopped;
}
restart() {
console.log('restart');
this.status = TaskStatus.Running;
}
}

27
src/index.ts

@ -1,10 +1,33 @@
import { DPStarter, DPTask } from './base/base';
import { DPStarter, DPTask } from './base/index';
import { clickForce } from './lib/utils';
class Hongguo extends DPStarter {
doInit() {
launchApp('com.hongguo.app');
console.log('初始化!');
makeSure(app.launch('com.phoenix.read'), {
closeBtns: ['android:id/button1'],
landing: [id('di1').text('福利').visibleToUser().selected(false).findOne(1000)]
});
const widget = id('di1').text('福利').visibleToUser().selected(false).findOne(1000);
widget && clickForce(widget);
}
}
const hongguo = new Hongguo();
const task1 = new DPTask(hongguo);
hongguo.start();
function makeSure(result: any, options: any) {
if (!result) {
console.log('result is null');
return;
}
if (options.closeBtns) {
options.closeBtns.forEach((cid: string) => {
const btn = id(cid).findOne(1000);
btn && clickForce(btn);
});
}
}

21
src/lib/utils.ts

@ -0,0 +1,21 @@
// 找到控件区域点击
export function clickForce(ele: UiObject) {
if (!ele) {
console.log('控件不存在');
return;
}
const bound = ele.bounds();
const x = bound.centerX();
const y = bound.centerY();
click(x, y);
click(x - 1, y + 1);
click(x + 1, y - 1);
}
export function clickForceXY(x: number, y: number, width: number, height: number) {
width ? (x = x + width / 2) : (x = x + 2);
height ? (y = y + height / 2) : (y = y + 2);
click(x, y);
click(x - 1, y + 1);
click(x + 1, y - 1);
}
Loading…
Cancel
Save