-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (114 loc) · 3.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*!
* index.js - Angular synchronization plugin.
* Copyright (c) 2018 - 2019 Richard Huang <rickypc@users.noreply.github.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const { PuppeteerExtraPlugin } = require('puppeteer-extra-plugin');
const Click = require('./lib/Click.js');
const Form = require('./lib/Form.js');
const Logger = require('./lib/Logger.js');
const Navigate = require('./lib/Navigate.js');
const Toggle = require('./lib/Toggle.js');
const Type = require('./lib/Type.js');
const Wait = require('./lib/Wait.js');
/**
* Provide puppeteer functionality with Angular synchronization support.
*
* @extends external:PuppeteerExtraPlugin
* @module puppeteer-extra-plugin-angular
* @param {Object} opts - Options
*
* @example
* const puppeteer = require('puppeteer-extra');
* puppeteer.use(require('puppeteer-extra-plugin-angular')());
*
* (async () => {
* const configs = [
* {
* label: 'Email',
* selector: 'input.email',
* type: 'type',
* value: 'theEmail'
* },
* {
* label: 'Subscribe',
* selector: 'input.subscribe',
* type: 'check',
* },
* {
* label: 'Send',
* selector: 'button',
* type: 'click',
* },
* ];
* const data = {
* theEmail: 'you@address.com',
* };
*
* const browser = await puppeteer.launch();
* const page = await browser.newPage();
*
* // Calling page.waitUntilActionReady internally.
* await page.navigateUntilReady('https://angular.io');
* await page.formFillOut(configs, data);
*
* // Calling page.waitUntilActionReady internally.
* await page.clickIfExists('a.link', 'A Link');
*
* await page.toggleUncheck('input.radio[value="1"]', 'Uncheck Radio');
* await page.toggleSelectByText('select1', 'Option 1', 'Selection');
* await page.toggleDeselectByText('select2', 'Option 2', 'Deselection');
* await page.toggleCheck('input.check', 'Checkbox');
*
* // Wait until both document.readyState is interactive or complete
* // and Angular is ready.
* await page.waitUntilActionReady();
*
* await page.typeIfExists('input.text', 'Something', 'Textfield');
*
* // Wait until Angular is ready.
* await page.waitUntilAngularReady();
*
* await page.close();
* await browser.close();
* })();
*/
/* eslint class-methods-use-this: ["error", { "exceptMethods": ["name"] }] */
class Plugin extends PuppeteerExtraPlugin {
constructor (opts = {}) {
super(opts);
this.logger = new Logger('pepa:index');
}
get name () {
return 'angular';
}
/**
* @private
*/
onPageCreated (page) {
page.clickIfExists = Click.ifExists.bind(page);
page.formFillOut = Form.fillOut.bind(page);
page.navigateUntilReady = Navigate.untilReady.bind(page);
page.toggleCheck = Toggle.check.bind(page);
page.toggleDeselectByText = Toggle.deselectByText.bind(page);
page.toggleSelectByText = Toggle.selectByText.bind(page);
page.toggleUncheck = Toggle.uncheck.bind(page);
page.typeIfExists = Type.ifExists.bind(page);
page.waitUntilActionReady = Wait.untilActionReady.bind(page);
page.waitUntilAngularReady = Wait.untilAngularReady.bind(page);
this.logger.debug('onPageCreated');
}
}
module.exports = (pluginConfig) => new Plugin(pluginConfig);