codeceptjs 3.5.14 → 3.6.0-beta.1.ai-healers
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/bin/codecept.js +66 -30
- package/docs/advanced.md +351 -0
- package/docs/ai.md +365 -0
- package/docs/api.md +323 -0
- package/docs/basics.md +979 -0
- package/docs/bdd.md +539 -0
- package/docs/best.md +237 -0
- package/docs/books.md +37 -0
- package/docs/bootstrap.md +135 -0
- package/docs/build/AI.js +124 -0
- package/docs/build/ApiDataFactory.js +410 -0
- package/docs/build/Appium.js +2027 -0
- package/docs/build/Expect.js +422 -0
- package/docs/build/FileSystem.js +228 -0
- package/docs/build/GraphQL.js +229 -0
- package/docs/build/GraphQLDataFactory.js +309 -0
- package/docs/build/JSONResponse.js +338 -0
- package/docs/build/Mochawesome.js +71 -0
- package/docs/build/Nightmare.js +2152 -0
- package/docs/build/Playwright.js +5110 -0
- package/docs/build/Protractor.js +2706 -0
- package/docs/build/Puppeteer.js +3905 -0
- package/docs/build/REST.js +344 -0
- package/docs/build/TestCafe.js +2125 -0
- package/docs/build/WebDriver.js +4240 -0
- package/docs/changelog.md +2572 -0
- package/docs/commands.md +266 -0
- package/docs/community-helpers.md +58 -0
- package/docs/configuration.md +157 -0
- package/docs/continuous-integration.md +22 -0
- package/docs/custom-helpers.md +306 -0
- package/docs/data.md +379 -0
- package/docs/detox.md +235 -0
- package/docs/docker.md +136 -0
- package/docs/email.md +183 -0
- package/docs/examples.md +149 -0
- package/docs/heal.md +186 -0
- package/docs/helpers/ApiDataFactory.md +266 -0
- package/docs/helpers/Appium.md +1374 -0
- package/docs/helpers/Detox.md +586 -0
- package/docs/helpers/Expect.md +275 -0
- package/docs/helpers/FileSystem.md +152 -0
- package/docs/helpers/GraphQL.md +151 -0
- package/docs/helpers/GraphQLDataFactory.md +226 -0
- package/docs/helpers/JSONResponse.md +254 -0
- package/docs/helpers/Mochawesome.md +8 -0
- package/docs/helpers/MockRequest.md +377 -0
- package/docs/helpers/Nightmare.md +1305 -0
- package/docs/helpers/OpenAI.md +70 -0
- package/docs/helpers/Playwright.md +2759 -0
- package/docs/helpers/Polly.md +44 -0
- package/docs/helpers/Protractor.md +1769 -0
- package/docs/helpers/Puppeteer-firefox.md +86 -0
- package/docs/helpers/Puppeteer.md +2317 -0
- package/docs/helpers/REST.md +218 -0
- package/docs/helpers/TestCafe.md +1321 -0
- package/docs/helpers/WebDriver.md +2547 -0
- package/docs/hooks.md +340 -0
- package/docs/index.md +111 -0
- package/docs/installation.md +75 -0
- package/docs/internal-api.md +266 -0
- package/docs/locators.md +339 -0
- package/docs/mobile-react-native-locators.md +67 -0
- package/docs/mobile.md +338 -0
- package/docs/pageobjects.md +291 -0
- package/docs/parallel.md +400 -0
- package/docs/playwright.md +632 -0
- package/docs/plugins.md +1247 -0
- package/docs/puppeteer.md +316 -0
- package/docs/quickstart.md +162 -0
- package/docs/react.md +70 -0
- package/docs/reports.md +392 -0
- package/docs/secrets.md +36 -0
- package/docs/shadow.md +68 -0
- package/docs/shared/keys.mustache +31 -0
- package/docs/shared/react.mustache +1 -0
- package/docs/testcafe.md +174 -0
- package/docs/translation.md +247 -0
- package/docs/tutorial.md +271 -0
- package/docs/typescript.md +180 -0
- package/docs/ui.md +59 -0
- package/docs/videos.md +28 -0
- package/docs/visual.md +202 -0
- package/docs/vue.md +143 -0
- package/docs/webdriver.md +701 -0
- package/docs/wiki/Books-&-Posts.md +27 -0
- package/docs/wiki/Community-Helpers-&-Plugins.md +53 -0
- package/docs/wiki/Converting-Playwright-to-Istanbul-Coverage.md +61 -0
- package/docs/wiki/Examples.md +145 -0
- package/docs/wiki/Google-Summer-of-Code-(GSoC)-2020.md +68 -0
- package/docs/wiki/Home.md +16 -0
- package/docs/wiki/Migration-to-Appium-v2---CodeceptJS.md +83 -0
- package/docs/wiki/Release-Process.md +24 -0
- package/docs/wiki/Roadmap.md +23 -0
- package/docs/wiki/Tests.md +1393 -0
- package/docs/wiki/Upgrading-to-CodeceptJS-3.md +153 -0
- package/docs/wiki/Videos.md +19 -0
- package/lib/actor.js +3 -6
- package/lib/ai.js +152 -80
- package/lib/cli.js +1 -0
- package/lib/command/generate.js +34 -0
- package/lib/command/run-workers.js +3 -0
- package/lib/command/run.js +3 -0
- package/lib/container.js +2 -0
- package/lib/heal.js +172 -0
- package/lib/helper/AI.js +124 -0
- package/lib/helper/Appium.js +12 -36
- package/lib/helper/Expect.js +7 -10
- package/lib/helper/JSONResponse.js +8 -8
- package/lib/helper/Playwright.js +240 -100
- package/lib/helper/Puppeteer.js +9 -61
- package/lib/helper/REST.js +1 -4
- package/lib/helper/WebDriver.js +10 -324
- package/lib/index.js +3 -0
- package/lib/listener/steps.js +0 -2
- package/lib/locator.js +4 -13
- package/lib/plugin/heal.js +26 -117
- package/lib/recorder.js +11 -5
- package/lib/step.js +1 -3
- package/lib/store.js +2 -0
- package/lib/template/heal.js +39 -0
- package/package.json +23 -27
- package/typings/index.d.ts +0 -16
- package/typings/promiseBasedTypes.d.ts +55 -338
- package/typings/types.d.ts +58 -353
- package/docs/webapi/dontSeeTraffic.mustache +0 -13
- package/docs/webapi/flushNetworkTraffics.mustache +0 -5
- package/docs/webapi/grabRecordedNetworkTraffics.mustache +0 -10
- package/docs/webapi/seeTraffic.mustache +0 -36
- package/docs/webapi/startRecordingTraffic.mustache +0 -8
- package/docs/webapi/stopRecordingTraffic.mustache +0 -5
- package/docs/webapi/waitForCookie.mustache +0 -9
- package/lib/helper/MockServer.js +0 -221
- package/lib/helper/errors/ElementAssertion.js +0 -38
- package/lib/helper/networkTraffics/utils.js +0 -137
- /package/{lib/helper → docs/build}/OpenAI.js +0 -0
|
@@ -0,0 +1,2706 @@
|
|
|
1
|
+
let EC;
|
|
2
|
+
let Key;
|
|
3
|
+
let Button;
|
|
4
|
+
let ProtractorBy;
|
|
5
|
+
let ProtractorExpectedConditions;
|
|
6
|
+
|
|
7
|
+
const path = require('path');
|
|
8
|
+
|
|
9
|
+
const Helper = require('@codeceptjs/helper');
|
|
10
|
+
const stringIncludes = require('../assert/include').includes;
|
|
11
|
+
const { urlEquals, equals } = require('../assert/equal');
|
|
12
|
+
const { empty } = require('../assert/empty');
|
|
13
|
+
const { truth } = require('../assert/truth');
|
|
14
|
+
const {
|
|
15
|
+
xpathLocator,
|
|
16
|
+
fileExists,
|
|
17
|
+
convertCssPropertiesToCamelCase,
|
|
18
|
+
screenshotOutputFolder,
|
|
19
|
+
} = require('../utils');
|
|
20
|
+
const {
|
|
21
|
+
isColorProperty,
|
|
22
|
+
convertColorToRGBA,
|
|
23
|
+
} = require('../colorUtils');
|
|
24
|
+
const ElementNotFound = require('./errors/ElementNotFound');
|
|
25
|
+
const ConnectionRefused = require('./errors/ConnectionRefused');
|
|
26
|
+
const Locator = require('../locator');
|
|
27
|
+
|
|
28
|
+
let withinStore = {};
|
|
29
|
+
let Runner;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Protractor helper is based on [Protractor library](http://www.protractortest.org) and used for testing web applications.
|
|
33
|
+
*
|
|
34
|
+
* Protractor requires [Selenium Server and ChromeDriver/GeckoDriver to be installed](http://codecept.io/quickstart/#prepare-selenium-server).
|
|
35
|
+
* To test non-Angular applications please make sure you have `angular: false` in configuration file.
|
|
36
|
+
*
|
|
37
|
+
* ### Configuration
|
|
38
|
+
*
|
|
39
|
+
* This helper should be configured in codecept.conf.ts or codecept.conf.js
|
|
40
|
+
*
|
|
41
|
+
* * `url` - base url of website to be tested
|
|
42
|
+
* * `browser` - browser in which perform testing
|
|
43
|
+
* * `angular` (optional, default: true): disable this option to run tests for non-Angular applications.
|
|
44
|
+
* * `driver` - which protractor driver to use (local, direct, session, hosted, sauce, browserstack). By default set to 'hosted' which requires selenium server to be started.
|
|
45
|
+
* * `restart` (optional, default: true) - restart browser between tests.
|
|
46
|
+
* * `smartWait`: (optional) **enables [SmartWait](http://codecept.io/acceptance/#smartwait)**; wait for additional milliseconds for element to appear. Enable for 5 secs: "smartWait": 5000
|
|
47
|
+
* * `disableScreenshots` (optional, default: false) - don't save screenshot on failure
|
|
48
|
+
* * `fullPageScreenshots` (optional, default: false) - make full page screenshots on failure.
|
|
49
|
+
* * `uniqueScreenshotNames` (optional, default: false) - option to prevent screenshot override if you have scenarios with the same name in different suites
|
|
50
|
+
* * `keepBrowserState` (optional, default: false) - keep browser state between tests when `restart` set to false.
|
|
51
|
+
* * `seleniumAddress` - Selenium address to connect (default: http://localhost:4444/wd/hub)
|
|
52
|
+
* * `rootElement` - Root element of AngularJS application (default: body)
|
|
53
|
+
* * `getPageTimeout` (optional) sets default timeout for a page to be loaded. 10000 by default.
|
|
54
|
+
* * `waitForTimeout`: (optional) sets default wait time in _ms_ for all `wait*` functions. 1000 by default.
|
|
55
|
+
* * `scriptsTimeout`: (optional) timeout in milliseconds for each script run on the browser, 10000 by default.
|
|
56
|
+
* * `windowSize`: (optional) default window size. Set to `maximize` or a dimension in the format `640x480`.
|
|
57
|
+
* * `manualStart` (optional, default: false) - do not start browser before a test, start it manually inside a helper with `this.helpers.WebDriver._startBrowser()`
|
|
58
|
+
* * `capabilities`: {} - list of [Desired Capabilities](https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities)
|
|
59
|
+
* * `proxy`: set proxy settings
|
|
60
|
+
*
|
|
61
|
+
* other options are the same as in [Protractor config](https://github.com/angular/protractor/blob/master/docs/referenceConf.js).
|
|
62
|
+
*
|
|
63
|
+
* #### Sample Config
|
|
64
|
+
*
|
|
65
|
+
* ```json
|
|
66
|
+
* {
|
|
67
|
+
* "helpers": {
|
|
68
|
+
* "Protractor" : {
|
|
69
|
+
* "url": "http://localhost",
|
|
70
|
+
* "browser": "chrome",
|
|
71
|
+
* "smartWait": 5000,
|
|
72
|
+
* "restart": false
|
|
73
|
+
* }
|
|
74
|
+
* }
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* #### Config for Non-Angular application:
|
|
79
|
+
*
|
|
80
|
+
* ```json
|
|
81
|
+
* {
|
|
82
|
+
* "helpers": {
|
|
83
|
+
* "Protractor" : {
|
|
84
|
+
* "url": "http://localhost",
|
|
85
|
+
* "browser": "chrome",
|
|
86
|
+
* "angular": false
|
|
87
|
+
* }
|
|
88
|
+
* }
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* #### Config for Headless Chrome
|
|
93
|
+
*
|
|
94
|
+
* ```json
|
|
95
|
+
* {
|
|
96
|
+
* "helpers": {
|
|
97
|
+
* "Protractor" : {
|
|
98
|
+
* "url": "http://localhost",
|
|
99
|
+
* "browser": "chrome",
|
|
100
|
+
* "capabilities": {
|
|
101
|
+
* "chromeOptions": {
|
|
102
|
+
* "args": [ "--headless", "--disable-gpu", "--no-sandbox" ]
|
|
103
|
+
* }
|
|
104
|
+
* }
|
|
105
|
+
* }
|
|
106
|
+
* }
|
|
107
|
+
* }
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* ## Access From Helpers
|
|
111
|
+
*
|
|
112
|
+
* Receive a WebDriverIO client from a custom helper by accessing `browser` property:
|
|
113
|
+
*
|
|
114
|
+
* ```js
|
|
115
|
+
* this.helpers['Protractor'].browser
|
|
116
|
+
* ```
|
|
117
|
+
*
|
|
118
|
+
* ## Methods
|
|
119
|
+
*/
|
|
120
|
+
class Protractor extends Helper {
|
|
121
|
+
constructor(config) {
|
|
122
|
+
// process.env.SELENIUM_PROMISE_MANAGER = false; // eslint-disable-line
|
|
123
|
+
super(config);
|
|
124
|
+
|
|
125
|
+
this.isRunning = false;
|
|
126
|
+
this._setConfig(config);
|
|
127
|
+
|
|
128
|
+
console.log('Protractor helper is deprecated as well as Protractor itself.\nThis helper will be removed in next major release');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
_validateConfig(config) {
|
|
132
|
+
const defaults = {
|
|
133
|
+
browser: 'chrome',
|
|
134
|
+
url: 'http://localhost',
|
|
135
|
+
seleniumAddress: 'http://localhost:4444/wd/hub',
|
|
136
|
+
fullPageScreenshots: false,
|
|
137
|
+
rootElement: 'body',
|
|
138
|
+
allScriptsTimeout: 10000,
|
|
139
|
+
scriptTimeout: 10000,
|
|
140
|
+
waitForTimeout: 1000, // ms
|
|
141
|
+
windowSize: null,
|
|
142
|
+
getPageTimeout: 10000,
|
|
143
|
+
driver: 'hosted',
|
|
144
|
+
capabilities: {},
|
|
145
|
+
angular: true,
|
|
146
|
+
restart: true,
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
config = Object.assign(defaults, config);
|
|
150
|
+
|
|
151
|
+
if (!config.allScriptsTimeout) config.allScriptsTimeout = config.scriptsTimeout;
|
|
152
|
+
if (!config.scriptTimeout) config.scriptTimeout = config.scriptsTimeout;
|
|
153
|
+
if (config.proxy) config.capabilities.proxy = config.proxy;
|
|
154
|
+
if (config.browser) config.capabilities.browserName = config.browser;
|
|
155
|
+
|
|
156
|
+
config.waitForTimeoutInSeconds = config.waitForTimeout / 1000; // convert to seconds
|
|
157
|
+
return config;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async _init() {
|
|
161
|
+
process.on('unhandledRejection', (reason) => {
|
|
162
|
+
if (reason.message.indexOf('ECONNREFUSED') > 0) {
|
|
163
|
+
this.browser = null;
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
Runner = require('protractor/built/runner').Runner;
|
|
168
|
+
ProtractorBy = require('protractor').ProtractorBy;
|
|
169
|
+
Key = require('protractor').Key;
|
|
170
|
+
Button = require('protractor').Button;
|
|
171
|
+
ProtractorExpectedConditions = require('protractor').ProtractorExpectedConditions;
|
|
172
|
+
|
|
173
|
+
return Promise.resolve();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
static _checkRequirements() {
|
|
177
|
+
try {
|
|
178
|
+
require('protractor');
|
|
179
|
+
require('assert').ok(require('protractor/built/runner').Runner);
|
|
180
|
+
} catch (e) {
|
|
181
|
+
return ['protractor@^5.3.0'];
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
static _config() {
|
|
186
|
+
return [
|
|
187
|
+
{ name: 'url', message: 'Base url of site to be tested', default: 'http://localhost' },
|
|
188
|
+
{ name: 'driver', message: 'Protractor driver (local, direct, session, hosted, sauce, browserstack)', default: 'hosted' },
|
|
189
|
+
{ name: 'browser', message: 'Browser in which testing will be performed', default: 'chrome' },
|
|
190
|
+
{ name: 'rootElement', message: 'Root element of AngularJS application', default: 'body' },
|
|
191
|
+
{
|
|
192
|
+
name: 'angular', message: 'Enable AngularJS synchronization', default: false, type: 'confirm',
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
];
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async _beforeStep() {
|
|
199
|
+
if (!this.insideAngular) {
|
|
200
|
+
return this.amOutsideAngularApp();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async _beforeSuite() {
|
|
205
|
+
if (!this.options.restart && !this.options.manualStart && !this.isRunning) {
|
|
206
|
+
this.debugSection('Session', 'Starting singleton browser session');
|
|
207
|
+
return this._startBrowser();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
async _startBrowser() {
|
|
212
|
+
try {
|
|
213
|
+
const runner = new Runner(this.options);
|
|
214
|
+
this.browser = runner.createBrowser();
|
|
215
|
+
await this.browser.ready;
|
|
216
|
+
} catch (err) {
|
|
217
|
+
if (err.toString().indexOf('ECONNREFUSED')) {
|
|
218
|
+
throw new ConnectionRefused(err);
|
|
219
|
+
}
|
|
220
|
+
throw err;
|
|
221
|
+
}
|
|
222
|
+
if (this.options.angular) {
|
|
223
|
+
await this.amInsideAngularApp();
|
|
224
|
+
} else {
|
|
225
|
+
await this.amOutsideAngularApp();
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
loadGlobals(this.browser);
|
|
229
|
+
|
|
230
|
+
if (this.options.windowSize === 'maximize') {
|
|
231
|
+
await this.resizeWindow(this.options.windowSize);
|
|
232
|
+
} else if (this.options.windowSize) {
|
|
233
|
+
const size = this.options.windowSize.split('x');
|
|
234
|
+
await this.resizeWindow(parseInt(size[0], 10), parseInt(size[1], 10));
|
|
235
|
+
}
|
|
236
|
+
this.context = this.options.rootElement;
|
|
237
|
+
this.isRunning = true;
|
|
238
|
+
return this.browser.ready;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async _before() {
|
|
242
|
+
if (this.options.restart && !this.options.manualStart) return this._startBrowser();
|
|
243
|
+
if (!this.isRunning && !this.options.manualStart) return this._startBrowser();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
async _after() {
|
|
247
|
+
if (!this.browser) return;
|
|
248
|
+
if (!this.isRunning) return;
|
|
249
|
+
if (this.options.restart) {
|
|
250
|
+
this.isRunning = false;
|
|
251
|
+
return this.browser.quit();
|
|
252
|
+
}
|
|
253
|
+
if (this.options.keepBrowserState) return;
|
|
254
|
+
|
|
255
|
+
const dialog = await this.grabPopupText();
|
|
256
|
+
if (dialog) {
|
|
257
|
+
await this.cancelPopup();
|
|
258
|
+
}
|
|
259
|
+
if (!this.options.keepCookies) {
|
|
260
|
+
await this.browser.manage().deleteAllCookies();
|
|
261
|
+
}
|
|
262
|
+
let url;
|
|
263
|
+
try {
|
|
264
|
+
url = await this.browser.getCurrentUrl();
|
|
265
|
+
} catch (err) {
|
|
266
|
+
// Ignore, as above will throw if no webpage has been loaded
|
|
267
|
+
}
|
|
268
|
+
if (url && !/data:,/i.test(url)) {
|
|
269
|
+
await this.browser.executeScript('localStorage.clear();');
|
|
270
|
+
}
|
|
271
|
+
return this.closeOtherTabs();
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async _failed() {
|
|
275
|
+
await this._withinEnd();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
async _finishTest() {
|
|
279
|
+
if (!this.options.restart && this.isRunning) return this.browser.quit();
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async _withinBegin(locator) {
|
|
283
|
+
withinStore.elFn = this.browser.findElement;
|
|
284
|
+
withinStore.elsFn = this.browser.findElements;
|
|
285
|
+
|
|
286
|
+
const frame = isFrameLocator(locator);
|
|
287
|
+
|
|
288
|
+
if (frame) {
|
|
289
|
+
if (Array.isArray(frame)) {
|
|
290
|
+
withinStore.frame = frame.join('>');
|
|
291
|
+
return this.switchTo(null)
|
|
292
|
+
.then(() => frame.reduce((p, frameLocator) => p.then(() => this.switchTo(frameLocator)), Promise.resolve()));
|
|
293
|
+
}
|
|
294
|
+
withinStore.frame = frame;
|
|
295
|
+
return this.switchTo(locator);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
this.context = locator;
|
|
299
|
+
const context = await global.element(guessLocator(locator) || global.by.css(locator));
|
|
300
|
+
if (!context) throw new ElementNotFound(locator);
|
|
301
|
+
|
|
302
|
+
this.browser.findElement = l => (l ? context.element(l).getWebElement() : context.getWebElement());
|
|
303
|
+
this.browser.findElements = l => context.all(l).getWebElements();
|
|
304
|
+
return context;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
async _withinEnd() {
|
|
308
|
+
if (!isWithin()) return;
|
|
309
|
+
if (withinStore.frame) {
|
|
310
|
+
withinStore = {};
|
|
311
|
+
return this.switchTo(null);
|
|
312
|
+
}
|
|
313
|
+
this.browser.findElement = withinStore.elFn;
|
|
314
|
+
this.browser.findElements = withinStore.elsFn;
|
|
315
|
+
withinStore = {};
|
|
316
|
+
this.context = this.options.rootElement;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
_session() {
|
|
320
|
+
const defaultSession = this.browser;
|
|
321
|
+
return {
|
|
322
|
+
start: async (opts) => {
|
|
323
|
+
opts = this._validateConfig(Object.assign(this.options, opts));
|
|
324
|
+
this.debugSection('New Browser', JSON.stringify(opts));
|
|
325
|
+
const runner = new Runner(opts);
|
|
326
|
+
const res = await this.browser.executeScript('return [window.outerWidth, window.outerHeight]');
|
|
327
|
+
const browser = runner.createBrowser(null, this.browser);
|
|
328
|
+
await browser.ready;
|
|
329
|
+
await browser.waitForAngularEnabled(this.insideAngular);
|
|
330
|
+
await browser.manage().window().setSize(parseInt(res[0], 10), parseInt(res[1], 10));
|
|
331
|
+
return browser.ready;
|
|
332
|
+
},
|
|
333
|
+
stop: async (browser) => {
|
|
334
|
+
return browser.close();
|
|
335
|
+
},
|
|
336
|
+
loadVars: async (browser) => {
|
|
337
|
+
if (isWithin()) throw new Error('Can\'t start session inside within block');
|
|
338
|
+
this.browser = browser;
|
|
339
|
+
loadGlobals(this.browser);
|
|
340
|
+
},
|
|
341
|
+
restoreVars: async () => {
|
|
342
|
+
if (isWithin()) await this._withinEnd();
|
|
343
|
+
this.browser = defaultSession;
|
|
344
|
+
loadGlobals(this.browser);
|
|
345
|
+
},
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Use [Protractor](https://www.protractortest.org/#/api) API inside a test.
|
|
351
|
+
*
|
|
352
|
+
* First argument is a description of an action.
|
|
353
|
+
* Second argument is async function that gets this helper as parameter.
|
|
354
|
+
*
|
|
355
|
+
* { [`browser`](https://www.protractortest.org/#/api?view=ProtractorBrowser)) } object from Protractor API is available.
|
|
356
|
+
*
|
|
357
|
+
* ```js
|
|
358
|
+
* I.useProtractorTo('change url via in-page navigation', async ({ browser }) {
|
|
359
|
+
* await browser.setLocation('api');
|
|
360
|
+
* });
|
|
361
|
+
* ```
|
|
362
|
+
*
|
|
363
|
+
* @param {string} description used to show in logs.
|
|
364
|
+
* @param {function} fn async functuion that executed with Protractor helper as argument
|
|
365
|
+
*/
|
|
366
|
+
useProtractorTo(description, fn) {
|
|
367
|
+
return this._useTo(...arguments);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Switch to non-Angular mode,
|
|
372
|
+
* start using WebDriver instead of Protractor in this session
|
|
373
|
+
*/
|
|
374
|
+
async amOutsideAngularApp() {
|
|
375
|
+
if (!this.browser) return;
|
|
376
|
+
await this.browser.waitForAngularEnabled(false);
|
|
377
|
+
return Promise.resolve(this.insideAngular = false);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Enters Angular mode (switched on by default)
|
|
382
|
+
* Should be used after "amOutsideAngularApp"
|
|
383
|
+
*/
|
|
384
|
+
async amInsideAngularApp() {
|
|
385
|
+
await this.browser.waitForAngularEnabled(true);
|
|
386
|
+
return Promise.resolve(this.insideAngular = true);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Get elements by different locator types, including strict locator
|
|
391
|
+
* Should be used in custom helpers:
|
|
392
|
+
*
|
|
393
|
+
* ```js
|
|
394
|
+
* this.helpers['Protractor']._locate({name: 'password'}).then //...
|
|
395
|
+
* ```
|
|
396
|
+
* To use SmartWait and wait for element to appear on a page, add `true` as second arg:
|
|
397
|
+
*
|
|
398
|
+
* ```js
|
|
399
|
+
* this.helpers['Protractor']._locate({name: 'password'}, true).then //...
|
|
400
|
+
* ```
|
|
401
|
+
*
|
|
402
|
+
*/
|
|
403
|
+
async _locate(locator, smartWait = false) {
|
|
404
|
+
return this._smartWait(() => this.browser.findElements(guessLocator(locator) || global.by.css(locator)), smartWait);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
async _smartWait(fn, enabled = true) {
|
|
408
|
+
if (!this.options.smartWait || !enabled) return fn();
|
|
409
|
+
await this.browser.manage().timeouts().implicitlyWait(this.options.smartWait);
|
|
410
|
+
const res = await fn();
|
|
411
|
+
await this.browser.manage().timeouts().implicitlyWait(0);
|
|
412
|
+
return res;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Find a checkbox by providing human readable text:
|
|
417
|
+
*
|
|
418
|
+
* ```js
|
|
419
|
+
* this.helpers['Protractor']._locateCheckable('I agree with terms and conditions').then // ...
|
|
420
|
+
* ```
|
|
421
|
+
*/
|
|
422
|
+
async _locateCheckable(locator) {
|
|
423
|
+
return findCheckable.call(this, this.browser, locator);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Find a clickable element by providing human readable text:
|
|
428
|
+
*
|
|
429
|
+
* ```js
|
|
430
|
+
* this.helpers['Protractor']._locateClickable('Next page').then // ...
|
|
431
|
+
* ```
|
|
432
|
+
*/
|
|
433
|
+
async _locateClickable(locator) {
|
|
434
|
+
return findClickable.call(this, this.browser, locator);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Find field elements by providing human readable text:
|
|
439
|
+
*
|
|
440
|
+
* ```js
|
|
441
|
+
* this.helpers['Protractor']._locateFields('Your email').then // ...
|
|
442
|
+
* ```
|
|
443
|
+
*/
|
|
444
|
+
async _locateFields(locator) {
|
|
445
|
+
return findFields.call(this, this.browser, locator);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Opens a web page in a browser. Requires relative or absolute url.
|
|
450
|
+
* If url starts with `/`, opens a web page of a site defined in `url` config parameter.
|
|
451
|
+
*
|
|
452
|
+
* ```js
|
|
453
|
+
* I.amOnPage('/'); // opens main page of website
|
|
454
|
+
* I.amOnPage('https://github.com'); // opens github
|
|
455
|
+
* I.amOnPage('/login'); // opens a login page
|
|
456
|
+
* ```
|
|
457
|
+
*
|
|
458
|
+
* @param {string} url url path or global url.
|
|
459
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
460
|
+
*
|
|
461
|
+
*/
|
|
462
|
+
async amOnPage(url) {
|
|
463
|
+
if (!(/^\w+\:\/\//.test(url))) {
|
|
464
|
+
url = this.options.url + url;
|
|
465
|
+
}
|
|
466
|
+
const res = await this.browser.get(url);
|
|
467
|
+
this.debug(`Visited ${url}`);
|
|
468
|
+
return res;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Perform a click on a link or a button, given by a locator.
|
|
473
|
+
* If a fuzzy locator is given, the page will be searched for a button, link, or image matching the locator string.
|
|
474
|
+
* For buttons, the "value" attribute, "name" attribute, and inner text are searched. For links, the link text is searched.
|
|
475
|
+
* For images, the "alt" attribute and inner text of any parent links are searched.
|
|
476
|
+
*
|
|
477
|
+
* The second parameter is a context (CSS or XPath locator) to narrow the search.
|
|
478
|
+
*
|
|
479
|
+
* ```js
|
|
480
|
+
* // simple link
|
|
481
|
+
* I.click('Logout');
|
|
482
|
+
* // button of form
|
|
483
|
+
* I.click('Submit');
|
|
484
|
+
* // CSS button
|
|
485
|
+
* I.click('#form input[type=submit]');
|
|
486
|
+
* // XPath
|
|
487
|
+
* I.click('//form/*[@type=submit]');
|
|
488
|
+
* // link in context
|
|
489
|
+
* I.click('Logout', '#nav');
|
|
490
|
+
* // using strict locator
|
|
491
|
+
* I.click({css: 'nav a.login'});
|
|
492
|
+
* ```
|
|
493
|
+
*
|
|
494
|
+
* @param {CodeceptJS.LocatorOrString} locator clickable link or button located by text, or any element located by CSS|XPath|strict locator.
|
|
495
|
+
* @param {?CodeceptJS.LocatorOrString | null} [context=null] (optional, `null` by default) element to search in CSS|XPath|Strict locator.
|
|
496
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
497
|
+
*
|
|
498
|
+
*/
|
|
499
|
+
async click(locator, context = null) {
|
|
500
|
+
let matcher = this.browser;
|
|
501
|
+
if (context) {
|
|
502
|
+
const els = await this._locate(context, true);
|
|
503
|
+
assertElementExists(els, context);
|
|
504
|
+
matcher = els[0];
|
|
505
|
+
}
|
|
506
|
+
const el = await findClickable.call(this, matcher, locator);
|
|
507
|
+
return el.click();
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/**
|
|
511
|
+
* Performs a double-click on an element matched by link|button|label|CSS or XPath.
|
|
512
|
+
* Context can be specified as second parameter to narrow search.
|
|
513
|
+
*
|
|
514
|
+
* ```js
|
|
515
|
+
* I.doubleClick('Edit');
|
|
516
|
+
* I.doubleClick('Edit', '.actions');
|
|
517
|
+
* I.doubleClick({css: 'button.accept'});
|
|
518
|
+
* I.doubleClick('.btn.edit');
|
|
519
|
+
* ```
|
|
520
|
+
*
|
|
521
|
+
* @param {CodeceptJS.LocatorOrString} locator clickable link or button located by text, or any element located by CSS|XPath|strict locator.
|
|
522
|
+
* @param {?CodeceptJS.LocatorOrString} [context=null] (optional, `null` by default) element to search in CSS|XPath|Strict locator.
|
|
523
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
524
|
+
*
|
|
525
|
+
*/
|
|
526
|
+
async doubleClick(locator, context = null) {
|
|
527
|
+
let matcher = this.browser;
|
|
528
|
+
if (context) {
|
|
529
|
+
const els = await this._locate(context, true);
|
|
530
|
+
assertElementExists(els, context);
|
|
531
|
+
matcher = els[0];
|
|
532
|
+
}
|
|
533
|
+
const el = await findClickable.call(this, matcher, locator);
|
|
534
|
+
return this.browser.actions().doubleClick(el).perform();
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Performs right click on a clickable element matched by semantic locator, CSS or XPath.
|
|
539
|
+
*
|
|
540
|
+
* ```js
|
|
541
|
+
* // right click element with id el
|
|
542
|
+
* I.rightClick('#el');
|
|
543
|
+
* // right click link or button with text "Click me"
|
|
544
|
+
* I.rightClick('Click me');
|
|
545
|
+
* // right click button with text "Click me" inside .context
|
|
546
|
+
* I.rightClick('Click me', '.context');
|
|
547
|
+
* ```
|
|
548
|
+
*
|
|
549
|
+
* @param {CodeceptJS.LocatorOrString} locator clickable element located by CSS|XPath|strict locator.
|
|
550
|
+
* @param {?CodeceptJS.LocatorOrString} [context=null] (optional, `null` by default) element located by CSS|XPath|strict locator.
|
|
551
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
552
|
+
*
|
|
553
|
+
*/
|
|
554
|
+
async rightClick(locator, context = null) {
|
|
555
|
+
/**
|
|
556
|
+
* just press button if no selector is given
|
|
557
|
+
*/
|
|
558
|
+
if (locator === undefined) {
|
|
559
|
+
return this.browser.actions().click(Button.RIGHT).perform();
|
|
560
|
+
}
|
|
561
|
+
let matcher = this.browser;
|
|
562
|
+
if (context) {
|
|
563
|
+
const els = await this._locate(context, true);
|
|
564
|
+
assertElementExists(els, context);
|
|
565
|
+
matcher = els[0];
|
|
566
|
+
}
|
|
567
|
+
const el = await findClickable.call(this, matcher, locator);
|
|
568
|
+
|
|
569
|
+
await this.browser.actions().mouseMove(el).perform();
|
|
570
|
+
return this.browser.actions().click(Button.RIGHT).perform();
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Moves cursor to element matched by locator.
|
|
575
|
+
* Extra shift can be set with offsetX and offsetY options.
|
|
576
|
+
*
|
|
577
|
+
* ```js
|
|
578
|
+
* I.moveCursorTo('.tooltip');
|
|
579
|
+
* I.moveCursorTo('#submit', 5,5);
|
|
580
|
+
* ```
|
|
581
|
+
*
|
|
582
|
+
* @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|strict locator.
|
|
583
|
+
* @param {number} [offsetX=0] (optional, `0` by default) X-axis offset.
|
|
584
|
+
* @param {number} [offsetY=0] (optional, `0` by default) Y-axis offset.
|
|
585
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
586
|
+
*
|
|
587
|
+
*/
|
|
588
|
+
async moveCursorTo(locator, offsetX = null, offsetY = null) {
|
|
589
|
+
let offset = null;
|
|
590
|
+
if (offsetX !== null || offsetY !== null) {
|
|
591
|
+
offset = { x: offsetX, y: offsetY };
|
|
592
|
+
}
|
|
593
|
+
const els = await this._locate(locator, true);
|
|
594
|
+
assertElementExists(els, locator);
|
|
595
|
+
return this.browser.actions().mouseMove(els[0], offset).perform();
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* Checks that a page contains a visible text.
|
|
600
|
+
* Use context parameter to narrow down the search.
|
|
601
|
+
*
|
|
602
|
+
* ```js
|
|
603
|
+
* I.see('Welcome'); // text welcome on a page
|
|
604
|
+
* I.see('Welcome', '.content'); // text inside .content div
|
|
605
|
+
* I.see('Register', {css: 'form.register'}); // use strict locator
|
|
606
|
+
* ```
|
|
607
|
+
* @param {string} text expected on page.
|
|
608
|
+
* @param {?CodeceptJS.LocatorOrString} [context=null] (optional, `null` by default) element located by CSS|Xpath|strict locator in which to search for text.
|
|
609
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
610
|
+
*
|
|
611
|
+
*/
|
|
612
|
+
async see(text, context = null) {
|
|
613
|
+
return proceedSee.call(this, 'assert', text, context);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* Checks that text is equal to provided one.
|
|
618
|
+
*
|
|
619
|
+
* ```js
|
|
620
|
+
* I.seeTextEquals('text', 'h1');
|
|
621
|
+
* ```
|
|
622
|
+
*
|
|
623
|
+
* @param {string} text element value to check.
|
|
624
|
+
* @param {CodeceptJS.LocatorOrString?} [context=null] element located by CSS|XPath|strict locator.
|
|
625
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
626
|
+
*
|
|
627
|
+
*/
|
|
628
|
+
async seeTextEquals(text, context = null) {
|
|
629
|
+
return proceedSee.call(this, 'assert', text, context, true);
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* Opposite to `see`. Checks that a text is not present on a page.
|
|
634
|
+
* Use context parameter to narrow down the search.
|
|
635
|
+
*
|
|
636
|
+
* ```js
|
|
637
|
+
* I.dontSee('Login'); // assume we are already logged in.
|
|
638
|
+
* I.dontSee('Login', '.nav'); // no login inside .nav element
|
|
639
|
+
* ```
|
|
640
|
+
*
|
|
641
|
+
* @param {string} text which is not present.
|
|
642
|
+
* @param {CodeceptJS.LocatorOrString} [context] (optional) element located by CSS|XPath|strict locator in which to perfrom search.
|
|
643
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
644
|
+
*
|
|
645
|
+
*/
|
|
646
|
+
dontSee(text, context = null) {
|
|
647
|
+
return proceedSee.call(this, 'negate', text, context);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* Get JS log from browser. Log buffer is reset after each request.
|
|
652
|
+
* Resumes test execution, so **should be used inside an async function with `await`** operator.
|
|
653
|
+
*
|
|
654
|
+
* ```js
|
|
655
|
+
* let logs = await I.grabBrowserLogs();
|
|
656
|
+
* console.log(JSON.stringify(logs))
|
|
657
|
+
* ```
|
|
658
|
+
*
|
|
659
|
+
* @returns {Promise<object[]>|undefined} all browser logs
|
|
660
|
+
*
|
|
661
|
+
*/
|
|
662
|
+
async grabBrowserLogs() {
|
|
663
|
+
return this.browser.manage().logs().get('browser');
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/**
|
|
667
|
+
* Get current URL from browser.
|
|
668
|
+
* Resumes test execution, so should be used inside an async function.
|
|
669
|
+
*
|
|
670
|
+
* ```js
|
|
671
|
+
* let url = await I.grabCurrentUrl();
|
|
672
|
+
* console.log(`Current URL is [${url}]`);
|
|
673
|
+
* ```
|
|
674
|
+
*
|
|
675
|
+
* @returns {Promise<string>} current URL
|
|
676
|
+
*/
|
|
677
|
+
async grabCurrentUrl() {
|
|
678
|
+
return this.browser.getCurrentUrl();
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* Selects an option in a drop-down select.
|
|
683
|
+
* Field is searched by label | name | CSS | XPath.
|
|
684
|
+
* Option is selected by visible text or by value.
|
|
685
|
+
*
|
|
686
|
+
* ```js
|
|
687
|
+
* I.selectOption('Choose Plan', 'Monthly'); // select by label
|
|
688
|
+
* I.selectOption('subscription', 'Monthly'); // match option by text
|
|
689
|
+
* I.selectOption('subscription', '0'); // or by value
|
|
690
|
+
* I.selectOption('//form/select[@name=account]','Premium');
|
|
691
|
+
* I.selectOption('form select[name=account]', 'Premium');
|
|
692
|
+
* I.selectOption({css: 'form select[name=account]'}, 'Premium');
|
|
693
|
+
* ```
|
|
694
|
+
*
|
|
695
|
+
* Provide an array for the second argument to select multiple options.
|
|
696
|
+
*
|
|
697
|
+
* ```js
|
|
698
|
+
* I.selectOption('Which OS do you use?', ['Android', 'iOS']);
|
|
699
|
+
* ```
|
|
700
|
+
* @param {LocatorOrString} select field located by label|name|CSS|XPath|strict locator.
|
|
701
|
+
* @param {string|Array<*>} option visible text or value of option.
|
|
702
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
703
|
+
*
|
|
704
|
+
*/
|
|
705
|
+
async selectOption(select, option) {
|
|
706
|
+
const fields = await findFields(this.browser, select);
|
|
707
|
+
assertElementExists(fields, select, 'Selectable field');
|
|
708
|
+
if (!Array.isArray(option)) {
|
|
709
|
+
option = [option];
|
|
710
|
+
}
|
|
711
|
+
const field = fields[0];
|
|
712
|
+
const promises = [];
|
|
713
|
+
for (const key in option) {
|
|
714
|
+
const opt = xpathLocator.literal(option[key]);
|
|
715
|
+
let els = await field.findElements(global.by.xpath(Locator.select.byVisibleText(opt)));
|
|
716
|
+
if (!els.length) {
|
|
717
|
+
els = await field.findElements(global.by.xpath(Locator.select.byValue(opt)));
|
|
718
|
+
}
|
|
719
|
+
els.forEach(el => promises.push(el.click()));
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
return Promise.all(promises);
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/**
|
|
726
|
+
* Fills a text field or textarea, after clearing its value, with the given string.
|
|
727
|
+
* Field is located by name, label, CSS, or XPath.
|
|
728
|
+
*
|
|
729
|
+
* ```js
|
|
730
|
+
* // by label
|
|
731
|
+
* I.fillField('Email', 'hello@world.com');
|
|
732
|
+
* // by name
|
|
733
|
+
* I.fillField('password', secret('123456'));
|
|
734
|
+
* // by CSS
|
|
735
|
+
* I.fillField('form#login input[name=username]', 'John');
|
|
736
|
+
* // or by strict locator
|
|
737
|
+
* I.fillField({css: 'form#login input[name=username]'}, 'John');
|
|
738
|
+
* ```
|
|
739
|
+
* @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator.
|
|
740
|
+
* @param {CodeceptJS.StringOrSecret} value text value to fill.
|
|
741
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
742
|
+
*
|
|
743
|
+
*/
|
|
744
|
+
async fillField(field, value) {
|
|
745
|
+
const els = await findFields(this.browser, field);
|
|
746
|
+
await els[0].clear();
|
|
747
|
+
return els[0].sendKeys(value.toString());
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* Presses a key on a focused element.
|
|
752
|
+
* Special keys like 'Enter', 'Control', [etc](https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element/:id/value)
|
|
753
|
+
* will be replaced with corresponding unicode.
|
|
754
|
+
* If modifier key is used (Control, Command, Alt, Shift) in array, it will be released afterwards.
|
|
755
|
+
*
|
|
756
|
+
* ```js
|
|
757
|
+
* I.pressKey('Enter');
|
|
758
|
+
* I.pressKey(['Control','a']);
|
|
759
|
+
* ```
|
|
760
|
+
*
|
|
761
|
+
* @param {string|string[]} key key or array of keys to press.
|
|
762
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
763
|
+
*
|
|
764
|
+
* {{ keys }}
|
|
765
|
+
*/
|
|
766
|
+
async pressKey(key) {
|
|
767
|
+
let modifier;
|
|
768
|
+
if (Array.isArray(key) && ~['Control', 'Command', 'Shift', 'Alt'].indexOf(key[0])) { // eslint-disable-line no-bitwise
|
|
769
|
+
modifier = Key[key[0].toUpperCase()];
|
|
770
|
+
key = key[1];
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
// guess special key in Selenium Webdriver list
|
|
774
|
+
if (Key[key.toUpperCase()]) {
|
|
775
|
+
key = Key[key.toUpperCase()];
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
const action = this.browser.actions();
|
|
779
|
+
if (modifier) action.keyDown(modifier);
|
|
780
|
+
action.sendKeys(key);
|
|
781
|
+
if (modifier) action.keyUp(modifier);
|
|
782
|
+
return action.perform();
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* Attaches a file to element located by label, name, CSS or XPath
|
|
787
|
+
* Path to file is relative current codecept directory (where codecept.conf.ts or codecept.conf.js is located).
|
|
788
|
+
* File will be uploaded to remote system (if tests are running remotely).
|
|
789
|
+
*
|
|
790
|
+
* ```js
|
|
791
|
+
* I.attachFile('Avatar', 'data/avatar.jpg');
|
|
792
|
+
* I.attachFile('form input[name=avatar]', 'data/avatar.jpg');
|
|
793
|
+
* ```
|
|
794
|
+
*
|
|
795
|
+
* @param {CodeceptJS.LocatorOrString} locator field located by label|name|CSS|XPath|strict locator.
|
|
796
|
+
* @param {string} pathToFile local file path relative to codecept.conf.ts or codecept.conf.js config file.
|
|
797
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
798
|
+
*
|
|
799
|
+
*/
|
|
800
|
+
async attachFile(locator, pathToFile) {
|
|
801
|
+
const file = path.join(global.codecept_dir, pathToFile);
|
|
802
|
+
if (!fileExists(file)) {
|
|
803
|
+
throw new Error(`File at ${file} can not be found on local system`);
|
|
804
|
+
}
|
|
805
|
+
const els = await findFields(this.browser, locator);
|
|
806
|
+
assertElementExists(els, locator, 'Field');
|
|
807
|
+
if (this.options.browser !== 'phantomjs') {
|
|
808
|
+
const remote = require('selenium-webdriver/remote');
|
|
809
|
+
this.browser.setFileDetector(new remote.FileDetector());
|
|
810
|
+
}
|
|
811
|
+
return els[0].sendKeys(file);
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
/**
|
|
815
|
+
* Checks that the given input field or textarea equals to given value.
|
|
816
|
+
* For fuzzy locators, fields are matched by label text, the "name" attribute, CSS, and XPath.
|
|
817
|
+
*
|
|
818
|
+
* ```js
|
|
819
|
+
* I.seeInField('Username', 'davert');
|
|
820
|
+
* I.seeInField({css: 'form textarea'},'Type your comment here');
|
|
821
|
+
* I.seeInField('form input[type=hidden]','hidden_value');
|
|
822
|
+
* I.seeInField('#searchform input','Search');
|
|
823
|
+
* ```
|
|
824
|
+
* @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator.
|
|
825
|
+
* @param {CodeceptJS.StringOrSecret} value value to check.
|
|
826
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
827
|
+
*
|
|
828
|
+
*/
|
|
829
|
+
async seeInField(field, value) {
|
|
830
|
+
const _value = (typeof value === 'boolean') ? value : value.toString();
|
|
831
|
+
return proceedSeeInField.call(this, 'assert', field, _value);
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
/**
|
|
835
|
+
* Checks that value of input field or textarea doesn't equal to given value
|
|
836
|
+
* Opposite to `seeInField`.
|
|
837
|
+
*
|
|
838
|
+
* ```js
|
|
839
|
+
* I.dontSeeInField('email', 'user@user.com'); // field by name
|
|
840
|
+
* I.dontSeeInField({ css: 'form input.email' }, 'user@user.com'); // field by CSS
|
|
841
|
+
* ```
|
|
842
|
+
*
|
|
843
|
+
* @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator.
|
|
844
|
+
* @param {CodeceptJS.StringOrSecret} value value to check.
|
|
845
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
846
|
+
*
|
|
847
|
+
*/
|
|
848
|
+
async dontSeeInField(field, value) {
|
|
849
|
+
const _value = (typeof value === 'boolean') ? value : value.toString();
|
|
850
|
+
return proceedSeeInField.call(this, 'negate', field, _value);
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
/**
|
|
854
|
+
* Appends text to a input field or textarea.
|
|
855
|
+
* Field is located by name, label, CSS or XPath
|
|
856
|
+
*
|
|
857
|
+
* ```js
|
|
858
|
+
* I.appendField('#myTextField', 'appended');
|
|
859
|
+
* // typing secret
|
|
860
|
+
* I.appendField('password', secret('123456'));
|
|
861
|
+
* ```
|
|
862
|
+
* @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator
|
|
863
|
+
* @param {string} value text value to append.
|
|
864
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
865
|
+
*
|
|
866
|
+
*/
|
|
867
|
+
async appendField(field, value) {
|
|
868
|
+
const els = await findFields(this.browser, field);
|
|
869
|
+
assertElementExists(els, field, 'Field');
|
|
870
|
+
return els[0].sendKeys(value.toString());
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* Clears a `<textarea>` or text `<input>` element's value.
|
|
875
|
+
*
|
|
876
|
+
* ```js
|
|
877
|
+
* I.clearField('Email');
|
|
878
|
+
* I.clearField('user[email]');
|
|
879
|
+
* I.clearField('#email');
|
|
880
|
+
* ```
|
|
881
|
+
* @param {LocatorOrString} editable field located by label|name|CSS|XPath|strict locator.
|
|
882
|
+
* @returns {void} automatically synchronized promise through #recorder.
|
|
883
|
+
*
|
|
884
|
+
*/
|
|
885
|
+
async clearField(field) {
|
|
886
|
+
const els = await findFields(this.browser, field);
|
|
887
|
+
assertElementExists(els, field, 'Field');
|
|
888
|
+
return els[0].clear();
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
/**
|
|
892
|
+
* Selects a checkbox or radio button.
|
|
893
|
+
* Element is located by label or name or CSS or XPath.
|
|
894
|
+
*
|
|
895
|
+
* The second parameter is a context (CSS or XPath locator) to narrow the search.
|
|
896
|
+
*
|
|
897
|
+
* ```js
|
|
898
|
+
* I.checkOption('#agree');
|
|
899
|
+
* I.checkOption('I Agree to Terms and Conditions');
|
|
900
|
+
* I.checkOption('agree', '//form');
|
|
901
|
+
* ```
|
|
902
|
+
* @param {CodeceptJS.LocatorOrString} field checkbox located by label | name | CSS | XPath | strict locator.
|
|
903
|
+
* @param {?CodeceptJS.LocatorOrString} [context=null] (optional, `null` by default) element located by CSS | XPath | strict locator.
|
|
904
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
905
|
+
*
|
|
906
|
+
*/
|
|
907
|
+
async checkOption(field, context = null) {
|
|
908
|
+
let matcher = this.browser;
|
|
909
|
+
if (context) {
|
|
910
|
+
const els = await this._locate(context, true);
|
|
911
|
+
assertElementExists(els, context);
|
|
912
|
+
matcher = els[0];
|
|
913
|
+
}
|
|
914
|
+
const els = await findCheckable(matcher, field);
|
|
915
|
+
assertElementExists(els, field, 'Checkbox or radio');
|
|
916
|
+
const isSelected = await els[0].isSelected();
|
|
917
|
+
if (!isSelected) return els[0].click();
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
/**
|
|
921
|
+
* Unselects a checkbox or radio button.
|
|
922
|
+
* Element is located by label or name or CSS or XPath.
|
|
923
|
+
*
|
|
924
|
+
* The second parameter is a context (CSS or XPath locator) to narrow the search.
|
|
925
|
+
*
|
|
926
|
+
* ```js
|
|
927
|
+
* I.uncheckOption('#agree');
|
|
928
|
+
* I.uncheckOption('I Agree to Terms and Conditions');
|
|
929
|
+
* I.uncheckOption('agree', '//form');
|
|
930
|
+
* ```
|
|
931
|
+
* @param {CodeceptJS.LocatorOrString} field checkbox located by label | name | CSS | XPath | strict locator.
|
|
932
|
+
* @param {?CodeceptJS.LocatorOrString} [context=null] (optional, `null` by default) element located by CSS | XPath | strict locator.
|
|
933
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
934
|
+
*
|
|
935
|
+
*/
|
|
936
|
+
async uncheckOption(field, context = null) {
|
|
937
|
+
let matcher = this.browser;
|
|
938
|
+
if (context) {
|
|
939
|
+
const els = await this._locate(context, true);
|
|
940
|
+
assertElementExists(els, context);
|
|
941
|
+
matcher = els[0];
|
|
942
|
+
}
|
|
943
|
+
const els = await findCheckable(matcher, field);
|
|
944
|
+
assertElementExists(els, field, 'Checkbox or radio');
|
|
945
|
+
const isSelected = await els[0].isSelected();
|
|
946
|
+
if (isSelected) return els[0].click();
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
/**
|
|
950
|
+
* Verifies that the specified checkbox is checked.
|
|
951
|
+
*
|
|
952
|
+
* ```js
|
|
953
|
+
* I.seeCheckboxIsChecked('Agree');
|
|
954
|
+
* I.seeCheckboxIsChecked('#agree'); // I suppose user agreed to terms
|
|
955
|
+
* I.seeCheckboxIsChecked({css: '#signup_form input[type=checkbox]'});
|
|
956
|
+
* ```
|
|
957
|
+
*
|
|
958
|
+
* @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator.
|
|
959
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
960
|
+
*
|
|
961
|
+
*/
|
|
962
|
+
async seeCheckboxIsChecked(field) {
|
|
963
|
+
return proceedIsChecked.call(this, 'assert', field);
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
/**
|
|
967
|
+
* Verifies that the specified checkbox is not checked.
|
|
968
|
+
*
|
|
969
|
+
* ```js
|
|
970
|
+
* I.dontSeeCheckboxIsChecked('#agree'); // located by ID
|
|
971
|
+
* I.dontSeeCheckboxIsChecked('I agree to terms'); // located by label
|
|
972
|
+
* I.dontSeeCheckboxIsChecked('agree'); // located by name
|
|
973
|
+
* ```
|
|
974
|
+
*
|
|
975
|
+
* @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator.
|
|
976
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
977
|
+
*
|
|
978
|
+
*/
|
|
979
|
+
async dontSeeCheckboxIsChecked(field) {
|
|
980
|
+
return proceedIsChecked.call(this, 'negate', field);
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
/**
|
|
984
|
+
* Retrieves all texts from an element located by CSS or XPath and returns it to test.
|
|
985
|
+
* Resumes test execution, so **should be used inside async with `await`** operator.
|
|
986
|
+
*
|
|
987
|
+
* ```js
|
|
988
|
+
* let pins = await I.grabTextFromAll('#pin li');
|
|
989
|
+
* ```
|
|
990
|
+
*
|
|
991
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
992
|
+
* @returns {Promise<string[]>} attribute value
|
|
993
|
+
*
|
|
994
|
+
*/
|
|
995
|
+
async grabTextFromAll(locator) {
|
|
996
|
+
const els = await this._locate(locator);
|
|
997
|
+
const texts = [];
|
|
998
|
+
for (const el of els) {
|
|
999
|
+
texts.push(await el.getText());
|
|
1000
|
+
}
|
|
1001
|
+
return texts;
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
/**
|
|
1005
|
+
* Retrieves a text from an element located by CSS or XPath and returns it to test.
|
|
1006
|
+
* Resumes test execution, so **should be used inside async with `await`** operator.
|
|
1007
|
+
*
|
|
1008
|
+
* ```js
|
|
1009
|
+
* let pin = await I.grabTextFrom('#pin');
|
|
1010
|
+
* ```
|
|
1011
|
+
* If multiple elements found returns first element.
|
|
1012
|
+
*
|
|
1013
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
1014
|
+
* @returns {Promise<string>} attribute value
|
|
1015
|
+
*
|
|
1016
|
+
*/
|
|
1017
|
+
async grabTextFrom(locator) {
|
|
1018
|
+
const texts = await this.grabTextFromAll(locator);
|
|
1019
|
+
assertElementExists(texts, locator);
|
|
1020
|
+
if (texts.length > 1) {
|
|
1021
|
+
this.debugSection('GrabText', `Using first element out of ${texts.length}`);
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
return texts[0];
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
/**
|
|
1028
|
+
* Retrieves all the innerHTML from elements located by CSS or XPath and returns it to test.
|
|
1029
|
+
* Resumes test execution, so **should be used inside async function with `await`** operator.
|
|
1030
|
+
*
|
|
1031
|
+
* ```js
|
|
1032
|
+
* let postHTMLs = await I.grabHTMLFromAll('.post');
|
|
1033
|
+
* ```
|
|
1034
|
+
*
|
|
1035
|
+
* @param {CodeceptJS.LocatorOrString} element located by CSS|XPath|strict locator.
|
|
1036
|
+
* @returns {Promise<string[]>} HTML code for an element
|
|
1037
|
+
*
|
|
1038
|
+
*/
|
|
1039
|
+
async grabHTMLFromAll(locator) {
|
|
1040
|
+
const els = await this._locate(locator);
|
|
1041
|
+
|
|
1042
|
+
const html = await Promise.all(els.map((el) => {
|
|
1043
|
+
return this.browser.executeScript('return arguments[0].innerHTML;', el);
|
|
1044
|
+
}));
|
|
1045
|
+
|
|
1046
|
+
return html;
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
/**
|
|
1050
|
+
* Retrieves the innerHTML from an element located by CSS or XPath and returns it to test.
|
|
1051
|
+
* Resumes test execution, so **should be used inside async function with `await`** operator.
|
|
1052
|
+
* If more than one element is found - HTML of first element is returned.
|
|
1053
|
+
*
|
|
1054
|
+
* ```js
|
|
1055
|
+
* let postHTML = await I.grabHTMLFrom('#post');
|
|
1056
|
+
* ```
|
|
1057
|
+
*
|
|
1058
|
+
* @param {CodeceptJS.LocatorOrString} element located by CSS|XPath|strict locator.
|
|
1059
|
+
* @returns {Promise<string>} HTML code for an element
|
|
1060
|
+
*
|
|
1061
|
+
*/
|
|
1062
|
+
async grabHTMLFrom(locator) {
|
|
1063
|
+
const html = await this.grabHTMLFromAll(locator);
|
|
1064
|
+
assertElementExists(html, locator);
|
|
1065
|
+
if (html.length > 1) {
|
|
1066
|
+
this.debugSection('GrabHTMl', `Using first element out of ${html.length}`);
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
return html[0];
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
/**
|
|
1073
|
+
* Retrieves an array of value from a form located by CSS or XPath and returns it to test.
|
|
1074
|
+
* Resumes test execution, so **should be used inside async function with `await`** operator.
|
|
1075
|
+
*
|
|
1076
|
+
* ```js
|
|
1077
|
+
* let inputs = await I.grabValueFromAll('//form/input');
|
|
1078
|
+
* ```
|
|
1079
|
+
* @param {CodeceptJS.LocatorOrString} locator field located by label|name|CSS|XPath|strict locator.
|
|
1080
|
+
* @returns {Promise<string[]>} attribute value
|
|
1081
|
+
*
|
|
1082
|
+
*/
|
|
1083
|
+
async grabValueFromAll(locator) {
|
|
1084
|
+
const els = await findFields(this.browser, locator);
|
|
1085
|
+
const values = await Promise.all(els.map(el => el.getAttribute('value')));
|
|
1086
|
+
|
|
1087
|
+
return values;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
/**
|
|
1091
|
+
* Retrieves a value from a form element located by CSS or XPath and returns it to test.
|
|
1092
|
+
* Resumes test execution, so **should be used inside async function with `await`** operator.
|
|
1093
|
+
* If more than one element is found - value of first element is returned.
|
|
1094
|
+
*
|
|
1095
|
+
* ```js
|
|
1096
|
+
* let email = await I.grabValueFrom('input[name=email]');
|
|
1097
|
+
* ```
|
|
1098
|
+
* @param {CodeceptJS.LocatorOrString} locator field located by label|name|CSS|XPath|strict locator.
|
|
1099
|
+
* @returns {Promise<string>} attribute value
|
|
1100
|
+
*
|
|
1101
|
+
*/
|
|
1102
|
+
async grabValueFrom(locator) {
|
|
1103
|
+
const values = await this.grabValueFromAll(locator);
|
|
1104
|
+
assertElementExists(values, locator, 'Field');
|
|
1105
|
+
if (values.length > 1) {
|
|
1106
|
+
this.debugSection('GrabValue', `Using first element out of ${values.length}`);
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
return values[0];
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
/**
|
|
1113
|
+
* Grab array of CSS properties for given locator
|
|
1114
|
+
* Resumes test execution, so **should be used inside an async function with `await`** operator.
|
|
1115
|
+
*
|
|
1116
|
+
* ```js
|
|
1117
|
+
* const values = await I.grabCssPropertyFromAll('h3', 'font-weight');
|
|
1118
|
+
* ```
|
|
1119
|
+
*
|
|
1120
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
1121
|
+
* @param {string} cssProperty CSS property name.
|
|
1122
|
+
* @returns {Promise<string[]>} CSS value
|
|
1123
|
+
*
|
|
1124
|
+
*/
|
|
1125
|
+
async grabCssPropertyFromAll(locator, cssProperty) {
|
|
1126
|
+
const els = await this._locate(locator, true);
|
|
1127
|
+
const values = await Promise.all(els.map(el => el.getCssValue(cssProperty)));
|
|
1128
|
+
|
|
1129
|
+
return values;
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
/**
|
|
1133
|
+
* Grab CSS property for given locator
|
|
1134
|
+
* Resumes test execution, so **should be used inside an async function with `await`** operator.
|
|
1135
|
+
* If more than one element is found - value of first element is returned.
|
|
1136
|
+
*
|
|
1137
|
+
* ```js
|
|
1138
|
+
* const value = await I.grabCssPropertyFrom('h3', 'font-weight');
|
|
1139
|
+
* ```
|
|
1140
|
+
*
|
|
1141
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
1142
|
+
* @param {string} cssProperty CSS property name.
|
|
1143
|
+
* @returns {Promise<string>} CSS value
|
|
1144
|
+
*
|
|
1145
|
+
*/
|
|
1146
|
+
async grabCssPropertyFrom(locator, cssProperty) {
|
|
1147
|
+
const cssValues = await this.grabCssPropertyFromAll(locator, cssProperty);
|
|
1148
|
+
assertElementExists(cssValues, locator);
|
|
1149
|
+
|
|
1150
|
+
if (cssValues.length > 1) {
|
|
1151
|
+
this.debugSection('GrabCSS', `Using first element out of ${cssValues.length}`);
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
return cssValues[0];
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
/**
|
|
1158
|
+
* Retrieves an array of attributes from elements located by CSS or XPath and returns it to test.
|
|
1159
|
+
* Resumes test execution, so **should be used inside async with `await`** operator.
|
|
1160
|
+
*
|
|
1161
|
+
* ```js
|
|
1162
|
+
* let hints = await I.grabAttributeFromAll('.tooltip', 'title');
|
|
1163
|
+
* ```
|
|
1164
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
1165
|
+
* @param {string} attr attribute name.
|
|
1166
|
+
* @returns {Promise<string[]>} attribute value
|
|
1167
|
+
*
|
|
1168
|
+
*/
|
|
1169
|
+
async grabAttributeFromAll(locator, attr) {
|
|
1170
|
+
const els = await this._locate(locator);
|
|
1171
|
+
const array = [];
|
|
1172
|
+
|
|
1173
|
+
for (let index = 0; index < els.length; index++) {
|
|
1174
|
+
const el = els[index];
|
|
1175
|
+
array.push(await el.getAttribute(attr));
|
|
1176
|
+
}
|
|
1177
|
+
return array;
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
/**
|
|
1181
|
+
* Retrieves an attribute from an element located by CSS or XPath and returns it to test.
|
|
1182
|
+
* Resumes test execution, so **should be used inside async with `await`** operator.
|
|
1183
|
+
* If more than one element is found - attribute of first element is returned.
|
|
1184
|
+
*
|
|
1185
|
+
* ```js
|
|
1186
|
+
* let hint = await I.grabAttributeFrom('#tooltip', 'title');
|
|
1187
|
+
* ```
|
|
1188
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
1189
|
+
* @param {string} attr attribute name.
|
|
1190
|
+
* @returns {Promise<string>} attribute value
|
|
1191
|
+
*
|
|
1192
|
+
*/
|
|
1193
|
+
async grabAttributeFrom(locator, attr) {
|
|
1194
|
+
const attrs = await this.grabAttributeFromAll(locator, attr);
|
|
1195
|
+
assertElementExists(attrs, locator);
|
|
1196
|
+
if (attrs.length > 1) {
|
|
1197
|
+
this.debugSection('GrabAttribute', `Using first element out of ${attrs.length}`);
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
return attrs[0];
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
/**
|
|
1204
|
+
* Checks that title contains text.
|
|
1205
|
+
*
|
|
1206
|
+
* ```js
|
|
1207
|
+
* I.seeInTitle('Home Page');
|
|
1208
|
+
* ```
|
|
1209
|
+
*
|
|
1210
|
+
* @param {string} text text value to check.
|
|
1211
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1212
|
+
*
|
|
1213
|
+
*/
|
|
1214
|
+
async seeInTitle(text) {
|
|
1215
|
+
return this.browser.getTitle().then(title => stringIncludes('web page title').assert(text, title));
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
/**
|
|
1219
|
+
* Checks that title is equal to provided one.
|
|
1220
|
+
*
|
|
1221
|
+
* ```js
|
|
1222
|
+
* I.seeTitleEquals('Test title.');
|
|
1223
|
+
* ```
|
|
1224
|
+
*
|
|
1225
|
+
* @param {string} text value to check.
|
|
1226
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1227
|
+
*
|
|
1228
|
+
*/
|
|
1229
|
+
async seeTitleEquals(text) {
|
|
1230
|
+
const title = await this.browser.getTitle();
|
|
1231
|
+
return equals('web page title').assert(title, text);
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
/**
|
|
1235
|
+
* Checks that title does not contain text.
|
|
1236
|
+
*
|
|
1237
|
+
* ```js
|
|
1238
|
+
* I.dontSeeInTitle('Error');
|
|
1239
|
+
* ```
|
|
1240
|
+
*
|
|
1241
|
+
* @param {string} text value to check.
|
|
1242
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1243
|
+
*
|
|
1244
|
+
*/
|
|
1245
|
+
async dontSeeInTitle(text) {
|
|
1246
|
+
return this.browser.getTitle().then(title => stringIncludes('web page title').negate(text, title));
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
/**
|
|
1250
|
+
* Retrieves a page title and returns it to test.
|
|
1251
|
+
* Resumes test execution, so **should be used inside async with `await`** operator.
|
|
1252
|
+
*
|
|
1253
|
+
* ```js
|
|
1254
|
+
* let title = await I.grabTitle();
|
|
1255
|
+
* ```
|
|
1256
|
+
*
|
|
1257
|
+
* @returns {Promise<string>} title
|
|
1258
|
+
*/
|
|
1259
|
+
async grabTitle() {
|
|
1260
|
+
return this.browser.getTitle().then((title) => {
|
|
1261
|
+
this.debugSection('Title', title);
|
|
1262
|
+
return title;
|
|
1263
|
+
});
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
/**
|
|
1267
|
+
* Checks that a given Element is visible
|
|
1268
|
+
* Element is located by CSS or XPath.
|
|
1269
|
+
*
|
|
1270
|
+
* ```js
|
|
1271
|
+
* I.seeElement('#modal');
|
|
1272
|
+
* ```
|
|
1273
|
+
* @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|strict locator.
|
|
1274
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1275
|
+
*
|
|
1276
|
+
*/
|
|
1277
|
+
async seeElement(locator) {
|
|
1278
|
+
let els = await this._locate(locator, true);
|
|
1279
|
+
els = await Promise.all(els.map(el => el.isDisplayed()));
|
|
1280
|
+
return empty('elements').negate(els.filter(v => v).fill('ELEMENT'));
|
|
1281
|
+
}
|
|
1282
|
+
|
|
1283
|
+
/**
|
|
1284
|
+
* Opposite to `seeElement`. Checks that element is not visible (or in DOM)
|
|
1285
|
+
*
|
|
1286
|
+
* ```js
|
|
1287
|
+
* I.dontSeeElement('.modal'); // modal is not shown
|
|
1288
|
+
* ```
|
|
1289
|
+
*
|
|
1290
|
+
* @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|Strict locator.
|
|
1291
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1292
|
+
*
|
|
1293
|
+
*/
|
|
1294
|
+
async dontSeeElement(locator) {
|
|
1295
|
+
let els = await this._locate(locator, false);
|
|
1296
|
+
els = await Promise.all(els.map(el => el.isDisplayed()));
|
|
1297
|
+
return empty('elements').assert(els.filter(v => v).fill('ELEMENT'));
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
/**
|
|
1301
|
+
* Checks that a given Element is present in the DOM
|
|
1302
|
+
* Element is located by CSS or XPath.
|
|
1303
|
+
*
|
|
1304
|
+
* ```js
|
|
1305
|
+
* I.seeElementInDOM('#modal');
|
|
1306
|
+
* ```
|
|
1307
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
1308
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1309
|
+
*
|
|
1310
|
+
*/
|
|
1311
|
+
async seeElementInDOM(locator) {
|
|
1312
|
+
return this.browser.findElements(guessLocator(locator) || global.by.css(locator)).then(els => empty('elements').negate(els.fill('ELEMENT')));
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
/**
|
|
1316
|
+
* Opposite to `seeElementInDOM`. Checks that element is not on page.
|
|
1317
|
+
*
|
|
1318
|
+
* ```js
|
|
1319
|
+
* I.dontSeeElementInDOM('.nav'); // checks that element is not on page visible or not
|
|
1320
|
+
* ```
|
|
1321
|
+
*
|
|
1322
|
+
* @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|Strict locator.
|
|
1323
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1324
|
+
*
|
|
1325
|
+
*/
|
|
1326
|
+
async dontSeeElementInDOM(locator) {
|
|
1327
|
+
return this.browser.findElements(guessLocator(locator) || global.by.css(locator)).then(els => empty('elements').assert(els.fill('ELEMENT')));
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
/**
|
|
1331
|
+
* Checks that the current page contains the given string in its raw source code.
|
|
1332
|
+
*
|
|
1333
|
+
* ```js
|
|
1334
|
+
* I.seeInSource('<h1>Green eggs & ham</h1>');
|
|
1335
|
+
* ```
|
|
1336
|
+
* @param {string} text value to check.
|
|
1337
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1338
|
+
*
|
|
1339
|
+
*/
|
|
1340
|
+
async seeInSource(text) {
|
|
1341
|
+
return this.browser.getPageSource().then(source => stringIncludes('HTML source of a page').assert(text, source));
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
/**
|
|
1345
|
+
* Retrieves page source and returns it to test.
|
|
1346
|
+
* Resumes test execution, so **should be used inside async function with `await`** operator.
|
|
1347
|
+
*
|
|
1348
|
+
* ```js
|
|
1349
|
+
* let pageSource = await I.grabSource();
|
|
1350
|
+
* ```
|
|
1351
|
+
*
|
|
1352
|
+
* @returns {Promise<string>} source code
|
|
1353
|
+
*/
|
|
1354
|
+
async grabSource() {
|
|
1355
|
+
return this.browser.getPageSource();
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
/**
|
|
1359
|
+
* Checks that the current page does not contains the given string in its raw source code.
|
|
1360
|
+
*
|
|
1361
|
+
* ```js
|
|
1362
|
+
* I.dontSeeInSource('<!--'); // no comments in source
|
|
1363
|
+
* ```
|
|
1364
|
+
*
|
|
1365
|
+
* @param {string} value to check.
|
|
1366
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1367
|
+
*
|
|
1368
|
+
*/
|
|
1369
|
+
async dontSeeInSource(text) {
|
|
1370
|
+
return this.browser.getPageSource().then(source => stringIncludes('HTML source of a page').negate(text, source));
|
|
1371
|
+
}
|
|
1372
|
+
|
|
1373
|
+
/**
|
|
1374
|
+
* Asserts that an element appears a given number of times in the DOM.
|
|
1375
|
+
* Element is located by label or name or CSS or XPath.
|
|
1376
|
+
*
|
|
1377
|
+
*
|
|
1378
|
+
* ```js
|
|
1379
|
+
* I.seeNumberOfElements('#submitBtn', 1);
|
|
1380
|
+
* ```
|
|
1381
|
+
*
|
|
1382
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
1383
|
+
* @param {number} num number of elements.
|
|
1384
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1385
|
+
*
|
|
1386
|
+
*/
|
|
1387
|
+
async seeNumberOfElements(locator, num) {
|
|
1388
|
+
const elements = await this._locate(locator);
|
|
1389
|
+
return equals(`expected number of elements (${(new Locator(locator))}) is ${num}, but found ${elements.length}`).assert(elements.length, num);
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
/**
|
|
1393
|
+
* Asserts that an element is visible a given number of times.
|
|
1394
|
+
* Element is located by CSS or XPath.
|
|
1395
|
+
*
|
|
1396
|
+
* ```js
|
|
1397
|
+
* I.seeNumberOfVisibleElements('.buttons', 3);
|
|
1398
|
+
* ```
|
|
1399
|
+
*
|
|
1400
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
1401
|
+
* @param {number} num number of elements.
|
|
1402
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1403
|
+
*
|
|
1404
|
+
*/
|
|
1405
|
+
async seeNumberOfVisibleElements(locator, num) {
|
|
1406
|
+
const res = await this.grabNumberOfVisibleElements(locator);
|
|
1407
|
+
return equals(`expected number of visible elements (${(new Locator(locator))}) is ${num}, but found ${res}`).assert(res, num);
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
/**
|
|
1411
|
+
* Grab number of visible elements by locator.
|
|
1412
|
+
* Resumes test execution, so **should be used inside async function with `await`** operator.
|
|
1413
|
+
*
|
|
1414
|
+
* ```js
|
|
1415
|
+
* let numOfElements = await I.grabNumberOfVisibleElements('p');
|
|
1416
|
+
* ```
|
|
1417
|
+
*
|
|
1418
|
+
* @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|strict locator.
|
|
1419
|
+
* @returns {Promise<number>} number of visible elements
|
|
1420
|
+
*/
|
|
1421
|
+
async grabNumberOfVisibleElements(locator) {
|
|
1422
|
+
let els = await this._locate(locator);
|
|
1423
|
+
els = await Promise.all(els.map(el => el.isDisplayed()));
|
|
1424
|
+
return els.length;
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
/**
|
|
1428
|
+
* Checks that all elements with given locator have given CSS properties.
|
|
1429
|
+
*
|
|
1430
|
+
* ```js
|
|
1431
|
+
* I.seeCssPropertiesOnElements('h3', { 'font-weight': "bold"});
|
|
1432
|
+
* ```
|
|
1433
|
+
*
|
|
1434
|
+
* @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|strict locator.
|
|
1435
|
+
* @param {object} cssProperties object with CSS properties and their values to check.
|
|
1436
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1437
|
+
*
|
|
1438
|
+
*/
|
|
1439
|
+
async seeCssPropertiesOnElements(locator, cssProperties) {
|
|
1440
|
+
const els = await this._locate(locator);
|
|
1441
|
+
assertElementExists(els, locator);
|
|
1442
|
+
|
|
1443
|
+
const cssPropertiesCamelCase = convertCssPropertiesToCamelCase(cssProperties);
|
|
1444
|
+
|
|
1445
|
+
const attributeNames = Object.keys(cssPropertiesCamelCase);
|
|
1446
|
+
const expectedValues = attributeNames.map(name => cssPropertiesCamelCase[name]);
|
|
1447
|
+
const missingAttributes = [];
|
|
1448
|
+
|
|
1449
|
+
for (const el of els) {
|
|
1450
|
+
const attributeValues = await Promise.all(attributeNames.map(attr => el.getCssValue(attr)));
|
|
1451
|
+
|
|
1452
|
+
const missing = attributeValues.filter((actual, i) => {
|
|
1453
|
+
const prop = attributeNames[i];
|
|
1454
|
+
let propValue = actual;
|
|
1455
|
+
if (isColorProperty(prop) && propValue) {
|
|
1456
|
+
propValue = convertColorToRGBA(propValue);
|
|
1457
|
+
}
|
|
1458
|
+
return propValue !== expectedValues[i];
|
|
1459
|
+
});
|
|
1460
|
+
if (missing.length) {
|
|
1461
|
+
missingAttributes.push(...missing);
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
return equals(`all elements (${(new Locator(locator))}) to have CSS property ${JSON.stringify(cssProperties)}`).assert(missingAttributes.length, 0);
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
/**
|
|
1468
|
+
* Checks that all elements with given locator have given attributes.
|
|
1469
|
+
*
|
|
1470
|
+
* ```js
|
|
1471
|
+
* I.seeAttributesOnElements('//form', { method: "post"});
|
|
1472
|
+
* ```
|
|
1473
|
+
*
|
|
1474
|
+
* @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|strict locator.
|
|
1475
|
+
* @param {object} attributes attributes and their values to check.
|
|
1476
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1477
|
+
*
|
|
1478
|
+
*/
|
|
1479
|
+
async seeAttributesOnElements(locator, attributes) {
|
|
1480
|
+
const els = await this._locate(locator);
|
|
1481
|
+
assertElementExists(els, locator);
|
|
1482
|
+
|
|
1483
|
+
const attributeNames = Object.keys(attributes);
|
|
1484
|
+
const expectedValues = attributeNames.map(name => attributes[name]);
|
|
1485
|
+
const missingAttributes = [];
|
|
1486
|
+
|
|
1487
|
+
for (const el of els) {
|
|
1488
|
+
const attributeValues = await Promise.all(attributeNames.map(attr => el.getAttribute(attr)));
|
|
1489
|
+
const missing = attributeValues.filter((actual, i) => {
|
|
1490
|
+
if (expectedValues[i] instanceof RegExp) {
|
|
1491
|
+
return expectedValues[i].test(actual);
|
|
1492
|
+
}
|
|
1493
|
+
return actual !== expectedValues[i];
|
|
1494
|
+
});
|
|
1495
|
+
if (missing.length) {
|
|
1496
|
+
missingAttributes.push(...missing);
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
return equals(`all elements (${(new Locator(locator))}) to have attributes ${JSON.stringify(attributes)}`).assert(missingAttributes.length, 0);
|
|
1501
|
+
}
|
|
1502
|
+
|
|
1503
|
+
/**
|
|
1504
|
+
* Executes sync script on a page.
|
|
1505
|
+
* Pass arguments to function as additional parameters.
|
|
1506
|
+
* Will return execution result to a test.
|
|
1507
|
+
* In this case you should use async function and await to receive results.
|
|
1508
|
+
*
|
|
1509
|
+
* Example with jQuery DatePicker:
|
|
1510
|
+
*
|
|
1511
|
+
* ```js
|
|
1512
|
+
* // change date of jQuery DatePicker
|
|
1513
|
+
* I.executeScript(function() {
|
|
1514
|
+
* // now we are inside browser context
|
|
1515
|
+
* $('date').datetimepicker('setDate', new Date());
|
|
1516
|
+
* });
|
|
1517
|
+
* ```
|
|
1518
|
+
* Can return values. Don't forget to use `await` to get them.
|
|
1519
|
+
*
|
|
1520
|
+
* ```js
|
|
1521
|
+
* let date = await I.executeScript(function(el) {
|
|
1522
|
+
* // only basic types can be returned
|
|
1523
|
+
* return $(el).datetimepicker('getDate').toString();
|
|
1524
|
+
* }, '#date'); // passing jquery selector
|
|
1525
|
+
* ```
|
|
1526
|
+
*
|
|
1527
|
+
* @param {string|function} fn function to be executed in browser context.
|
|
1528
|
+
* @param {...any} args to be passed to function.
|
|
1529
|
+
* @returns {Promise<any>} script return value
|
|
1530
|
+
*
|
|
1531
|
+
*/
|
|
1532
|
+
async executeScript() {
|
|
1533
|
+
return this.browser.executeScript.apply(this.browser, arguments);
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
/**
|
|
1537
|
+
* Executes async script on page.
|
|
1538
|
+
* Provided function should execute a passed callback (as first argument) to signal it is finished.
|
|
1539
|
+
*
|
|
1540
|
+
* Example: In Vue.js to make components completely rendered we are waiting for [nextTick](https://vuejs.org/v2/api/#Vue-nextTick).
|
|
1541
|
+
*
|
|
1542
|
+
* ```js
|
|
1543
|
+
* I.executeAsyncScript(function(done) {
|
|
1544
|
+
* Vue.nextTick(done); // waiting for next tick
|
|
1545
|
+
* });
|
|
1546
|
+
* ```
|
|
1547
|
+
*
|
|
1548
|
+
* By passing value to `done()` function you can return values.
|
|
1549
|
+
* Additional arguments can be passed as well, while `done` function is always last parameter in arguments list.
|
|
1550
|
+
*
|
|
1551
|
+
* ```js
|
|
1552
|
+
* let val = await I.executeAsyncScript(function(url, done) {
|
|
1553
|
+
* // in browser context
|
|
1554
|
+
* $.ajax(url, { success: (data) => done(data); }
|
|
1555
|
+
* }, 'http://ajax.callback.url/');
|
|
1556
|
+
* ```
|
|
1557
|
+
*
|
|
1558
|
+
* @param {string|function} fn function to be executed in browser context.
|
|
1559
|
+
* @param {...any} args to be passed to function.
|
|
1560
|
+
* @returns {Promise<any>} script return value
|
|
1561
|
+
*
|
|
1562
|
+
*/
|
|
1563
|
+
async executeAsyncScript() {
|
|
1564
|
+
this.browser.manage().timeouts().setScriptTimeout(this.options.scriptTimeout);
|
|
1565
|
+
return this.browser.executeAsyncScript.apply(this.browser, arguments);
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
/**
|
|
1569
|
+
* Checks that current url contains a provided fragment.
|
|
1570
|
+
*
|
|
1571
|
+
* ```js
|
|
1572
|
+
* I.seeInCurrentUrl('/register'); // we are on registration page
|
|
1573
|
+
* ```
|
|
1574
|
+
*
|
|
1575
|
+
* @param {string} url a fragment to check
|
|
1576
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1577
|
+
*
|
|
1578
|
+
*/
|
|
1579
|
+
async seeInCurrentUrl(url) {
|
|
1580
|
+
return this.browser.getCurrentUrl().then(currentUrl => stringIncludes('url').assert(url, currentUrl));
|
|
1581
|
+
}
|
|
1582
|
+
|
|
1583
|
+
/**
|
|
1584
|
+
* Checks that current url does not contain a provided fragment.
|
|
1585
|
+
*
|
|
1586
|
+
* @param {string} url value to check.
|
|
1587
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1588
|
+
*
|
|
1589
|
+
*/
|
|
1590
|
+
async dontSeeInCurrentUrl(url) {
|
|
1591
|
+
return this.browser.getCurrentUrl().then(currentUrl => stringIncludes('url').negate(url, currentUrl));
|
|
1592
|
+
}
|
|
1593
|
+
|
|
1594
|
+
/**
|
|
1595
|
+
* Checks that current url is equal to provided one.
|
|
1596
|
+
* If a relative url provided, a configured url will be prepended to it.
|
|
1597
|
+
* So both examples will work:
|
|
1598
|
+
*
|
|
1599
|
+
* ```js
|
|
1600
|
+
* I.seeCurrentUrlEquals('/register');
|
|
1601
|
+
* I.seeCurrentUrlEquals('http://my.site.com/register');
|
|
1602
|
+
* ```
|
|
1603
|
+
*
|
|
1604
|
+
* @param {string} url value to check.
|
|
1605
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1606
|
+
*
|
|
1607
|
+
*/
|
|
1608
|
+
async seeCurrentUrlEquals(url) {
|
|
1609
|
+
return this.browser.getCurrentUrl().then(currentUrl => urlEquals(this.options.url).assert(url, currentUrl));
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
/**
|
|
1613
|
+
* Checks that current url is not equal to provided one.
|
|
1614
|
+
* If a relative url provided, a configured url will be prepended to it.
|
|
1615
|
+
*
|
|
1616
|
+
* ```js
|
|
1617
|
+
* I.dontSeeCurrentUrlEquals('/login'); // relative url are ok
|
|
1618
|
+
* I.dontSeeCurrentUrlEquals('http://mysite.com/login'); // absolute urls are also ok
|
|
1619
|
+
* ```
|
|
1620
|
+
*
|
|
1621
|
+
* @param {string} url value to check.
|
|
1622
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1623
|
+
*
|
|
1624
|
+
*/
|
|
1625
|
+
async dontSeeCurrentUrlEquals(url) {
|
|
1626
|
+
return this.browser.getCurrentUrl().then(currentUrl => urlEquals(this.options.url).negate(url, currentUrl));
|
|
1627
|
+
}
|
|
1628
|
+
|
|
1629
|
+
/**
|
|
1630
|
+
* Saves screenshot of the specified locator to ouput folder (set in codecept.conf.ts or codecept.conf.js).
|
|
1631
|
+
* Filename is relative to output folder.
|
|
1632
|
+
*
|
|
1633
|
+
* ```js
|
|
1634
|
+
* I.saveElementScreenshot(`#submit`,'debug.png');
|
|
1635
|
+
* ```
|
|
1636
|
+
*
|
|
1637
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
1638
|
+
* @param {string} fileName file name to save.
|
|
1639
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1640
|
+
*
|
|
1641
|
+
*
|
|
1642
|
+
*/
|
|
1643
|
+
async saveElementScreenshot(locator, fileName) {
|
|
1644
|
+
const outputFile = screenshotOutputFolder(fileName);
|
|
1645
|
+
|
|
1646
|
+
const writeFile = (png, outputFile) => {
|
|
1647
|
+
const fs = require('fs');
|
|
1648
|
+
const stream = fs.createWriteStream(outputFile);
|
|
1649
|
+
stream.write(Buffer.from(png, 'base64'));
|
|
1650
|
+
stream.end();
|
|
1651
|
+
return new Promise(resolve => stream.on('finish', resolve)); // eslint-disable-line no-promise-executor-return
|
|
1652
|
+
};
|
|
1653
|
+
|
|
1654
|
+
const res = await this._locate(locator);
|
|
1655
|
+
assertElementExists(res, locator);
|
|
1656
|
+
if (res.length > 1) this.debug(`[Elements] Using first element out of ${res.length}`);
|
|
1657
|
+
const elem = res[0];
|
|
1658
|
+
this.debug(`Screenshot of ${(new Locator(locator))} element has been saved to ${outputFile}`);
|
|
1659
|
+
const png = await elem.takeScreenshot();
|
|
1660
|
+
return writeFile(png, outputFile);
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
/**
|
|
1664
|
+
* Saves a screenshot to ouput folder (set in codecept.conf.ts or codecept.conf.js).
|
|
1665
|
+
* Filename is relative to output folder.
|
|
1666
|
+
* Optionally resize the window to the full available page `scrollHeight` and `scrollWidth` to capture the entire page by passing `true` in as the second argument.
|
|
1667
|
+
*
|
|
1668
|
+
* ```js
|
|
1669
|
+
* I.saveScreenshot('debug.png');
|
|
1670
|
+
* I.saveScreenshot('debug.png', true) //resizes to available scrollHeight and scrollWidth before taking screenshot
|
|
1671
|
+
* ```
|
|
1672
|
+
*
|
|
1673
|
+
* @param {string} fileName file name to save.
|
|
1674
|
+
* @param {boolean} [fullPage=false] (optional, `false` by default) flag to enable fullscreen screenshot mode.
|
|
1675
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1676
|
+
*
|
|
1677
|
+
*/
|
|
1678
|
+
async saveScreenshot(fileName, fullPage = false) {
|
|
1679
|
+
const outputFile = screenshotOutputFolder(fileName);
|
|
1680
|
+
|
|
1681
|
+
const writeFile = (png, outputFile) => {
|
|
1682
|
+
const fs = require('fs');
|
|
1683
|
+
const stream = fs.createWriteStream(outputFile);
|
|
1684
|
+
stream.write(Buffer.from(png, 'base64'));
|
|
1685
|
+
stream.end();
|
|
1686
|
+
return new Promise(resolve => stream.on('finish', resolve)); // eslint-disable-line no-promise-executor-return
|
|
1687
|
+
};
|
|
1688
|
+
|
|
1689
|
+
if (!fullPage) {
|
|
1690
|
+
this.debug(`Screenshot has been saved to ${outputFile}`);
|
|
1691
|
+
const png = await this.browser.takeScreenshot();
|
|
1692
|
+
return writeFile(png, outputFile);
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
let { width, height } = await this.browser.executeScript(() => ({ // eslint-disable-line
|
|
1696
|
+
height: document.body.scrollHeight,
|
|
1697
|
+
width: document.body.scrollWidth,
|
|
1698
|
+
}));
|
|
1699
|
+
|
|
1700
|
+
if (height < 100) height = 500;
|
|
1701
|
+
|
|
1702
|
+
await this.browser.manage().window().setSize(width, height);
|
|
1703
|
+
this.debug(`Screenshot has been saved to ${outputFile}, size: ${width}x${height}`);
|
|
1704
|
+
const png = await this.browser.takeScreenshot();
|
|
1705
|
+
return writeFile(png, outputFile);
|
|
1706
|
+
}
|
|
1707
|
+
|
|
1708
|
+
/**
|
|
1709
|
+
* Clears a cookie by name,
|
|
1710
|
+
* if none provided clears all cookies.
|
|
1711
|
+
*
|
|
1712
|
+
* ```js
|
|
1713
|
+
* I.clearCookie();
|
|
1714
|
+
* I.clearCookie('test'); // Playwright currently doesn't support clear a particular cookie name
|
|
1715
|
+
* ```
|
|
1716
|
+
*
|
|
1717
|
+
* @param {?string} [cookie=null] (optional, `null` by default) cookie name
|
|
1718
|
+
*
|
|
1719
|
+
*/
|
|
1720
|
+
async clearCookie(cookie = null) {
|
|
1721
|
+
if (!cookie) {
|
|
1722
|
+
return this.browser.manage().deleteAllCookies();
|
|
1723
|
+
}
|
|
1724
|
+
return this.browser.manage().deleteCookie(cookie);
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
/**
|
|
1728
|
+
* Checks that cookie with given name exists.
|
|
1729
|
+
*
|
|
1730
|
+
* ```js
|
|
1731
|
+
* I.seeCookie('Auth');
|
|
1732
|
+
* ```
|
|
1733
|
+
*
|
|
1734
|
+
* @param {string} name cookie name.
|
|
1735
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1736
|
+
*
|
|
1737
|
+
*/
|
|
1738
|
+
async seeCookie(name) {
|
|
1739
|
+
return this.browser.manage().getCookie(name).then(res => truth(`cookie ${name}`, 'to be set').assert(res));
|
|
1740
|
+
}
|
|
1741
|
+
|
|
1742
|
+
/**
|
|
1743
|
+
* Checks that cookie with given name does not exist.
|
|
1744
|
+
*
|
|
1745
|
+
* ```js
|
|
1746
|
+
* I.dontSeeCookie('auth'); // no auth cookie
|
|
1747
|
+
* ```
|
|
1748
|
+
*
|
|
1749
|
+
* @param {string} name cookie name.
|
|
1750
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1751
|
+
*
|
|
1752
|
+
*/
|
|
1753
|
+
async dontSeeCookie(name) {
|
|
1754
|
+
return this.browser.manage().getCookie(name).then(res => truth(`cookie ${name}`, 'to be set').negate(res));
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
/**
|
|
1758
|
+
* Gets a cookie object by name.
|
|
1759
|
+
* If none provided gets all cookies.
|
|
1760
|
+
* Resumes test execution, so **should be used inside async function with `await`** operator.
|
|
1761
|
+
*
|
|
1762
|
+
* ```js
|
|
1763
|
+
* let cookie = await I.grabCookie('auth');
|
|
1764
|
+
* assert(cookie.value, '123456');
|
|
1765
|
+
* ```
|
|
1766
|
+
*
|
|
1767
|
+
* @param {?string} [name=null] cookie name.
|
|
1768
|
+
* @returns {any} attribute value
|
|
1769
|
+
*
|
|
1770
|
+
*
|
|
1771
|
+
* Returns cookie in JSON [format](https://code.google.com/p/selenium/wiki/JsonWireProtocol#Cookie_JSON_Object).
|
|
1772
|
+
*/
|
|
1773
|
+
async grabCookie(name) {
|
|
1774
|
+
if (!name) return this.browser.manage().getCookies();
|
|
1775
|
+
return this.browser.manage().getCookie(name);
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
/**
|
|
1779
|
+
* Accepts the active JavaScript native popup window, as created by window.alert|window.confirm|window.prompt.
|
|
1780
|
+
* Don't confuse popups with modal windows, as created by [various
|
|
1781
|
+
* libraries](http://jster.net/category/windows-modals-popups). Appium: support only web testing
|
|
1782
|
+
*/
|
|
1783
|
+
async acceptPopup() {
|
|
1784
|
+
return this.browser.switchTo().alert().accept();
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1787
|
+
/**
|
|
1788
|
+
* Dismisses the active JavaScript popup, as created by window.alert|window.confirm|window.prompt.
|
|
1789
|
+
*/
|
|
1790
|
+
async cancelPopup() {
|
|
1791
|
+
return this.browser.switchTo().alert().dismiss();
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
/**
|
|
1795
|
+
* Checks that the active JavaScript popup, as created by `window.alert|window.confirm|window.prompt`, contains the
|
|
1796
|
+
* given string.
|
|
1797
|
+
*
|
|
1798
|
+
* ```js
|
|
1799
|
+
* I.seeInPopup('Popup text');
|
|
1800
|
+
* ```
|
|
1801
|
+
* @param {string} text value to check.
|
|
1802
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1803
|
+
*
|
|
1804
|
+
*/
|
|
1805
|
+
async seeInPopup(text) {
|
|
1806
|
+
const popupAlert = await this.browser.switchTo().alert();
|
|
1807
|
+
const res = await popupAlert.getText();
|
|
1808
|
+
if (res === null) {
|
|
1809
|
+
throw new Error('Popup is not opened');
|
|
1810
|
+
}
|
|
1811
|
+
stringIncludes('text in popup').assert(text, res);
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
/**
|
|
1815
|
+
* Grab the text within the popup. If no popup is visible then it will return null
|
|
1816
|
+
*
|
|
1817
|
+
* ```js
|
|
1818
|
+
* await I.grabPopupText();
|
|
1819
|
+
* ```
|
|
1820
|
+
*/
|
|
1821
|
+
async grabPopupText() {
|
|
1822
|
+
try {
|
|
1823
|
+
const dialog = await this.browser.switchTo().alert();
|
|
1824
|
+
|
|
1825
|
+
if (dialog) {
|
|
1826
|
+
return dialog.getText();
|
|
1827
|
+
}
|
|
1828
|
+
} catch (e) {
|
|
1829
|
+
if (e.message.match(/no.*?(alert|modal)/i)) {
|
|
1830
|
+
// Don't throw an error
|
|
1831
|
+
return null;
|
|
1832
|
+
}
|
|
1833
|
+
throw e;
|
|
1834
|
+
}
|
|
1835
|
+
}
|
|
1836
|
+
|
|
1837
|
+
/**
|
|
1838
|
+
* Resize the current window to provided width and height.
|
|
1839
|
+
* First parameter can be set to `maximize`.
|
|
1840
|
+
*
|
|
1841
|
+
* @param {number} width width in pixels or `maximize`.
|
|
1842
|
+
* @param {number} height height in pixels.
|
|
1843
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1844
|
+
*
|
|
1845
|
+
*/
|
|
1846
|
+
async resizeWindow(width, height) {
|
|
1847
|
+
if (width === 'maximize') {
|
|
1848
|
+
const res = await this.browser.executeScript('return [screen.width, screen.height]');
|
|
1849
|
+
return this.browser.manage().window().setSize(parseInt(res[0], 10), parseInt(res[1], 10));
|
|
1850
|
+
}
|
|
1851
|
+
return this.browser.manage().window().setSize(parseInt(width, 10), parseInt(height, 10));
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
/**
|
|
1855
|
+
* Drag an item to a destination element.
|
|
1856
|
+
*
|
|
1857
|
+
* ```js
|
|
1858
|
+
* I.dragAndDrop('#dragHandle', '#container');
|
|
1859
|
+
* ```
|
|
1860
|
+
*
|
|
1861
|
+
* @param {LocatorOrString} srcElement located by CSS|XPath|strict locator.
|
|
1862
|
+
* @param {LocatorOrString} destElement located by CSS|XPath|strict locator.
|
|
1863
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
1864
|
+
*
|
|
1865
|
+
*/
|
|
1866
|
+
async dragAndDrop(srcElement, destElement) {
|
|
1867
|
+
const srcEl = await this._locate(srcElement, true);
|
|
1868
|
+
const destEl = await this._locate(destElement, true);
|
|
1869
|
+
assertElementExists(srcEl, srcElement);
|
|
1870
|
+
assertElementExists(destEl, destElement);
|
|
1871
|
+
return this.browser
|
|
1872
|
+
.actions()
|
|
1873
|
+
.dragAndDrop(srcEl[0], destEl[0])
|
|
1874
|
+
.perform();
|
|
1875
|
+
}
|
|
1876
|
+
|
|
1877
|
+
/**
|
|
1878
|
+
* Close all tabs except for the current one.
|
|
1879
|
+
*
|
|
1880
|
+
* ```js
|
|
1881
|
+
* I.closeOtherTabs();
|
|
1882
|
+
* ```
|
|
1883
|
+
*/
|
|
1884
|
+
async closeOtherTabs() {
|
|
1885
|
+
const client = this.browser;
|
|
1886
|
+
|
|
1887
|
+
const handles = await client.getAllWindowHandles();
|
|
1888
|
+
const currentHandle = await client.getWindowHandle();
|
|
1889
|
+
const otherHandles = handles.filter(handle => handle !== currentHandle);
|
|
1890
|
+
|
|
1891
|
+
if (!otherHandles || !otherHandles.length) return;
|
|
1892
|
+
let p = Promise.resolve();
|
|
1893
|
+
otherHandles.forEach((handle) => {
|
|
1894
|
+
p = p.then(() => client.switchTo().window(handle).then(() => client.close()));
|
|
1895
|
+
});
|
|
1896
|
+
p = p.then(() => client.switchTo().window(currentHandle));
|
|
1897
|
+
return p;
|
|
1898
|
+
}
|
|
1899
|
+
|
|
1900
|
+
/**
|
|
1901
|
+
* Close current tab
|
|
1902
|
+
*
|
|
1903
|
+
* ```js
|
|
1904
|
+
* I.closeCurrentTab();
|
|
1905
|
+
* ```
|
|
1906
|
+
*/
|
|
1907
|
+
async closeCurrentTab() {
|
|
1908
|
+
const client = this.browser;
|
|
1909
|
+
|
|
1910
|
+
const currentHandle = await client.getWindowHandle();
|
|
1911
|
+
const nextHandle = await this._getWindowHandle(-1);
|
|
1912
|
+
|
|
1913
|
+
await client.switchTo().window(currentHandle);
|
|
1914
|
+
await client.close();
|
|
1915
|
+
return client.switchTo().window(nextHandle);
|
|
1916
|
+
}
|
|
1917
|
+
|
|
1918
|
+
/**
|
|
1919
|
+
* Get the window handle relative to the current handle. i.e. the next handle or the previous.
|
|
1920
|
+
* @param {Number} offset Offset from current handle index. i.e. offset < 0 will go to the previous handle and positive number will go to the next window handle in sequence.
|
|
1921
|
+
*/
|
|
1922
|
+
async _getWindowHandle(offset = 0) {
|
|
1923
|
+
const client = this.browser;
|
|
1924
|
+
const handles = await client.getAllWindowHandles();
|
|
1925
|
+
const index = handles.indexOf(await client.getWindowHandle());
|
|
1926
|
+
const nextIndex = index + offset;
|
|
1927
|
+
|
|
1928
|
+
return handles[nextIndex];
|
|
1929
|
+
// return handles[(index + offset) % handles.length];
|
|
1930
|
+
}
|
|
1931
|
+
|
|
1932
|
+
/**
|
|
1933
|
+
* Open new tab and switch to it
|
|
1934
|
+
*
|
|
1935
|
+
* ```js
|
|
1936
|
+
* I.openNewTab();
|
|
1937
|
+
* ```
|
|
1938
|
+
*/
|
|
1939
|
+
async openNewTab() {
|
|
1940
|
+
const client = this.browser;
|
|
1941
|
+
await this.executeScript('window.open("about:blank")');
|
|
1942
|
+
const handles = await client.getAllWindowHandles();
|
|
1943
|
+
await client.switchTo().window(handles[handles.length - 1]);
|
|
1944
|
+
}
|
|
1945
|
+
|
|
1946
|
+
/**
|
|
1947
|
+
* Switch focus to a particular tab by its number. It waits tabs loading and then switch tab
|
|
1948
|
+
*
|
|
1949
|
+
* ```js
|
|
1950
|
+
* I.switchToNextTab();
|
|
1951
|
+
* I.switchToNextTab(2);
|
|
1952
|
+
* ```
|
|
1953
|
+
*/
|
|
1954
|
+
async switchToNextTab(num = 1) {
|
|
1955
|
+
const client = this.browser;
|
|
1956
|
+
const newHandle = await this._getWindowHandle(num);
|
|
1957
|
+
|
|
1958
|
+
if (!newHandle) {
|
|
1959
|
+
throw new Error(`There is no ability to switch to next tab with offset ${num}`);
|
|
1960
|
+
}
|
|
1961
|
+
return client.switchTo().window(newHandle);
|
|
1962
|
+
}
|
|
1963
|
+
|
|
1964
|
+
/**
|
|
1965
|
+
* Switch focus to a particular tab by its number. It waits tabs loading and then switch tab
|
|
1966
|
+
*
|
|
1967
|
+
* ```js
|
|
1968
|
+
* I.switchToPreviousTab();
|
|
1969
|
+
* I.switchToPreviousTab(2);
|
|
1970
|
+
* ```
|
|
1971
|
+
*/
|
|
1972
|
+
async switchToPreviousTab(num = 1) {
|
|
1973
|
+
const client = this.browser;
|
|
1974
|
+
const newHandle = await this._getWindowHandle(-1 * num);
|
|
1975
|
+
|
|
1976
|
+
if (!newHandle) {
|
|
1977
|
+
throw new Error(`There is no ability to switch to previous tab with offset ${num}`);
|
|
1978
|
+
}
|
|
1979
|
+
return client.switchTo().window(newHandle);
|
|
1980
|
+
}
|
|
1981
|
+
|
|
1982
|
+
/**
|
|
1983
|
+
* Grab number of open tabs.
|
|
1984
|
+
* Resumes test execution, so **should be used inside async function with `await`** operator.
|
|
1985
|
+
*
|
|
1986
|
+
* ```js
|
|
1987
|
+
* let tabs = await I.grabNumberOfOpenTabs();
|
|
1988
|
+
* ```
|
|
1989
|
+
*
|
|
1990
|
+
* @returns {Promise<number>} number of open tabs
|
|
1991
|
+
*
|
|
1992
|
+
*/
|
|
1993
|
+
async grabNumberOfOpenTabs() {
|
|
1994
|
+
const pages = await this.browser.getAllWindowHandles();
|
|
1995
|
+
return pages.length;
|
|
1996
|
+
}
|
|
1997
|
+
|
|
1998
|
+
/**
|
|
1999
|
+
* Switches frame or in case of null locator reverts to parent.
|
|
2000
|
+
*
|
|
2001
|
+
* ```js
|
|
2002
|
+
* I.switchTo('iframe'); // switch to first iframe
|
|
2003
|
+
* I.switchTo(); // switch back to main page
|
|
2004
|
+
* ```
|
|
2005
|
+
*
|
|
2006
|
+
* @param {?CodeceptJS.LocatorOrString} [locator=null] (optional, `null` by default) element located by CSS|XPath|strict locator.
|
|
2007
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2008
|
+
*
|
|
2009
|
+
*/
|
|
2010
|
+
async switchTo(locator) {
|
|
2011
|
+
if (Number.isInteger(locator)) {
|
|
2012
|
+
return this.browser.switchTo().frame(locator);
|
|
2013
|
+
}
|
|
2014
|
+
if (!locator) {
|
|
2015
|
+
return this.browser.switchTo().frame(null);
|
|
2016
|
+
}
|
|
2017
|
+
|
|
2018
|
+
const els = await this._locate(withStrictLocator.call(this, locator), true);
|
|
2019
|
+
assertElementExists(els, locator);
|
|
2020
|
+
return this.browser.switchTo().frame(els[0]);
|
|
2021
|
+
}
|
|
2022
|
+
|
|
2023
|
+
/**
|
|
2024
|
+
* Pauses execution for a number of seconds.
|
|
2025
|
+
*
|
|
2026
|
+
* ```js
|
|
2027
|
+
* I.wait(2); // wait 2 secs
|
|
2028
|
+
* ```
|
|
2029
|
+
*
|
|
2030
|
+
* @param {number} sec number of second to wait.
|
|
2031
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2032
|
+
*
|
|
2033
|
+
*/
|
|
2034
|
+
wait(sec) {
|
|
2035
|
+
return this.browser.sleep(sec * 1000);
|
|
2036
|
+
}
|
|
2037
|
+
|
|
2038
|
+
/**
|
|
2039
|
+
* Waits for element to be present on page (by default waits for 1sec).
|
|
2040
|
+
* Element can be located by CSS or XPath.
|
|
2041
|
+
*
|
|
2042
|
+
* ```js
|
|
2043
|
+
* I.waitForElement('.btn.continue');
|
|
2044
|
+
* I.waitForElement('.btn.continue', 5); // wait for 5 secs
|
|
2045
|
+
* ```
|
|
2046
|
+
*
|
|
2047
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
2048
|
+
* @param {number} [sec] (optional, `1` by default) time in seconds to wait
|
|
2049
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2050
|
+
*
|
|
2051
|
+
*/
|
|
2052
|
+
async waitForElement(locator, sec = null) {
|
|
2053
|
+
const aSec = sec || this.options.waitForTimeoutInSeconds;
|
|
2054
|
+
const el = global.element(guessLocator(locator) || global.by.css(locator));
|
|
2055
|
+
return this.browser.wait(EC.presenceOf(el), aSec * 1000);
|
|
2056
|
+
}
|
|
2057
|
+
|
|
2058
|
+
async waitUntilExists(locator, sec = null) {
|
|
2059
|
+
console.log(`waitUntilExists deprecated:
|
|
2060
|
+
* use 'waitForElement' to wait for element to be attached
|
|
2061
|
+
* use 'waitForDetached to wait for element to be removed'`);
|
|
2062
|
+
return this.waitForDetached(locator, sec);
|
|
2063
|
+
}
|
|
2064
|
+
|
|
2065
|
+
/**
|
|
2066
|
+
* Waits for an element to become not attached to the DOM on a page (by default waits for 1sec).
|
|
2067
|
+
* Element can be located by CSS or XPath.
|
|
2068
|
+
*
|
|
2069
|
+
* ```js
|
|
2070
|
+
* I.waitForDetached('#popup');
|
|
2071
|
+
* ```
|
|
2072
|
+
*
|
|
2073
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
2074
|
+
* @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
|
|
2075
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2076
|
+
*
|
|
2077
|
+
*/
|
|
2078
|
+
async waitForDetached(locator, sec = null) {
|
|
2079
|
+
const aSec = sec || this.options.waitForTimeoutInSeconds;
|
|
2080
|
+
const el = global.element(guessLocator(locator) || global.by.css(locator));
|
|
2081
|
+
return this.browser.wait(EC.not(EC.presenceOf(el)), aSec * 1000).catch((err) => {
|
|
2082
|
+
if (err.message && err.message.indexOf('Wait timed out after') > -1) {
|
|
2083
|
+
throw new Error(`element (${JSON.stringify(locator)}) still on page after ${sec} sec`);
|
|
2084
|
+
} else throw err;
|
|
2085
|
+
});
|
|
2086
|
+
}
|
|
2087
|
+
|
|
2088
|
+
/**
|
|
2089
|
+
* Waits for element to become clickable for number of seconds.
|
|
2090
|
+
*
|
|
2091
|
+
* ```js
|
|
2092
|
+
* I.waitForClickable('#link');
|
|
2093
|
+
* ```
|
|
2094
|
+
*/
|
|
2095
|
+
async waitForClickable(locator, sec = null) {
|
|
2096
|
+
const aSec = sec || this.options.waitForTimeoutInSeconds;
|
|
2097
|
+
const el = global.element(guessLocator(locator) || global.by.css(locator));
|
|
2098
|
+
return this.browser.wait(EC.elementToBeClickable(el), aSec * 1000);
|
|
2099
|
+
}
|
|
2100
|
+
|
|
2101
|
+
/**
|
|
2102
|
+
* Waits for an element to become visible on a page (by default waits for 1sec).
|
|
2103
|
+
* Element can be located by CSS or XPath.
|
|
2104
|
+
*
|
|
2105
|
+
* ```js
|
|
2106
|
+
* I.waitForVisible('#popup');
|
|
2107
|
+
* ```
|
|
2108
|
+
*
|
|
2109
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
2110
|
+
* @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
|
|
2111
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2112
|
+
*
|
|
2113
|
+
*/
|
|
2114
|
+
async waitForVisible(locator, sec = null) {
|
|
2115
|
+
const aSec = sec || this.options.waitForTimeoutInSeconds;
|
|
2116
|
+
const el = global.element(guessLocator(locator) || global.by.css(locator));
|
|
2117
|
+
return this.browser.wait(EC.visibilityOf(el), aSec * 1000);
|
|
2118
|
+
}
|
|
2119
|
+
|
|
2120
|
+
/**
|
|
2121
|
+
* Waits for an element to hide (by default waits for 1sec).
|
|
2122
|
+
* Element can be located by CSS or XPath.
|
|
2123
|
+
*
|
|
2124
|
+
* ```js
|
|
2125
|
+
* I.waitToHide('#popup');
|
|
2126
|
+
* ```
|
|
2127
|
+
*
|
|
2128
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
2129
|
+
* @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
|
|
2130
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2131
|
+
*
|
|
2132
|
+
*/
|
|
2133
|
+
async waitToHide(locator, sec = null) {
|
|
2134
|
+
return this.waitForInvisible(locator, sec);
|
|
2135
|
+
}
|
|
2136
|
+
|
|
2137
|
+
/**
|
|
2138
|
+
* Waits for an element to be removed or become invisible on a page (by default waits for 1sec).
|
|
2139
|
+
* Element can be located by CSS or XPath.
|
|
2140
|
+
*
|
|
2141
|
+
* ```js
|
|
2142
|
+
* I.waitForInvisible('#popup');
|
|
2143
|
+
* ```
|
|
2144
|
+
*
|
|
2145
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
2146
|
+
* @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
|
|
2147
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2148
|
+
*
|
|
2149
|
+
*/
|
|
2150
|
+
async waitForInvisible(locator, sec = null) {
|
|
2151
|
+
const aSec = sec || this.options.waitForTimeoutInSeconds;
|
|
2152
|
+
const el = global.element(guessLocator(locator) || global.by.css(locator));
|
|
2153
|
+
return this.browser.wait(EC.invisibilityOf(el), aSec * 1000);
|
|
2154
|
+
}
|
|
2155
|
+
|
|
2156
|
+
async waitForStalenessOf(locator, sec = null) {
|
|
2157
|
+
console.log(`waitForStalenessOf deprecated.
|
|
2158
|
+
* Use waitForDetached to wait for element to be removed from page
|
|
2159
|
+
* Use waitForInvisible to wait for element to be hidden on page`);
|
|
2160
|
+
return this.waitForInvisible(locator, sec);
|
|
2161
|
+
}
|
|
2162
|
+
|
|
2163
|
+
/**
|
|
2164
|
+
* Waits for a specified number of elements on the page.
|
|
2165
|
+
*
|
|
2166
|
+
* ```js
|
|
2167
|
+
* I.waitNumberOfVisibleElements('a', 3);
|
|
2168
|
+
* ```
|
|
2169
|
+
*
|
|
2170
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
2171
|
+
* @param {number} num number of elements.
|
|
2172
|
+
* @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
|
|
2173
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2174
|
+
*
|
|
2175
|
+
*/
|
|
2176
|
+
async waitNumberOfVisibleElements(locator, num, sec = null) {
|
|
2177
|
+
function visibilityCountOf(loc, expectedCount) {
|
|
2178
|
+
return function () {
|
|
2179
|
+
return global.element.all(loc)
|
|
2180
|
+
.filter(el => el.isDisplayed())
|
|
2181
|
+
.count()
|
|
2182
|
+
.then(count => count === expectedCount);
|
|
2183
|
+
};
|
|
2184
|
+
}
|
|
2185
|
+
|
|
2186
|
+
const aSec = sec || this.options.waitForTimeoutInSeconds;
|
|
2187
|
+
const guessLoc = guessLocator(locator) || global.by.css(locator);
|
|
2188
|
+
|
|
2189
|
+
return this.browser.wait(visibilityCountOf(guessLoc, num), aSec * 1000)
|
|
2190
|
+
.catch(() => {
|
|
2191
|
+
throw Error(`The number of elements (${(new Locator(locator))}) is not ${num} after ${aSec} sec`);
|
|
2192
|
+
});
|
|
2193
|
+
}
|
|
2194
|
+
|
|
2195
|
+
/**
|
|
2196
|
+
* Waits for element to become enabled (by default waits for 1sec).
|
|
2197
|
+
* Element can be located by CSS or XPath.
|
|
2198
|
+
*
|
|
2199
|
+
* @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
|
|
2200
|
+
* @param {number} [sec=1] (optional) time in seconds to wait, 1 by default.
|
|
2201
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2202
|
+
*
|
|
2203
|
+
*/
|
|
2204
|
+
async waitForEnabled(locator, sec = null) {
|
|
2205
|
+
const aSec = sec || this.options.waitForTimeoutInSeconds;
|
|
2206
|
+
const el = global.element(guessLocator(locator) || global.by.css(locator));
|
|
2207
|
+
|
|
2208
|
+
return this.browser.wait(EC.elementToBeClickable(el), aSec * 1000)
|
|
2209
|
+
.catch(() => {
|
|
2210
|
+
throw Error(`element (${(new Locator(locator))}) still not enabled after ${aSec} sec`);
|
|
2211
|
+
});
|
|
2212
|
+
}
|
|
2213
|
+
|
|
2214
|
+
/**
|
|
2215
|
+
* Waits for the specified value to be in value attribute.
|
|
2216
|
+
*
|
|
2217
|
+
* ```js
|
|
2218
|
+
* I.waitForValue('//input', "GoodValue");
|
|
2219
|
+
* ```
|
|
2220
|
+
*
|
|
2221
|
+
* @param {LocatorOrString} field input field.
|
|
2222
|
+
* @param {string }value expected value.
|
|
2223
|
+
* @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
|
|
2224
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2225
|
+
*
|
|
2226
|
+
*/
|
|
2227
|
+
async waitForValue(field, value, sec = null) {
|
|
2228
|
+
const aSec = sec || this.options.waitForTimeoutInSeconds;
|
|
2229
|
+
|
|
2230
|
+
const valueToBeInElementValue = (loc) => {
|
|
2231
|
+
return async () => {
|
|
2232
|
+
const els = await findFields(this.browser, loc);
|
|
2233
|
+
|
|
2234
|
+
if (!els) {
|
|
2235
|
+
return false;
|
|
2236
|
+
}
|
|
2237
|
+
const values = await Promise.all(els.map(el => el.getAttribute('value')));
|
|
2238
|
+
return values.filter(part => part.indexOf(value) >= 0).length > 0;
|
|
2239
|
+
};
|
|
2240
|
+
};
|
|
2241
|
+
|
|
2242
|
+
return this.browser.wait(valueToBeInElementValue(field, value), aSec * 1000)
|
|
2243
|
+
.catch(() => {
|
|
2244
|
+
throw Error(`element (${field}) is not in DOM or there is no element(${field}) with value "${value}" after ${aSec} sec`);
|
|
2245
|
+
});
|
|
2246
|
+
}
|
|
2247
|
+
|
|
2248
|
+
/**
|
|
2249
|
+
* Waits for a function to return true (waits for 1 sec by default).
|
|
2250
|
+
* Running in browser context.
|
|
2251
|
+
*
|
|
2252
|
+
* ```js
|
|
2253
|
+
* I.waitForFunction(fn[, [args[, timeout]])
|
|
2254
|
+
* ```
|
|
2255
|
+
*
|
|
2256
|
+
* ```js
|
|
2257
|
+
* I.waitForFunction(() => window.requests == 0);
|
|
2258
|
+
* I.waitForFunction(() => window.requests == 0, 5); // waits for 5 sec
|
|
2259
|
+
* I.waitForFunction((count) => window.requests == count, [3], 5) // pass args and wait for 5 sec
|
|
2260
|
+
* ```
|
|
2261
|
+
*
|
|
2262
|
+
* @param {string|function} fn to be executed in browser context.
|
|
2263
|
+
* @param {any[]|number} [argsOrSec] (optional, `1` by default) arguments for function or seconds.
|
|
2264
|
+
* @param {number} [sec] (optional, `1` by default) time in seconds to wait
|
|
2265
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2266
|
+
*
|
|
2267
|
+
*/
|
|
2268
|
+
async waitForFunction(fn, argsOrSec = null, sec = null) {
|
|
2269
|
+
let args = [];
|
|
2270
|
+
if (argsOrSec) {
|
|
2271
|
+
if (Array.isArray(argsOrSec)) {
|
|
2272
|
+
args = argsOrSec;
|
|
2273
|
+
} else if (typeof argsOrSec === 'number') {
|
|
2274
|
+
sec = argsOrSec;
|
|
2275
|
+
}
|
|
2276
|
+
}
|
|
2277
|
+
|
|
2278
|
+
const aSec = sec || this.options.waitForTimeoutInSeconds;
|
|
2279
|
+
return this.browser.wait(() => this.browser.executeScript.call(this.browser, fn, ...args), aSec * 1000);
|
|
2280
|
+
}
|
|
2281
|
+
|
|
2282
|
+
/**
|
|
2283
|
+
* Waiting for the part of the URL to match the expected. Useful for SPA to understand that page was changed.
|
|
2284
|
+
*
|
|
2285
|
+
* ```js
|
|
2286
|
+
* I.waitInUrl('/info', 2);
|
|
2287
|
+
* ```
|
|
2288
|
+
*
|
|
2289
|
+
* @param {string} urlPart value to check.
|
|
2290
|
+
* @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
|
|
2291
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2292
|
+
*
|
|
2293
|
+
*/
|
|
2294
|
+
async waitInUrl(urlPart, sec = null) {
|
|
2295
|
+
const aSec = sec || this.options.waitForTimeoutInSeconds;
|
|
2296
|
+
const waitTimeout = aSec * 1000;
|
|
2297
|
+
|
|
2298
|
+
return this.browser.wait(EC.urlContains(urlPart), waitTimeout)
|
|
2299
|
+
.catch(async (e) => {
|
|
2300
|
+
const currUrl = await this.browser.getCurrentUrl();
|
|
2301
|
+
if (/wait timed out after/i.test(e.message)) {
|
|
2302
|
+
throw new Error(`expected url to include ${urlPart}, but found ${currUrl}`);
|
|
2303
|
+
} else {
|
|
2304
|
+
throw e;
|
|
2305
|
+
}
|
|
2306
|
+
});
|
|
2307
|
+
}
|
|
2308
|
+
|
|
2309
|
+
/**
|
|
2310
|
+
* Waits for the entire URL to match the expected
|
|
2311
|
+
*
|
|
2312
|
+
* ```js
|
|
2313
|
+
* I.waitUrlEquals('/info', 2);
|
|
2314
|
+
* I.waitUrlEquals('http://127.0.0.1:8000/info');
|
|
2315
|
+
* ```
|
|
2316
|
+
*
|
|
2317
|
+
* @param {string} urlPart value to check.
|
|
2318
|
+
* @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
|
|
2319
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2320
|
+
*
|
|
2321
|
+
*/
|
|
2322
|
+
async waitUrlEquals(urlPart, sec = null) {
|
|
2323
|
+
const aSec = sec || this.options.waitForTimeoutInSeconds;
|
|
2324
|
+
const waitTimeout = aSec * 1000;
|
|
2325
|
+
const baseUrl = this.options.url;
|
|
2326
|
+
if (urlPart.indexOf('http') < 0) {
|
|
2327
|
+
urlPart = baseUrl + urlPart;
|
|
2328
|
+
}
|
|
2329
|
+
|
|
2330
|
+
return this.browser.wait(EC.urlIs(urlPart), waitTimeout)
|
|
2331
|
+
.catch(async (e) => {
|
|
2332
|
+
const currUrl = await this.browser.getCurrentUrl();
|
|
2333
|
+
if (/wait timed out after/i.test(e.message)) {
|
|
2334
|
+
throw new Error(`expected url to be ${urlPart}, but found ${currUrl}`);
|
|
2335
|
+
} else {
|
|
2336
|
+
throw e;
|
|
2337
|
+
}
|
|
2338
|
+
});
|
|
2339
|
+
}
|
|
2340
|
+
|
|
2341
|
+
/**
|
|
2342
|
+
* Waits for a text to appear (by default waits for 1sec).
|
|
2343
|
+
* Element can be located by CSS or XPath.
|
|
2344
|
+
* Narrow down search results by providing context.
|
|
2345
|
+
*
|
|
2346
|
+
* ```js
|
|
2347
|
+
* I.waitForText('Thank you, form has been submitted');
|
|
2348
|
+
* I.waitForText('Thank you, form has been submitted', 5, '#modal');
|
|
2349
|
+
* ```
|
|
2350
|
+
*
|
|
2351
|
+
* @param {string }text to wait for.
|
|
2352
|
+
* @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
|
|
2353
|
+
* @param {CodeceptJS.LocatorOrString} [context] (optional) element located by CSS|XPath|strict locator.
|
|
2354
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2355
|
+
*
|
|
2356
|
+
*/
|
|
2357
|
+
async waitForText(text, sec = null, context = null) {
|
|
2358
|
+
if (!context) {
|
|
2359
|
+
context = this.context;
|
|
2360
|
+
}
|
|
2361
|
+
const el = global.element(guessLocator(context) || global.by.css(context));
|
|
2362
|
+
const aSec = sec || this.options.waitForTimeoutInSeconds;
|
|
2363
|
+
return this.browser.wait(EC.textToBePresentInElement(el, text), aSec * 1000);
|
|
2364
|
+
}
|
|
2365
|
+
|
|
2366
|
+
// ANGULAR SPECIFIC
|
|
2367
|
+
|
|
2368
|
+
/**
|
|
2369
|
+
* Moves to url
|
|
2370
|
+
*/
|
|
2371
|
+
moveTo(path) {
|
|
2372
|
+
return this.browser.setLocation(path);
|
|
2373
|
+
}
|
|
2374
|
+
|
|
2375
|
+
/**
|
|
2376
|
+
* Reload the current page.
|
|
2377
|
+
*
|
|
2378
|
+
* ```js
|
|
2379
|
+
* I.refreshPage();
|
|
2380
|
+
* ```
|
|
2381
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2382
|
+
*
|
|
2383
|
+
*/
|
|
2384
|
+
refreshPage() {
|
|
2385
|
+
return this.browser.refresh();
|
|
2386
|
+
}
|
|
2387
|
+
|
|
2388
|
+
/**
|
|
2389
|
+
* Reloads page
|
|
2390
|
+
*/
|
|
2391
|
+
refresh() {
|
|
2392
|
+
console.log('Deprecated in favor of refreshPage');
|
|
2393
|
+
return this.browser.refresh();
|
|
2394
|
+
}
|
|
2395
|
+
|
|
2396
|
+
/**
|
|
2397
|
+
* Scrolls to element matched by locator.
|
|
2398
|
+
* Extra shift can be set with offsetX and offsetY options.
|
|
2399
|
+
*
|
|
2400
|
+
* ```js
|
|
2401
|
+
* I.scrollTo('footer');
|
|
2402
|
+
* I.scrollTo('#submit', 5, 5);
|
|
2403
|
+
* ```
|
|
2404
|
+
*
|
|
2405
|
+
* @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|strict locator.
|
|
2406
|
+
* @param {number} [offsetX=0] (optional, `0` by default) X-axis offset.
|
|
2407
|
+
* @param {number} [offsetY=0] (optional, `0` by default) Y-axis offset.
|
|
2408
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2409
|
+
*
|
|
2410
|
+
*/
|
|
2411
|
+
async scrollTo(locator, offsetX = 0, offsetY = 0) {
|
|
2412
|
+
if (typeof locator === 'number' && typeof offsetX === 'number') {
|
|
2413
|
+
offsetY = offsetX;
|
|
2414
|
+
offsetX = locator;
|
|
2415
|
+
locator = null;
|
|
2416
|
+
}
|
|
2417
|
+
|
|
2418
|
+
if (locator) {
|
|
2419
|
+
const res = await this._locate(locator, true);
|
|
2420
|
+
if (!res || res.length === 0) {
|
|
2421
|
+
return truth(`elements of ${(new Locator(locator))}`, 'to be seen').assert(false);
|
|
2422
|
+
}
|
|
2423
|
+
const elem = res[0];
|
|
2424
|
+
const location = await elem.getLocation();
|
|
2425
|
+
/* eslint-disable prefer-arrow-callback */
|
|
2426
|
+
return this.executeScript(function (x, y) { return window.scrollTo(x, y); }, location.x + offsetX, location.y + offsetY);
|
|
2427
|
+
/* eslint-enable */
|
|
2428
|
+
}
|
|
2429
|
+
|
|
2430
|
+
/* eslint-disable prefer-arrow-callback, comma-dangle */
|
|
2431
|
+
return this.executeScript(function (x, y) { return window.scrollTo(x, y); }, offsetX, offsetY);
|
|
2432
|
+
/* eslint-enable */
|
|
2433
|
+
}
|
|
2434
|
+
|
|
2435
|
+
/**
|
|
2436
|
+
* Scroll page to the top.
|
|
2437
|
+
*
|
|
2438
|
+
* ```js
|
|
2439
|
+
* I.scrollPageToTop();
|
|
2440
|
+
* ```
|
|
2441
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2442
|
+
*
|
|
2443
|
+
*/
|
|
2444
|
+
async scrollPageToTop() {
|
|
2445
|
+
return this.executeScript('window.scrollTo(0, 0);');
|
|
2446
|
+
}
|
|
2447
|
+
|
|
2448
|
+
/**
|
|
2449
|
+
* Scroll page to the bottom.
|
|
2450
|
+
*
|
|
2451
|
+
* ```js
|
|
2452
|
+
* I.scrollPageToBottom();
|
|
2453
|
+
* ```
|
|
2454
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2455
|
+
*
|
|
2456
|
+
*/
|
|
2457
|
+
async scrollPageToBottom() {
|
|
2458
|
+
/* eslint-disable prefer-arrow-callback, comma-dangle */
|
|
2459
|
+
return this.executeScript(function () {
|
|
2460
|
+
const body = document.body;
|
|
2461
|
+
const html = document.documentElement;
|
|
2462
|
+
window.scrollTo(0, Math.max(
|
|
2463
|
+
body.scrollHeight,
|
|
2464
|
+
body.offsetHeight,
|
|
2465
|
+
html.clientHeight,
|
|
2466
|
+
html.scrollHeight,
|
|
2467
|
+
html.offsetHeight
|
|
2468
|
+
));
|
|
2469
|
+
});
|
|
2470
|
+
/* eslint-enable */
|
|
2471
|
+
}
|
|
2472
|
+
|
|
2473
|
+
/**
|
|
2474
|
+
* Retrieves a page scroll position and returns it to test.
|
|
2475
|
+
* Resumes test execution, so **should be used inside an async function with `await`** operator.
|
|
2476
|
+
*
|
|
2477
|
+
* ```js
|
|
2478
|
+
* let { x, y } = await I.grabPageScrollPosition();
|
|
2479
|
+
* ```
|
|
2480
|
+
*
|
|
2481
|
+
* @returns {Promise<PageScrollPosition>} scroll position
|
|
2482
|
+
*
|
|
2483
|
+
*/
|
|
2484
|
+
async grabPageScrollPosition() {
|
|
2485
|
+
/* eslint-disable comma-dangle */
|
|
2486
|
+
function getScrollPosition() {
|
|
2487
|
+
return {
|
|
2488
|
+
x: window.pageXOffset,
|
|
2489
|
+
y: window.pageYOffset
|
|
2490
|
+
};
|
|
2491
|
+
}
|
|
2492
|
+
/* eslint-enable comma-dangle */
|
|
2493
|
+
return this.executeScript(getScrollPosition);
|
|
2494
|
+
}
|
|
2495
|
+
|
|
2496
|
+
/**
|
|
2497
|
+
* Injects Angular module.
|
|
2498
|
+
*
|
|
2499
|
+
* ```js
|
|
2500
|
+
* I.haveModule('modName', function() {
|
|
2501
|
+
* angular.module('modName', []).value('foo', 'bar');
|
|
2502
|
+
* });
|
|
2503
|
+
* ```
|
|
2504
|
+
*/
|
|
2505
|
+
haveModule(modName, fn) {
|
|
2506
|
+
return this.browser.addMockModule(modName, fn);
|
|
2507
|
+
}
|
|
2508
|
+
|
|
2509
|
+
/**
|
|
2510
|
+
* Removes mocked Angular module. If modName not specified - clears all mock modules.
|
|
2511
|
+
*
|
|
2512
|
+
* ```js
|
|
2513
|
+
* I.resetModule(); // clears all
|
|
2514
|
+
* I.resetModule('modName');
|
|
2515
|
+
* ```
|
|
2516
|
+
*/
|
|
2517
|
+
resetModule(modName) {
|
|
2518
|
+
if (!modName) {
|
|
2519
|
+
return this.browser.clearMockModules();
|
|
2520
|
+
}
|
|
2521
|
+
return this.browser.removeMockModule(modName);
|
|
2522
|
+
}
|
|
2523
|
+
|
|
2524
|
+
/**
|
|
2525
|
+
* Sets cookie(s).
|
|
2526
|
+
*
|
|
2527
|
+
* Can be a single cookie object or an array of cookies:
|
|
2528
|
+
*
|
|
2529
|
+
* ```js
|
|
2530
|
+
* I.setCookie({name: 'auth', value: true});
|
|
2531
|
+
*
|
|
2532
|
+
* // as array
|
|
2533
|
+
* I.setCookie([
|
|
2534
|
+
* {name: 'auth', value: true},
|
|
2535
|
+
* {name: 'agree', value: true}
|
|
2536
|
+
* ]);
|
|
2537
|
+
* ```
|
|
2538
|
+
*
|
|
2539
|
+
* @param {Cookie|Array<Cookie>} cookie a cookie object or array of cookie objects.
|
|
2540
|
+
* @returns {void} automatically synchronized promise through #recorder
|
|
2541
|
+
*
|
|
2542
|
+
*/
|
|
2543
|
+
setCookie(cookie) {
|
|
2544
|
+
return this.browser.manage().addCookie(cookie);
|
|
2545
|
+
}
|
|
2546
|
+
}
|
|
2547
|
+
|
|
2548
|
+
module.exports = Protractor;
|
|
2549
|
+
|
|
2550
|
+
async function findCheckable(client, locator) {
|
|
2551
|
+
const matchedLocator = guessLocator(locator);
|
|
2552
|
+
if (matchedLocator) {
|
|
2553
|
+
return client.findElements(matchedLocator);
|
|
2554
|
+
}
|
|
2555
|
+
const literal = xpathLocator.literal(locator);
|
|
2556
|
+
let els = await client.findElements(global.by.xpath(Locator.checkable.byText(literal)));
|
|
2557
|
+
if (els.length) {
|
|
2558
|
+
return els;
|
|
2559
|
+
}
|
|
2560
|
+
els = await client.findElements(global.by.xpath(Locator.checkable.byName(literal)));
|
|
2561
|
+
if (els.length) {
|
|
2562
|
+
return els;
|
|
2563
|
+
}
|
|
2564
|
+
return client.findElements(global.by.css(locator));
|
|
2565
|
+
}
|
|
2566
|
+
|
|
2567
|
+
function withStrictLocator(locator) {
|
|
2568
|
+
locator = new Locator(locator);
|
|
2569
|
+
if (locator.isAccessibilityId()) return withAccessiblitiyLocator.call(this, locator.value);
|
|
2570
|
+
return locator.simplify();
|
|
2571
|
+
}
|
|
2572
|
+
|
|
2573
|
+
function withAccessiblitiyLocator(locator) {
|
|
2574
|
+
if (this.isWeb === false) {
|
|
2575
|
+
return `accessibility id:${locator.slice(1)}`;
|
|
2576
|
+
}
|
|
2577
|
+
return `[aria-label="${locator.slice(1)}"]`;
|
|
2578
|
+
// hook before webdriverio supports native ~ locators in web
|
|
2579
|
+
}
|
|
2580
|
+
|
|
2581
|
+
async function findFields(client, locator) {
|
|
2582
|
+
const matchedLocator = guessLocator(locator);
|
|
2583
|
+
if (matchedLocator) {
|
|
2584
|
+
return client.findElements(matchedLocator);
|
|
2585
|
+
}
|
|
2586
|
+
const literal = xpathLocator.literal(locator);
|
|
2587
|
+
|
|
2588
|
+
let els = await client.findElements(global.by.xpath(Locator.field.labelEquals(literal)));
|
|
2589
|
+
if (els.length) {
|
|
2590
|
+
return els;
|
|
2591
|
+
}
|
|
2592
|
+
|
|
2593
|
+
els = await client.findElements(global.by.xpath(Locator.field.labelContains(literal)));
|
|
2594
|
+
if (els.length) {
|
|
2595
|
+
return els;
|
|
2596
|
+
}
|
|
2597
|
+
els = await client.findElements(global.by.xpath(Locator.field.byName(literal)));
|
|
2598
|
+
if (els.length) {
|
|
2599
|
+
return els;
|
|
2600
|
+
}
|
|
2601
|
+
return client.findElements(global.by.css(locator));
|
|
2602
|
+
}
|
|
2603
|
+
|
|
2604
|
+
async function proceedSee(assertType, text, context) {
|
|
2605
|
+
let description;
|
|
2606
|
+
let locator;
|
|
2607
|
+
if (!context) {
|
|
2608
|
+
if (this.context === this.options.rootElement) {
|
|
2609
|
+
locator = guessLocator(this.context) || global.by.css(this.context);
|
|
2610
|
+
description = 'web application';
|
|
2611
|
+
} else {
|
|
2612
|
+
// inside within block
|
|
2613
|
+
locator = global.by.xpath('.//*');
|
|
2614
|
+
description = `current context ${(new Locator(context)).toString()}`;
|
|
2615
|
+
}
|
|
2616
|
+
} else {
|
|
2617
|
+
locator = guessLocator(context) || global.by.css(context);
|
|
2618
|
+
description = `element ${(new Locator(context)).toString()}`;
|
|
2619
|
+
}
|
|
2620
|
+
const enableSmartWait = !!this.context && assertType === 'assert';
|
|
2621
|
+
const els = await this._smartWait(() => this.browser.findElements(locator), enableSmartWait);
|
|
2622
|
+
const promises = [];
|
|
2623
|
+
let source = '';
|
|
2624
|
+
els.forEach(el => promises.push(el.getText().then(elText => source += `| ${elText}`)));
|
|
2625
|
+
await Promise.all(promises);
|
|
2626
|
+
return stringIncludes(description)[assertType](text, source);
|
|
2627
|
+
}
|
|
2628
|
+
|
|
2629
|
+
async function proceedSeeInField(assertType, field, value) {
|
|
2630
|
+
const els = await findFields(this.browser, field);
|
|
2631
|
+
assertElementExists(els, field, 'Field');
|
|
2632
|
+
const el = els[0];
|
|
2633
|
+
const tag = await el.getTagName();
|
|
2634
|
+
const fieldVal = await el.getAttribute('value');
|
|
2635
|
+
if (tag === 'select') {
|
|
2636
|
+
// locate option by values and check them
|
|
2637
|
+
const literal = xpathLocator.literal(fieldVal);
|
|
2638
|
+
const textEl = await el.findElement(global.by.xpath(Locator.select.byValue(literal)));
|
|
2639
|
+
const text = await textEl.getText();
|
|
2640
|
+
return equals(`select option by ${field}`)[assertType](value, text);
|
|
2641
|
+
}
|
|
2642
|
+
return stringIncludes(`field by ${field}`)[assertType](value, fieldVal);
|
|
2643
|
+
}
|
|
2644
|
+
|
|
2645
|
+
async function proceedIsChecked(assertType, option) {
|
|
2646
|
+
const els = await findCheckable(this.browser, option);
|
|
2647
|
+
assertElementExists(els, option, 'Option');
|
|
2648
|
+
const elsSelected = [];
|
|
2649
|
+
els.forEach(el => elsSelected.push(el.isSelected()));
|
|
2650
|
+
const values = await Promise.all(elsSelected);
|
|
2651
|
+
const selected = values.reduce((prev, cur) => prev || cur);
|
|
2652
|
+
return truth(`checkable ${option}`, 'to be checked')[assertType](selected);
|
|
2653
|
+
}
|
|
2654
|
+
|
|
2655
|
+
async function findClickable(matcher, locator) {
|
|
2656
|
+
locator = new Locator(locator);
|
|
2657
|
+
if (!locator.isFuzzy()) {
|
|
2658
|
+
const els = await this._locate(locator, true);
|
|
2659
|
+
assertElementExists(els, locator.value);
|
|
2660
|
+
return els[0];
|
|
2661
|
+
}
|
|
2662
|
+
const literal = xpathLocator.literal(locator.value);
|
|
2663
|
+
const narrowLocator = Locator.clickable.narrow(literal);
|
|
2664
|
+
let els = await matcher.findElements(global.by.xpath(narrowLocator));
|
|
2665
|
+
if (els.length) {
|
|
2666
|
+
return els[0];
|
|
2667
|
+
}
|
|
2668
|
+
|
|
2669
|
+
els = await matcher.findElements(global.by.xpath(Locator.clickable.wide(literal)));
|
|
2670
|
+
if (els.length) {
|
|
2671
|
+
return els[0];
|
|
2672
|
+
}
|
|
2673
|
+
return matcher.findElement(global.by.css(locator.value));
|
|
2674
|
+
}
|
|
2675
|
+
|
|
2676
|
+
function guessLocator(locator) {
|
|
2677
|
+
const l = new Locator(locator);
|
|
2678
|
+
if (l.isFuzzy()) return false;
|
|
2679
|
+
if (l.type) return global.by[l.type](l.value);
|
|
2680
|
+
return false;
|
|
2681
|
+
}
|
|
2682
|
+
|
|
2683
|
+
function assertElementExists(res, locator, prefix, suffix) {
|
|
2684
|
+
if (!res.length) {
|
|
2685
|
+
throw new ElementNotFound(locator, prefix, suffix);
|
|
2686
|
+
}
|
|
2687
|
+
}
|
|
2688
|
+
|
|
2689
|
+
function isFrameLocator(locator) {
|
|
2690
|
+
locator = new Locator(locator);
|
|
2691
|
+
if (locator.isFrame()) return locator.value;
|
|
2692
|
+
return false;
|
|
2693
|
+
}
|
|
2694
|
+
|
|
2695
|
+
function isWithin() {
|
|
2696
|
+
return Object.keys(withinStore).length !== 0;
|
|
2697
|
+
}
|
|
2698
|
+
|
|
2699
|
+
function loadGlobals(browser) {
|
|
2700
|
+
global.browser = browser;
|
|
2701
|
+
global.$ = browser.$;
|
|
2702
|
+
global.$$ = browser.$$;
|
|
2703
|
+
global.element = browser.element;
|
|
2704
|
+
global.by = global.By = new ProtractorBy();
|
|
2705
|
+
global.ExpectedConditions = EC = new ProtractorExpectedConditions(browser);
|
|
2706
|
+
}
|