@tecgovtnzwebdev/puppeteer 1.0.1
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 +26 -0
- package/cjs/checkbox.d.ts +20 -0
- package/cjs/checkbox.js +66 -0
- package/cjs/click-helper.d.ts +9 -0
- package/cjs/click-helper.js +21 -0
- package/cjs/find-element-with-text.d.ts +8 -0
- package/cjs/find-element-with-text.js +33 -0
- package/cjs/get-attribute.d.ts +8 -0
- package/cjs/get-attribute.js +40 -0
- package/cjs/get-text.d.ts +7 -0
- package/cjs/get-text.js +31 -0
- package/cjs/index.d.ts +10 -0
- package/cjs/index.js +22 -0
- package/cjs/input.d.ts +12 -0
- package/cjs/input.js +47 -0
- package/cjs/is-visible.d.ts +2 -0
- package/cjs/is-visible.js +26 -0
- package/cjs/mail-hog.d.ts +10 -0
- package/cjs/mail-hog.js +35 -0
- package/cjs/select.d.ts +28 -0
- package/cjs/select.js +90 -0
- package/cjs/throw-helper.d.ts +2 -0
- package/cjs/throw-helper.js +9 -0
- package/cjs/wait-until.d.ts +2 -0
- package/cjs/wait-until.js +26 -0
- package/package.json +29 -0
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Puppeteer helper packages
|
|
2
|
+
## Getting started
|
|
3
|
+
```
|
|
4
|
+
npm ci
|
|
5
|
+
```
|
|
6
|
+
## Dev
|
|
7
|
+
Helpers are written in typescript and then transpiled out to various formats and module systems (currently only CJS).
|
|
8
|
+
|
|
9
|
+
### Build
|
|
10
|
+
Run the following command. This will emit the `cjs` directory.
|
|
11
|
+
```
|
|
12
|
+
npx tsc
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Use
|
|
16
|
+
`npm` has support for `bitbucket` package sources (https://docs.npmjs.com/cli/v7/commands/npm-install)
|
|
17
|
+
|
|
18
|
+
So to install the `@tecgovtnz/puppeteer` package, run:
|
|
19
|
+
```
|
|
20
|
+
npm i bitbucket:careersnz/npm-puppeteer
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
If you are developing a new package and which to test your branch you can use the syntax:
|
|
24
|
+
```
|
|
25
|
+
npm i bitbucket:careersnz/npm-puppeteer\#BRANCH_NAME
|
|
26
|
+
```
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import * as puppeteer from 'puppeteer';
|
|
2
|
+
/**
|
|
3
|
+
* Is a checkbox checked?
|
|
4
|
+
* @param selector The selector for the HTMLInputElement element.
|
|
5
|
+
* @param page The puppeteer page.
|
|
6
|
+
* @returns true if the checkbox is checked, false otherwise.
|
|
7
|
+
*/
|
|
8
|
+
export declare function isChecked(page: puppeteer.Page, selector: string): Promise<boolean>;
|
|
9
|
+
/**
|
|
10
|
+
* Checks a checkbox.
|
|
11
|
+
* @param page The puppeteer page.
|
|
12
|
+
* @param selector The selector for the HTMLInputElement element.
|
|
13
|
+
*/
|
|
14
|
+
export declare function check(page: puppeteer.Page, selector: string): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Unchecks a checkbox.
|
|
17
|
+
* @param page The puppeteer page.
|
|
18
|
+
* @param selector The selector for the HTMLInputElement element.
|
|
19
|
+
*/
|
|
20
|
+
export declare function uncheck(page: puppeteer.Page, selector: string): Promise<void>;
|
package/cjs/checkbox.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
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.uncheck = exports.check = exports.isChecked = void 0;
|
|
13
|
+
/**
|
|
14
|
+
* Is a checkbox checked?
|
|
15
|
+
* @param selector The selector for the HTMLInputElement element.
|
|
16
|
+
* @param page The puppeteer page.
|
|
17
|
+
* @returns true if the checkbox is checked, false otherwise.
|
|
18
|
+
*/
|
|
19
|
+
function isChecked(page, selector) {
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
const isChecked = yield page.evaluate((selector_) => {
|
|
22
|
+
const checkbox = document.querySelector(selector_);
|
|
23
|
+
if (!checkbox) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
return checkbox.checked;
|
|
27
|
+
}, selector);
|
|
28
|
+
if (isChecked === null) {
|
|
29
|
+
throw new Error(`Failed to get the checked value of '${selector}'`);
|
|
30
|
+
}
|
|
31
|
+
return isChecked;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
exports.isChecked = isChecked;
|
|
35
|
+
/**
|
|
36
|
+
* Checks a checkbox.
|
|
37
|
+
* @param page The puppeteer page.
|
|
38
|
+
* @param selector The selector for the HTMLInputElement element.
|
|
39
|
+
*/
|
|
40
|
+
function check(page, selector) {
|
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
if (!(yield isChecked(page, selector))) {
|
|
43
|
+
yield page.click(selector);
|
|
44
|
+
if (!(yield isChecked(page, selector))) {
|
|
45
|
+
throw new Error(`Expected checkbox '${selector}' to be checked`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
exports.check = check;
|
|
51
|
+
/**
|
|
52
|
+
* Unchecks a checkbox.
|
|
53
|
+
* @param page The puppeteer page.
|
|
54
|
+
* @param selector The selector for the HTMLInputElement element.
|
|
55
|
+
*/
|
|
56
|
+
function uncheck(page, selector) {
|
|
57
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
58
|
+
if (yield isChecked(page, selector)) {
|
|
59
|
+
yield page.click(selector);
|
|
60
|
+
if (yield isChecked(page, selector)) {
|
|
61
|
+
throw new Error(`Expected checkbox '${selector}' to be unchecked`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
exports.uncheck = uncheck;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as puppeteer from 'puppeteer';
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* @param page The puppeteer page.
|
|
5
|
+
* @param elementOrSelector The HTMLElement or string selector to click.
|
|
6
|
+
* @param waitUntil Optional wait settings.
|
|
7
|
+
* @returns A promise which resolves after the page has navigated.
|
|
8
|
+
*/
|
|
9
|
+
export declare function clickAndWaitForNavigation(page: puppeteer.Page, elementOrSelector: HTMLElement | string | puppeteer.ElementHandle, waitUntil?: puppeteer.WaitForOptions): Promise<[puppeteer.HTTPResponse | null, void]>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.clickAndWaitForNavigation = void 0;
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
* @param page The puppeteer page.
|
|
7
|
+
* @param elementOrSelector The HTMLElement or string selector to click.
|
|
8
|
+
* @param waitUntil Optional wait settings.
|
|
9
|
+
* @returns A promise which resolves after the page has navigated.
|
|
10
|
+
*/
|
|
11
|
+
function clickAndWaitForNavigation(page, elementOrSelector, waitUntil) {
|
|
12
|
+
const click = typeof elementOrSelector === 'string'
|
|
13
|
+
? page.click(elementOrSelector)
|
|
14
|
+
: elementOrSelector.click();
|
|
15
|
+
return Promise.all([
|
|
16
|
+
page.waitForNavigation(waitUntil),
|
|
17
|
+
click
|
|
18
|
+
]);
|
|
19
|
+
}
|
|
20
|
+
exports.clickAndWaitForNavigation = clickAndWaitForNavigation;
|
|
21
|
+
;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as puppeteer from 'puppeteer';
|
|
2
|
+
/**
|
|
3
|
+
* Tries to find an element containing the specified text in its innerText value.
|
|
4
|
+
* @param page The puppeteer page.
|
|
5
|
+
* @param elementSelector The set of elements to search in.
|
|
6
|
+
* @param text The text to search for. This value is case-sensitive.
|
|
7
|
+
*/
|
|
8
|
+
export declare function findElementWithText(page: puppeteer.Page, elementSelector: string, text: string): Promise<puppeteer.ElementHandle<Element> | null>;
|
|
@@ -0,0 +1,33 @@
|
|
|
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.findElementWithText = void 0;
|
|
13
|
+
const get_text_1 = require("./get-text");
|
|
14
|
+
/**
|
|
15
|
+
* Tries to find an element containing the specified text in its innerText value.
|
|
16
|
+
* @param page The puppeteer page.
|
|
17
|
+
* @param elementSelector The set of elements to search in.
|
|
18
|
+
* @param text The text to search for. This value is case-sensitive.
|
|
19
|
+
*/
|
|
20
|
+
function findElementWithText(page, elementSelector, text) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
const elements = yield page.$$(elementSelector);
|
|
23
|
+
for (const element of elements) {
|
|
24
|
+
const textOfElement = yield get_text_1.getText(page, element);
|
|
25
|
+
if (textOfElement && textOfElement.includes(text)) {
|
|
26
|
+
return element;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
exports.findElementWithText = findElementWithText;
|
|
33
|
+
;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as puppeteer from 'puppeteer';
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* @param page The puppeteer page.
|
|
5
|
+
* @param elementSelectorOrHandle An element selector or handle to an element.
|
|
6
|
+
* @param attributeName The name of the attribute to get the value for.
|
|
7
|
+
*/
|
|
8
|
+
export declare function getAttribute(page: puppeteer.Page, elementSelectorOrHandle: string | puppeteer.ElementHandle<Element>, attributeName: string): Promise<string | null | undefined>;
|
|
@@ -0,0 +1,40 @@
|
|
|
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.getAttribute = void 0;
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* @param page The puppeteer page.
|
|
16
|
+
* @param elementSelectorOrHandle An element selector or handle to an element.
|
|
17
|
+
* @param attributeName The name of the attribute to get the value for.
|
|
18
|
+
*/
|
|
19
|
+
function getAttribute(page, elementSelectorOrHandle, attributeName) {
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
let element;
|
|
22
|
+
switch (typeof elementSelectorOrHandle) {
|
|
23
|
+
case 'string':
|
|
24
|
+
const elementBySelector = yield page.waitForSelector(elementSelectorOrHandle);
|
|
25
|
+
if (!elementBySelector) {
|
|
26
|
+
throw new Error(`Failed to find element with selector '${elementSelectorOrHandle}'`);
|
|
27
|
+
}
|
|
28
|
+
element = elementBySelector;
|
|
29
|
+
break;
|
|
30
|
+
case 'object':
|
|
31
|
+
element = elementSelectorOrHandle;
|
|
32
|
+
break;
|
|
33
|
+
default:
|
|
34
|
+
throw new Error(`Unsupported type ${typeof elementSelectorOrHandle}`);
|
|
35
|
+
}
|
|
36
|
+
return (yield page.evaluate((el, att) => el.getAttribute(att), element, attributeName));
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
exports.getAttribute = getAttribute;
|
|
40
|
+
;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as puppeteer from 'puppeteer';
|
|
2
|
+
/**
|
|
3
|
+
* Gets the innerText value of an element.
|
|
4
|
+
* @param page The puppeteer page.
|
|
5
|
+
* @param elementSelectorOrHandle The HTMLElement or string selector to get the innerText value from.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getText(page: puppeteer.Page, elementSelectorOrHandle: string | puppeteer.ElementHandle<Element>): Promise<string | null | undefined>;
|
package/cjs/get-text.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
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.getText = void 0;
|
|
13
|
+
/**
|
|
14
|
+
* Gets the innerText value of an element.
|
|
15
|
+
* @param page The puppeteer page.
|
|
16
|
+
* @param elementSelectorOrHandle The HTMLElement or string selector to get the innerText value from.
|
|
17
|
+
*/
|
|
18
|
+
function getText(page, elementSelectorOrHandle) {
|
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
+
switch (typeof elementSelectorOrHandle) {
|
|
21
|
+
case 'string':
|
|
22
|
+
const element = yield page.waitForSelector(elementSelectorOrHandle);
|
|
23
|
+
return (page.evaluate(el => el.innerText, element));
|
|
24
|
+
case 'object':
|
|
25
|
+
return (page.evaluate(el => el.innerText, elementSelectorOrHandle));
|
|
26
|
+
default:
|
|
27
|
+
throw new Error(`Unsupported type ${typeof elementSelectorOrHandle}`);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
exports.getText = getText;
|
package/cjs/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './checkbox';
|
|
2
|
+
export * from './click-helper';
|
|
3
|
+
export * from './find-element-with-text';
|
|
4
|
+
export * from './get-attribute';
|
|
5
|
+
export * from './get-text';
|
|
6
|
+
export * from './input';
|
|
7
|
+
export * from './is-visible';
|
|
8
|
+
export * from './mail-hog';
|
|
9
|
+
export * from './select';
|
|
10
|
+
export * from './wait-until';
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
__exportStar(require("./checkbox"), exports);
|
|
14
|
+
__exportStar(require("./click-helper"), exports);
|
|
15
|
+
__exportStar(require("./find-element-with-text"), exports);
|
|
16
|
+
__exportStar(require("./get-attribute"), exports);
|
|
17
|
+
__exportStar(require("./get-text"), exports);
|
|
18
|
+
__exportStar(require("./input"), exports);
|
|
19
|
+
__exportStar(require("./is-visible"), exports);
|
|
20
|
+
__exportStar(require("./mail-hog"), exports);
|
|
21
|
+
__exportStar(require("./select"), exports);
|
|
22
|
+
__exportStar(require("./wait-until"), exports);
|
package/cjs/input.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as puppeteer from 'puppeteer';
|
|
2
|
+
/**
|
|
3
|
+
* Gets the value from an HTMLInputElement.
|
|
4
|
+
* @param page The puppeteer page.
|
|
5
|
+
* @param elementSelectorOrHandle A selector for a <input /> element, or a handle.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getValue(page: puppeteer.Page, elementSelectorOrHandle: string | puppeteer.ElementHandle<Element>): Promise<any>;
|
|
8
|
+
/**
|
|
9
|
+
* @param selector A selector for a <input /> element.
|
|
10
|
+
* @param text The text to type into the <input />.
|
|
11
|
+
*/
|
|
12
|
+
export declare function clearAndType(page: puppeteer.Page, selector: string, text: string): Promise<void>;
|
package/cjs/input.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
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.clearAndType = exports.getValue = void 0;
|
|
13
|
+
const throw_helper_1 = require("./throw-helper");
|
|
14
|
+
/**
|
|
15
|
+
* Gets the value from an HTMLInputElement.
|
|
16
|
+
* @param page The puppeteer page.
|
|
17
|
+
* @param elementSelectorOrHandle A selector for a <input /> element, or a handle.
|
|
18
|
+
*/
|
|
19
|
+
function getValue(page, elementSelectorOrHandle) {
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
switch (typeof elementSelectorOrHandle) {
|
|
22
|
+
case 'string':
|
|
23
|
+
const element = yield page.waitForSelector(elementSelectorOrHandle);
|
|
24
|
+
return page.evaluate(el => el.value, element);
|
|
25
|
+
case 'object':
|
|
26
|
+
return page.evaluate(el => el.value, elementSelectorOrHandle);
|
|
27
|
+
default:
|
|
28
|
+
throw new Error(`Unsupported type ${typeof elementSelectorOrHandle}`);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
exports.getValue = getValue;
|
|
33
|
+
/**
|
|
34
|
+
* @param selector A selector for a <input /> element.
|
|
35
|
+
* @param text The text to type into the <input />.
|
|
36
|
+
*/
|
|
37
|
+
function clearAndType(page, selector, text) {
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
const input = yield page.$(selector);
|
|
40
|
+
throw_helper_1.assertSuccessfulSelector(selector, input);
|
|
41
|
+
yield page.evaluate(i => {
|
|
42
|
+
i.value = '';
|
|
43
|
+
}, input);
|
|
44
|
+
yield page.type(selector, text);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
exports.clearAndType = clearAndType;
|
|
@@ -0,0 +1,26 @@
|
|
|
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.isVisible = void 0;
|
|
13
|
+
function isVisible(page, selector) {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
return page.evaluate((selector_) => {
|
|
16
|
+
const element = document.querySelector(selector_);
|
|
17
|
+
if (!element) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
const style = getComputedStyle(element);
|
|
21
|
+
const rect = element.getBoundingClientRect();
|
|
22
|
+
return style.visibility !== 'hidden' && !!(rect.bottom || rect.top || rect.height || rect.width) && style.opacity !== '0';
|
|
23
|
+
}, selector);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
exports.isVisible = isVisible;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import parseEmail from 'mailparser';
|
|
2
|
+
export declare class MailHog {
|
|
3
|
+
private _mailHogUrl;
|
|
4
|
+
constructor(mailHogUrl: string);
|
|
5
|
+
/**
|
|
6
|
+
* Gets email from the mailhog inbox which match the specified 'to' address.
|
|
7
|
+
* @param addressNeedle A 'to' address filter.
|
|
8
|
+
*/
|
|
9
|
+
get(addressNeedle: string): Promise<parseEmail.ParsedMail[]>;
|
|
10
|
+
}
|
package/cjs/mail-hog.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.MailHog = void 0;
|
|
16
|
+
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
17
|
+
const mailparser_1 = __importDefault(require("mailparser"));
|
|
18
|
+
class MailHog {
|
|
19
|
+
constructor(mailHogUrl) {
|
|
20
|
+
this._mailHogUrl = mailHogUrl;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Gets email from the mailhog inbox which match the specified 'to' address.
|
|
24
|
+
* @param addressNeedle A 'to' address filter.
|
|
25
|
+
*/
|
|
26
|
+
get(addressNeedle) {
|
|
27
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
const response = yield node_fetch_1.default(`${this._mailHogUrl}/api/v2/search?kind=to&query=${encodeURIComponent(addressNeedle)}`);
|
|
29
|
+
const messagesJson = yield response.json();
|
|
30
|
+
const messages = messagesJson.items;
|
|
31
|
+
return Promise.all(messages.map(message => mailparser_1.default.simpleParser(message.Raw.Data)));
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.MailHog = MailHog;
|
package/cjs/select.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import * as puppeteer from 'puppeteer';
|
|
2
|
+
/**
|
|
3
|
+
* Selects one (or many) dropdown items by matching their <option /> text values.
|
|
4
|
+
* @param page The puppeteer page.
|
|
5
|
+
* @param selector A selector to a <select /> element.
|
|
6
|
+
* @param texts A set of <option /> innerText values to match on.
|
|
7
|
+
* @returns An array of option values that have been successfully selected.
|
|
8
|
+
*/
|
|
9
|
+
export declare function selectWithText(page: puppeteer.Page, selector: string, ...texts: string[]): Promise<string[]>;
|
|
10
|
+
/**
|
|
11
|
+
* Gets the text of the currently selected option of a <select />.
|
|
12
|
+
* @param page The puppeteer page.
|
|
13
|
+
* @param selector A selector to a <select /> element.
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
export declare function getTextOfSelectedOption(page: puppeteer.Page, selector: string): Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* Gets the set of text of the currently selected options of a <select /> element.
|
|
19
|
+
* @param page The puppeteer page.
|
|
20
|
+
* @param selector A selector to a <select /> element.
|
|
21
|
+
*/
|
|
22
|
+
export declare function getTextsOfSelectedOptions(page: puppeteer.Page, selector: string): Promise<string[]>;
|
|
23
|
+
/**
|
|
24
|
+
* Uncheck all options of a select.
|
|
25
|
+
* @param page The puppeteer page.
|
|
26
|
+
* @param selector A selector to a <select /> element.
|
|
27
|
+
*/
|
|
28
|
+
export declare function uncheckAll(page: puppeteer.Page, selector: string): Promise<void>;
|
package/cjs/select.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
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.uncheckAll = exports.getTextsOfSelectedOptions = exports.getTextOfSelectedOption = exports.selectWithText = void 0;
|
|
13
|
+
const throw_helper_1 = require("./throw-helper");
|
|
14
|
+
/**
|
|
15
|
+
* Selects one (or many) dropdown items by matching their <option /> text values.
|
|
16
|
+
* @param page The puppeteer page.
|
|
17
|
+
* @param selector A selector to a <select /> element.
|
|
18
|
+
* @param texts A set of <option /> innerText values to match on.
|
|
19
|
+
* @returns An array of option values that have been successfully selected.
|
|
20
|
+
*/
|
|
21
|
+
function selectWithText(page, selector, ...texts) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
const select = (yield page.$(selector));
|
|
24
|
+
throw_helper_1.assertSuccessfulSelector(selector, select);
|
|
25
|
+
const options = yield select.$$('option');
|
|
26
|
+
// Pull out just the option value and text
|
|
27
|
+
const optionData = (yield Promise.all(options.map((opt) => __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
const value = yield page.evaluate(element => element.value, opt);
|
|
29
|
+
const text = (yield page.evaluate(element => element.textContent, opt)).trim();
|
|
30
|
+
return {
|
|
31
|
+
value: value.toString(),
|
|
32
|
+
text
|
|
33
|
+
};
|
|
34
|
+
})))).filter(opt => texts.includes(opt.text));
|
|
35
|
+
return select.select(...optionData.map(opt => opt.value));
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
exports.selectWithText = selectWithText;
|
|
39
|
+
/**
|
|
40
|
+
* Gets the text of the currently selected option of a <select />.
|
|
41
|
+
* @param page The puppeteer page.
|
|
42
|
+
* @param selector A selector to a <select /> element.
|
|
43
|
+
* @returns
|
|
44
|
+
*/
|
|
45
|
+
function getTextOfSelectedOption(page, selector) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
const select = (yield page.$(selector));
|
|
48
|
+
throw_helper_1.assertSuccessfulSelector(selector, select);
|
|
49
|
+
return page.evaluate((select) => select.options[select.selectedIndex].text, select);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
exports.getTextOfSelectedOption = getTextOfSelectedOption;
|
|
53
|
+
/**
|
|
54
|
+
* Gets the set of text of the currently selected options of a <select /> element.
|
|
55
|
+
* @param page The puppeteer page.
|
|
56
|
+
* @param selector A selector to a <select /> element.
|
|
57
|
+
*/
|
|
58
|
+
function getTextsOfSelectedOptions(page, selector) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
return yield page.evaluate((selector_) => {
|
|
61
|
+
const select = document.querySelector(selector_);
|
|
62
|
+
if (!select) {
|
|
63
|
+
throw new Error(`Could find select element by '${selector}'`);
|
|
64
|
+
}
|
|
65
|
+
return Array.from(select.options)
|
|
66
|
+
.filter(opt => opt.selected)
|
|
67
|
+
.map(opt => opt.innerText);
|
|
68
|
+
}, selector);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
exports.getTextsOfSelectedOptions = getTextsOfSelectedOptions;
|
|
72
|
+
/**
|
|
73
|
+
* Uncheck all options of a select.
|
|
74
|
+
* @param page The puppeteer page.
|
|
75
|
+
* @param selector A selector to a <select /> element.
|
|
76
|
+
*/
|
|
77
|
+
function uncheckAll(page, selector) {
|
|
78
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
yield page.evaluate((selector_) => {
|
|
80
|
+
const select = document.querySelector(selector_);
|
|
81
|
+
if (!select) {
|
|
82
|
+
throw new Error(`Could not find select with selector '${selector_}'`);
|
|
83
|
+
}
|
|
84
|
+
for (const opt of select.options) {
|
|
85
|
+
opt.selected = false;
|
|
86
|
+
}
|
|
87
|
+
}, selector);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
exports.uncheckAll = uncheckAll;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assertSuccessfulSelector = void 0;
|
|
4
|
+
function assertSuccessfulSelector(selector, element) {
|
|
5
|
+
if (!element) {
|
|
6
|
+
throw new Error(`Failed to find object by selector '${selector}'`);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
exports.assertSuccessfulSelector = assertSuccessfulSelector;
|
|
@@ -0,0 +1,26 @@
|
|
|
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.waitUntil = void 0;
|
|
13
|
+
function waitUntil(page, waitFunction, errorMessage = 'Timed out waiting for something', timeOut = 5000, pollInterval = 100) {
|
|
14
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
15
|
+
const endTime = Date.now() + timeOut;
|
|
16
|
+
let conditionMet = yield waitFunction();
|
|
17
|
+
while (!conditionMet && Date.now() < endTime) {
|
|
18
|
+
yield page.waitForTimeout(pollInterval);
|
|
19
|
+
conditionMet = yield waitFunction();
|
|
20
|
+
}
|
|
21
|
+
if (!conditionMet) {
|
|
22
|
+
throw new Error(errorMessage);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
exports.waitUntil = waitUntil;
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tecgovtnzwebdev/puppeteer",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "puppeteer helpers",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
7
|
+
},
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"/cjs"
|
|
13
|
+
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://bitbucket.org/careersnz/npm-puppeteer.git"
|
|
17
|
+
},
|
|
18
|
+
"author": "",
|
|
19
|
+
"license": "ISC",
|
|
20
|
+
"homepage": "https://bitbucket.org/careersnz/npm-puppeteer#readme",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@types/mailparser": "^3.0.2",
|
|
23
|
+
"@types/node-fetch": "^2.5.10",
|
|
24
|
+
"mailparser": "^3.2.0",
|
|
25
|
+
"node-fetch": "^2.6.1",
|
|
26
|
+
"puppeteer": "^9.1.1",
|
|
27
|
+
"typescript": "^4.2.4"
|
|
28
|
+
}
|
|
29
|
+
}
|