codeceptjs 2.4.3 → 2.6.2
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/CHANGELOG.md +117 -0
- package/README.md +32 -7
- package/bin/codecept.js +3 -0
- package/docs/basics.md +11 -5
- package/docs/bdd.md +4 -4
- package/docs/build/MockRequest.js +3 -0
- package/docs/build/Nightmare.js +10 -2
- package/docs/build/Playwright.js +3187 -0
- package/docs/build/Protractor.js +16 -2
- package/docs/build/Puppeteer.js +126 -19
- package/docs/build/REST.js +3 -1
- package/docs/build/TestCafe.js +11 -3
- package/docs/build/WebDriver.js +361 -28
- package/docs/changelog.md +116 -0
- package/docs/configuration.md +2 -2
- package/docs/custom-helpers.md +55 -10
- package/docs/helpers/Appium.md +81 -1
- package/docs/helpers/MockRequest.md +281 -38
- package/docs/helpers/Nightmare.md +10 -2
- package/docs/helpers/Playwright.md +1770 -0
- package/docs/helpers/Protractor.md +15 -3
- package/docs/helpers/Puppeteer-firefox.md +32 -1
- package/docs/helpers/Puppeteer.md +126 -76
- package/docs/helpers/TestCafe.md +10 -2
- package/docs/helpers/WebDriver.md +208 -118
- package/docs/locators.md +2 -0
- package/docs/playwright.md +306 -0
- package/docs/plugins.md +103 -0
- package/docs/reports.md +12 -0
- package/docs/shadow.md +68 -0
- package/docs/visual.md +0 -73
- package/docs/webapi/forceClick.mustache +27 -0
- package/docs/webapi/seeInPopup.mustache +7 -0
- package/docs/webapi/setCookie.mustache +10 -2
- package/docs/webapi/type.mustache +12 -0
- package/docs/webdriver.md +7 -3
- package/lib/codecept.js +1 -1
- package/lib/command/definitions.js +2 -2
- package/lib/command/generate.js +4 -4
- package/lib/command/gherkin/snippets.js +4 -4
- package/lib/command/init.js +1 -1
- package/lib/command/interactive.js +3 -0
- package/lib/command/run-multiple.js +2 -2
- package/lib/command/run-rerun.js +2 -0
- package/lib/command/run-workers.js +22 -8
- package/lib/command/run.js +2 -0
- package/lib/command/workers/runTests.js +1 -0
- package/lib/container.js +1 -1
- package/lib/event.js +2 -0
- package/lib/helper/MockRequest.js +3 -0
- package/lib/helper/Playwright.js +2422 -0
- package/lib/helper/Protractor.js +1 -2
- package/lib/helper/Puppeteer.js +84 -19
- package/lib/helper/REST.js +3 -1
- package/lib/helper/TestCafe.js +1 -1
- package/lib/helper/WebDriver.js +313 -26
- package/lib/helper/extras/PlaywrightPropEngine.js +53 -0
- package/lib/helper/scripts/isElementClickable.js +54 -14
- package/lib/interfaces/gherkin.js +1 -1
- package/lib/listener/helpers.js +3 -0
- package/lib/locator.js +5 -0
- package/lib/mochaFactory.js +12 -10
- package/lib/plugin/allure.js +8 -1
- package/lib/plugin/autoDelay.js +1 -8
- package/lib/plugin/commentStep.js +133 -0
- package/lib/plugin/screenshotOnFail.js +3 -10
- package/lib/plugin/selenoid.js +2 -2
- package/lib/plugin/standardActingHelpers.js +13 -0
- package/lib/plugin/stepByStepReport.js +1 -8
- package/lib/plugin/wdio.js +10 -1
- package/lib/reporter/cli.js +30 -1
- package/lib/session.js +7 -4
- package/package.json +13 -10
- package/typings/Mocha.d.ts +567 -16
- package/typings/index.d.ts +9 -5
- package/typings/types.d.ts +1634 -74
|
@@ -0,0 +1,2422 @@
|
|
|
1
|
+
const requireg = require('requireg');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const fsExtra = require('fs-extra');
|
|
5
|
+
|
|
6
|
+
const Helper = require('../helper');
|
|
7
|
+
const Locator = require('../locator');
|
|
8
|
+
const recorder = require('../recorder');
|
|
9
|
+
const stringIncludes = require('../assert/include').includes;
|
|
10
|
+
const { urlEquals } = require('../assert/equal');
|
|
11
|
+
const { equals } = require('../assert/equal');
|
|
12
|
+
const { empty } = require('../assert/empty');
|
|
13
|
+
const { truth } = require('../assert/truth');
|
|
14
|
+
const isElementClickable = require('./scripts/isElementClickable');
|
|
15
|
+
const {
|
|
16
|
+
xpathLocator,
|
|
17
|
+
ucfirst,
|
|
18
|
+
fileExists,
|
|
19
|
+
chunkArray,
|
|
20
|
+
toCamelCase,
|
|
21
|
+
convertCssPropertiesToCamelCase,
|
|
22
|
+
screenshotOutputFolder,
|
|
23
|
+
getNormalizedKeyAttributeValue,
|
|
24
|
+
isModifierKey,
|
|
25
|
+
} = require('../utils');
|
|
26
|
+
const {
|
|
27
|
+
isColorProperty,
|
|
28
|
+
convertColorToRGBA,
|
|
29
|
+
} = require('../colorUtils');
|
|
30
|
+
const ElementNotFound = require('./errors/ElementNotFound');
|
|
31
|
+
const RemoteBrowserConnectionRefused = require('./errors/RemoteBrowserConnectionRefused');
|
|
32
|
+
const Popup = require('./extras/Popup');
|
|
33
|
+
const Console = require('./extras/Console');
|
|
34
|
+
|
|
35
|
+
let playwright;
|
|
36
|
+
let perfTiming;
|
|
37
|
+
let defaultSelectorEnginesInitialized = false;
|
|
38
|
+
|
|
39
|
+
const popupStore = new Popup();
|
|
40
|
+
const consoleLogStore = new Console();
|
|
41
|
+
const availableBrowsers = ['chromium', 'webkit', 'firefox'];
|
|
42
|
+
|
|
43
|
+
const { createValueEngine, createDisabledEngine } = require('./extras/PlaywrightPropEngine');
|
|
44
|
+
/**
|
|
45
|
+
* Uses [Playwright](https://github.com/microsoft/playwright) library to run tests inside:
|
|
46
|
+
*
|
|
47
|
+
* * Chromium
|
|
48
|
+
* * Firefox
|
|
49
|
+
* * Webkit (Safari)
|
|
50
|
+
*
|
|
51
|
+
* This helper works with a browser out of the box with no additional tools required to install.
|
|
52
|
+
*
|
|
53
|
+
* Requires `playwright` package version ^0.12.1 to be installed:
|
|
54
|
+
*
|
|
55
|
+
* ```
|
|
56
|
+
* npm i playwright@^0.12.1 --save
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* ## Configuration
|
|
60
|
+
*
|
|
61
|
+
* This helper should be configured in codecept.json or codecept.conf.js
|
|
62
|
+
*
|
|
63
|
+
* * `url`: base url of website to be tested
|
|
64
|
+
* * `browser`: a browser to test on, either: `chromium`, `firefox`, `webkit`. Default: chromium.
|
|
65
|
+
* * `show`: (optional, default: false) - show browser window.
|
|
66
|
+
* * `restart`: (optional, default: true) - restart browser between tests.
|
|
67
|
+
* * `disableScreenshots`: (optional, default: false) - don't save screenshot on failure.
|
|
68
|
+
* * `emulate`: (optional, default: {}) launch browser in device emulation mode.
|
|
69
|
+
* * `fullPageScreenshots` (optional, default: false) - make full page screenshots on failure.
|
|
70
|
+
* * `uniqueScreenshotNames`: (optional, default: false) - option to prevent screenshot override if you have scenarios with the same name in different suites.
|
|
71
|
+
* * `keepBrowserState`: (optional, default: false) - keep browser state between tests when `restart` is set to false.
|
|
72
|
+
* * `keepCookies`: (optional, default: false) - keep cookies between tests when `restart` is set to false.
|
|
73
|
+
* * `waitForAction`: (optional) how long to wait after click, doubleClick or PressKey actions in ms. Default: 100.
|
|
74
|
+
* * `waitForNavigation`: (optional, default: 'load'). When to consider navigation succeeded. Possible options: `load`, `domcontentloaded`, `networkidle0`, `networkidle2`. Choose one of those options is possible. See [Playwright API](https://github.com/microsoft/playwright/blob/master/docs/api.md#pagewaitfornavigationoptions).
|
|
75
|
+
* * `pressKeyDelay`: (optional, default: '10'). Delay between key presses in ms. Used when calling Playwrights page.type(...) in fillField/appendField
|
|
76
|
+
* * `getPageTimeout` (optional, default: '0') config option to set maximum navigation time in milliseconds.
|
|
77
|
+
* * `waitForTimeout`: (optional) default wait* timeout in ms. Default: 1000.
|
|
78
|
+
* * `basicAuth`: (optional) the basic authentication to pass to base url. Example: {username: 'username', password: 'password'}
|
|
79
|
+
* * `windowSize`: (optional) default window size. Set a dimension like `640x480`.
|
|
80
|
+
* * `userAgent`: (optional) user-agent string.
|
|
81
|
+
* * `manualStart`: (optional, default: false) - do not start browser before a test, start it manually inside a helper with `this.helpers["Playwright"]._startBrowser()`.
|
|
82
|
+
* * `chromium`: (optional) pass additional chromium options
|
|
83
|
+
*
|
|
84
|
+
* #### Example #1: Wait for 0 network connections.
|
|
85
|
+
*
|
|
86
|
+
* ```js
|
|
87
|
+
* {
|
|
88
|
+
* helpers: {
|
|
89
|
+
* Playwright : {
|
|
90
|
+
* url: "http://localhost",
|
|
91
|
+
* restart: false,
|
|
92
|
+
* waitForNavigation: "networkidle0",
|
|
93
|
+
* waitForAction: 500
|
|
94
|
+
* }
|
|
95
|
+
* }
|
|
96
|
+
* }
|
|
97
|
+
* ```
|
|
98
|
+
*
|
|
99
|
+
* #### Example #2: Wait for DOMContentLoaded event
|
|
100
|
+
*
|
|
101
|
+
* ```js
|
|
102
|
+
* {
|
|
103
|
+
* helpers: {
|
|
104
|
+
* Playwright : {
|
|
105
|
+
* url: "http://localhost",
|
|
106
|
+
* restart: false,
|
|
107
|
+
* waitForNavigation: "domcontentloaded",
|
|
108
|
+
* waitForAction: 500
|
|
109
|
+
* }
|
|
110
|
+
* }
|
|
111
|
+
* }
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* #### Example #3: Debug in window mode
|
|
115
|
+
*
|
|
116
|
+
* ```js
|
|
117
|
+
* {
|
|
118
|
+
* helpers: {
|
|
119
|
+
* Playwright : {
|
|
120
|
+
* url: "http://localhost",
|
|
121
|
+
* show: true
|
|
122
|
+
* }
|
|
123
|
+
* }
|
|
124
|
+
* }
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* #### Example #4: Connect to remote browser by specifying [websocket endpoint](https://chromedevtools.github.io/devtools-protocol/#how-do-i-access-the-browser-target)
|
|
128
|
+
*
|
|
129
|
+
* ```js
|
|
130
|
+
* {
|
|
131
|
+
* helpers: {
|
|
132
|
+
* Playwright: {
|
|
133
|
+
* url: "http://localhost",
|
|
134
|
+
* chromium: {
|
|
135
|
+
* browserWSEndpoint: "ws://localhost:9222/devtools/browser/c5aa6160-b5bc-4d53-bb49-6ecb36cd2e0a"
|
|
136
|
+
* }
|
|
137
|
+
* }
|
|
138
|
+
* }
|
|
139
|
+
* }
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* #### Example #5: Testing with Chromium extensions
|
|
143
|
+
*
|
|
144
|
+
* [official docs](https://github.com/microsoft/playwright/blob/v0.11.0/docs/api.md#working-with-chrome-extensions)
|
|
145
|
+
*
|
|
146
|
+
* ```js
|
|
147
|
+
* {
|
|
148
|
+
* helpers: {
|
|
149
|
+
* Playwright: {
|
|
150
|
+
* url: "http://localhost",
|
|
151
|
+
* show: true // headless mode not supported for extensions
|
|
152
|
+
* chromium: {
|
|
153
|
+
* args: [
|
|
154
|
+
* `--disable-extensions-except=${pathToExtension}`,
|
|
155
|
+
* `--load-extension=${pathToExtension}`
|
|
156
|
+
* ]
|
|
157
|
+
* }
|
|
158
|
+
* }
|
|
159
|
+
* }
|
|
160
|
+
* }
|
|
161
|
+
* ```
|
|
162
|
+
*
|
|
163
|
+
* #### Example #6: Lunach tests emulating iPhone 6
|
|
164
|
+
*
|
|
165
|
+
*
|
|
166
|
+
*
|
|
167
|
+
* ```js
|
|
168
|
+
* const { devices } = require('playwright');
|
|
169
|
+
*
|
|
170
|
+
* {
|
|
171
|
+
* helpers: {
|
|
172
|
+
* Playwright: {
|
|
173
|
+
* url: "http://localhost",
|
|
174
|
+
* emulate: devices['iPhone 6'],
|
|
175
|
+
* }
|
|
176
|
+
* }
|
|
177
|
+
* }
|
|
178
|
+
* ```
|
|
179
|
+
*
|
|
180
|
+
* Note: When connecting to remote browser `show` and specific `chrome` options (e.g. `headless` or `devtools`) are ignored.
|
|
181
|
+
*
|
|
182
|
+
* ## Access From Helpers
|
|
183
|
+
*
|
|
184
|
+
* Receive Playwright client from a custom helper by accessing `browser` for the Browser object or `page` for the current Page object:
|
|
185
|
+
*
|
|
186
|
+
* ```js
|
|
187
|
+
* const { browser } = this.helpers.Playwright;
|
|
188
|
+
* await browser.pages(); // List of pages in the browser
|
|
189
|
+
*
|
|
190
|
+
* // get current page
|
|
191
|
+
* const { page } = this.helpers.Playwright;
|
|
192
|
+
* await page.url(); // Get the url of the current page
|
|
193
|
+
*
|
|
194
|
+
* const { browserContext } = this.helpers.Playwright;
|
|
195
|
+
* await browserContext.cookies(); // get current browser context
|
|
196
|
+
* ```
|
|
197
|
+
*
|
|
198
|
+
* ## Methods
|
|
199
|
+
*/
|
|
200
|
+
class Playwright extends Helper {
|
|
201
|
+
constructor(config) {
|
|
202
|
+
super(config);
|
|
203
|
+
|
|
204
|
+
playwright = require('playwright');
|
|
205
|
+
|
|
206
|
+
// set defaults
|
|
207
|
+
this.isRemoteBrowser = false;
|
|
208
|
+
this.isRunning = false;
|
|
209
|
+
this.isAuthenticated = false;
|
|
210
|
+
this.sessionPages = {};
|
|
211
|
+
this.activeSessionName = '';
|
|
212
|
+
|
|
213
|
+
// override defaults with config
|
|
214
|
+
this._setConfig(config);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
_validateConfig(config) {
|
|
218
|
+
const defaults = {
|
|
219
|
+
// options to emulate context
|
|
220
|
+
emulate: {},
|
|
221
|
+
|
|
222
|
+
browser: 'chromium',
|
|
223
|
+
waitForAction: 100,
|
|
224
|
+
waitForTimeout: 1000,
|
|
225
|
+
pressKeyDelay: 10,
|
|
226
|
+
fullPageScreenshots: false,
|
|
227
|
+
disableScreenshots: false,
|
|
228
|
+
uniqueScreenshotNames: false,
|
|
229
|
+
manualStart: false,
|
|
230
|
+
getPageTimeout: 0,
|
|
231
|
+
waitForNavigation: 'load',
|
|
232
|
+
restart: false,
|
|
233
|
+
keepCookies: false,
|
|
234
|
+
keepBrowserState: false,
|
|
235
|
+
show: false,
|
|
236
|
+
defaultPopupAction: 'accept',
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
config = Object.assign(defaults, config);
|
|
240
|
+
|
|
241
|
+
if (availableBrowsers.indexOf(config.browser) < 0) {
|
|
242
|
+
throw new Error(`Invalid config. Can't use browser "${config.browser}". Accepted values: ${availableBrowsers.join(', ')}`);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return config;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
_getOptionsForBrowser(config) {
|
|
249
|
+
return config[config.browser] || {};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
_setConfig(config) {
|
|
253
|
+
this.options = this._validateConfig(config);
|
|
254
|
+
this.playwrightOptions = {
|
|
255
|
+
headless: !this.options.show,
|
|
256
|
+
...this._getOptionsForBrowser(config),
|
|
257
|
+
};
|
|
258
|
+
this.isRemoteBrowser = !!this.playwrightOptions.browserWSEndpoint;
|
|
259
|
+
popupStore.defaultAction = this.options.defaultPopupAction;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
static _config() {
|
|
263
|
+
return [
|
|
264
|
+
{ name: 'url', message: 'Base url of site to be tested', default: 'http://localhost' },
|
|
265
|
+
{
|
|
266
|
+
name: 'show', message: 'Show browser window', default: true, type: 'confirm',
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
name: 'browser',
|
|
270
|
+
message: 'Browser in which testing will be performed. Possible options: chromium, firefox or webkit',
|
|
271
|
+
default: 'chromium',
|
|
272
|
+
},
|
|
273
|
+
];
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
static _checkRequirements() {
|
|
277
|
+
try {
|
|
278
|
+
requireg('playwright');
|
|
279
|
+
} catch (e) {
|
|
280
|
+
return ['playwright@^0.12.1'];
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async _init() {
|
|
285
|
+
// register an internal selector engine for reading value property of elements in a selector
|
|
286
|
+
if (defaultSelectorEnginesInitialized) return;
|
|
287
|
+
defaultSelectorEnginesInitialized = true;
|
|
288
|
+
try {
|
|
289
|
+
await playwright.selectors.register('__value', createValueEngine);
|
|
290
|
+
await playwright.selectors.register('__disabled', createDisabledEngine);
|
|
291
|
+
} catch (e) {
|
|
292
|
+
console.warn(e);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
_beforeSuite() {
|
|
297
|
+
if (!this.options.restart && !this.options.manualStart && !this.isRunning) {
|
|
298
|
+
this.debugSection('Session', 'Starting singleton browser session');
|
|
299
|
+
return this._startBrowser();
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
async _before() {
|
|
305
|
+
recorder.retry({
|
|
306
|
+
retries: 5,
|
|
307
|
+
when: err => err.message.indexOf('context') > -1, // ignore context errors
|
|
308
|
+
});
|
|
309
|
+
if (this.options.restart && !this.options.manualStart) return this._startBrowser();
|
|
310
|
+
if (!this.isRunning && !this.options.manualStart) return this._startBrowser();
|
|
311
|
+
return this.browser;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
async _after() {
|
|
315
|
+
if (!this.isRunning) return;
|
|
316
|
+
|
|
317
|
+
// close other sessions
|
|
318
|
+
const contexts = await this.browser.contexts();
|
|
319
|
+
contexts.shift();
|
|
320
|
+
|
|
321
|
+
await Promise.all(contexts.map(c => c.close()));
|
|
322
|
+
|
|
323
|
+
if (this.options.restart) {
|
|
324
|
+
this.isRunning = false;
|
|
325
|
+
return this._stopBrowser();
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// ensure current page is in default context
|
|
329
|
+
if (this.page) {
|
|
330
|
+
const existingPages = await this.browserContext.pages();
|
|
331
|
+
await this._setPage(existingPages[0]);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (this.options.keepBrowserState) return;
|
|
335
|
+
|
|
336
|
+
if (!this.options.keepCookies) {
|
|
337
|
+
this.debugSection('Session', 'cleaning cookies and localStorage');
|
|
338
|
+
await this.clearCookie();
|
|
339
|
+
}
|
|
340
|
+
const currentUrl = await this.grabCurrentUrl();
|
|
341
|
+
|
|
342
|
+
if (currentUrl.startsWith('http')) {
|
|
343
|
+
await this.executeScript('localStorage.clear();').catch((err) => {
|
|
344
|
+
if (!(err.message.indexOf("Storage is disabled inside 'data:' URLs.") > -1)) throw err;
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
// await this.closeOtherTabs();
|
|
348
|
+
return this.browser;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
_afterSuite() {
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
_finishTest() {
|
|
355
|
+
if (!this.options.restart && this.isRunning) return this._stopBrowser();
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
_session() {
|
|
359
|
+
const defaultContext = this.browserContext;
|
|
360
|
+
return {
|
|
361
|
+
start: async (sessionName = '', config) => {
|
|
362
|
+
this.debugSection('New Context', config ? JSON.stringify(config) : 'opened');
|
|
363
|
+
this.activeSessionName = sessionName;
|
|
364
|
+
|
|
365
|
+
const bc = await this.browser.newContext(config);
|
|
366
|
+
const page = await bc.newPage();
|
|
367
|
+
targetCreatedHandler.call(this, page);
|
|
368
|
+
this._setPage(page);
|
|
369
|
+
// Create a new page inside context.
|
|
370
|
+
return bc;
|
|
371
|
+
},
|
|
372
|
+
stop: async (context) => {
|
|
373
|
+
// is closed by _after
|
|
374
|
+
},
|
|
375
|
+
loadVars: async (context) => {
|
|
376
|
+
this.browserContext = context;
|
|
377
|
+
const existingPages = await context.pages();
|
|
378
|
+
this.sessionPages[this.activeSessionName] = existingPages[0];
|
|
379
|
+
return this._setPage(this.sessionPages[this.activeSessionName]);
|
|
380
|
+
},
|
|
381
|
+
restoreVars: async (session) => {
|
|
382
|
+
this.withinLocator = null;
|
|
383
|
+
this.browserContext = defaultContext;
|
|
384
|
+
|
|
385
|
+
if (!session) {
|
|
386
|
+
this.activeSessionName = '';
|
|
387
|
+
} else {
|
|
388
|
+
this.activeSessionName = session;
|
|
389
|
+
}
|
|
390
|
+
const existingPages = await this.browserContext.pages();
|
|
391
|
+
await this._setPage(existingPages[0]);
|
|
392
|
+
|
|
393
|
+
return this._waitForAction();
|
|
394
|
+
},
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Set the automatic popup response to Accept.
|
|
401
|
+
* This must be set before a popup is triggered.
|
|
402
|
+
*
|
|
403
|
+
* ```js
|
|
404
|
+
* I.amAcceptingPopups();
|
|
405
|
+
* I.click('#triggerPopup');
|
|
406
|
+
* I.acceptPopup();
|
|
407
|
+
* ```
|
|
408
|
+
*/
|
|
409
|
+
amAcceptingPopups() {
|
|
410
|
+
popupStore.actionType = 'accept';
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Accepts the active JavaScript native popup window, as created by window.alert|window.confirm|window.prompt.
|
|
415
|
+
* Don't confuse popups with modal windows, as created by [various
|
|
416
|
+
* libraries](http://jster.net/category/windows-modals-popups).
|
|
417
|
+
*/
|
|
418
|
+
acceptPopup() {
|
|
419
|
+
popupStore.assertPopupActionType('accept');
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Set the automatic popup response to Cancel/Dismiss.
|
|
424
|
+
* This must be set before a popup is triggered.
|
|
425
|
+
*
|
|
426
|
+
* ```js
|
|
427
|
+
* I.amCancellingPopups();
|
|
428
|
+
* I.click('#triggerPopup');
|
|
429
|
+
* I.cancelPopup();
|
|
430
|
+
* ```
|
|
431
|
+
*/
|
|
432
|
+
amCancellingPopups() {
|
|
433
|
+
popupStore.actionType = 'cancel';
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Dismisses the active JavaScript popup, as created by window.alert|window.confirm|window.prompt.
|
|
438
|
+
*/
|
|
439
|
+
cancelPopup() {
|
|
440
|
+
popupStore.assertPopupActionType('cancel');
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* {{> seeInPopup }}
|
|
445
|
+
*/
|
|
446
|
+
async seeInPopup(text) {
|
|
447
|
+
popupStore.assertPopupVisible();
|
|
448
|
+
const popupText = await popupStore.popup.message();
|
|
449
|
+
stringIncludes('text in popup').assert(text, popupText);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Set current page
|
|
454
|
+
* @param {object} page page to set
|
|
455
|
+
*/
|
|
456
|
+
async _setPage(page) {
|
|
457
|
+
page = await page;
|
|
458
|
+
this._addPopupListener(page);
|
|
459
|
+
this.page = page;
|
|
460
|
+
if (!page) return;
|
|
461
|
+
page.setDefaultNavigationTimeout(this.options.getPageTimeout);
|
|
462
|
+
this.context = await this.page.$('body');
|
|
463
|
+
if (this.config.browser === 'chrome') {
|
|
464
|
+
await page.bringToFront();
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Add the 'dialog' event listener to a page
|
|
470
|
+
* @page {playwright.Page}
|
|
471
|
+
*
|
|
472
|
+
* The popup listener handles the dialog with the predefined action when it appears on the page.
|
|
473
|
+
* It also saves a reference to the object which is used in seeInPopup.
|
|
474
|
+
*/
|
|
475
|
+
_addPopupListener(page) {
|
|
476
|
+
if (!page) {
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
page.on('dialog', async (dialog) => {
|
|
480
|
+
popupStore.popup = dialog;
|
|
481
|
+
const action = popupStore.actionType || this.options.defaultPopupAction;
|
|
482
|
+
await this._waitForAction();
|
|
483
|
+
|
|
484
|
+
switch (action) {
|
|
485
|
+
case 'accept':
|
|
486
|
+
return dialog.accept();
|
|
487
|
+
|
|
488
|
+
case 'cancel':
|
|
489
|
+
return dialog.dismiss();
|
|
490
|
+
|
|
491
|
+
default: {
|
|
492
|
+
throw new Error('Unknown popup action type. Only "accept" or "cancel" are accepted');
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Gets page URL including hash.
|
|
500
|
+
*/
|
|
501
|
+
async _getPageUrl() {
|
|
502
|
+
return this.executeScript(() => window.location.href);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Grab the text within the popup. If no popup is visible then it will return null
|
|
507
|
+
*
|
|
508
|
+
* ```js
|
|
509
|
+
* await I.grabPopupText();
|
|
510
|
+
* ```
|
|
511
|
+
* @return {Promise<string | null>}
|
|
512
|
+
*/
|
|
513
|
+
async grabPopupText() {
|
|
514
|
+
if (popupStore.popup) {
|
|
515
|
+
return popupStore.popup.message();
|
|
516
|
+
}
|
|
517
|
+
return null;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
async _startBrowser() {
|
|
521
|
+
if (this.isRemoteBrowser) {
|
|
522
|
+
try {
|
|
523
|
+
this.browser = await playwright[this.options.browser].connect(this.playwrightOptions);
|
|
524
|
+
} catch (err) {
|
|
525
|
+
if (err.toString().indexOf('ECONNREFUSED')) {
|
|
526
|
+
throw new RemoteBrowserConnectionRefused(err);
|
|
527
|
+
}
|
|
528
|
+
throw err;
|
|
529
|
+
}
|
|
530
|
+
} else {
|
|
531
|
+
this.browser = await playwright[this.options.browser].launch(this.playwrightOptions);
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// works only for Chromium
|
|
535
|
+
this.browser.on('targetchanged', (target) => {
|
|
536
|
+
this.debugSection('Url', target.url());
|
|
537
|
+
});
|
|
538
|
+
this.browserContext = await this.browser.newContext({ acceptDownloads: true, ...this.options.emulate });
|
|
539
|
+
|
|
540
|
+
const existingPages = await this.browserContext.pages();
|
|
541
|
+
|
|
542
|
+
const mainPage = existingPages[0] || await this.browserContext.newPage();
|
|
543
|
+
targetCreatedHandler.call(this, mainPage);
|
|
544
|
+
|
|
545
|
+
await this._setPage(mainPage);
|
|
546
|
+
await this.closeOtherTabs();
|
|
547
|
+
|
|
548
|
+
this.isRunning = true;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
async _stopBrowser() {
|
|
552
|
+
this.withinLocator = null;
|
|
553
|
+
this._setPage(null);
|
|
554
|
+
this.context = null;
|
|
555
|
+
popupStore.clear();
|
|
556
|
+
|
|
557
|
+
if (this.isRemoteBrowser) {
|
|
558
|
+
await this.browser.disconnect();
|
|
559
|
+
} else {
|
|
560
|
+
await this.browser.close();
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
async _evaluateHandeInContext(...args) {
|
|
565
|
+
const context = await this._getContext();
|
|
566
|
+
return context.evaluateHandle(...args);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
|
|
570
|
+
async _withinBegin(locator) {
|
|
571
|
+
if (this.withinLocator) {
|
|
572
|
+
throw new Error('Can\'t start within block inside another within block');
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
const frame = isFrameLocator(locator);
|
|
576
|
+
|
|
577
|
+
if (frame) {
|
|
578
|
+
if (Array.isArray(frame)) {
|
|
579
|
+
await this.switchTo(null);
|
|
580
|
+
return frame.reduce((p, frameLocator) => p.then(() => this.switchTo(frameLocator)), Promise.resolve());
|
|
581
|
+
}
|
|
582
|
+
await this.switchTo(locator);
|
|
583
|
+
this.withinLocator = new Locator(locator);
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
const els = await this._locate(locator);
|
|
588
|
+
assertElementExists(els, locator);
|
|
589
|
+
this.context = els[0];
|
|
590
|
+
|
|
591
|
+
this.withinLocator = new Locator(locator);
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
async _withinEnd() {
|
|
595
|
+
this.withinLocator = null;
|
|
596
|
+
this.context = await this.page.mainFrame().$('body');
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
_extractDataFromPerformanceTiming(timing, ...dataNames) {
|
|
600
|
+
const navigationStart = timing.navigationStart;
|
|
601
|
+
|
|
602
|
+
const extractedData = {};
|
|
603
|
+
dataNames.forEach((name) => {
|
|
604
|
+
extractedData[name] = timing[name] - navigationStart;
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
return extractedData;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* {{> amOnPage }}
|
|
612
|
+
*/
|
|
613
|
+
async amOnPage(url) {
|
|
614
|
+
if (!(/^\w+\:\/\//.test(url))) {
|
|
615
|
+
url = this.options.url + url;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
if (this.config.basicAuth && (this.isAuthenticated !== true)) {
|
|
619
|
+
if (url.includes(this.options.url)) {
|
|
620
|
+
await this.browserContext.setHTTPCredentials(this.config.basicAuth);
|
|
621
|
+
this.isAuthenticated = true;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
await this.page.goto(url, { waitUntil: this.options.waitForNavigation });
|
|
626
|
+
|
|
627
|
+
const performanceTiming = JSON.parse(await this.page.evaluate(() => JSON.stringify(window.performance.timing)));
|
|
628
|
+
|
|
629
|
+
perfTiming = this._extractDataFromPerformanceTiming(
|
|
630
|
+
performanceTiming,
|
|
631
|
+
'responseEnd',
|
|
632
|
+
'domInteractive',
|
|
633
|
+
'domContentLoadedEventEnd',
|
|
634
|
+
'loadEventEnd',
|
|
635
|
+
);
|
|
636
|
+
|
|
637
|
+
return this._waitForAction();
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/**
|
|
641
|
+
* {{> resizeWindow }}
|
|
642
|
+
*
|
|
643
|
+
* Unlike other drivers Playwright changes the size of a viewport, not the window!
|
|
644
|
+
* Playwright does not control the window of a browser so it can't adjust its real size.
|
|
645
|
+
* It also can't maximize a window.
|
|
646
|
+
*
|
|
647
|
+
* Update configuration to change real window size on start:
|
|
648
|
+
*
|
|
649
|
+
* ```js
|
|
650
|
+
* // inside codecept.conf.js
|
|
651
|
+
* // @codeceptjs/configure package must be installed
|
|
652
|
+
* { setWindowSize } = require('@codeceptjs/configure');
|
|
653
|
+
* ````
|
|
654
|
+
*/
|
|
655
|
+
async resizeWindow(width, height) {
|
|
656
|
+
if (width === 'maximize') {
|
|
657
|
+
throw new Error('Playwright can\'t control windows, so it can\'t maximize it');
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
await this.page.setViewportSize({ width, height });
|
|
661
|
+
return this._waitForAction();
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* Set headers for all next requests
|
|
666
|
+
*
|
|
667
|
+
* ```js
|
|
668
|
+
* I.haveRequestHeaders({
|
|
669
|
+
* 'X-Sent-By': 'CodeceptJS',
|
|
670
|
+
* });
|
|
671
|
+
* ```
|
|
672
|
+
*
|
|
673
|
+
* @param {object} customHeaders headers to set
|
|
674
|
+
*/
|
|
675
|
+
async haveRequestHeaders(customHeaders) {
|
|
676
|
+
if (!customHeaders) {
|
|
677
|
+
throw new Error('Cannot send empty headers.');
|
|
678
|
+
}
|
|
679
|
+
return this.page.setExtraHTTPHeaders(customHeaders);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* {{> moveCursorTo }}
|
|
684
|
+
*
|
|
685
|
+
*/
|
|
686
|
+
async moveCursorTo(locator, offsetX = 0, offsetY = 0) {
|
|
687
|
+
const els = await this._locate(locator);
|
|
688
|
+
assertElementExists(els);
|
|
689
|
+
|
|
690
|
+
// Use manual mouse.move instead of .hover() so the offset can be added to the coordinates
|
|
691
|
+
const { x, y } = await els[0]._clickablePoint();
|
|
692
|
+
await this.page.mouse.move(x + offsetX, y + offsetY);
|
|
693
|
+
return this._waitForAction();
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* {{> dragAndDrop }}
|
|
698
|
+
*/
|
|
699
|
+
async dragAndDrop(srcElement, destElement) {
|
|
700
|
+
return proceedDragAndDrop.call(this, srcElement, destElement);
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* {{> refreshPage }}
|
|
705
|
+
*/
|
|
706
|
+
async refreshPage() {
|
|
707
|
+
return this.page.reload({ timeout: this.options.getPageTimeout, waitUntil: this.options.waitForNavigation });
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* {{> scrollPageToTop }}
|
|
712
|
+
*/
|
|
713
|
+
scrollPageToTop() {
|
|
714
|
+
return this.executeScript(() => {
|
|
715
|
+
window.scrollTo(0, 0);
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* {{> scrollPageToBottom }}
|
|
721
|
+
*/
|
|
722
|
+
scrollPageToBottom() {
|
|
723
|
+
return this.executeScript(() => {
|
|
724
|
+
const body = document.body;
|
|
725
|
+
const html = document.documentElement;
|
|
726
|
+
window.scrollTo(0, Math.max(
|
|
727
|
+
body.scrollHeight, body.offsetHeight,
|
|
728
|
+
html.clientHeight, html.scrollHeight, html.offsetHeight,
|
|
729
|
+
));
|
|
730
|
+
});
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* {{> scrollTo }}
|
|
735
|
+
*/
|
|
736
|
+
async scrollTo(locator, offsetX = 0, offsetY = 0) {
|
|
737
|
+
if (typeof locator === 'number' && typeof offsetX === 'number') {
|
|
738
|
+
offsetY = offsetX;
|
|
739
|
+
offsetX = locator;
|
|
740
|
+
locator = null;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
if (locator) {
|
|
744
|
+
const els = await this._locate(locator);
|
|
745
|
+
assertElementExists(els, locator, 'Element');
|
|
746
|
+
await els[0].scrollIntoViewIfNeeded();
|
|
747
|
+
const elementCoordinates = await els[0]._clickablePoint();
|
|
748
|
+
await this.executeScript((offsetX, offsetY) => window.scrollBy(offsetX, offsetY), { offsetX: elementCoordinates.x + offsetX, offsetY: elementCoordinates.y + offsetY });
|
|
749
|
+
} else {
|
|
750
|
+
await this.executeScript(({ offsetX, offsetY }) => window.scrollTo(offsetX, offsetY), { offsetX, offsetY });
|
|
751
|
+
}
|
|
752
|
+
return this._waitForAction();
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* {{> seeInTitle }}
|
|
757
|
+
*/
|
|
758
|
+
async seeInTitle(text) {
|
|
759
|
+
const title = await this.page.title();
|
|
760
|
+
stringIncludes('web page title').assert(text, title);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* {{> grabPageScrollPosition }}
|
|
765
|
+
*/
|
|
766
|
+
async grabPageScrollPosition() {
|
|
767
|
+
/* eslint-disable comma-dangle */
|
|
768
|
+
function getScrollPosition() {
|
|
769
|
+
return {
|
|
770
|
+
x: window.pageXOffset,
|
|
771
|
+
y: window.pageYOffset
|
|
772
|
+
};
|
|
773
|
+
}
|
|
774
|
+
/* eslint-enable comma-dangle */
|
|
775
|
+
return this.executeScript(getScrollPosition);
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
/**
|
|
779
|
+
* Checks that title is equal to provided one.
|
|
780
|
+
*
|
|
781
|
+
* ```js
|
|
782
|
+
* I.seeTitleEquals('Test title.');
|
|
783
|
+
* ```
|
|
784
|
+
*/
|
|
785
|
+
async seeTitleEquals(text) {
|
|
786
|
+
const title = await this.page.title();
|
|
787
|
+
return equals('web page title').assert(title, text);
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/**
|
|
791
|
+
* {{> dontSeeInTitle }}
|
|
792
|
+
*/
|
|
793
|
+
async dontSeeInTitle(text) {
|
|
794
|
+
const title = await this.page.title();
|
|
795
|
+
stringIncludes('web page title').negate(text, title);
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* {{> grabTitle }}
|
|
800
|
+
*/
|
|
801
|
+
async grabTitle() {
|
|
802
|
+
return this.page.title();
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* Get elements by different locator types, including strict locator
|
|
807
|
+
* Should be used in custom helpers:
|
|
808
|
+
*
|
|
809
|
+
* ```js
|
|
810
|
+
* const elements = await this.helpers['Playwright']._locate({name: 'password'});
|
|
811
|
+
* ```
|
|
812
|
+
*
|
|
813
|
+
*
|
|
814
|
+
*/
|
|
815
|
+
async _locate(locator) {
|
|
816
|
+
return findElements(await this.context, locator);
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
/**
|
|
820
|
+
* Find a checkbox by providing human readable text:
|
|
821
|
+
* NOTE: Assumes the checkable element exists
|
|
822
|
+
*
|
|
823
|
+
* ```js
|
|
824
|
+
* this.helpers['Playwright']._locateCheckable('I agree with terms and conditions').then // ...
|
|
825
|
+
* ```
|
|
826
|
+
*/
|
|
827
|
+
async _locateCheckable(locator, providedContext = null) {
|
|
828
|
+
const context = providedContext || await this._getContext();
|
|
829
|
+
const els = await findCheckable.call(this, locator, context);
|
|
830
|
+
assertElementExists(els[0], locator, 'Checkbox or radio');
|
|
831
|
+
return els[0];
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* Find a clickable element by providing human readable text:
|
|
836
|
+
*
|
|
837
|
+
* ```js
|
|
838
|
+
* this.helpers['Playwright']._locateClickable('Next page').then // ...
|
|
839
|
+
* ```
|
|
840
|
+
*/
|
|
841
|
+
async _locateClickable(locator) {
|
|
842
|
+
const context = await this._getContext();
|
|
843
|
+
return findClickable.call(this, context, locator);
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
/**
|
|
847
|
+
* Find field elements by providing human readable text:
|
|
848
|
+
*
|
|
849
|
+
* ```js
|
|
850
|
+
* this.helpers['Playwright']._locateFields('Your email').then // ...
|
|
851
|
+
* ```
|
|
852
|
+
*/
|
|
853
|
+
async _locateFields(locator) {
|
|
854
|
+
return findFields.call(this, locator);
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
/**
|
|
858
|
+
* Switch focus to a particular tab by its number. It waits tabs loading and then switch tab
|
|
859
|
+
*
|
|
860
|
+
* ```js
|
|
861
|
+
* I.switchToNextTab();
|
|
862
|
+
* I.switchToNextTab(2);
|
|
863
|
+
* ```
|
|
864
|
+
*
|
|
865
|
+
* @param {number} [num=1]
|
|
866
|
+
*/
|
|
867
|
+
async switchToNextTab(num = 1) {
|
|
868
|
+
const pages = await this.browserContext.pages();
|
|
869
|
+
|
|
870
|
+
const index = pages.indexOf(this.page);
|
|
871
|
+
this.withinLocator = null;
|
|
872
|
+
const page = pages[index + num];
|
|
873
|
+
|
|
874
|
+
if (!page) {
|
|
875
|
+
throw new Error(`There is no ability to switch to next tab with offset ${num}`);
|
|
876
|
+
}
|
|
877
|
+
await this._setPage(page);
|
|
878
|
+
return this._waitForAction();
|
|
879
|
+
}
|
|
880
|
+
|
|
881
|
+
/**
|
|
882
|
+
* Switch focus to a particular tab by its number. It waits tabs loading and then switch tab
|
|
883
|
+
*
|
|
884
|
+
* ```js
|
|
885
|
+
* I.switchToPreviousTab();
|
|
886
|
+
* I.switchToPreviousTab(2);
|
|
887
|
+
* ```
|
|
888
|
+
* @param {number} [num=1]
|
|
889
|
+
*/
|
|
890
|
+
async switchToPreviousTab(num = 1) {
|
|
891
|
+
const pages = await this.browserContext.pages();
|
|
892
|
+
const index = pages.indexOf(this.page);
|
|
893
|
+
this.withinLocator = null;
|
|
894
|
+
const page = pages[index - num];
|
|
895
|
+
|
|
896
|
+
if (!page) {
|
|
897
|
+
throw new Error(`There is no ability to switch to previous tab with offset ${num}`);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
await this._setPage(page);
|
|
901
|
+
return this._waitForAction();
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
/**
|
|
905
|
+
* Close current tab and switches to previous.
|
|
906
|
+
*
|
|
907
|
+
* ```js
|
|
908
|
+
* I.closeCurrentTab();
|
|
909
|
+
* ```
|
|
910
|
+
*/
|
|
911
|
+
async closeCurrentTab() {
|
|
912
|
+
const oldPage = this.page;
|
|
913
|
+
await this.switchToPreviousTab();
|
|
914
|
+
await oldPage.close();
|
|
915
|
+
return this._waitForAction();
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* Close all tabs except for the current one.
|
|
920
|
+
*
|
|
921
|
+
* ```js
|
|
922
|
+
* I.closeOtherTabs();
|
|
923
|
+
* ```
|
|
924
|
+
*/
|
|
925
|
+
async closeOtherTabs() {
|
|
926
|
+
const pages = await this.browserContext.pages();
|
|
927
|
+
const otherPages = pages.filter(page => page !== this.page);
|
|
928
|
+
if (otherPages.length) {
|
|
929
|
+
this.debug(`Closing ${otherPages.length} tabs`);
|
|
930
|
+
return Promise.all(otherPages.map(p => p.close()));
|
|
931
|
+
}
|
|
932
|
+
return Promise.resolve();
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
/**
|
|
936
|
+
* Open new tab and switch to it
|
|
937
|
+
*
|
|
938
|
+
* ```js
|
|
939
|
+
* I.openNewTab();
|
|
940
|
+
* ```
|
|
941
|
+
*
|
|
942
|
+
* You can pass in [page options](https://github.com/microsoft/playwright/blob/v0.12.1/docs/api.md#browsernewpageoptions) to emulate device on this page
|
|
943
|
+
*
|
|
944
|
+
* ```js
|
|
945
|
+
* // enable mobile
|
|
946
|
+
* I.openNewTab({ isMobile: true });
|
|
947
|
+
* ```
|
|
948
|
+
*/
|
|
949
|
+
async openNewTab(options) {
|
|
950
|
+
await this._setPage(await this.browserContext.newPage(options));
|
|
951
|
+
return this._waitForAction();
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
/**
|
|
955
|
+
* {{> grabNumberOfOpenTabs }}
|
|
956
|
+
*/
|
|
957
|
+
async grabNumberOfOpenTabs() {
|
|
958
|
+
const pages = await this.browserContext.pages();
|
|
959
|
+
return pages.length;
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
/**
|
|
963
|
+
* {{> seeElement }}
|
|
964
|
+
*
|
|
965
|
+
*/
|
|
966
|
+
async seeElement(locator) {
|
|
967
|
+
let els = await this._locate(locator);
|
|
968
|
+
els = await Promise.all(els.map(el => el.boundingBox()));
|
|
969
|
+
return empty('visible elements').negate(els.filter(v => v).fill('ELEMENT'));
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
/**
|
|
973
|
+
* {{> dontSeeElement }}
|
|
974
|
+
*
|
|
975
|
+
*/
|
|
976
|
+
async dontSeeElement(locator) {
|
|
977
|
+
let els = await this._locate(locator);
|
|
978
|
+
els = await Promise.all(els.map(el => el.boundingBox()));
|
|
979
|
+
return empty('visible elements').assert(els.filter(v => v).fill('ELEMENT'));
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
/**
|
|
983
|
+
* {{> seeElementInDOM }}
|
|
984
|
+
*/
|
|
985
|
+
async seeElementInDOM(locator) {
|
|
986
|
+
const els = await this._locate(locator);
|
|
987
|
+
return empty('elements on page').negate(els.filter(v => v).fill('ELEMENT'));
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
/**
|
|
991
|
+
* {{> dontSeeElementInDOM }}
|
|
992
|
+
*/
|
|
993
|
+
async dontSeeElementInDOM(locator) {
|
|
994
|
+
const els = await this._locate(locator);
|
|
995
|
+
return empty('elements on a page').assert(els.filter(v => v).fill('ELEMENT'));
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
/**
|
|
999
|
+
* Handles a file download.Aa file name is required to save the file on disk.
|
|
1000
|
+
* Files are saved to "output" directory.
|
|
1001
|
+
*
|
|
1002
|
+
* Should be used with [FileSystem helper](https://codecept.io/helpers/FileSystem) to check that file were downloaded correctly.
|
|
1003
|
+
*
|
|
1004
|
+
* ```js
|
|
1005
|
+
* I.handleDownloads('downloads/avatar.jpg');
|
|
1006
|
+
* I.click('Download Avatar');
|
|
1007
|
+
* I.amInPath('output/downloads');
|
|
1008
|
+
* I.waitForFile('downloads/avatar.jpg', 5);
|
|
1009
|
+
*
|
|
1010
|
+
* ```
|
|
1011
|
+
*
|
|
1012
|
+
* @param {string} [fileName] set filename for downloaded file
|
|
1013
|
+
*/
|
|
1014
|
+
async handleDownloads(fileName = 'downloads') {
|
|
1015
|
+
this.page.waitForEvent('download').then(async (download) => {
|
|
1016
|
+
const filePath = await download.path();
|
|
1017
|
+
const downloadPath = path.join(global.output_dir, fileName || path.basename(filePath));
|
|
1018
|
+
if (!fs.existsSync(path.dirname(downloadPath))) {
|
|
1019
|
+
fs.mkdirSync(path.dirname(downloadPath), '0777');
|
|
1020
|
+
}
|
|
1021
|
+
fs.copyFileSync(filePath, downloadPath);
|
|
1022
|
+
this.debug('Download completed');
|
|
1023
|
+
this.debugSection('Downloaded From', await download.url());
|
|
1024
|
+
this.debugSection('Downloaded To', downloadPath);
|
|
1025
|
+
});
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
/**
|
|
1029
|
+
* {{> click }}
|
|
1030
|
+
*
|
|
1031
|
+
*
|
|
1032
|
+
*/
|
|
1033
|
+
async click(locator, context = null) {
|
|
1034
|
+
return proceedClick.call(this, locator, context);
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
/**
|
|
1038
|
+
* Clicks link and waits for navigation (deprecated)
|
|
1039
|
+
*/
|
|
1040
|
+
async clickLink(locator, context = null) {
|
|
1041
|
+
console.log('clickLink deprecated: Playwright automatically waits for navigation to happen.');
|
|
1042
|
+
console.log('Replace I.clickLink with I.click');
|
|
1043
|
+
return this.click(locator, context);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
/**
|
|
1047
|
+
*
|
|
1048
|
+
* Force clicks an element without waiting for it to become visible and not animating.
|
|
1049
|
+
*
|
|
1050
|
+
* ```js
|
|
1051
|
+
* I.forceClick('#hiddenButton');
|
|
1052
|
+
* I.forceClick('Click me', '#hidden');
|
|
1053
|
+
* ```
|
|
1054
|
+
*
|
|
1055
|
+
*/
|
|
1056
|
+
async forceClick(locator, context = null) {
|
|
1057
|
+
return proceedClick.call(this, locator, context, { force: true });
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
/**
|
|
1062
|
+
* {{> doubleClick }}
|
|
1063
|
+
*
|
|
1064
|
+
*
|
|
1065
|
+
*/
|
|
1066
|
+
async doubleClick(locator, context = null) {
|
|
1067
|
+
return proceedClick.call(this, locator, context, { clickCount: 2 });
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
/**
|
|
1071
|
+
* {{> rightClick }}
|
|
1072
|
+
*
|
|
1073
|
+
*
|
|
1074
|
+
*/
|
|
1075
|
+
async rightClick(locator, context = null) {
|
|
1076
|
+
return proceedClick.call(this, locator, context, { button: 'right' });
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
/**
|
|
1080
|
+
* {{> checkOption }}
|
|
1081
|
+
*/
|
|
1082
|
+
async checkOption(field, context = null) {
|
|
1083
|
+
const elm = await this._locateCheckable(field, context);
|
|
1084
|
+
const curentlyChecked = await elm.getProperty('checked')
|
|
1085
|
+
.then(checkedProperty => checkedProperty.jsonValue());
|
|
1086
|
+
// Only check if NOT currently checked
|
|
1087
|
+
if (!curentlyChecked) {
|
|
1088
|
+
await elm.click();
|
|
1089
|
+
return this._waitForAction();
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
/**
|
|
1094
|
+
* {{> uncheckOption }}
|
|
1095
|
+
*/
|
|
1096
|
+
async uncheckOption(field, context = null) {
|
|
1097
|
+
const elm = await this._locateCheckable(field, context);
|
|
1098
|
+
const curentlyChecked = await elm.getProperty('checked')
|
|
1099
|
+
.then(checkedProperty => checkedProperty.jsonValue());
|
|
1100
|
+
// Only uncheck if currently checked
|
|
1101
|
+
if (curentlyChecked) {
|
|
1102
|
+
await elm.click();
|
|
1103
|
+
return this._waitForAction();
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
/**
|
|
1108
|
+
* {{> seeCheckboxIsChecked }}
|
|
1109
|
+
*/
|
|
1110
|
+
async seeCheckboxIsChecked(field) {
|
|
1111
|
+
return proceedIsChecked.call(this, 'assert', field);
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
/**
|
|
1115
|
+
* {{> dontSeeCheckboxIsChecked }}
|
|
1116
|
+
*/
|
|
1117
|
+
async dontSeeCheckboxIsChecked(field) {
|
|
1118
|
+
return proceedIsChecked.call(this, 'negate', field);
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
/**
|
|
1122
|
+
* {{> pressKeyDown }}
|
|
1123
|
+
*/
|
|
1124
|
+
async pressKeyDown(key) {
|
|
1125
|
+
key = getNormalizedKey.call(this, key);
|
|
1126
|
+
await this.page.keyboard.down(key);
|
|
1127
|
+
return this._waitForAction();
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
/**
|
|
1131
|
+
* {{> pressKeyUp }}
|
|
1132
|
+
*/
|
|
1133
|
+
async pressKeyUp(key) {
|
|
1134
|
+
key = getNormalizedKey.call(this, key);
|
|
1135
|
+
await this.page.keyboard.up(key);
|
|
1136
|
+
return this._waitForAction();
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
/**
|
|
1140
|
+
* {{> pressKeyWithKeyNormalization }}
|
|
1141
|
+
*
|
|
1142
|
+
* _Note:_ Shortcuts like `'Meta'` + `'A'` do not work on macOS ([GoogleChrome/Playwright#1313](https://github.com/GoogleChrome/Playwright/issues/1313)).
|
|
1143
|
+
*/
|
|
1144
|
+
async pressKey(key) {
|
|
1145
|
+
const modifiers = [];
|
|
1146
|
+
if (Array.isArray(key)) {
|
|
1147
|
+
for (let k of key) {
|
|
1148
|
+
k = getNormalizedKey.call(this, k);
|
|
1149
|
+
if (isModifierKey(k)) {
|
|
1150
|
+
modifiers.push(k);
|
|
1151
|
+
} else {
|
|
1152
|
+
key = k;
|
|
1153
|
+
break;
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
} else {
|
|
1157
|
+
key = getNormalizedKey.call(this, key);
|
|
1158
|
+
}
|
|
1159
|
+
for (const modifier of modifiers) {
|
|
1160
|
+
await this.page.keyboard.down(modifier);
|
|
1161
|
+
}
|
|
1162
|
+
await this.page.keyboard.press(key);
|
|
1163
|
+
for (const modifier of modifiers) {
|
|
1164
|
+
await this.page.keyboard.up(modifier);
|
|
1165
|
+
}
|
|
1166
|
+
return this._waitForAction();
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
/**
|
|
1170
|
+
* {{> fillField }}
|
|
1171
|
+
*
|
|
1172
|
+
*/
|
|
1173
|
+
async fillField(field, value) {
|
|
1174
|
+
const els = await findFields.call(this, field);
|
|
1175
|
+
assertElementExists(els, field, 'Field');
|
|
1176
|
+
const el = els[0];
|
|
1177
|
+
const tag = await el.getProperty('tagName').then(el => el.jsonValue());
|
|
1178
|
+
const editable = await el.getProperty('contenteditable').then(el => el.jsonValue());
|
|
1179
|
+
if (tag === 'INPUT' || tag === 'TEXTAREA') {
|
|
1180
|
+
await this._evaluateHandeInContext(el => el.value = '', el);
|
|
1181
|
+
} else if (editable) {
|
|
1182
|
+
await this._evaluateHandeInContext(el => el.innerHTML = '', el);
|
|
1183
|
+
}
|
|
1184
|
+
await el.type(value.toString(), { delay: this.options.pressKeyDelay });
|
|
1185
|
+
return this._waitForAction();
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
/**
|
|
1190
|
+
* {{> clearField }}
|
|
1191
|
+
*/
|
|
1192
|
+
async clearField(field) {
|
|
1193
|
+
return this.fillField(field, '');
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
/**
|
|
1197
|
+
* {{> appendField }}
|
|
1198
|
+
*
|
|
1199
|
+
*
|
|
1200
|
+
*/
|
|
1201
|
+
async appendField(field, value) {
|
|
1202
|
+
const els = await findFields.call(this, field);
|
|
1203
|
+
assertElementExists(els, field, 'Field');
|
|
1204
|
+
await els[0].press('End');
|
|
1205
|
+
await els[0].type(value, { delay: this.options.pressKeyDelay });
|
|
1206
|
+
return this._waitForAction();
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
/**
|
|
1210
|
+
* {{> seeInField }}
|
|
1211
|
+
*/
|
|
1212
|
+
async seeInField(field, value) {
|
|
1213
|
+
return proceedSeeInField.call(this, 'assert', field, value);
|
|
1214
|
+
}
|
|
1215
|
+
|
|
1216
|
+
/**
|
|
1217
|
+
* {{> dontSeeInField }}
|
|
1218
|
+
*/
|
|
1219
|
+
async dontSeeInField(field, value) {
|
|
1220
|
+
return proceedSeeInField.call(this, 'negate', field, value);
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
/**
|
|
1225
|
+
* {{> attachFile }}
|
|
1226
|
+
*
|
|
1227
|
+
*/
|
|
1228
|
+
async attachFile(locator, pathToFile) {
|
|
1229
|
+
const file = path.join(global.codecept_dir, pathToFile);
|
|
1230
|
+
|
|
1231
|
+
if (!fileExists(file)) {
|
|
1232
|
+
throw new Error(`File at ${file} can not be found on local system`);
|
|
1233
|
+
}
|
|
1234
|
+
const els = await findFields.call(this, locator);
|
|
1235
|
+
assertElementExists(els, 'Field');
|
|
1236
|
+
await els[0].setInputFiles(file);
|
|
1237
|
+
return this._waitForAction();
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
/**
|
|
1241
|
+
* {{> selectOption }}
|
|
1242
|
+
*/
|
|
1243
|
+
async selectOption(select, option) {
|
|
1244
|
+
const els = await findFields.call(this, select);
|
|
1245
|
+
assertElementExists(els, select, 'Selectable field');
|
|
1246
|
+
const el = els[0];
|
|
1247
|
+
if (await el.getProperty('tagName').then(t => t.jsonValue()) !== 'SELECT') {
|
|
1248
|
+
throw new Error('Element is not <select>');
|
|
1249
|
+
}
|
|
1250
|
+
if (!Array.isArray(option)) option = [option];
|
|
1251
|
+
|
|
1252
|
+
for (const key in option) {
|
|
1253
|
+
const opt = xpathLocator.literal(option[key]);
|
|
1254
|
+
let optEl = await findElements.call(this, el, { xpath: Locator.select.byVisibleText(opt) });
|
|
1255
|
+
if (optEl.length) {
|
|
1256
|
+
this._evaluateHandeInContext(el => el.selected = true, optEl[0]);
|
|
1257
|
+
continue;
|
|
1258
|
+
}
|
|
1259
|
+
optEl = await findElements.call(this, el, { xpath: Locator.select.byValue(opt) });
|
|
1260
|
+
if (optEl.length) {
|
|
1261
|
+
this._evaluateHandeInContext(el => el.selected = true, optEl[0]);
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
await this._evaluateHandeInContext((element) => {
|
|
1265
|
+
element.dispatchEvent(new Event('input', { bubbles: true }));
|
|
1266
|
+
element.dispatchEvent(new Event('change', { bubbles: true }));
|
|
1267
|
+
}, el);
|
|
1268
|
+
|
|
1269
|
+
return this._waitForAction();
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
/**
|
|
1273
|
+
* {{> grabNumberOfVisibleElements }}
|
|
1274
|
+
*
|
|
1275
|
+
*/
|
|
1276
|
+
async grabNumberOfVisibleElements(locator) {
|
|
1277
|
+
let els = await this._locate(locator);
|
|
1278
|
+
els = await Promise.all(els.map(el => el.boundingBox()));
|
|
1279
|
+
return els.filter(v => v).length;
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
/**
|
|
1283
|
+
* {{> seeInCurrentUrl }}
|
|
1284
|
+
*/
|
|
1285
|
+
async seeInCurrentUrl(url) {
|
|
1286
|
+
stringIncludes('url').assert(url, await this._getPageUrl());
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
/**
|
|
1290
|
+
* {{> dontSeeInCurrentUrl }}
|
|
1291
|
+
*/
|
|
1292
|
+
async dontSeeInCurrentUrl(url) {
|
|
1293
|
+
stringIncludes('url').negate(url, await this._getPageUrl());
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
/**
|
|
1297
|
+
* {{> seeCurrentUrlEquals }}
|
|
1298
|
+
*/
|
|
1299
|
+
async seeCurrentUrlEquals(url) {
|
|
1300
|
+
urlEquals(this.options.url).assert(url, await this._getPageUrl());
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
/**
|
|
1304
|
+
* {{> dontSeeCurrentUrlEquals }}
|
|
1305
|
+
*/
|
|
1306
|
+
async dontSeeCurrentUrlEquals(url) {
|
|
1307
|
+
urlEquals(this.options.url).negate(url, await this._getPageUrl());
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
/**
|
|
1311
|
+
* {{> see }}
|
|
1312
|
+
*
|
|
1313
|
+
*
|
|
1314
|
+
*/
|
|
1315
|
+
async see(text, context = null) {
|
|
1316
|
+
return proceedSee.call(this, 'assert', text, context);
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
/**
|
|
1320
|
+
* {{> seeTextEquals }}
|
|
1321
|
+
*/
|
|
1322
|
+
async seeTextEquals(text, context = null) {
|
|
1323
|
+
return proceedSee.call(this, 'assert', text, context, true);
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
/**
|
|
1327
|
+
* {{> dontSee }}
|
|
1328
|
+
*
|
|
1329
|
+
*
|
|
1330
|
+
*/
|
|
1331
|
+
async dontSee(text, context = null) {
|
|
1332
|
+
return proceedSee.call(this, 'negate', text, context);
|
|
1333
|
+
}
|
|
1334
|
+
|
|
1335
|
+
/**
|
|
1336
|
+
* {{> grabSource }}
|
|
1337
|
+
*/
|
|
1338
|
+
async grabSource() {
|
|
1339
|
+
return this.page.content();
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
/**
|
|
1343
|
+
* Get JS log from browser.
|
|
1344
|
+
*
|
|
1345
|
+
* ```js
|
|
1346
|
+
* let logs = await I.grabBrowserLogs();
|
|
1347
|
+
* console.log(JSON.stringify(logs))
|
|
1348
|
+
* ```
|
|
1349
|
+
* @return {Promise<any[]>}
|
|
1350
|
+
*/
|
|
1351
|
+
async grabBrowserLogs() {
|
|
1352
|
+
const logs = consoleLogStore.entries;
|
|
1353
|
+
consoleLogStore.clear();
|
|
1354
|
+
return logs;
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
/**
|
|
1358
|
+
* {{> grabCurrentUrl }}
|
|
1359
|
+
*/
|
|
1360
|
+
async grabCurrentUrl() {
|
|
1361
|
+
return this._getPageUrl();
|
|
1362
|
+
}
|
|
1363
|
+
|
|
1364
|
+
/**
|
|
1365
|
+
* {{> seeInSource }}
|
|
1366
|
+
*/
|
|
1367
|
+
async seeInSource(text) {
|
|
1368
|
+
const source = await this.page.content();
|
|
1369
|
+
stringIncludes('HTML source of a page').assert(text, source);
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
/**
|
|
1373
|
+
* {{> dontSeeInSource }}
|
|
1374
|
+
*/
|
|
1375
|
+
async dontSeeInSource(text) {
|
|
1376
|
+
const source = await this.page.content();
|
|
1377
|
+
stringIncludes('HTML source of a page').negate(text, source);
|
|
1378
|
+
}
|
|
1379
|
+
|
|
1380
|
+
|
|
1381
|
+
/**
|
|
1382
|
+
* {{> seeNumberOfElements }}
|
|
1383
|
+
*
|
|
1384
|
+
*
|
|
1385
|
+
*/
|
|
1386
|
+
async seeNumberOfElements(locator, num) {
|
|
1387
|
+
const elements = await this._locate(locator);
|
|
1388
|
+
return equals(`expected number of elements (${locator}) is ${num}, but found ${elements.length}`).assert(elements.length, num);
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
/**
|
|
1392
|
+
* {{> seeNumberOfVisibleElements }}
|
|
1393
|
+
*
|
|
1394
|
+
*
|
|
1395
|
+
*/
|
|
1396
|
+
async seeNumberOfVisibleElements(locator, num) {
|
|
1397
|
+
const res = await this.grabNumberOfVisibleElements(locator);
|
|
1398
|
+
return equals(`expected number of visible elements (${locator}) is ${num}, but found ${res}`).assert(res, num);
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
/**
|
|
1402
|
+
* {{> setCookie }}
|
|
1403
|
+
*/
|
|
1404
|
+
async setCookie(cookie) {
|
|
1405
|
+
if (Array.isArray(cookie)) {
|
|
1406
|
+
return this.browserContext.addCookies(...cookie);
|
|
1407
|
+
}
|
|
1408
|
+
return this.browserContext.addCookies([cookie]);
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
/**
|
|
1412
|
+
* {{> seeCookie }}
|
|
1413
|
+
*
|
|
1414
|
+
*/
|
|
1415
|
+
async seeCookie(name) {
|
|
1416
|
+
const cookies = await this.browserContext.cookies();
|
|
1417
|
+
empty(`cookie ${name} to be set`).negate(cookies.filter(c => c.name === name));
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
/**
|
|
1421
|
+
* {{> dontSeeCookie }}
|
|
1422
|
+
*/
|
|
1423
|
+
async dontSeeCookie(name) {
|
|
1424
|
+
const cookies = await this.browserContext.cookies();
|
|
1425
|
+
empty(`cookie ${name} to be set`).assert(cookies.filter(c => c.name === name));
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
/**
|
|
1429
|
+
* {{> grabCookie }}
|
|
1430
|
+
*
|
|
1431
|
+
* Returns cookie in JSON format. If name not passed returns all cookies for this domain.
|
|
1432
|
+
*/
|
|
1433
|
+
async grabCookie(name) {
|
|
1434
|
+
const cookies = await this.browserContext.cookies();
|
|
1435
|
+
if (!name) return cookies;
|
|
1436
|
+
const cookie = cookies.filter(c => c.name === name);
|
|
1437
|
+
if (cookie[0]) return cookie[0];
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
/**
|
|
1441
|
+
* {{> clearCookie }}
|
|
1442
|
+
*/
|
|
1443
|
+
async clearCookie() {
|
|
1444
|
+
// Playwright currently doesn't support to delete a certain cookie
|
|
1445
|
+
// https://github.com/microsoft/playwright/blob/master/docs/api.md#class-browsercontext
|
|
1446
|
+
return this.browserContext.clearCookies();
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
/**
|
|
1450
|
+
* Executes a script on the page:
|
|
1451
|
+
*
|
|
1452
|
+
* ```js
|
|
1453
|
+
* I.executeScript(() => window.alert('Hello world'));
|
|
1454
|
+
* ```
|
|
1455
|
+
*
|
|
1456
|
+
* Additional parameters of the function can be passed as an object argument:
|
|
1457
|
+
*
|
|
1458
|
+
* ```js
|
|
1459
|
+
* I.executeScript(({x, y}) => x + y, {x, y});
|
|
1460
|
+
* ```
|
|
1461
|
+
* You can pass only one parameter into a function
|
|
1462
|
+
* but you can pass in array or object.
|
|
1463
|
+
*
|
|
1464
|
+
* ```js
|
|
1465
|
+
* I.executeScript(([x, y]) => x + y, [x, y]);
|
|
1466
|
+
* ```
|
|
1467
|
+
* If a function returns a Promise it will wait for its resolution.
|
|
1468
|
+
*/
|
|
1469
|
+
async executeScript(fn, arg) {
|
|
1470
|
+
let context = this.page;
|
|
1471
|
+
if (this.context && this.context.constructor.name === 'Frame') {
|
|
1472
|
+
context = this.context; // switching to iframe context
|
|
1473
|
+
}
|
|
1474
|
+
return context.evaluate.apply(context, [fn, arg]);
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
/**
|
|
1478
|
+
* {{> grabTextFrom }}
|
|
1479
|
+
*
|
|
1480
|
+
*/
|
|
1481
|
+
async grabTextFrom(locator) {
|
|
1482
|
+
const els = await this._locate(locator);
|
|
1483
|
+
assertElementExists(els, locator);
|
|
1484
|
+
const texts = [];
|
|
1485
|
+
for (const el of els) {
|
|
1486
|
+
texts.push(await (await el.getProperty('innerText')).jsonValue());
|
|
1487
|
+
}
|
|
1488
|
+
if (texts.length === 1) return texts[0];
|
|
1489
|
+
return texts;
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
/**
|
|
1493
|
+
* {{> grabValueFrom }}
|
|
1494
|
+
*/
|
|
1495
|
+
async grabValueFrom(locator) {
|
|
1496
|
+
const els = await findFields.call(this, locator);
|
|
1497
|
+
assertElementExists(els, locator);
|
|
1498
|
+
return els[0].getProperty('value').then(t => t.jsonValue());
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
/**
|
|
1502
|
+
* {{> grabHTMLFrom }}
|
|
1503
|
+
*/
|
|
1504
|
+
async grabHTMLFrom(locator) {
|
|
1505
|
+
const els = await this._locate(locator);
|
|
1506
|
+
assertElementExists(els, locator);
|
|
1507
|
+
const values = await Promise.all(els.map(el => el.$eval('xpath=.', element => element.innerHTML, el)));
|
|
1508
|
+
if (Array.isArray(values) && values.length === 1) {
|
|
1509
|
+
return values[0];
|
|
1510
|
+
}
|
|
1511
|
+
return values;
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
/**
|
|
1515
|
+
* {{> grabCssPropertyFrom }}
|
|
1516
|
+
*
|
|
1517
|
+
*/
|
|
1518
|
+
async grabCssPropertyFrom(locator, cssProperty) {
|
|
1519
|
+
const els = await this._locate(locator);
|
|
1520
|
+
const res = await Promise.all(els.map(el => el.$eval('xpath=.', el => JSON.parse(JSON.stringify(getComputedStyle(el))), el)));
|
|
1521
|
+
const cssValues = res.map(props => props[toCamelCase(cssProperty)]);
|
|
1522
|
+
|
|
1523
|
+
if (res.length > 0) {
|
|
1524
|
+
return cssValues;
|
|
1525
|
+
}
|
|
1526
|
+
return cssValues[0];
|
|
1527
|
+
}
|
|
1528
|
+
|
|
1529
|
+
/**
|
|
1530
|
+
* {{> seeCssPropertiesOnElements }}
|
|
1531
|
+
*
|
|
1532
|
+
*/
|
|
1533
|
+
async seeCssPropertiesOnElements(locator, cssProperties) {
|
|
1534
|
+
const res = await this._locate(locator);
|
|
1535
|
+
assertElementExists(res, locator);
|
|
1536
|
+
|
|
1537
|
+
const cssPropertiesCamelCase = convertCssPropertiesToCamelCase(cssProperties);
|
|
1538
|
+
const elemAmount = res.length;
|
|
1539
|
+
const commands = [];
|
|
1540
|
+
res.forEach((el) => {
|
|
1541
|
+
Object.keys(cssPropertiesCamelCase).forEach((prop) => {
|
|
1542
|
+
commands.push(el.$eval('xpath=.', (el) => {
|
|
1543
|
+
const style = window.getComputedStyle ? getComputedStyle(el) : el.currentStyle;
|
|
1544
|
+
return JSON.parse(JSON.stringify(style));
|
|
1545
|
+
}, el)
|
|
1546
|
+
.then((props) => {
|
|
1547
|
+
if (isColorProperty(prop)) {
|
|
1548
|
+
return convertColorToRGBA(props[prop]);
|
|
1549
|
+
}
|
|
1550
|
+
return props[prop];
|
|
1551
|
+
}));
|
|
1552
|
+
});
|
|
1553
|
+
});
|
|
1554
|
+
let props = await Promise.all(commands);
|
|
1555
|
+
const values = Object.keys(cssPropertiesCamelCase).map(key => cssPropertiesCamelCase[key]);
|
|
1556
|
+
if (!Array.isArray(props)) props = [props];
|
|
1557
|
+
let chunked = chunkArray(props, values.length);
|
|
1558
|
+
chunked = chunked.filter((val) => {
|
|
1559
|
+
for (let i = 0; i < val.length; ++i) {
|
|
1560
|
+
if (val[i] !== values[i]) return false;
|
|
1561
|
+
}
|
|
1562
|
+
return true;
|
|
1563
|
+
});
|
|
1564
|
+
return equals(`all elements (${locator}) to have CSS property ${JSON.stringify(cssProperties)}`).assert(chunked.length, elemAmount);
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
/**
|
|
1568
|
+
* {{> seeAttributesOnElements }}
|
|
1569
|
+
*
|
|
1570
|
+
*/
|
|
1571
|
+
async seeAttributesOnElements(locator, attributes) {
|
|
1572
|
+
const res = await this._locate(locator);
|
|
1573
|
+
assertElementExists(res, locator);
|
|
1574
|
+
|
|
1575
|
+
const elemAmount = res.length;
|
|
1576
|
+
const commands = [];
|
|
1577
|
+
res.forEach((el) => {
|
|
1578
|
+
Object.keys(attributes).forEach((prop) => {
|
|
1579
|
+
commands.push(el
|
|
1580
|
+
.$eval('xpath=.', (el, attr) => el[attr] || el.getAttribute(attr), prop));
|
|
1581
|
+
});
|
|
1582
|
+
});
|
|
1583
|
+
let attrs = await Promise.all(commands);
|
|
1584
|
+
const values = Object.keys(attributes).map(key => attributes[key]);
|
|
1585
|
+
if (!Array.isArray(attrs)) attrs = [attrs];
|
|
1586
|
+
let chunked = chunkArray(attrs, values.length);
|
|
1587
|
+
chunked = chunked.filter((val) => {
|
|
1588
|
+
for (let i = 0; i < val.length; ++i) {
|
|
1589
|
+
if (val[i] !== values[i]) return false;
|
|
1590
|
+
}
|
|
1591
|
+
return true;
|
|
1592
|
+
});
|
|
1593
|
+
return equals(`all elements (${locator}) to have attributes ${JSON.stringify(attributes)}`).assert(chunked.length, elemAmount);
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
/**
|
|
1597
|
+
* {{> dragSlider }}
|
|
1598
|
+
*
|
|
1599
|
+
*/
|
|
1600
|
+
async dragSlider(locator, offsetX = 0) {
|
|
1601
|
+
const src = await this._locate(locator);
|
|
1602
|
+
assertElementExists(src, locator, 'Slider Element');
|
|
1603
|
+
|
|
1604
|
+
// Note: Using private api ._clickablePoint because the .BoundingBox does not take into account iframe offsets!
|
|
1605
|
+
const sliderSource = await src[0]._clickablePoint();
|
|
1606
|
+
|
|
1607
|
+
// Drag start point
|
|
1608
|
+
await this.page.mouse.move(sliderSource.x, sliderSource.y, { steps: 5 });
|
|
1609
|
+
await this.page.mouse.down();
|
|
1610
|
+
|
|
1611
|
+
// Drag destination
|
|
1612
|
+
await this.page.mouse.move(sliderSource.x + offsetX, sliderSource.y, { steps: 5 });
|
|
1613
|
+
await this.page.mouse.up();
|
|
1614
|
+
|
|
1615
|
+
return this._waitForAction();
|
|
1616
|
+
}
|
|
1617
|
+
|
|
1618
|
+
/**
|
|
1619
|
+
* {{> grabAttributeFrom }}
|
|
1620
|
+
*
|
|
1621
|
+
*/
|
|
1622
|
+
async grabAttributeFrom(locator, attr) {
|
|
1623
|
+
const els = await this._locate(locator);
|
|
1624
|
+
assertElementExists(els, locator);
|
|
1625
|
+
const array = [];
|
|
1626
|
+
|
|
1627
|
+
for (let index = 0; index < els.length; index++) {
|
|
1628
|
+
const a = await this._evaluateHandeInContext(([el, attr]) => el[attr] || el.getAttribute(attr), [els[index], attr]);
|
|
1629
|
+
array.push(await a.jsonValue());
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
return array.length === 1 ? array[0] : array;
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
/**
|
|
1636
|
+
* {{> saveScreenshot }}
|
|
1637
|
+
*/
|
|
1638
|
+
async saveScreenshot(fileName, fullPage) {
|
|
1639
|
+
const fullPageOption = fullPage || this.options.fullPageScreenshots;
|
|
1640
|
+
const outputFile = screenshotOutputFolder(fileName);
|
|
1641
|
+
|
|
1642
|
+
this.debug(`Screenshot is saving to ${outputFile}`);
|
|
1643
|
+
|
|
1644
|
+
if (this.activeSessionName) {
|
|
1645
|
+
const activeSessionPage = this.sessionPages[this.activeSessionName];
|
|
1646
|
+
|
|
1647
|
+
if (activeSessionPage) {
|
|
1648
|
+
return activeSessionPage.screenshot({
|
|
1649
|
+
path: outputFile,
|
|
1650
|
+
fullPage: fullPageOption,
|
|
1651
|
+
type: 'png',
|
|
1652
|
+
});
|
|
1653
|
+
}
|
|
1654
|
+
}
|
|
1655
|
+
|
|
1656
|
+
return this.page.screenshot({ path: outputFile, fullPage: fullPageOption, type: 'png' });
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
async _failed(test) {
|
|
1660
|
+
await this._withinEnd();
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
/**
|
|
1664
|
+
* {{> wait }}
|
|
1665
|
+
*/
|
|
1666
|
+
async wait(sec) {
|
|
1667
|
+
return new Promise(((done) => {
|
|
1668
|
+
setTimeout(done, sec * 1000);
|
|
1669
|
+
}));
|
|
1670
|
+
}
|
|
1671
|
+
|
|
1672
|
+
/**
|
|
1673
|
+
* {{> waitForEnabled }}
|
|
1674
|
+
*/
|
|
1675
|
+
async waitForEnabled(locator, sec) {
|
|
1676
|
+
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
|
|
1677
|
+
locator = new Locator(locator, 'css');
|
|
1678
|
+
const matcher = await this.context;
|
|
1679
|
+
let waiter;
|
|
1680
|
+
const context = await this._getContext();
|
|
1681
|
+
if (!locator.isXPath()) {
|
|
1682
|
+
// playwright combined selectors
|
|
1683
|
+
waiter = context.waitForSelector(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()} >> __disabled=false`, { timeout: waitTimeout });
|
|
1684
|
+
} else {
|
|
1685
|
+
const enabledFn = function ([locator, $XPath]) {
|
|
1686
|
+
eval($XPath); // eslint-disable-line no-eval
|
|
1687
|
+
return $XPath(null, locator).filter(el => !el.disabled).length > 0;
|
|
1688
|
+
};
|
|
1689
|
+
waiter = context.waitForFunction(enabledFn, [locator.value, $XPath.toString()], { timeout: waitTimeout });
|
|
1690
|
+
}
|
|
1691
|
+
return waiter.catch((err) => {
|
|
1692
|
+
throw new Error(`element (${locator.toString()}) still not enabled after ${waitTimeout / 1000} sec\n${err.message}`);
|
|
1693
|
+
});
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1696
|
+
/**
|
|
1697
|
+
* {{> waitForValue }}
|
|
1698
|
+
*/
|
|
1699
|
+
async waitForValue(field, value, sec) {
|
|
1700
|
+
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
|
|
1701
|
+
const locator = new Locator(field, 'css');
|
|
1702
|
+
const matcher = await this.context;
|
|
1703
|
+
let waiter;
|
|
1704
|
+
const context = await this._getContext();
|
|
1705
|
+
if (!locator.isXPath()) {
|
|
1706
|
+
// uses a custom selector engine for finding value properties on elements
|
|
1707
|
+
waiter = context.waitForSelector(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()} >> __value=${value}`, { timeout: waitTimeout, waitFor: 'visible' });
|
|
1708
|
+
} else {
|
|
1709
|
+
const valueFn = function ([locator, $XPath, value]) {
|
|
1710
|
+
eval($XPath); // eslint-disable-line no-eval
|
|
1711
|
+
return $XPath(null, locator).filter(el => (el.value || '').indexOf(value) !== -1).length > 0;
|
|
1712
|
+
};
|
|
1713
|
+
waiter = context.waitForFunction(valueFn, [locator.value, $XPath.toString(), value], { timeout: waitTimeout });
|
|
1714
|
+
}
|
|
1715
|
+
return waiter.catch((err) => {
|
|
1716
|
+
const loc = locator.toString();
|
|
1717
|
+
throw new Error(`element (${loc}) is not in DOM or there is no element(${loc}) with value "${value}" after ${waitTimeout / 1000} sec\n${err.message}`);
|
|
1718
|
+
});
|
|
1719
|
+
}
|
|
1720
|
+
|
|
1721
|
+
/**
|
|
1722
|
+
* {{> waitNumberOfVisibleElements }}
|
|
1723
|
+
*
|
|
1724
|
+
*/
|
|
1725
|
+
async waitNumberOfVisibleElements(locator, num, sec) {
|
|
1726
|
+
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
|
|
1727
|
+
locator = new Locator(locator, 'css');
|
|
1728
|
+
const matcher = await this.context;
|
|
1729
|
+
let waiter;
|
|
1730
|
+
const context = await this._getContext();
|
|
1731
|
+
if (locator.isCSS()) {
|
|
1732
|
+
const visibleFn = function ([locator, num]) {
|
|
1733
|
+
const els = document.querySelectorAll(locator);
|
|
1734
|
+
if (!els || els.length === 0) {
|
|
1735
|
+
return false;
|
|
1736
|
+
}
|
|
1737
|
+
return Array.prototype.filter.call(els, el => el.offsetParent !== null).length === num;
|
|
1738
|
+
};
|
|
1739
|
+
waiter = context.waitForFunction(visibleFn, [locator.value, num], { timeout: waitTimeout });
|
|
1740
|
+
} else {
|
|
1741
|
+
const visibleFn = function ([locator, $XPath, num]) {
|
|
1742
|
+
eval($XPath); // eslint-disable-line no-eval
|
|
1743
|
+
return $XPath(null, locator).filter(el => el.offsetParent !== null).length === num;
|
|
1744
|
+
};
|
|
1745
|
+
waiter = context.waitForFunction(visibleFn, [locator.value, $XPath.toString(), num], { timeout: waitTimeout });
|
|
1746
|
+
}
|
|
1747
|
+
return waiter.catch((err) => {
|
|
1748
|
+
throw new Error(`The number of elements (${locator.toString()}) is not ${num} after ${waitTimeout / 1000} sec\n${err.message}`);
|
|
1749
|
+
});
|
|
1750
|
+
}
|
|
1751
|
+
|
|
1752
|
+
/**
|
|
1753
|
+
* {{> waitForClickable }}
|
|
1754
|
+
*/
|
|
1755
|
+
async waitForClickable(locator, waitTimeout) {
|
|
1756
|
+
console.log('I.waitForClickable is DEPRECATED: This is no longer needed, Playwright automatically waits for element to be clikable');
|
|
1757
|
+
console.log('Remove usage of this function');
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
/**
|
|
1761
|
+
* {{> waitForElement }}
|
|
1762
|
+
*
|
|
1763
|
+
*/
|
|
1764
|
+
async waitForElement(locator, sec) {
|
|
1765
|
+
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
|
|
1766
|
+
locator = new Locator(locator, 'css');
|
|
1767
|
+
|
|
1768
|
+
const context = await this._getContext();
|
|
1769
|
+
const waiter = context.waitForSelector(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()}`, { timeout: waitTimeout });
|
|
1770
|
+
return waiter.catch((err) => {
|
|
1771
|
+
throw new Error(`element (${locator.toString()}) still not present on page after ${waitTimeout / 1000} sec\n${err.message}`);
|
|
1772
|
+
});
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
/**
|
|
1776
|
+
* {{> waitForVisible }}
|
|
1777
|
+
*
|
|
1778
|
+
* This method accepts [React selectors](https://codecept.io/react).
|
|
1779
|
+
*/
|
|
1780
|
+
async waitForVisible(locator, sec) {
|
|
1781
|
+
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
|
|
1782
|
+
locator = new Locator(locator, 'css');
|
|
1783
|
+
const context = await this._getContext();
|
|
1784
|
+
const waiter = context.waitForSelector(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()}`, { timeout: waitTimeout, waitFor: 'visible' });
|
|
1785
|
+
return waiter.catch((err) => {
|
|
1786
|
+
throw new Error(`element (${locator.toString()}) still not visible after ${waitTimeout / 1000} sec\n${err.message}`);
|
|
1787
|
+
});
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
/**
|
|
1791
|
+
* {{> waitForInvisible }}
|
|
1792
|
+
*/
|
|
1793
|
+
async waitForInvisible(locator, sec) {
|
|
1794
|
+
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
|
|
1795
|
+
locator = new Locator(locator, 'css');
|
|
1796
|
+
const context = await this._getContext();
|
|
1797
|
+
const waiter = context.waitForSelector(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()}`, { timeout: waitTimeout, waitFor: 'hidden' });
|
|
1798
|
+
return waiter.catch((err) => {
|
|
1799
|
+
throw new Error(`element (${locator.toString()}) still visible after ${waitTimeout / 1000} sec\n${err.message}`);
|
|
1800
|
+
});
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1803
|
+
/**
|
|
1804
|
+
* {{> waitToHide }}
|
|
1805
|
+
*/
|
|
1806
|
+
async waitToHide(locator, sec) {
|
|
1807
|
+
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
|
|
1808
|
+
locator = new Locator(locator, 'css');
|
|
1809
|
+
const context = await this._getContext();
|
|
1810
|
+
return context.waitForSelector(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()}`, { timeout: waitTimeout, waitFor: 'hidden' }).catch((err) => {
|
|
1811
|
+
throw new Error(`element (${locator.toString()}) still not hidden after ${waitTimeout / 1000} sec\n${err.message}`);
|
|
1812
|
+
});
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
async _getContext() {
|
|
1816
|
+
if (this.context && this.context.constructor.name === 'Frame') {
|
|
1817
|
+
return this.context;
|
|
1818
|
+
}
|
|
1819
|
+
return this.page;
|
|
1820
|
+
}
|
|
1821
|
+
|
|
1822
|
+
/**
|
|
1823
|
+
* {{> waitInUrl }}
|
|
1824
|
+
*/
|
|
1825
|
+
async waitInUrl(urlPart, sec = null) {
|
|
1826
|
+
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
|
|
1827
|
+
|
|
1828
|
+
return this.page.waitForFunction((urlPart) => {
|
|
1829
|
+
const currUrl = decodeURIComponent(decodeURIComponent(decodeURIComponent(window.location.href)));
|
|
1830
|
+
return currUrl.indexOf(urlPart) > -1;
|
|
1831
|
+
}, urlPart, { timeout: waitTimeout }).catch(async (e) => {
|
|
1832
|
+
const currUrl = await this._getPageUrl(); // Required because the waitForFunction can't return data.
|
|
1833
|
+
if (/failed: timeout/i.test(e.message)) {
|
|
1834
|
+
throw new Error(`expected url to include ${urlPart}, but found ${currUrl}`);
|
|
1835
|
+
} else {
|
|
1836
|
+
throw e;
|
|
1837
|
+
}
|
|
1838
|
+
});
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
/**
|
|
1842
|
+
* {{> waitUrlEquals }}
|
|
1843
|
+
*/
|
|
1844
|
+
async waitUrlEquals(urlPart, sec = null) {
|
|
1845
|
+
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
|
|
1846
|
+
|
|
1847
|
+
const baseUrl = this.options.url;
|
|
1848
|
+
if (urlPart.indexOf('http') < 0) {
|
|
1849
|
+
urlPart = baseUrl + urlPart;
|
|
1850
|
+
}
|
|
1851
|
+
|
|
1852
|
+
return this.page.waitForFunction((urlPart) => {
|
|
1853
|
+
const currUrl = decodeURIComponent(decodeURIComponent(decodeURIComponent(window.location.href)));
|
|
1854
|
+
return currUrl.indexOf(urlPart) > -1;
|
|
1855
|
+
}, urlPart, { timeout: waitTimeout }).catch(async (e) => {
|
|
1856
|
+
const currUrl = await this._getPageUrl(); // Required because the waitForFunction can't return data.
|
|
1857
|
+
if (/failed: timeout/i.test(e.message)) {
|
|
1858
|
+
throw new Error(`expected url to be ${urlPart}, but found ${currUrl}`);
|
|
1859
|
+
} else {
|
|
1860
|
+
throw e;
|
|
1861
|
+
}
|
|
1862
|
+
});
|
|
1863
|
+
}
|
|
1864
|
+
|
|
1865
|
+
/**
|
|
1866
|
+
* {{> waitForText }}
|
|
1867
|
+
*/
|
|
1868
|
+
async waitForText(text, sec = null, context = null) {
|
|
1869
|
+
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
|
|
1870
|
+
let waiter;
|
|
1871
|
+
|
|
1872
|
+
const contextObject = await this._getContext();
|
|
1873
|
+
|
|
1874
|
+
if (context) {
|
|
1875
|
+
const locator = new Locator(context, 'css');
|
|
1876
|
+
if (!locator.isXPath()) {
|
|
1877
|
+
waiter = contextObject.waitFor(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()} >> text=${text}`, { timeout: waitTimeout, waitFor: 'visible' });
|
|
1878
|
+
}
|
|
1879
|
+
|
|
1880
|
+
if (locator.isXPath()) {
|
|
1881
|
+
waiter = contextObject.waitForFunction(([locator, text, $XPath]) => {
|
|
1882
|
+
eval($XPath); // eslint-disable-line no-eval
|
|
1883
|
+
const el = $XPath(null, locator);
|
|
1884
|
+
if (!el.length) return false;
|
|
1885
|
+
return el[0].innerText.indexOf(text) > -1;
|
|
1886
|
+
}, [locator.value, text, $XPath.toString()], { timeout: waitTimeout });
|
|
1887
|
+
}
|
|
1888
|
+
} else {
|
|
1889
|
+
waiter = contextObject.waitForFunction(text => document.body && document.body.innerText.indexOf(text) > -1, text, { timeout: waitTimeout });
|
|
1890
|
+
}
|
|
1891
|
+
return waiter.catch((err) => {
|
|
1892
|
+
throw new Error(`Text "${text}" was not found on page after ${waitTimeout / 1000} sec\n${err.message}`);
|
|
1893
|
+
});
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
/**
|
|
1897
|
+
* Waits for a network request.
|
|
1898
|
+
*
|
|
1899
|
+
* ```js
|
|
1900
|
+
* I.waitForRequest('http://example.com/resource');
|
|
1901
|
+
* I.waitForRequest(request => request.url() === 'http://example.com' && request.method() === 'GET');
|
|
1902
|
+
* ```
|
|
1903
|
+
*
|
|
1904
|
+
* @param {string|function} urlOrPredicate
|
|
1905
|
+
* @param {?number} [sec=null] seconds to wait
|
|
1906
|
+
*/
|
|
1907
|
+
async waitForRequest(urlOrPredicate, sec = null) {
|
|
1908
|
+
const timeout = sec ? sec * 1000 : this.options.waitForTimeout;
|
|
1909
|
+
return this.page.waitForRequest(urlOrPredicate, { timeout });
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
/**
|
|
1913
|
+
* Waits for a network request.
|
|
1914
|
+
*
|
|
1915
|
+
* ```js
|
|
1916
|
+
* I.waitForResponse('http://example.com/resource');
|
|
1917
|
+
* I.waitForResponse(request => request.url() === 'http://example.com' && request.method() === 'GET');
|
|
1918
|
+
* ```
|
|
1919
|
+
*
|
|
1920
|
+
* @param {string|function} urlOrPredicate
|
|
1921
|
+
* @param {?number} [sec=null] number of seconds to wait
|
|
1922
|
+
*/
|
|
1923
|
+
async waitForResponse(urlOrPredicate, sec = null) {
|
|
1924
|
+
const timeout = sec ? sec * 1000 : this.options.waitForTimeout;
|
|
1925
|
+
return this.page.waitForResponse(urlOrPredicate, { timeout });
|
|
1926
|
+
}
|
|
1927
|
+
|
|
1928
|
+
/**
|
|
1929
|
+
* {{> switchTo }}
|
|
1930
|
+
*/
|
|
1931
|
+
async switchTo(locator) {
|
|
1932
|
+
if (Number.isInteger(locator)) {
|
|
1933
|
+
// Select by frame index of current context
|
|
1934
|
+
|
|
1935
|
+
let childFrames = null;
|
|
1936
|
+
if (this.context && typeof this.context.childFrames === 'function') {
|
|
1937
|
+
childFrames = this.context.childFrames();
|
|
1938
|
+
} else {
|
|
1939
|
+
childFrames = this.page.mainFrame().childFrames();
|
|
1940
|
+
}
|
|
1941
|
+
|
|
1942
|
+
if (locator >= 0 && locator < childFrames.length) {
|
|
1943
|
+
this.context = childFrames[locator];
|
|
1944
|
+
} else {
|
|
1945
|
+
throw new Error('Element #invalidIframeSelector was not found by text|CSS|XPath');
|
|
1946
|
+
}
|
|
1947
|
+
return;
|
|
1948
|
+
}
|
|
1949
|
+
if (!locator) {
|
|
1950
|
+
this.context = await this.page.mainFrame().$('body');
|
|
1951
|
+
return;
|
|
1952
|
+
}
|
|
1953
|
+
|
|
1954
|
+
// iframe by selector
|
|
1955
|
+
const els = await this._locate(locator);
|
|
1956
|
+
assertElementExists(els, locator);
|
|
1957
|
+
const contentFrame = await els[0].contentFrame();
|
|
1958
|
+
|
|
1959
|
+
if (contentFrame) {
|
|
1960
|
+
this.context = contentFrame;
|
|
1961
|
+
} else {
|
|
1962
|
+
this.context = els[0];
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
|
|
1966
|
+
/**
|
|
1967
|
+
* {{> waitForFunction }}
|
|
1968
|
+
*/
|
|
1969
|
+
async waitForFunction(fn, argsOrSec = null, sec = null) {
|
|
1970
|
+
let args = [];
|
|
1971
|
+
if (argsOrSec) {
|
|
1972
|
+
if (Array.isArray(argsOrSec)) {
|
|
1973
|
+
args = argsOrSec;
|
|
1974
|
+
} else if (typeof argsOrSec === 'number') {
|
|
1975
|
+
sec = argsOrSec;
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
|
|
1979
|
+
const context = await this._getContext();
|
|
1980
|
+
return context.waitForFunction(fn, args, { timeout: waitTimeout });
|
|
1981
|
+
}
|
|
1982
|
+
|
|
1983
|
+
/**
|
|
1984
|
+
* Waits for navigation to finish. By default takes configured `waitForNavigation` option.
|
|
1985
|
+
*
|
|
1986
|
+
* See [Pupeteer's reference](https://github.com/GoogleChrome/Playwright/blob/master/docs/api.md#pagewaitfornavigationoptions)
|
|
1987
|
+
*
|
|
1988
|
+
* @param {*} opts
|
|
1989
|
+
*/
|
|
1990
|
+
async waitForNavigation(opts = {}) {
|
|
1991
|
+
opts = {
|
|
1992
|
+
timeout: this.options.getPageTimeout,
|
|
1993
|
+
waitUntil: this.options.waitForNavigation,
|
|
1994
|
+
...opts,
|
|
1995
|
+
};
|
|
1996
|
+
return this.page.waitForNavigation(opts);
|
|
1997
|
+
}
|
|
1998
|
+
|
|
1999
|
+
/**
|
|
2000
|
+
* {{> waitUntil }}
|
|
2001
|
+
*/
|
|
2002
|
+
async waitUntil(fn, sec = null) {
|
|
2003
|
+
console.log('This method will remove in CodeceptJS 1.4; use `waitForFunction` instead!');
|
|
2004
|
+
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
|
|
2005
|
+
const context = await this._getContext();
|
|
2006
|
+
return context.waitForFunction(fn, { timeout: waitTimeout });
|
|
2007
|
+
}
|
|
2008
|
+
|
|
2009
|
+
async waitUntilExists(locator, sec) {
|
|
2010
|
+
console.log(`waitUntilExists deprecated:
|
|
2011
|
+
* use 'waitForElement' to wait for element to be attached
|
|
2012
|
+
* use 'waitForDetached to wait for element to be removed'`);
|
|
2013
|
+
return this.waitForDetached(locator, sec);
|
|
2014
|
+
}
|
|
2015
|
+
|
|
2016
|
+
/**
|
|
2017
|
+
* {{> waitForDetached }}
|
|
2018
|
+
*/
|
|
2019
|
+
async waitForDetached(locator, sec) {
|
|
2020
|
+
const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
|
|
2021
|
+
locator = new Locator(locator, 'css');
|
|
2022
|
+
|
|
2023
|
+
let waiter;
|
|
2024
|
+
const context = await this._getContext();
|
|
2025
|
+
if (!locator.isXPath()) {
|
|
2026
|
+
waiter = context.waitForSelector(`${locator.isCustom() ? `${locator.type}=${locator.value}` : locator.simplify()}`, { timeout: waitTimeout, waitFor: 'detached' });
|
|
2027
|
+
} else {
|
|
2028
|
+
const visibleFn = function ([locator, $XPath]) {
|
|
2029
|
+
eval($XPath); // eslint-disable-line no-eval
|
|
2030
|
+
return $XPath(null, locator).length === 0;
|
|
2031
|
+
};
|
|
2032
|
+
waiter = context.waitForFunction(visibleFn, [locator.value, $XPath.toString()], { timeout: waitTimeout });
|
|
2033
|
+
}
|
|
2034
|
+
return waiter.catch((err) => {
|
|
2035
|
+
throw new Error(`element (${locator.toString()}) still on page after ${waitTimeout / 1000} sec\n${err.message}`);
|
|
2036
|
+
});
|
|
2037
|
+
}
|
|
2038
|
+
|
|
2039
|
+
async _waitForAction() {
|
|
2040
|
+
return this.wait(this.options.waitForAction / 1000);
|
|
2041
|
+
}
|
|
2042
|
+
|
|
2043
|
+
/**
|
|
2044
|
+
* {{> grabDataFromPerformanceTiming }}
|
|
2045
|
+
*/
|
|
2046
|
+
async grabDataFromPerformanceTiming() {
|
|
2047
|
+
return perfTiming;
|
|
2048
|
+
}
|
|
2049
|
+
|
|
2050
|
+
/**
|
|
2051
|
+
* {{> grabElementBoundingRect }}
|
|
2052
|
+
*/
|
|
2053
|
+
async grabElementBoundingRect(locator, prop) {
|
|
2054
|
+
const els = await this._locate(locator);
|
|
2055
|
+
assertElementExists(els, locator);
|
|
2056
|
+
const rect = await els[0].boundingBox();
|
|
2057
|
+
if (prop) return rect[prop];
|
|
2058
|
+
return rect;
|
|
2059
|
+
}
|
|
2060
|
+
}
|
|
2061
|
+
|
|
2062
|
+
module.exports = Playwright;
|
|
2063
|
+
|
|
2064
|
+
async function findElements(matcher, locator) {
|
|
2065
|
+
locator = new Locator(locator, 'css');
|
|
2066
|
+
if (locator.isCustom()) {
|
|
2067
|
+
return matcher.$$(`${locator.type}=${locator.value}`);
|
|
2068
|
+
} if (!locator.isXPath()) {
|
|
2069
|
+
return matcher.$$(locator.simplify());
|
|
2070
|
+
}
|
|
2071
|
+
return matcher.$$(`xpath=${locator.value}`);
|
|
2072
|
+
}
|
|
2073
|
+
|
|
2074
|
+
async function proceedClick(locator, context = null, options = {}) {
|
|
2075
|
+
let matcher = await this.context;
|
|
2076
|
+
if (context) {
|
|
2077
|
+
const els = await this._locate(context);
|
|
2078
|
+
assertElementExists(els, context);
|
|
2079
|
+
matcher = els[0];
|
|
2080
|
+
}
|
|
2081
|
+
const els = await findClickable.call(this, matcher, locator);
|
|
2082
|
+
if (context) {
|
|
2083
|
+
assertElementExists(els, locator, 'Clickable element', `was not found inside element ${new Locator(context).toString()}`);
|
|
2084
|
+
} else {
|
|
2085
|
+
assertElementExists(els, locator, 'Clickable element');
|
|
2086
|
+
}
|
|
2087
|
+
await els[0].click(options);
|
|
2088
|
+
const promises = [];
|
|
2089
|
+
if (options.waitForNavigation) {
|
|
2090
|
+
promises.push(this.waitForNavigation());
|
|
2091
|
+
}
|
|
2092
|
+
promises.push(this._waitForAction());
|
|
2093
|
+
return Promise.all(promises);
|
|
2094
|
+
}
|
|
2095
|
+
|
|
2096
|
+
async function findClickable(matcher, locator) {
|
|
2097
|
+
locator = new Locator(locator);
|
|
2098
|
+
if (!locator.isFuzzy()) return findElements.call(this, matcher, locator);
|
|
2099
|
+
|
|
2100
|
+
let els;
|
|
2101
|
+
const literal = xpathLocator.literal(locator.value);
|
|
2102
|
+
|
|
2103
|
+
els = await findElements.call(this, matcher, Locator.clickable.narrow(literal));
|
|
2104
|
+
if (els.length) return els;
|
|
2105
|
+
|
|
2106
|
+
els = await findElements.call(this, matcher, Locator.clickable.wide(literal));
|
|
2107
|
+
if (els.length) return els;
|
|
2108
|
+
|
|
2109
|
+
try {
|
|
2110
|
+
els = await findElements.call(this, matcher, Locator.clickable.self(literal));
|
|
2111
|
+
if (els.length) return els;
|
|
2112
|
+
} catch (err) {
|
|
2113
|
+
// Do nothing
|
|
2114
|
+
}
|
|
2115
|
+
|
|
2116
|
+
return findElements.call(this, matcher, locator.value); // by css or xpath
|
|
2117
|
+
}
|
|
2118
|
+
|
|
2119
|
+
async function proceedSee(assertType, text, context, strict = false) {
|
|
2120
|
+
let description;
|
|
2121
|
+
let allText;
|
|
2122
|
+
if (!context) {
|
|
2123
|
+
let el = await this.context;
|
|
2124
|
+
|
|
2125
|
+
if (el && !el.getProperty) {
|
|
2126
|
+
// Fallback to body
|
|
2127
|
+
el = await this.context.$('body');
|
|
2128
|
+
}
|
|
2129
|
+
|
|
2130
|
+
allText = [await el.getProperty('innerText').then(p => p.jsonValue())];
|
|
2131
|
+
description = 'web application';
|
|
2132
|
+
} else {
|
|
2133
|
+
const locator = new Locator(context, 'css');
|
|
2134
|
+
description = `element ${locator.toString()}`;
|
|
2135
|
+
const els = await this._locate(locator);
|
|
2136
|
+
assertElementExists(els, locator.toString());
|
|
2137
|
+
allText = await Promise.all(els.map(el => el.getProperty('innerText').then(p => p.jsonValue())));
|
|
2138
|
+
}
|
|
2139
|
+
|
|
2140
|
+
if (strict) {
|
|
2141
|
+
return allText.map(elText => equals(description)[assertType](text, elText));
|
|
2142
|
+
}
|
|
2143
|
+
return stringIncludes(description)[assertType](text, allText.join(' | '));
|
|
2144
|
+
}
|
|
2145
|
+
|
|
2146
|
+
async function findCheckable(locator, context) {
|
|
2147
|
+
let contextEl = await this.context;
|
|
2148
|
+
if (typeof context === 'string') {
|
|
2149
|
+
contextEl = await findElements.call(this, contextEl, (new Locator(context, 'css')).simplify());
|
|
2150
|
+
contextEl = contextEl[0];
|
|
2151
|
+
}
|
|
2152
|
+
|
|
2153
|
+
const matchedLocator = new Locator(locator);
|
|
2154
|
+
if (!matchedLocator.isFuzzy()) {
|
|
2155
|
+
return findElements.call(this, contextEl, matchedLocator.simplify());
|
|
2156
|
+
}
|
|
2157
|
+
|
|
2158
|
+
const literal = xpathLocator.literal(locator);
|
|
2159
|
+
let els = await findElements.call(this, contextEl, Locator.checkable.byText(literal));
|
|
2160
|
+
if (els.length) {
|
|
2161
|
+
return els;
|
|
2162
|
+
}
|
|
2163
|
+
els = await findElements.call(this, contextEl, Locator.checkable.byName(literal));
|
|
2164
|
+
if (els.length) {
|
|
2165
|
+
return els;
|
|
2166
|
+
}
|
|
2167
|
+
return findElements.call(this, contextEl, locator);
|
|
2168
|
+
}
|
|
2169
|
+
|
|
2170
|
+
async function proceedIsChecked(assertType, option) {
|
|
2171
|
+
let els = await findCheckable.call(this, option);
|
|
2172
|
+
assertElementExists(els, option, 'Checkable');
|
|
2173
|
+
els = await Promise.all(els.map(el => el.getProperty('checked')));
|
|
2174
|
+
els = await Promise.all(els.map(el => el.jsonValue()));
|
|
2175
|
+
const selected = els.reduce((prev, cur) => prev || cur);
|
|
2176
|
+
return truth(`checkable ${option}`, 'to be checked')[assertType](selected);
|
|
2177
|
+
}
|
|
2178
|
+
|
|
2179
|
+
async function findFields(locator) {
|
|
2180
|
+
const matchedLocator = new Locator(locator);
|
|
2181
|
+
if (!matchedLocator.isFuzzy()) {
|
|
2182
|
+
return this._locate(matchedLocator);
|
|
2183
|
+
}
|
|
2184
|
+
const literal = xpathLocator.literal(locator);
|
|
2185
|
+
|
|
2186
|
+
let els = await this._locate({ xpath: Locator.field.labelEquals(literal) });
|
|
2187
|
+
if (els.length) {
|
|
2188
|
+
return els;
|
|
2189
|
+
}
|
|
2190
|
+
|
|
2191
|
+
els = await this._locate({ xpath: Locator.field.labelContains(literal) });
|
|
2192
|
+
if (els.length) {
|
|
2193
|
+
return els;
|
|
2194
|
+
}
|
|
2195
|
+
els = await this._locate({ xpath: Locator.field.byName(literal) });
|
|
2196
|
+
if (els.length) {
|
|
2197
|
+
return els;
|
|
2198
|
+
}
|
|
2199
|
+
return this._locate({ css: locator });
|
|
2200
|
+
}
|
|
2201
|
+
|
|
2202
|
+
async function proceedDragAndDrop(sourceLocator, destinationLocator, options = {}) {
|
|
2203
|
+
const src = await this._locate(sourceLocator);
|
|
2204
|
+
assertElementExists(src, sourceLocator, 'Source Element');
|
|
2205
|
+
|
|
2206
|
+
const dst = await this._locate(destinationLocator);
|
|
2207
|
+
assertElementExists(dst, destinationLocator, 'Destination Element');
|
|
2208
|
+
|
|
2209
|
+
// Note: Using private api ._clickablePoint becaues the .BoundingBox does not take into account iframe offsets!
|
|
2210
|
+
const dragSource = await src[0]._clickablePoint();
|
|
2211
|
+
const dragDestination = await dst[0]._clickablePoint();
|
|
2212
|
+
|
|
2213
|
+
// Drag start point
|
|
2214
|
+
await this.page.mouse.move(dragSource.x, dragSource.y, { steps: 5 });
|
|
2215
|
+
await this.page.mouse.down();
|
|
2216
|
+
|
|
2217
|
+
// Drag destination
|
|
2218
|
+
await this.page.mouse.move(dragDestination.x, dragDestination.y, { steps: 5 });
|
|
2219
|
+
await this.page.mouse.up();
|
|
2220
|
+
await this._waitForAction();
|
|
2221
|
+
}
|
|
2222
|
+
|
|
2223
|
+
async function proceedSeeInField(assertType, field, value) {
|
|
2224
|
+
const els = await findFields.call(this, field);
|
|
2225
|
+
assertElementExists(els, field, 'Field');
|
|
2226
|
+
const el = els[0];
|
|
2227
|
+
const tag = await el.getProperty('tagName').then(el => el.jsonValue());
|
|
2228
|
+
const fieldType = await el.getProperty('type').then(el => el.jsonValue());
|
|
2229
|
+
|
|
2230
|
+
const proceedMultiple = async (elements) => {
|
|
2231
|
+
const fields = Array.isArray(elements) ? elements : [elements];
|
|
2232
|
+
|
|
2233
|
+
const elementValues = [];
|
|
2234
|
+
for (const element of fields) {
|
|
2235
|
+
elementValues.push(await element.getProperty('value').then(el => el.jsonValue()));
|
|
2236
|
+
}
|
|
2237
|
+
|
|
2238
|
+
if (typeof value === 'boolean') {
|
|
2239
|
+
equals(`no. of items matching > 0: ${field}`)[assertType](value, !!elementValues.length);
|
|
2240
|
+
} else {
|
|
2241
|
+
if (assertType === 'assert') {
|
|
2242
|
+
equals(`select option by ${field}`)[assertType](true, elementValues.length > 0);
|
|
2243
|
+
}
|
|
2244
|
+
elementValues.forEach(val => stringIncludes(`fields by ${field}`)[assertType](value, val));
|
|
2245
|
+
}
|
|
2246
|
+
};
|
|
2247
|
+
|
|
2248
|
+
if (tag === 'SELECT') {
|
|
2249
|
+
const selectedOptions = await el.$$('option:checked');
|
|
2250
|
+
// locate option by values and check them
|
|
2251
|
+
if (value === '') {
|
|
2252
|
+
return proceedMultiple(selectedOptions);
|
|
2253
|
+
}
|
|
2254
|
+
|
|
2255
|
+
const options = await filterFieldsByValue(selectedOptions, value, true);
|
|
2256
|
+
return proceedMultiple(options);
|
|
2257
|
+
}
|
|
2258
|
+
|
|
2259
|
+
if (tag === 'INPUT') {
|
|
2260
|
+
if (fieldType === 'checkbox' || fieldType === 'radio') {
|
|
2261
|
+
if (typeof value === 'boolean') {
|
|
2262
|
+
// Filter by values
|
|
2263
|
+
const options = await filterFieldsBySelectionState(els, true);
|
|
2264
|
+
return proceedMultiple(options);
|
|
2265
|
+
}
|
|
2266
|
+
|
|
2267
|
+
const options = await filterFieldsByValue(els, value, true);
|
|
2268
|
+
return proceedMultiple(options);
|
|
2269
|
+
}
|
|
2270
|
+
return proceedMultiple(els[0]);
|
|
2271
|
+
}
|
|
2272
|
+
const fieldVal = await el.getProperty('value').then(el => el.jsonValue());
|
|
2273
|
+
return stringIncludes(`fields by ${field}`)[assertType](value, fieldVal);
|
|
2274
|
+
}
|
|
2275
|
+
|
|
2276
|
+
async function filterFieldsByValue(elements, value, onlySelected) {
|
|
2277
|
+
const matches = [];
|
|
2278
|
+
for (const element of elements) {
|
|
2279
|
+
const val = await element.getProperty('value').then(el => el.jsonValue());
|
|
2280
|
+
let isSelected = true;
|
|
2281
|
+
if (onlySelected) {
|
|
2282
|
+
isSelected = await elementSelected(element);
|
|
2283
|
+
}
|
|
2284
|
+
if ((value == null || val.indexOf(value) > -1) && isSelected) {
|
|
2285
|
+
matches.push(element);
|
|
2286
|
+
}
|
|
2287
|
+
}
|
|
2288
|
+
return matches;
|
|
2289
|
+
}
|
|
2290
|
+
|
|
2291
|
+
async function filterFieldsBySelectionState(elements, state) {
|
|
2292
|
+
const matches = [];
|
|
2293
|
+
for (const element of elements) {
|
|
2294
|
+
const isSelected = await elementSelected(element);
|
|
2295
|
+
if (isSelected === state) {
|
|
2296
|
+
matches.push(element);
|
|
2297
|
+
}
|
|
2298
|
+
}
|
|
2299
|
+
return matches;
|
|
2300
|
+
}
|
|
2301
|
+
|
|
2302
|
+
async function elementSelected(element) {
|
|
2303
|
+
const type = await element.getProperty('type').then(el => el.jsonValue());
|
|
2304
|
+
|
|
2305
|
+
if (type === 'checkbox' || type === 'radio') {
|
|
2306
|
+
return element.getProperty('checked').then(el => el.jsonValue());
|
|
2307
|
+
}
|
|
2308
|
+
return element.getProperty('selected').then(el => el.jsonValue());
|
|
2309
|
+
}
|
|
2310
|
+
|
|
2311
|
+
function isFrameLocator(locator) {
|
|
2312
|
+
locator = new Locator(locator);
|
|
2313
|
+
if (locator.isFrame()) return locator.value;
|
|
2314
|
+
return false;
|
|
2315
|
+
}
|
|
2316
|
+
|
|
2317
|
+
|
|
2318
|
+
function assertElementExists(res, locator, prefix, suffix) {
|
|
2319
|
+
if (!res || res.length === 0) {
|
|
2320
|
+
throw new ElementNotFound(locator, prefix, suffix);
|
|
2321
|
+
}
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
function $XPath(element, selector) {
|
|
2325
|
+
const found = document.evaluate(selector, element || document.body, null, 5, null);
|
|
2326
|
+
const res = [];
|
|
2327
|
+
let current = null;
|
|
2328
|
+
while (current = found.iterateNext()) {
|
|
2329
|
+
res.push(current);
|
|
2330
|
+
}
|
|
2331
|
+
return res;
|
|
2332
|
+
}
|
|
2333
|
+
|
|
2334
|
+
async function targetCreatedHandler(page) {
|
|
2335
|
+
if (!page) return;
|
|
2336
|
+
this.withinLocator = null;
|
|
2337
|
+
page.on('load', (frame) => {
|
|
2338
|
+
page.$('body')
|
|
2339
|
+
.catch(() => null)
|
|
2340
|
+
.then(context => this.context = context);
|
|
2341
|
+
});
|
|
2342
|
+
page.on('console', (msg) => {
|
|
2343
|
+
this.debugSection(`Browser:${ucfirst(msg.type())}`, (msg._text || '') + msg.args().join(' '));
|
|
2344
|
+
consoleLogStore.add(msg);
|
|
2345
|
+
});
|
|
2346
|
+
|
|
2347
|
+
if (this.options.userAgent) {
|
|
2348
|
+
await page.setUserAgent(this.options.userAgent);
|
|
2349
|
+
}
|
|
2350
|
+
if (this.options.windowSize && this.options.windowSize.indexOf('x') > 0) {
|
|
2351
|
+
const dimensions = this.options.windowSize.split('x');
|
|
2352
|
+
const width = parseInt(dimensions[0], 10);
|
|
2353
|
+
const height = parseInt(dimensions[1], 10);
|
|
2354
|
+
await page.setViewportSize({ width, height });
|
|
2355
|
+
}
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2358
|
+
// List of key values to key definitions
|
|
2359
|
+
// https://github.com/GoogleChrome/Playwright/blob/v1.20.0/lib/USKeyboardLayout.js
|
|
2360
|
+
const keyDefinitionMap = {
|
|
2361
|
+
/* eslint-disable quote-props */
|
|
2362
|
+
'0': 'Digit0',
|
|
2363
|
+
'1': 'Digit1',
|
|
2364
|
+
'2': 'Digit2',
|
|
2365
|
+
'3': 'Digit3',
|
|
2366
|
+
'4': 'Digit4',
|
|
2367
|
+
'5': 'Digit5',
|
|
2368
|
+
'6': 'Digit6',
|
|
2369
|
+
'7': 'Digit7',
|
|
2370
|
+
'8': 'Digit8',
|
|
2371
|
+
'9': 'Digit9',
|
|
2372
|
+
'a': 'KeyA',
|
|
2373
|
+
'b': 'KeyB',
|
|
2374
|
+
'c': 'KeyC',
|
|
2375
|
+
'd': 'KeyD',
|
|
2376
|
+
'e': 'KeyE',
|
|
2377
|
+
'f': 'KeyF',
|
|
2378
|
+
'g': 'KeyG',
|
|
2379
|
+
'h': 'KeyH',
|
|
2380
|
+
'i': 'KeyI',
|
|
2381
|
+
'j': 'KeyJ',
|
|
2382
|
+
'k': 'KeyK',
|
|
2383
|
+
'l': 'KeyL',
|
|
2384
|
+
'm': 'KeyM',
|
|
2385
|
+
'n': 'KeyN',
|
|
2386
|
+
'o': 'KeyO',
|
|
2387
|
+
'p': 'KeyP',
|
|
2388
|
+
'q': 'KeyQ',
|
|
2389
|
+
'r': 'KeyR',
|
|
2390
|
+
's': 'KeyS',
|
|
2391
|
+
't': 'KeyT',
|
|
2392
|
+
'u': 'KeyU',
|
|
2393
|
+
'v': 'KeyV',
|
|
2394
|
+
'w': 'KeyW',
|
|
2395
|
+
'x': 'KeyX',
|
|
2396
|
+
'y': 'KeyY',
|
|
2397
|
+
'z': 'KeyZ',
|
|
2398
|
+
';': 'Semicolon',
|
|
2399
|
+
'=': 'Equal',
|
|
2400
|
+
',': 'Comma',
|
|
2401
|
+
'-': 'Minus',
|
|
2402
|
+
'.': 'Period',
|
|
2403
|
+
'/': 'Slash',
|
|
2404
|
+
'`': 'Backquote',
|
|
2405
|
+
'[': 'BracketLeft',
|
|
2406
|
+
'\\': 'Backslash',
|
|
2407
|
+
']': 'BracketRight',
|
|
2408
|
+
'\'': 'Quote',
|
|
2409
|
+
/* eslint-enable quote-props */
|
|
2410
|
+
};
|
|
2411
|
+
|
|
2412
|
+
function getNormalizedKey(key) {
|
|
2413
|
+
const normalizedKey = getNormalizedKeyAttributeValue(key);
|
|
2414
|
+
if (key !== normalizedKey) {
|
|
2415
|
+
this.debugSection('Input', `Mapping key '${key}' to '${normalizedKey}'`);
|
|
2416
|
+
}
|
|
2417
|
+
// Use key definition to ensure correct key is displayed when Shift modifier is active
|
|
2418
|
+
if (Object.prototype.hasOwnProperty.call(keyDefinitionMap, normalizedKey)) {
|
|
2419
|
+
return keyDefinitionMap[normalizedKey];
|
|
2420
|
+
}
|
|
2421
|
+
return normalizedKey;
|
|
2422
|
+
}
|