pp-robot-module 0.2.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -0
- package/dist/MR/Ai.d.ts +8 -0
- package/dist/MR/Ai.js +24 -0
- package/dist/MR/Clipboard.d.ts +20 -0
- package/dist/MR/Clipboard.js +60 -0
- package/dist/MR/Csv.d.ts +20 -0
- package/dist/MR/Csv.js +56 -0
- package/dist/MR/Date.d.ts +60 -0
- package/dist/MR/Date.js +94 -0
- package/dist/MR/DbClient.d.ts +97 -0
- package/dist/MR/DbClient.js +269 -0
- package/dist/MR/Dialog.d.ts +0 -0
- package/dist/MR/Dialog.js +40 -0
- package/dist/MR/Excel.d.ts +110 -0
- package/dist/MR/Excel.js +316 -0
- package/dist/MR/File.d.ts +162 -0
- package/dist/MR/File.js +399 -0
- package/dist/MR/Hash.d.ts +12 -0
- package/dist/MR/Hash.js +21 -0
- package/dist/MR/Html.d.ts +38 -0
- package/dist/MR/Html.js +163 -0
- package/dist/MR/Http.d.ts +33 -0
- package/dist/MR/Http.js +121 -0
- package/dist/MR/Imap.d.ts +15 -0
- package/dist/MR/Imap.js +52 -0
- package/dist/MR/Ini.d.ts +31 -0
- package/dist/MR/Ini.js +55 -0
- package/dist/MR/Keyboard.d.ts +59 -0
- package/dist/MR/Keyboard.js +515 -0
- package/dist/MR/Log.d.ts +7 -0
- package/dist/MR/Log.js +52 -0
- package/dist/MR/Mouse.d.ts +51 -0
- package/dist/MR/Mouse.js +152 -0
- package/dist/MR/Net.d.ts +17 -0
- package/dist/MR/Net.js +74 -0
- package/dist/MR/Pdf.d.ts +113 -0
- package/dist/MR/Pdf.js +197 -0
- package/dist/MR/Process.d.ts +46 -0
- package/dist/MR/Process.js +106 -0
- package/dist/MR/QRcode.d.ts +13 -0
- package/dist/MR/QRcode.js +37 -0
- package/dist/MR/RedisClient.d.ts +40 -0
- package/dist/MR/RedisClient.js +183 -0
- package/dist/MR/Regex.d.ts +16 -0
- package/dist/MR/Regex.js +20 -0
- package/dist/MR/Robot.d.ts +19 -0
- package/dist/MR/Robot.js +31 -0
- package/dist/MR/Screen.d.ts +20 -0
- package/dist/MR/Screen.js +79 -0
- package/dist/MR/Smtp.d.ts +21 -0
- package/dist/MR/Smtp.js +83 -0
- package/dist/MR/String.d.ts +104 -0
- package/dist/MR/String.js +166 -0
- package/dist/MR/System.d.ts +31 -0
- package/dist/MR/System.js +201 -0
- package/dist/MR/Validator.d.ts +30 -0
- package/dist/MR/Validator.js +104 -0
- package/dist/MR/WebBrowser.d.ts +210 -0
- package/dist/MR/WebBrowser.js +484 -0
- package/dist/MR/WebBrowser2.d.ts +0 -0
- package/dist/MR/WebBrowser2.js +504 -0
- package/dist/MR/WebElement.d.ts +123 -0
- package/dist/MR/WebElement.js +172 -0
- package/dist/MR/WebKeyboard.d.ts +21 -0
- package/dist/MR/WebKeyboard.js +48 -0
- package/dist/MR/WebMouse.d.ts +46 -0
- package/dist/MR/WebMouse.js +79 -0
- package/dist/MR/Window.d.ts +8 -0
- package/dist/MR/Window.js +34 -0
- package/dist/MR/Word.d.ts +56 -0
- package/dist/MR/Word.js +220 -0
- package/dist/MR/Zip.d.ts +25 -0
- package/dist/MR/Zip.js +89 -0
- package/dist/bin/inputting-method.exe +0 -0
- package/dist/bin/keyboard-password.exe +0 -0
- package/dist/bin/lock-window.exe +0 -0
- package/dist/bin/printer-pdf.exe +0 -0
- package/dist/dotenv.d.ts +0 -0
- package/dist/dotenv.js +2 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +63 -0
- package/dist/util.d.ts +17 -0
- package/dist/util.js +41 -0
- package/package.json +120 -0
@@ -0,0 +1,210 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
import { Browser, Page, Response, Frame, Locator, Cookie, Dialog, ConsoleMessage, BrowserContext } from 'playwright';
|
3
|
+
import Robot from './Robot';
|
4
|
+
import WebMouse from './WebMouse';
|
5
|
+
import WebKeyboard from './WebKeyboard';
|
6
|
+
import WebElement from './WebElement';
|
7
|
+
export declare namespace MR {
|
8
|
+
class WebBrowser {
|
9
|
+
private static webBrowser;
|
10
|
+
context: BrowserContext;
|
11
|
+
browser: Browser;
|
12
|
+
page: Page;
|
13
|
+
myframe: Frame;
|
14
|
+
Mouse: WebMouse;
|
15
|
+
Keyboard: WebKeyboard;
|
16
|
+
Element: WebElement;
|
17
|
+
private robotDetail;
|
18
|
+
/**
|
19
|
+
*
|
20
|
+
* @param {function(WebBot)} action 要执行的函数
|
21
|
+
* @param {*} robotDetail 要执行的数智人的对象
|
22
|
+
* @param {*} httpReqParams
|
23
|
+
* @param {} options
|
24
|
+
*/
|
25
|
+
launch(params?: {
|
26
|
+
robotDetail?: any;
|
27
|
+
_options: {
|
28
|
+
_browserName?: string;
|
29
|
+
_browserPath?: string;
|
30
|
+
_browserType?: string;
|
31
|
+
_remotePath?: string;
|
32
|
+
_headless?: boolean;
|
33
|
+
_slowMo?: number;
|
34
|
+
_devtools?: boolean;
|
35
|
+
};
|
36
|
+
}): Promise<void>;
|
37
|
+
prepareMouse(): Promise<void>;
|
38
|
+
prepareKeyBoard(): Promise<void>;
|
39
|
+
attachRobot(detail: Robot): Promise<void>;
|
40
|
+
private openCloud;
|
41
|
+
private openLocal;
|
42
|
+
private constructor();
|
43
|
+
static get instance(): WebBrowser;
|
44
|
+
/**
|
45
|
+
* 内部打印日志 并记录跟踪指令
|
46
|
+
* @param {} args
|
47
|
+
*/
|
48
|
+
private printer;
|
49
|
+
/**
|
50
|
+
* Navigate to the given URL.
|
51
|
+
* @param url
|
52
|
+
*/
|
53
|
+
goto(params: {
|
54
|
+
_url: string;
|
55
|
+
} | string): Promise<null | Response>;
|
56
|
+
maximize(): Promise<void>;
|
57
|
+
setViewportSize(params: {
|
58
|
+
_width: number;
|
59
|
+
_height: number;
|
60
|
+
}): Promise<void>;
|
61
|
+
cookies(): Promise<Array<Cookie>>;
|
62
|
+
addCookies(cookies: Array<{
|
63
|
+
name: string;
|
64
|
+
value: string;
|
65
|
+
domain: string;
|
66
|
+
path: string;
|
67
|
+
url?: string;
|
68
|
+
expires?: number;
|
69
|
+
httpOnly?: boolean;
|
70
|
+
secure?: boolean;
|
71
|
+
sameSite?: "Strict" | "Lax" | "None";
|
72
|
+
}>): Promise<void>;
|
73
|
+
/**
|
74
|
+
* Quit the current session.
|
75
|
+
* After calling quit, this instance will be invalidated and
|
76
|
+
* may no longer be used to issue commands against the WebBrowser.
|
77
|
+
*/
|
78
|
+
close(): Promise<void>;
|
79
|
+
viewportSize(): Promise<{
|
80
|
+
width: number;
|
81
|
+
height: number;
|
82
|
+
}>;
|
83
|
+
url(): Promise<string>;
|
84
|
+
title(): Promise<string>;
|
85
|
+
goForward(): Promise<null | Response>;
|
86
|
+
goBack(): Promise<null | Response>;
|
87
|
+
reload(): Promise<null | Response>;
|
88
|
+
/**
|
89
|
+
* Take a screenshot.
|
90
|
+
*/
|
91
|
+
screenshot(params?: {
|
92
|
+
_filepath?: string;
|
93
|
+
_filename?: string;
|
94
|
+
_fullpage?: boolean;
|
95
|
+
}): Promise<Buffer>;
|
96
|
+
/**
|
97
|
+
* save pdf
|
98
|
+
*/
|
99
|
+
pdf(params?: {
|
100
|
+
_filepath?: string;
|
101
|
+
_filename?: string;
|
102
|
+
}): Promise<Buffer>;
|
103
|
+
/**
|
104
|
+
* 打印输出所有Frame
|
105
|
+
*/
|
106
|
+
dumpFrame(): void;
|
107
|
+
private dumpFrameTree;
|
108
|
+
/**
|
109
|
+
* 获取全部Frame
|
110
|
+
*/
|
111
|
+
frames(): Frame[];
|
112
|
+
/**
|
113
|
+
* 获取主Frame
|
114
|
+
*/
|
115
|
+
mainFrame(): Frame;
|
116
|
+
/**
|
117
|
+
* 获取指定名称的Frame
|
118
|
+
* @param {*} name
|
119
|
+
* @returns Frame
|
120
|
+
* Frame常用方法 name(),title(),url(),content()
|
121
|
+
*/
|
122
|
+
frame(params: {
|
123
|
+
_name?: string;
|
124
|
+
_url?: string | RegExp | ((url: URL) => boolean);
|
125
|
+
}): Frame;
|
126
|
+
/**
|
127
|
+
* 普通方式元素定位
|
128
|
+
* @param url
|
129
|
+
*/
|
130
|
+
locator(params: {
|
131
|
+
_selector: string;
|
132
|
+
_isframe?: boolean;
|
133
|
+
}, options?: {
|
134
|
+
has?: Locator;
|
135
|
+
hasText?: string | RegExp;
|
136
|
+
}): Locator;
|
137
|
+
/**
|
138
|
+
* 角色方式元素定位
|
139
|
+
* @param url
|
140
|
+
*/
|
141
|
+
getByRole(role: "alert" | "alertdialog" | "application" | "article" | "banner" | "blockquote" | "button" | "caption" | "cell" | "checkbox" | "code" | "columnheader" | "combobox" | "complementary" | "contentinfo" | "definition" | "deletion" | "dialog" | "directory" | "document" | "emphasis" | "feed" | "figure" | "form" | "generic" | "grid" | "gridcell" | "group" | "heading" | "img" | "insertion" | "link" | "list" | "listbox" | "listitem" | "log" | "main" | "marquee" | "math" | "meter" | "menu" | "menubar" | "menuitem" | "menuitemcheckbox" | "menuitemradio" | "navigation" | "none" | "note" | "option" | "paragraph" | "presentation" | "progressbar" | "radio" | "radiogroup" | "region" | "row" | "rowgroup" | "rowheader" | "scrollbar" | "search" | "searchbox" | "separator" | "slider" | "spinbutton" | "status" | "strong" | "subscript" | "superscript" | "switch" | "tab" | "table" | "tablist" | "tabpanel" | "term" | "textbox" | "time" | "timer" | "toolbar" | "tooltip" | "tree" | "treegrid" | "treeitem", options?: {
|
142
|
+
checked?: boolean;
|
143
|
+
disabled?: boolean;
|
144
|
+
exact?: boolean;
|
145
|
+
expanded?: boolean;
|
146
|
+
includeHidden?: boolean;
|
147
|
+
level?: number;
|
148
|
+
name?: string | RegExp;
|
149
|
+
pressed?: boolean;
|
150
|
+
selected?: boolean;
|
151
|
+
}, _isframe?: boolean): Locator;
|
152
|
+
executeJs<R>(params: {
|
153
|
+
_code: string;
|
154
|
+
_isframe?: boolean;
|
155
|
+
}): Promise<R>;
|
156
|
+
download(params: {
|
157
|
+
_selector: string;
|
158
|
+
_isframe?: boolean;
|
159
|
+
_filename?: string;
|
160
|
+
}): Promise<void>;
|
161
|
+
/**
|
162
|
+
* 监听控制台消息
|
163
|
+
* const msgPromise = console()
|
164
|
+
* page.xxxx //操作元素
|
165
|
+
* await msgPromise
|
166
|
+
* @returns
|
167
|
+
*/
|
168
|
+
console(): Promise<ConsoleMessage>;
|
169
|
+
onDialog(listener: (dialog: Dialog) => void): void;
|
170
|
+
onConsole(listener: (consoleMessage: ConsoleMessage) => void): void;
|
171
|
+
dragAndDrop(source: string, target: string, options?: {
|
172
|
+
force?: boolean;
|
173
|
+
noWaitAfter?: boolean;
|
174
|
+
sourcePosition?: {
|
175
|
+
x: number;
|
176
|
+
y: number;
|
177
|
+
};
|
178
|
+
strict?: boolean;
|
179
|
+
targetPosition?: {
|
180
|
+
x: number;
|
181
|
+
y: number;
|
182
|
+
};
|
183
|
+
timeout?: number;
|
184
|
+
trial?: boolean;
|
185
|
+
}): Promise<void>;
|
186
|
+
addInitScript(params: {
|
187
|
+
_path: string;
|
188
|
+
}): Promise<void>;
|
189
|
+
content(): Promise<string>;
|
190
|
+
/**
|
191
|
+
* Enable file downloads in Chrome running in headless mode
|
192
|
+
*/
|
193
|
+
private enableDownloadInHeadlessChrome;
|
194
|
+
/**
|
195
|
+
* Wait for the download to complete.
|
196
|
+
* @returns The name of the downloaded file.
|
197
|
+
*/
|
198
|
+
waitForDownload(params: {
|
199
|
+
/** Function to start the download. */
|
200
|
+
downloader: () => Promise<void>;
|
201
|
+
/** Extension name of the file to download. */
|
202
|
+
extname?: string;
|
203
|
+
/** How long to wait, in milliseconds, for the download to complete. */
|
204
|
+
timeout?: number;
|
205
|
+
}): Promise<string>;
|
206
|
+
private toStr;
|
207
|
+
}
|
208
|
+
}
|
209
|
+
declare const _default: MR.WebBrowser;
|
210
|
+
export default _default;
|
@@ -0,0 +1,484 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
exports.MR = void 0;
|
13
|
+
const playwright_1 = require("playwright");
|
14
|
+
const util_1 = require("../util");
|
15
|
+
const Log_1 = require("./Log");
|
16
|
+
const File_1 = require("./File");
|
17
|
+
const Robot_1 = require("./Robot");
|
18
|
+
const WebMouse_1 = require("./WebMouse");
|
19
|
+
const WebKeyboard_1 = require("./WebKeyboard");
|
20
|
+
const WebElement_1 = require("./WebElement");
|
21
|
+
var MR;
|
22
|
+
(function (MR) {
|
23
|
+
class WebBrowser {
|
24
|
+
/**
|
25
|
+
*
|
26
|
+
* @param {function(WebBot)} action 要执行的函数
|
27
|
+
* @param {*} robotDetail 要执行的数智人的对象
|
28
|
+
* @param {*} httpReqParams
|
29
|
+
* @param {} options
|
30
|
+
*/
|
31
|
+
launch(params = {
|
32
|
+
_options: {
|
33
|
+
_browserName: 'chrome', _browserPath: '', _browserType: 'local',
|
34
|
+
_headless: false, _slowMo: 50, _devtools: false
|
35
|
+
}
|
36
|
+
}) {
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
38
|
+
this.attachRobot(params.robotDetail);
|
39
|
+
if (params._options._browserType === 'cloud') {
|
40
|
+
yield this.openCloud(params._options);
|
41
|
+
}
|
42
|
+
else {
|
43
|
+
yield this.openLocal(params._options);
|
44
|
+
}
|
45
|
+
});
|
46
|
+
}
|
47
|
+
prepareMouse() {
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
49
|
+
this.Mouse = new WebMouse_1.default(this.page.mouse, this.robotDetail);
|
50
|
+
});
|
51
|
+
}
|
52
|
+
prepareKeyBoard() {
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
54
|
+
this.Keyboard = new WebKeyboard_1.default(this.page.keyboard, this.robotDetail);
|
55
|
+
});
|
56
|
+
}
|
57
|
+
attachRobot(detail) {
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
59
|
+
if (!detail) {
|
60
|
+
this.robotDetail = Robot_1.default.create();
|
61
|
+
}
|
62
|
+
else {
|
63
|
+
this.robotDetail = detail;
|
64
|
+
}
|
65
|
+
});
|
66
|
+
}
|
67
|
+
openCloud(params) {
|
68
|
+
return __awaiter(this, void 0, void 0, function* () {
|
69
|
+
const argObj = { action: "WebBrowser.openCloud", note: "打开远程浏览器", param: this.toStr(params) };
|
70
|
+
this.printer(argObj);
|
71
|
+
this.browser = yield playwright_1.chromium.connectOverCDP('ws://' + params._remotePath);
|
72
|
+
this.page = yield this.browser.newPage();
|
73
|
+
});
|
74
|
+
}
|
75
|
+
//另一种写法 直接解耦
|
76
|
+
openLocal(params) {
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
78
|
+
const argObj = { action: "WebBrowser.openLocal", note: "打开本地浏览器", param: this.toStr(params) };
|
79
|
+
this.printer(argObj);
|
80
|
+
const opt = {
|
81
|
+
headless: params._headless,
|
82
|
+
defaultViewport: { width: 1920, height: 1080 },
|
83
|
+
executablePath: params._browserPath,
|
84
|
+
slowMo: params._slowMo,
|
85
|
+
devtools: params._devtools,
|
86
|
+
args: [
|
87
|
+
`--disable-notifications=true`,
|
88
|
+
`--no-sandbox`,
|
89
|
+
`--disable-setuid-sandbox`,
|
90
|
+
`--disable-infobars`,
|
91
|
+
`--window-position=0,0`,
|
92
|
+
`--ignore-certifcate-errors`,
|
93
|
+
`--ignore-certifcate-errors-spki-list`,
|
94
|
+
`--disable-web-security`,
|
95
|
+
`--disable-features=IsolateOrigins,site-per-process`,
|
96
|
+
`--lang=zh-CN,zn`,
|
97
|
+
`--start-maximized`
|
98
|
+
],
|
99
|
+
ignoreDefaultArgs: ['--enable-automation'],
|
100
|
+
//env: { LANGUAGE: "zh-CN" }
|
101
|
+
};
|
102
|
+
if (params._browserName.toLocaleLowerCase() === 'firefox') {
|
103
|
+
this.browser = yield playwright_1.firefox.launch(opt);
|
104
|
+
}
|
105
|
+
else if (params._browserName.toLocaleLowerCase() === 'webkit') {
|
106
|
+
this.browser = yield playwright_1.webkit.launch(opt);
|
107
|
+
}
|
108
|
+
else {
|
109
|
+
this.browser = yield playwright_1.chromium.launch(opt);
|
110
|
+
}
|
111
|
+
this.page = yield this.browser.newPage();
|
112
|
+
});
|
113
|
+
}
|
114
|
+
constructor() {
|
115
|
+
}
|
116
|
+
static get instance() {
|
117
|
+
if (!this.webBrowser) {
|
118
|
+
this.webBrowser = new WebBrowser();
|
119
|
+
}
|
120
|
+
return this.webBrowser;
|
121
|
+
}
|
122
|
+
/**
|
123
|
+
* 内部打印日志 并记录跟踪指令
|
124
|
+
* @param {} args
|
125
|
+
*/
|
126
|
+
printer(args) {
|
127
|
+
Log_1.default.debug(args);
|
128
|
+
this.robotDetail.addTrail(args);
|
129
|
+
}
|
130
|
+
/**
|
131
|
+
* Navigate to the given URL.
|
132
|
+
* @param url
|
133
|
+
*/
|
134
|
+
goto(params) {
|
135
|
+
const argObj = { action: "WebBrowser.goto", note: "跳转至URL", param: this.toStr(params) };
|
136
|
+
this.printer(argObj);
|
137
|
+
if (typeof params == "string") {
|
138
|
+
return this.page.goto(params);
|
139
|
+
}
|
140
|
+
else {
|
141
|
+
return this.page.goto(params._url);
|
142
|
+
}
|
143
|
+
}
|
144
|
+
maximize() {
|
145
|
+
return __awaiter(this, void 0, void 0, function* () {
|
146
|
+
const argObj = { action: "WebBrowser.maximize", note: "设置视窗最大化大小" };
|
147
|
+
this.printer(argObj);
|
148
|
+
const { width, height } = yield this.page.evaluate(() => ({
|
149
|
+
width: window.screen.width,
|
150
|
+
height: window.screen.height,
|
151
|
+
}));
|
152
|
+
console.log(width, height);
|
153
|
+
yield this.page.setViewportSize({
|
154
|
+
width: Math.floor(width),
|
155
|
+
height: Math.floor(height),
|
156
|
+
});
|
157
|
+
});
|
158
|
+
}
|
159
|
+
setViewportSize(params) {
|
160
|
+
return __awaiter(this, void 0, void 0, function* () {
|
161
|
+
const argObj = { action: "WebBrowser.setViewportSize", note: "设置视窗大小", param: this.toStr(params) };
|
162
|
+
this.printer(argObj);
|
163
|
+
yield this.page.setViewportSize({ width: params._width, height: params._height });
|
164
|
+
});
|
165
|
+
}
|
166
|
+
cookies() {
|
167
|
+
return __awaiter(this, void 0, void 0, function* () {
|
168
|
+
const argObj = { action: "WebBrowser.cookies", note: "获取cookies" };
|
169
|
+
this.printer(argObj);
|
170
|
+
return yield this.page.context().cookies();
|
171
|
+
});
|
172
|
+
}
|
173
|
+
addCookies(cookies) {
|
174
|
+
return __awaiter(this, void 0, void 0, function* () {
|
175
|
+
const argObj = { action: "WebBrowser.addCookies", note: "添加cookies", param: this.toStr(cookies) };
|
176
|
+
this.printer(argObj);
|
177
|
+
yield this.page.context().addCookies(cookies);
|
178
|
+
});
|
179
|
+
}
|
180
|
+
/**
|
181
|
+
* Quit the current session.
|
182
|
+
* After calling quit, this instance will be invalidated and
|
183
|
+
* may no longer be used to issue commands against the WebBrowser.
|
184
|
+
*/
|
185
|
+
close() {
|
186
|
+
const argObj = { action: "WebBrowser.close", note: "关闭浏览器" };
|
187
|
+
this.printer(argObj);
|
188
|
+
return this.browser.close();
|
189
|
+
}
|
190
|
+
viewportSize() {
|
191
|
+
return __awaiter(this, void 0, void 0, function* () {
|
192
|
+
const argObj = { action: "WebBrowser.viewportSize", note: "获取浏览器视窗长宽" };
|
193
|
+
this.printer(argObj);
|
194
|
+
return yield this.page.viewportSize();
|
195
|
+
});
|
196
|
+
}
|
197
|
+
url() {
|
198
|
+
return __awaiter(this, void 0, void 0, function* () {
|
199
|
+
const argObj = { action: "WebBrowser.url", note: "获取网址" };
|
200
|
+
this.printer(argObj);
|
201
|
+
return yield this.page.url();
|
202
|
+
});
|
203
|
+
}
|
204
|
+
title() {
|
205
|
+
return __awaiter(this, void 0, void 0, function* () {
|
206
|
+
const argObj = { action: "WebBrowser.title", note: "获取标题" };
|
207
|
+
this.printer(argObj);
|
208
|
+
return yield this.page.title();
|
209
|
+
});
|
210
|
+
}
|
211
|
+
goForward() {
|
212
|
+
return __awaiter(this, void 0, void 0, function* () {
|
213
|
+
const argObj = { action: "WebBrowser.goForward", note: "网页前进" };
|
214
|
+
this.printer(argObj);
|
215
|
+
return yield this.page.goForward();
|
216
|
+
});
|
217
|
+
}
|
218
|
+
goBack() {
|
219
|
+
return __awaiter(this, void 0, void 0, function* () {
|
220
|
+
const argObj = { action: "WebBrowser.goBack", note: "网页回退" };
|
221
|
+
this.printer(argObj);
|
222
|
+
return yield this.page.goBack();
|
223
|
+
});
|
224
|
+
}
|
225
|
+
reload() {
|
226
|
+
return __awaiter(this, void 0, void 0, function* () {
|
227
|
+
const argObj = { action: "WebBrowser.reload", note: "网页刷新" };
|
228
|
+
this.printer(argObj);
|
229
|
+
return yield this.page.reload();
|
230
|
+
});
|
231
|
+
}
|
232
|
+
/**
|
233
|
+
* Take a screenshot.
|
234
|
+
*/
|
235
|
+
screenshot(params = { _filepath: '.', _fullpage: true }) {
|
236
|
+
return __awaiter(this, void 0, void 0, function* () {
|
237
|
+
const argObj = { action: "WebBrowser.screenshot", note: "网页截图", param: this.toStr(params) };
|
238
|
+
this.printer(argObj);
|
239
|
+
let filepath = '.';
|
240
|
+
if (params._filepath)
|
241
|
+
filepath = params._filepath;
|
242
|
+
let filename = `${Math.round(new Date().getTime() / 1000)}.png`;
|
243
|
+
if (params._filename)
|
244
|
+
filename = params._filename;
|
245
|
+
return this.page.screenshot({ path: `${filepath}/${filename}`, fullPage: params._fullpage });
|
246
|
+
});
|
247
|
+
}
|
248
|
+
/**
|
249
|
+
* save pdf
|
250
|
+
*/
|
251
|
+
pdf(params = { _filepath: '.' }) {
|
252
|
+
return __awaiter(this, void 0, void 0, function* () {
|
253
|
+
const argObj = { action: "WebBrowser.pdf", note: "保存为PDF", param: this.toStr(params) };
|
254
|
+
this.printer(argObj);
|
255
|
+
let filepath = '.';
|
256
|
+
if (params._filepath)
|
257
|
+
filepath = params._filepath;
|
258
|
+
let filename = `${Math.round(new Date().getTime() / 1000)}.pdf`;
|
259
|
+
if (params._filename)
|
260
|
+
filename = params._filename;
|
261
|
+
yield this.page.emulateMedia({ media: 'screen' });
|
262
|
+
return yield this.page.pdf({ path: `${filepath}/${filename}` });
|
263
|
+
});
|
264
|
+
}
|
265
|
+
/**
|
266
|
+
* 打印输出所有Frame
|
267
|
+
*/
|
268
|
+
dumpFrame() {
|
269
|
+
const argObj = { action: "WebBrowser.dumpFrame", note: "打印输出所有Frame" };
|
270
|
+
this.printer(argObj);
|
271
|
+
this.dumpFrameTree(this.page.mainFrame(), '');
|
272
|
+
}
|
273
|
+
dumpFrameTree(frame, indent) {
|
274
|
+
console.log(indent + frame.url());
|
275
|
+
for (const child of frame.childFrames()) {
|
276
|
+
this.dumpFrameTree(child, indent + ' ');
|
277
|
+
}
|
278
|
+
}
|
279
|
+
/**
|
280
|
+
* 获取全部Frame
|
281
|
+
*/
|
282
|
+
frames() {
|
283
|
+
const argObj = { action: "WebBrowser.frames", note: "获取全部Frame" };
|
284
|
+
this.printer(argObj);
|
285
|
+
const frames = this.page.frames();
|
286
|
+
return frames;
|
287
|
+
}
|
288
|
+
/**
|
289
|
+
* 获取主Frame
|
290
|
+
*/
|
291
|
+
mainFrame() {
|
292
|
+
const argObj = { action: "WebBrowser.mainFrame", note: "获取主Frame" };
|
293
|
+
this.printer(argObj);
|
294
|
+
const frame = this.page.mainFrame();
|
295
|
+
this.myframe = frame;
|
296
|
+
return frame;
|
297
|
+
}
|
298
|
+
/**
|
299
|
+
* 获取指定名称的Frame
|
300
|
+
* @param {*} name
|
301
|
+
* @returns Frame
|
302
|
+
* Frame常用方法 name(),title(),url(),content()
|
303
|
+
*/
|
304
|
+
frame(params) {
|
305
|
+
const argObj = { action: "WebBrowser.frame", note: "获取指定名称的Frame", param: this.toStr(params) };
|
306
|
+
this.printer(argObj);
|
307
|
+
const frame = this.page.frame({ name: params._name, url: params._url });
|
308
|
+
this.myframe = frame;
|
309
|
+
return frame;
|
310
|
+
}
|
311
|
+
/**
|
312
|
+
* 普通方式元素定位
|
313
|
+
* @param url
|
314
|
+
*/
|
315
|
+
locator(params, options) {
|
316
|
+
const argObj = { action: "WebBrowser.locator", note: "普通方式元素定位", element: this.toStr(params._selector), param: this.toStr(options) };
|
317
|
+
this.printer(argObj);
|
318
|
+
let _element = this.page.locator(params._selector, options);
|
319
|
+
if (params._isframe && this.myframe) {
|
320
|
+
_element = this.myframe.locator(params._selector, options);
|
321
|
+
}
|
322
|
+
this.Element = new WebElement_1.default(_element, this.robotDetail);
|
323
|
+
return _element;
|
324
|
+
}
|
325
|
+
/**
|
326
|
+
* 角色方式元素定位
|
327
|
+
* @param url
|
328
|
+
*/
|
329
|
+
getByRole(role, options, _isframe) {
|
330
|
+
const argObj = { action: "WebBrowser.getByRole", note: "角色方式元素定位", element: this.toStr(role), param: this.toStr(options) };
|
331
|
+
this.printer(argObj);
|
332
|
+
let _element = this.page.getByRole(role, options);
|
333
|
+
if (_isframe && this.myframe) {
|
334
|
+
_element = this.myframe.getByRole(role, options);
|
335
|
+
}
|
336
|
+
this.Element = new WebElement_1.default(_element, this.robotDetail);
|
337
|
+
return _element;
|
338
|
+
}
|
339
|
+
executeJs(params) {
|
340
|
+
return __awaiter(this, void 0, void 0, function* () {
|
341
|
+
const argObj = { action: "WebBrowser.executeJs", note: "执行JS代码", param: this.toStr(params) };
|
342
|
+
this.printer(argObj);
|
343
|
+
if (params._isframe && this.myframe) {
|
344
|
+
return this.myframe.evaluate(params._code);
|
345
|
+
}
|
346
|
+
else {
|
347
|
+
return this.page.evaluate(params._code);
|
348
|
+
}
|
349
|
+
});
|
350
|
+
}
|
351
|
+
download(params) {
|
352
|
+
return __awaiter(this, void 0, void 0, function* () {
|
353
|
+
const argObj = { action: "WebBrowser.download", note: "触发下载文件", param: this.toStr(params) };
|
354
|
+
this.printer(argObj);
|
355
|
+
const downloadPromise = this.page.waitForEvent('download');
|
356
|
+
if (params._isframe && this.myframe) {
|
357
|
+
yield this.myframe.locator(params._selector).click();
|
358
|
+
}
|
359
|
+
else {
|
360
|
+
yield this.page.locator(params._selector).click();
|
361
|
+
}
|
362
|
+
const saveName = params._filename ? params._filename : '/path/to/save/new_file_name';
|
363
|
+
const savePath = saveName.substring(0, saveName.lastIndexOf('/') + 1);
|
364
|
+
const fileName = saveName.substring(saveName.lastIndexOf('/') + 1);
|
365
|
+
yield (yield downloadPromise).saveAs(savePath + fileName);
|
366
|
+
});
|
367
|
+
}
|
368
|
+
/**
|
369
|
+
* 监听控制台消息
|
370
|
+
* const msgPromise = console()
|
371
|
+
* page.xxxx //操作元素
|
372
|
+
* await msgPromise
|
373
|
+
* @returns
|
374
|
+
*/
|
375
|
+
console() {
|
376
|
+
return __awaiter(this, void 0, void 0, function* () {
|
377
|
+
const argObj = { action: "WebBrowser.console", note: "监听控制台消息" };
|
378
|
+
this.printer(argObj);
|
379
|
+
const msgPromise = this.page.waitForEvent('console');
|
380
|
+
return msgPromise;
|
381
|
+
});
|
382
|
+
}
|
383
|
+
//对话框设置 放在goto之后
|
384
|
+
onDialog(listener) {
|
385
|
+
this.page.on('dialog', listener);
|
386
|
+
}
|
387
|
+
//控制台消息设置 放在goto之后
|
388
|
+
onConsole(listener) {
|
389
|
+
this.page.on('console', listener);
|
390
|
+
}
|
391
|
+
dragAndDrop(source, target, options) {
|
392
|
+
return __awaiter(this, void 0, void 0, function* () {
|
393
|
+
const argObj = { action: "WebBrowser.dragAndDrop", note: "从一个元素拖至另一个元素", element: this.toStr(source), param: this.toStr(target) };
|
394
|
+
this.printer(argObj);
|
395
|
+
return this.page.dragAndDrop(source, target, options);
|
396
|
+
});
|
397
|
+
}
|
398
|
+
addInitScript(params) {
|
399
|
+
return __awaiter(this, void 0, void 0, function* () {
|
400
|
+
const argObj = { action: "WebBrowser.addInitScript", note: "加载外部JS文件", param: this.toStr(params) };
|
401
|
+
this.printer(argObj);
|
402
|
+
return this.page.addInitScript({ path: params._path });
|
403
|
+
});
|
404
|
+
}
|
405
|
+
content() {
|
406
|
+
return __awaiter(this, void 0, void 0, function* () {
|
407
|
+
const argObj = { action: "WebBrowser.content", note: "获取网页源码" };
|
408
|
+
this.printer(argObj);
|
409
|
+
return this.page.content();
|
410
|
+
});
|
411
|
+
}
|
412
|
+
/**
|
413
|
+
* Enable file downloads in Chrome running in headless mode
|
414
|
+
*/
|
415
|
+
enableDownloadInHeadlessChrome() {
|
416
|
+
/*
|
417
|
+
const executor = (this.driver as any).getExecutor
|
418
|
+
? (this.driver as any).getExecutor()
|
419
|
+
: (this.driver as any).executor_;
|
420
|
+
executor.defineCommand(
|
421
|
+
"send_command",
|
422
|
+
"POST",
|
423
|
+
"/session/:sessionId/chromium/send_command"
|
424
|
+
);
|
425
|
+
const params = {
|
426
|
+
cmd: "Page.setDownloadBehavior",
|
427
|
+
params: {
|
428
|
+
behavior: "allow",
|
429
|
+
downloadPath: WebWebBrowser.outDir
|
430
|
+
}
|
431
|
+
};
|
432
|
+
this.driver.execute(new Command("send_command").setParameters(params)); */
|
433
|
+
}
|
434
|
+
/**
|
435
|
+
* Wait for the download to complete.
|
436
|
+
* @returns The name of the downloaded file.
|
437
|
+
*/
|
438
|
+
// eslint-disable-next-line class-methods-use-this
|
439
|
+
waitForDownload(params) {
|
440
|
+
return __awaiter(this, void 0, void 0, function* () {
|
441
|
+
const timeout = params.timeout || 30000;
|
442
|
+
let extname = params.extname || "";
|
443
|
+
if (extname && !extname.startsWith(".")) {
|
444
|
+
extname = `.${extname}`;
|
445
|
+
}
|
446
|
+
Log_1.default.debug("WebWebBrowser.download", { timeout, extname });
|
447
|
+
const startedAt = Date.now();
|
448
|
+
yield params.downloader();
|
449
|
+
// Omit the debug logs temporarily
|
450
|
+
const logLevel = Log_1.default.level;
|
451
|
+
Log_1.default.level = Log_1.default.isInfoEnabled() ? "INFO" : Log_1.default.level;
|
452
|
+
try {
|
453
|
+
while (Date.now() < startedAt + timeout) {
|
454
|
+
const files = File_1.default.listFiles({
|
455
|
+
sortType: File_1.default.SortType.Mtime,
|
456
|
+
orderBy: File_1.default.OrderBy.DESC
|
457
|
+
})
|
458
|
+
.filter((filename) => !filename.endsWith(".crdownload"))
|
459
|
+
.filter((filename) => filename.endsWith(extname));
|
460
|
+
if (files.length > 0) {
|
461
|
+
const latestFile = files[0];
|
462
|
+
if (File_1.default.getStats({ _filename: latestFile }).mtimeMs > startedAt) {
|
463
|
+
return latestFile;
|
464
|
+
}
|
465
|
+
}
|
466
|
+
yield util_1.default.sleep(100); // eslint-disable-line no-await-in-loop
|
467
|
+
}
|
468
|
+
}
|
469
|
+
finally {
|
470
|
+
Log_1.default.level = logLevel;
|
471
|
+
}
|
472
|
+
throw new Error("Download timed out");
|
473
|
+
});
|
474
|
+
}
|
475
|
+
toStr(obj) {
|
476
|
+
if (obj === undefined || obj === '' || obj === null) {
|
477
|
+
return '';
|
478
|
+
}
|
479
|
+
return JSON.stringify(obj);
|
480
|
+
}
|
481
|
+
}
|
482
|
+
MR.WebBrowser = WebBrowser;
|
483
|
+
})(MR = exports.MR || (exports.MR = {}));
|
484
|
+
exports.default = MR.WebBrowser.instance;
|
File without changes
|