codeceptjs 3.5.0 → 3.5.1-2.beta.7

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.
Files changed (273) hide show
  1. package/README.md +24 -25
  2. package/lib/actor.js +6 -3
  3. package/lib/ai.js +12 -3
  4. package/lib/cli.js +12 -2
  5. package/lib/codecept.js +4 -0
  6. package/lib/colorUtils.js +10 -0
  7. package/lib/command/definitions.js +2 -7
  8. package/lib/command/dryRun.js +2 -1
  9. package/lib/command/info.js +24 -0
  10. package/lib/command/init.js +51 -5
  11. package/lib/command/run-multiple/collection.js +17 -5
  12. package/lib/command/run-multiple.js +4 -2
  13. package/lib/command/run-workers.js +66 -4
  14. package/lib/command/run.js +7 -0
  15. package/lib/command/workers/runTests.js +39 -0
  16. package/lib/data/context.js +14 -6
  17. package/lib/event.js +4 -0
  18. package/lib/helper/ApiDataFactory.js +2 -1
  19. package/lib/helper/Appium.js +73 -24
  20. package/lib/helper/Expect.js +422 -0
  21. package/lib/helper/FileSystem.js +1 -1
  22. package/lib/helper/GraphQL.js +25 -0
  23. package/lib/helper/Nightmare.js +9 -4
  24. package/lib/helper/OpenAI.js +14 -10
  25. package/lib/helper/Playwright.js +1205 -288
  26. package/lib/helper/Protractor.js +11 -6
  27. package/lib/helper/Puppeteer.js +173 -61
  28. package/lib/helper/TestCafe.js +44 -9
  29. package/lib/helper/WebDriver.js +231 -82
  30. package/lib/helper/errors/ElementNotFound.js +2 -1
  31. package/lib/helper/extras/PlaywrightReactVueLocator.js +38 -0
  32. package/lib/helper/scripts/blurElement.js +17 -0
  33. package/lib/helper/scripts/focusElement.js +17 -0
  34. package/lib/helper/scripts/highlightElement.js +2 -2
  35. package/lib/html.js +3 -3
  36. package/lib/interfaces/bdd.js +1 -1
  37. package/lib/interfaces/gherkin.js +37 -3
  38. package/lib/interfaces/scenarioConfig.js +1 -0
  39. package/lib/locator.js +17 -4
  40. package/lib/mochaFactory.js +2 -1
  41. package/lib/output.js +1 -1
  42. package/lib/pause.js +12 -9
  43. package/lib/plugin/autoLogin.js +45 -10
  44. package/lib/plugin/heal.js +47 -17
  45. package/lib/plugin/retryFailedStep.js +10 -1
  46. package/lib/plugin/retryTo.js +2 -4
  47. package/lib/plugin/selenoid.js +6 -1
  48. package/lib/plugin/standardActingHelpers.js +0 -2
  49. package/lib/plugin/stepByStepReport.js +2 -2
  50. package/lib/plugin/tryTo.js +5 -7
  51. package/lib/plugin/wdio.js +0 -1
  52. package/lib/recorder.js +20 -9
  53. package/lib/session.js +1 -1
  54. package/lib/step.js +30 -11
  55. package/lib/ui.js +1 -0
  56. package/lib/utils.js +18 -1
  57. package/lib/workers.js +28 -3
  58. package/package.json +108 -98
  59. package/translations/de-DE.js +5 -0
  60. package/translations/fr-FR.js +14 -1
  61. package/translations/it-IT.js +1 -0
  62. package/translations/ja-JP.js +5 -0
  63. package/translations/pl-PL.js +5 -0
  64. package/translations/pt-BR.js +1 -0
  65. package/translations/ru-RU.js +1 -0
  66. package/translations/zh-CN.js +5 -0
  67. package/translations/zh-TW.js +5 -0
  68. package/typings/index.d.ts +8 -6
  69. package/typings/promiseBasedTypes.d.ts +784 -822
  70. package/typings/types.d.ts +1214 -727
  71. package/CHANGELOG.md +0 -2492
  72. package/docs/advanced.md +0 -351
  73. package/docs/ai.md +0 -246
  74. package/docs/api.md +0 -323
  75. package/docs/basics.md +0 -980
  76. package/docs/bdd.md +0 -535
  77. package/docs/best.md +0 -237
  78. package/docs/books.md +0 -37
  79. package/docs/bootstrap.md +0 -135
  80. package/docs/build/ApiDataFactory.js +0 -409
  81. package/docs/build/Appium.js +0 -1978
  82. package/docs/build/FileSystem.js +0 -228
  83. package/docs/build/GraphQL.js +0 -204
  84. package/docs/build/GraphQLDataFactory.js +0 -309
  85. package/docs/build/JSONResponse.js +0 -338
  86. package/docs/build/Mochawesome.js +0 -71
  87. package/docs/build/Nightmare.js +0 -2147
  88. package/docs/build/OpenAI.js +0 -122
  89. package/docs/build/Playwright.js +0 -4134
  90. package/docs/build/Polly.js +0 -42
  91. package/docs/build/Protractor.js +0 -2701
  92. package/docs/build/Puppeteer.js +0 -3743
  93. package/docs/build/REST.js +0 -344
  94. package/docs/build/SeleniumWebdriver.js +0 -76
  95. package/docs/build/TestCafe.js +0 -2059
  96. package/docs/build/WebDriver.js +0 -4042
  97. package/docs/changelog.md +0 -2501
  98. package/docs/commands.md +0 -254
  99. package/docs/community-helpers.md +0 -58
  100. package/docs/configuration.md +0 -157
  101. package/docs/continuous-integration.md +0 -22
  102. package/docs/custom-helpers.md +0 -306
  103. package/docs/data.md +0 -375
  104. package/docs/detox.md +0 -235
  105. package/docs/docker.md +0 -137
  106. package/docs/email.md +0 -183
  107. package/docs/examples.md +0 -149
  108. package/docs/helpers/ApiDataFactory.md +0 -266
  109. package/docs/helpers/Appium.md +0 -1317
  110. package/docs/helpers/Detox.md +0 -586
  111. package/docs/helpers/FileSystem.md +0 -152
  112. package/docs/helpers/GraphQL.md +0 -130
  113. package/docs/helpers/GraphQLDataFactory.md +0 -226
  114. package/docs/helpers/JSONResponse.md +0 -254
  115. package/docs/helpers/Mochawesome.md +0 -8
  116. package/docs/helpers/MockRequest.md +0 -377
  117. package/docs/helpers/Nightmare.md +0 -1258
  118. package/docs/helpers/OpenAI.md +0 -70
  119. package/docs/helpers/Playwright.md +0 -2250
  120. package/docs/helpers/Polly.md +0 -44
  121. package/docs/helpers/Puppeteer-firefox.md +0 -86
  122. package/docs/helpers/Puppeteer.md +0 -2147
  123. package/docs/helpers/REST.md +0 -218
  124. package/docs/helpers/TestCafe.md +0 -1224
  125. package/docs/helpers/WebDriver.md +0 -2325
  126. package/docs/hooks.md +0 -340
  127. package/docs/index.md +0 -111
  128. package/docs/installation.md +0 -75
  129. package/docs/internal-api.md +0 -265
  130. package/docs/locators.md +0 -331
  131. package/docs/mobile-react-native-locators.md +0 -67
  132. package/docs/mobile.md +0 -344
  133. package/docs/nightmare.md +0 -223
  134. package/docs/pageobjects.md +0 -291
  135. package/docs/parallel.md +0 -288
  136. package/docs/playwright.md +0 -609
  137. package/docs/plugins.md +0 -1225
  138. package/docs/puppeteer.md +0 -316
  139. package/docs/quickstart.md +0 -163
  140. package/docs/react.md +0 -69
  141. package/docs/reports.md +0 -392
  142. package/docs/secrets.md +0 -36
  143. package/docs/shadow.md +0 -68
  144. package/docs/shared/keys.mustache +0 -31
  145. package/docs/shared/react.mustache +0 -1
  146. package/docs/testcafe.md +0 -174
  147. package/docs/translation.md +0 -247
  148. package/docs/tutorial.md +0 -271
  149. package/docs/typescript.md +0 -180
  150. package/docs/ui.md +0 -59
  151. package/docs/videos.md +0 -28
  152. package/docs/visual.md +0 -202
  153. package/docs/vue.md +0 -121
  154. package/docs/webapi/amOnPage.mustache +0 -11
  155. package/docs/webapi/appendField.mustache +0 -11
  156. package/docs/webapi/attachFile.mustache +0 -12
  157. package/docs/webapi/checkOption.mustache +0 -13
  158. package/docs/webapi/clearCookie.mustache +0 -10
  159. package/docs/webapi/clearField.mustache +0 -9
  160. package/docs/webapi/click.mustache +0 -25
  161. package/docs/webapi/clickLink.mustache +0 -8
  162. package/docs/webapi/closeCurrentTab.mustache +0 -7
  163. package/docs/webapi/closeOtherTabs.mustache +0 -8
  164. package/docs/webapi/dontSee.mustache +0 -11
  165. package/docs/webapi/dontSeeCheckboxIsChecked.mustache +0 -10
  166. package/docs/webapi/dontSeeCookie.mustache +0 -8
  167. package/docs/webapi/dontSeeCurrentUrlEquals.mustache +0 -10
  168. package/docs/webapi/dontSeeElement.mustache +0 -8
  169. package/docs/webapi/dontSeeElementInDOM.mustache +0 -8
  170. package/docs/webapi/dontSeeInCurrentUrl.mustache +0 -4
  171. package/docs/webapi/dontSeeInField.mustache +0 -11
  172. package/docs/webapi/dontSeeInSource.mustache +0 -8
  173. package/docs/webapi/dontSeeInTitle.mustache +0 -8
  174. package/docs/webapi/doubleClick.mustache +0 -13
  175. package/docs/webapi/downloadFile.mustache +0 -12
  176. package/docs/webapi/dragAndDrop.mustache +0 -9
  177. package/docs/webapi/dragSlider.mustache +0 -11
  178. package/docs/webapi/executeAsyncScript.mustache +0 -24
  179. package/docs/webapi/executeScript.mustache +0 -26
  180. package/docs/webapi/fillField.mustache +0 -16
  181. package/docs/webapi/forceClick.mustache +0 -28
  182. package/docs/webapi/forceRightClick.mustache +0 -18
  183. package/docs/webapi/grabAllWindowHandles.mustache +0 -7
  184. package/docs/webapi/grabAttributeFrom.mustache +0 -10
  185. package/docs/webapi/grabAttributeFromAll.mustache +0 -9
  186. package/docs/webapi/grabBrowserLogs.mustache +0 -9
  187. package/docs/webapi/grabCookie.mustache +0 -11
  188. package/docs/webapi/grabCssPropertyFrom.mustache +0 -11
  189. package/docs/webapi/grabCssPropertyFromAll.mustache +0 -10
  190. package/docs/webapi/grabCurrentUrl.mustache +0 -9
  191. package/docs/webapi/grabCurrentWindowHandle.mustache +0 -6
  192. package/docs/webapi/grabDataFromPerformanceTiming.mustache +0 -20
  193. package/docs/webapi/grabElementBoundingRect.mustache +0 -20
  194. package/docs/webapi/grabGeoLocation.mustache +0 -8
  195. package/docs/webapi/grabHTMLFrom.mustache +0 -10
  196. package/docs/webapi/grabHTMLFromAll.mustache +0 -9
  197. package/docs/webapi/grabNumberOfOpenTabs.mustache +0 -8
  198. package/docs/webapi/grabNumberOfVisibleElements.mustache +0 -9
  199. package/docs/webapi/grabPageScrollPosition.mustache +0 -8
  200. package/docs/webapi/grabPopupText.mustache +0 -5
  201. package/docs/webapi/grabSource.mustache +0 -8
  202. package/docs/webapi/grabTextFrom.mustache +0 -10
  203. package/docs/webapi/grabTextFromAll.mustache +0 -9
  204. package/docs/webapi/grabTitle.mustache +0 -8
  205. package/docs/webapi/grabValueFrom.mustache +0 -9
  206. package/docs/webapi/grabValueFromAll.mustache +0 -8
  207. package/docs/webapi/moveCursorTo.mustache +0 -12
  208. package/docs/webapi/openNewTab.mustache +0 -7
  209. package/docs/webapi/pressKey.mustache +0 -12
  210. package/docs/webapi/pressKeyDown.mustache +0 -12
  211. package/docs/webapi/pressKeyUp.mustache +0 -12
  212. package/docs/webapi/pressKeyWithKeyNormalization.mustache +0 -60
  213. package/docs/webapi/refreshPage.mustache +0 -6
  214. package/docs/webapi/resizeWindow.mustache +0 -6
  215. package/docs/webapi/rightClick.mustache +0 -14
  216. package/docs/webapi/saveElementScreenshot.mustache +0 -10
  217. package/docs/webapi/saveScreenshot.mustache +0 -12
  218. package/docs/webapi/say.mustache +0 -10
  219. package/docs/webapi/scrollIntoView.mustache +0 -11
  220. package/docs/webapi/scrollPageToBottom.mustache +0 -6
  221. package/docs/webapi/scrollPageToTop.mustache +0 -6
  222. package/docs/webapi/scrollTo.mustache +0 -12
  223. package/docs/webapi/see.mustache +0 -11
  224. package/docs/webapi/seeAttributesOnElements.mustache +0 -9
  225. package/docs/webapi/seeCheckboxIsChecked.mustache +0 -10
  226. package/docs/webapi/seeCookie.mustache +0 -8
  227. package/docs/webapi/seeCssPropertiesOnElements.mustache +0 -9
  228. package/docs/webapi/seeCurrentUrlEquals.mustache +0 -11
  229. package/docs/webapi/seeElement.mustache +0 -8
  230. package/docs/webapi/seeElementInDOM.mustache +0 -8
  231. package/docs/webapi/seeInCurrentUrl.mustache +0 -8
  232. package/docs/webapi/seeInField.mustache +0 -12
  233. package/docs/webapi/seeInPopup.mustache +0 -8
  234. package/docs/webapi/seeInSource.mustache +0 -7
  235. package/docs/webapi/seeInTitle.mustache +0 -8
  236. package/docs/webapi/seeNumberOfElements.mustache +0 -11
  237. package/docs/webapi/seeNumberOfVisibleElements.mustache +0 -10
  238. package/docs/webapi/seeTextEquals.mustache +0 -9
  239. package/docs/webapi/seeTitleEquals.mustache +0 -8
  240. package/docs/webapi/selectOption.mustache +0 -21
  241. package/docs/webapi/setCookie.mustache +0 -16
  242. package/docs/webapi/setGeoLocation.mustache +0 -12
  243. package/docs/webapi/switchTo.mustache +0 -9
  244. package/docs/webapi/switchToNextTab.mustache +0 -10
  245. package/docs/webapi/switchToPreviousTab.mustache +0 -10
  246. package/docs/webapi/type.mustache +0 -21
  247. package/docs/webapi/uncheckOption.mustache +0 -13
  248. package/docs/webapi/wait.mustache +0 -8
  249. package/docs/webapi/waitForClickable.mustache +0 -11
  250. package/docs/webapi/waitForDetached.mustache +0 -10
  251. package/docs/webapi/waitForElement.mustache +0 -11
  252. package/docs/webapi/waitForEnabled.mustache +0 -6
  253. package/docs/webapi/waitForFunction.mustache +0 -17
  254. package/docs/webapi/waitForInvisible.mustache +0 -10
  255. package/docs/webapi/waitForText.mustache +0 -13
  256. package/docs/webapi/waitForValue.mustache +0 -10
  257. package/docs/webapi/waitForVisible.mustache +0 -10
  258. package/docs/webapi/waitInUrl.mustache +0 -9
  259. package/docs/webapi/waitNumberOfVisibleElements.mustache +0 -10
  260. package/docs/webapi/waitToHide.mustache +0 -10
  261. package/docs/webapi/waitUrlEquals.mustache +0 -10
  262. package/docs/webdriver.md +0 -657
  263. package/docs/wiki/Books-&-Posts.md +0 -27
  264. package/docs/wiki/Community-Helpers-&-Plugins.md +0 -49
  265. package/docs/wiki/Converting-Playwright-to-Istanbul-Coverage.md +0 -29
  266. package/docs/wiki/Examples.md +0 -139
  267. package/docs/wiki/Google-Summer-of-Code-(GSoC)-2020.md +0 -68
  268. package/docs/wiki/Home.md +0 -16
  269. package/docs/wiki/Release-Process.md +0 -24
  270. package/docs/wiki/Roadmap.md +0 -23
  271. package/docs/wiki/Tests.md +0 -1393
  272. package/docs/wiki/Upgrading-to-CodeceptJS-3.md +0 -153
  273. package/docs/wiki/Videos.md +0 -19
@@ -1,2147 +0,0 @@
1
- const path = require('path');
2
-
3
- const urlResolve = require('url').resolve;
4
-
5
- const Helper = require('@codeceptjs/helper');
6
- const { includes: stringIncludes } = require('../assert/include');
7
- const { urlEquals } = require('../assert/equal');
8
- const { equals } = require('../assert/equal');
9
- const { empty } = require('../assert/empty');
10
- const { truth } = require('../assert/truth');
11
- const Locator = require('../locator');
12
- const ElementNotFound = require('./errors/ElementNotFound');
13
- const {
14
- xpathLocator,
15
- fileExists,
16
- screenshotOutputFolder,
17
- toCamelCase,
18
- } = require('../utils');
19
-
20
- const specialKeys = {
21
- Backspace: '\u0008',
22
- Enter: '\u000d',
23
- Delete: '\u007f',
24
- };
25
-
26
- let withinStatus = false;
27
-
28
- /**
29
- * Nightmare helper wraps [Nightmare](https://github.com/segmentio/nightmare) library to provide
30
- * fastest headless testing using Electron engine. Unlike Selenium-based drivers this uses
31
- * Chromium-based browser with Electron with lots of client side scripts, thus should be less stable and
32
- * less trusted.
33
- *
34
- * Requires `nightmare` package to be installed.
35
- *
36
- * ## Configuration
37
- *
38
- * This helper should be configured in codecept.conf.ts or codecept.conf.js
39
- *
40
- * * `url` - base url of website to be tested
41
- * * `restart` (optional, default: true) - restart browser between tests.
42
- * * `disableScreenshots` (optional, default: false) - don't save screenshot on failure.
43
- * * `uniqueScreenshotNames` (optional, default: false) - option to prevent screenshot override if you have scenarios with the same name in different suites.
44
- * * `fullPageScreenshots` (optional, default: false) - make full page screenshots on failure.
45
- * * `keepBrowserState` (optional, default: false) - keep browser state between tests when `restart` set to false.
46
- * * `keepCookies` (optional, default: false) - keep cookies between tests when `restart` set to false.
47
- * * `waitForAction`: (optional) how long to wait after click, doubleClick or PressKey actions in ms. Default: 500.
48
- * * `waitForTimeout`: (optional) default wait* timeout in ms. Default: 1000.
49
- * * `windowSize`: (optional) default window size. Set a dimension like `640x480`.
50
- *
51
- * + options from [Nightmare configuration](https://github.com/segmentio/nightmare#api)
52
- *
53
- * ## Methods
54
- */
55
- class Nightmare extends Helper {
56
- constructor(config) {
57
- super(config);
58
-
59
- this.isRunning = false;
60
-
61
- // override defaults with config
62
- this._setConfig(config);
63
- }
64
-
65
- _validateConfig(config) {
66
- const defaults = {
67
- waitForAction: 500,
68
- waitForTimeout: 1000,
69
- fullPageScreenshots: false,
70
- disableScreenshots: false,
71
- uniqueScreenshotNames: false,
72
- rootElement: 'body',
73
- restart: true,
74
- keepBrowserState: false,
75
- keepCookies: false,
76
- js_errors: null,
77
- enableHAR: false,
78
- };
79
-
80
- return Object.assign(defaults, config);
81
- }
82
-
83
- static _config() {
84
- return [
85
- { name: 'url', message: 'Base url of site to be tested', default: 'http://localhost' },
86
- {
87
- name: 'show', message: 'Show browser window', default: true, type: 'confirm',
88
- },
89
- ];
90
- }
91
-
92
- static _checkRequirements() {
93
- try {
94
- require('nightmare');
95
- } catch (e) {
96
- return ['nightmare'];
97
- }
98
- }
99
-
100
- async _init() {
101
- this.Nightmare = require('nightmare');
102
-
103
- if (this.options.enableHAR) {
104
- require('nightmare-har-plugin').install(this.Nightmare);
105
- }
106
-
107
- this.Nightmare.action('findElements', function (locator, contextEl, done) {
108
- if (!done) {
109
- done = contextEl;
110
- contextEl = null;
111
- }
112
-
113
- const by = Object.keys(locator)[0];
114
- const value = locator[by];
115
-
116
- this.evaluate_now((by, locator, contextEl) => window.codeceptjs.findAndStoreElements(by, locator, contextEl), done, by, value, contextEl);
117
- });
118
-
119
- this.Nightmare.action('findElement', function (locator, contextEl, done) {
120
- if (!done) {
121
- done = contextEl;
122
- contextEl = null;
123
- }
124
-
125
- const by = Object.keys(locator)[0];
126
- const value = locator[by];
127
-
128
- this.evaluate_now((by, locator, contextEl) => {
129
- const res = window.codeceptjs.findAndStoreElement(by, locator, contextEl);
130
- if (res === null) {
131
- throw new Error(`Element ${(new Locator(locator))} couldn't be located by ${by}`);
132
- }
133
- return res;
134
- }, done, by, value, contextEl);
135
- });
136
-
137
- this.Nightmare.action('asyncScript', function () {
138
- let args = Array.prototype.slice.call(arguments);
139
- const done = args.pop();
140
- args = args.splice(1, 0, done);
141
- this.evaluate_now.apply(this, args);
142
- });
143
-
144
- this.Nightmare.action('enterText', function (el, text, clean, done) {
145
- const child = this.child;
146
- const typeFn = () => child.call('type', text, done);
147
-
148
- this.evaluate_now((el, clean) => {
149
- const element = window.codeceptjs.fetchElement(el);
150
- if (clean) element.value = '';
151
- element.focus();
152
- }, () => {
153
- if (clean) return typeFn();
154
- child.call('pressKey', 'End', typeFn); // type End before
155
- }, el, clean);
156
- });
157
-
158
- this.Nightmare.action('pressKey', (ns, options, parent, win, renderer, done) => {
159
- parent.respondTo('pressKey', (ch, done) => {
160
- win.webContents.sendInputEvent({
161
- type: 'keyDown',
162
- keyCode: ch,
163
- });
164
-
165
- win.webContents.sendInputEvent({
166
- type: 'char',
167
- keyCode: ch,
168
- });
169
-
170
- win.webContents.sendInputEvent({
171
- type: 'keyUp',
172
- keyCode: ch,
173
- });
174
- done();
175
- });
176
- done();
177
- }, function (key, done) {
178
- this.child.call('pressKey', key, done);
179
- });
180
-
181
- this.Nightmare.action('triggerMouseEvent', (ns, options, parent, win, renderer, done) => {
182
- parent.respondTo('triggerMouseEvent', (evt, done) => {
183
- win.webContents.sendInputEvent(evt);
184
- done();
185
- });
186
- done();
187
- }, function (event, done) {
188
- this.child.call('triggerMouseEvent', event, done);
189
- });
190
-
191
- this.Nightmare.action(
192
- 'upload',
193
- (ns, options, parent, win, renderer, done) => {
194
- parent.respondTo('upload', (selector, pathsToUpload, done) => {
195
- parent.emit('log', 'paths', pathsToUpload);
196
- try {
197
- // attach the debugger
198
- // NOTE: this will fail if devtools is open
199
- win.webContents.debugger.attach('1.1');
200
- } catch (e) {
201
- parent.emit('log', 'problem attaching', e);
202
- return done(e);
203
- }
204
-
205
- win.webContents.debugger.sendCommand('DOM.getDocument', {}, (err, domDocument) => {
206
- win.webContents.debugger.sendCommand('DOM.querySelector', {
207
- nodeId: domDocument.root.nodeId,
208
- selector,
209
- }, (err, queryResult) => {
210
- // HACK: chromium errors appear to be unpopulated objects?
211
- if (Object.keys(err)
212
- .length > 0) {
213
- parent.emit('log', 'problem selecting', err);
214
- return done(err);
215
- }
216
- win.webContents.debugger.sendCommand('DOM.setFileInputFiles', {
217
- nodeId: queryResult.nodeId,
218
- files: pathsToUpload,
219
- }, (err) => {
220
- if (Object.keys(err)
221
- .length > 0) {
222
- parent.emit('log', 'problem setting input', err);
223
- return done(err);
224
- }
225
- win.webContents.debugger.detach();
226
- done(null, pathsToUpload);
227
- });
228
- });
229
- });
230
- });
231
- done();
232
- },
233
- function (selector, pathsToUpload, done) {
234
- if (!Array.isArray(pathsToUpload)) {
235
- pathsToUpload = [pathsToUpload];
236
- }
237
- this.child.call('upload', selector, pathsToUpload, (err, stuff) => {
238
- done(err, stuff);
239
- });
240
- },
241
- );
242
-
243
- return Promise.resolve();
244
- }
245
-
246
- async _beforeSuite() {
247
- if (!this.options.restart && !this.isRunning) {
248
- this.debugSection('Session', 'Starting singleton browser session');
249
- return this._startBrowser();
250
- }
251
- }
252
-
253
- async _before() {
254
- if (this.options.restart) return this._startBrowser();
255
- if (!this.isRunning) return this._startBrowser();
256
- return this.browser;
257
- }
258
-
259
- async _after() {
260
- if (!this.isRunning) return;
261
- if (this.options.restart) {
262
- this.isRunning = false;
263
- return this._stopBrowser();
264
- }
265
- if (this.options.enableHAR) {
266
- await this.browser.resetHAR();
267
- }
268
- if (this.options.keepBrowserState) return;
269
- if (this.options.keepCookies) {
270
- await this.browser.cookies.clearAll();
271
- }
272
- this.debugSection('Session', 'cleaning up');
273
- return this.executeScript(() => localStorage.clear());
274
- }
275
-
276
- _afterSuite() {
277
- }
278
-
279
- _finishTest() {
280
- if (!this.options.restart && this.isRunning) {
281
- this._stopBrowser();
282
- }
283
- }
284
-
285
- async _startBrowser() {
286
- this.context = this.options.rootElement;
287
- if (this.options.enableHAR) {
288
- this.browser = this.Nightmare(Object.assign(require('nightmare-har-plugin').getDevtoolsOptions(), this.options));
289
- await this.browser;
290
- await this.browser.waitForDevtools();
291
- } else {
292
- this.browser = this.Nightmare(this.options);
293
- await this.browser;
294
- }
295
- await this.browser.goto('about:blank'); // Load a blank page so .saveScreenshot (/evaluate) will work
296
- this.isRunning = true;
297
- this.browser.on('dom-ready', () => this._injectClientScripts());
298
- this.browser.on('did-start-loading', () => this._injectClientScripts());
299
- this.browser.on('will-navigate', () => this._injectClientScripts());
300
- this.browser.on('console', (type, message) => {
301
- this.debug(`${type}: ${message}`);
302
- });
303
-
304
- if (this.options.windowSize) {
305
- const size = this.options.windowSize.split('x');
306
- return this.browser.viewport(parseInt(size[0], 10), parseInt(size[1], 10));
307
- }
308
- }
309
-
310
- /**
311
- * Get HAR
312
- *
313
- * ```js
314
- * let har = await I.grabHAR();
315
- * fs.writeFileSync('sample.har', JSON.stringify({log: har}));
316
- * ```
317
- */
318
- async grabHAR() {
319
- return this.browser.getHAR();
320
- }
321
-
322
- async saveHAR(fileName) {
323
- const outputFile = path.join(global.output_dir, fileName);
324
- this.debug(`HAR is saving to ${outputFile}`);
325
-
326
- await this.browser.getHAR().then((har) => {
327
- require('fs').writeFileSync(outputFile, JSON.stringify({ log: har }));
328
- });
329
- }
330
-
331
- async resetHAR() {
332
- await this.browser.resetHAR();
333
- }
334
-
335
- async _stopBrowser() {
336
- return this.browser.end().catch((error) => {
337
- this.debugSection('Error on End', error);
338
- });
339
- }
340
-
341
- async _withinBegin(locator) {
342
- this.context = locator;
343
- locator = new Locator(locator, 'css');
344
- withinStatus = true;
345
- return this.browser.evaluate((by, locator) => {
346
- const el = window.codeceptjs.findElement(by, locator);
347
- if (!el) throw new Error(`Element by ${by}: ${locator} not found`);
348
- window.codeceptjs.within = el;
349
- }, locator.type, locator.value);
350
- }
351
-
352
- _withinEnd() {
353
- this.context = this.options.rootElement;
354
- withinStatus = false;
355
- return this.browser.evaluate(() => {
356
- window.codeceptjs.within = null;
357
- });
358
- }
359
-
360
- /**
361
- * Locate elements by different locator types, including strict locator.
362
- * Should be used in custom helpers.
363
- *
364
- * This method return promise with array of IDs of found elements.
365
- * Actual elements can be accessed inside `evaluate` by using `codeceptjs.fetchElement()`
366
- * client-side function:
367
- *
368
- * ```js
369
- * // get an inner text of an element
370
- *
371
- * let browser = this.helpers['Nightmare'].browser;
372
- * let value = this.helpers['Nightmare']._locate({name: 'password'}).then(function(els) {
373
- * return browser.evaluate(function(el) {
374
- * return codeceptjs.fetchElement(el).value;
375
- * }, els[0]);
376
- * });
377
- * ```
378
- */
379
- _locate(locator) {
380
- locator = new Locator(locator, 'css');
381
- return this.browser.evaluate((by, locator) => {
382
- return window.codeceptjs.findAndStoreElements(by, locator);
383
- }, locator.type, locator.value);
384
- }
385
-
386
- /**
387
- * Add a header override for all HTTP requests. If header is undefined, the header overrides will be reset.
388
- *
389
- * ```js
390
- * I.haveHeader('x-my-custom-header', 'some value');
391
- * I.haveHeader(); // clear headers
392
- * ```
393
- */
394
- haveHeader(header, value) {
395
- return this.browser.header(header, value);
396
- }
397
-
398
- /**
399
- * Opens a web page in a browser. Requires relative or absolute url.
400
- * If url starts with `/`, opens a web page of a site defined in `url` config parameter.
401
- *
402
- * ```js
403
- * I.amOnPage('/'); // opens main page of website
404
- * I.amOnPage('https://github.com'); // opens github
405
- * I.amOnPage('/login'); // opens a login page
406
- * ```
407
- *
408
- * @param {string} url url path or global url.
409
- * @return {void} automatically synchronized promise with recorder #!
410
- * @param {?object} headers list of request headers can be passed
411
- *
412
- */
413
- async amOnPage(url, headers = null) {
414
- if (!(/^\w+\:\/\//.test(url))) {
415
- url = urlResolve(this.options.url, url);
416
- }
417
- const currentUrl = await this.browser.url();
418
- if (url === currentUrl) {
419
- // navigating to the same url will cause an error in nightmare, so don't do it
420
- return;
421
- }
422
- return this.browser.goto(url, headers).then((res) => {
423
- this.debugSection('URL', res.url);
424
- this.debugSection('Code', res.code);
425
- this.debugSection('Headers', JSON.stringify(res.headers));
426
- });
427
- }
428
-
429
- /**
430
- * Checks that title contains text.
431
- *
432
- * ```js
433
- * I.seeInTitle('Home Page');
434
- * ```
435
- *
436
- * @param {string} text text value to check.
437
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
438
- *
439
- */
440
- async seeInTitle(text) {
441
- const title = await this.browser.title();
442
- stringIncludes('web page title').assert(text, title);
443
- }
444
-
445
- /**
446
- * Checks that title does not contain text.
447
- *
448
- * ```js
449
- * I.dontSeeInTitle('Error');
450
- * ```
451
- *
452
- * @param {string} text value to check.
453
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
454
- *
455
- */
456
- async dontSeeInTitle(text) {
457
- const title = await this.browser.title();
458
- stringIncludes('web page title').negate(text, title);
459
- }
460
-
461
- /**
462
- * Retrieves a page title and returns it to test.
463
- * Resumes test execution, so **should be used inside async with `await`** operator.
464
- *
465
- * ```js
466
- * let title = await I.grabTitle();
467
- * ```
468
- *
469
- * @returns {Promise<string>} title
470
- */
471
- async grabTitle() {
472
- return this.browser.title();
473
- }
474
-
475
- /**
476
- * Get current URL from browser.
477
- * Resumes test execution, so should be used inside an async function.
478
- *
479
- * ```js
480
- * let url = await I.grabCurrentUrl();
481
- * console.log(`Current URL is [${url}]`);
482
- * ```
483
- *
484
- * @returns {Promise<string>} current URL
485
- */
486
- async grabCurrentUrl() {
487
- return this.browser.url();
488
- }
489
-
490
- /**
491
- * Checks that current url contains a provided fragment.
492
- *
493
- * ```js
494
- * I.seeInCurrentUrl('/register'); // we are on registration page
495
- * ```
496
- *
497
- * @param {string} url a fragment to check
498
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
499
- *
500
- */
501
- async seeInCurrentUrl(url) {
502
- const currentUrl = await this.browser.url();
503
- stringIncludes('url').assert(url, currentUrl);
504
- }
505
-
506
- /**
507
- * Checks that current url does not contain a provided fragment.
508
- *
509
- * @param {string} url value to check.
510
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
511
- *
512
- */
513
- async dontSeeInCurrentUrl(url) {
514
- const currentUrl = await this.browser.url();
515
- stringIncludes('url').negate(url, currentUrl);
516
- }
517
-
518
- /**
519
- * Checks that current url is equal to provided one.
520
- * If a relative url provided, a configured url will be prepended to it.
521
- * So both examples will work:
522
- *
523
- * ```js
524
- * I.seeCurrentUrlEquals('/register');
525
- * I.seeCurrentUrlEquals('http://my.site.com/register');
526
- * ```
527
- *
528
- * @param {string} url value to check.
529
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
530
- *
531
- */
532
- async seeCurrentUrlEquals(url) {
533
- const currentUrl = await this.browser.url();
534
- urlEquals(this.options.url).assert(url, currentUrl);
535
- }
536
-
537
- /**
538
- * Checks that current url is not equal to provided one.
539
- * If a relative url provided, a configured url will be prepended to it.
540
- *
541
- * ```js
542
- * I.dontSeeCurrentUrlEquals('/login'); // relative url are ok
543
- * I.dontSeeCurrentUrlEquals('http://mysite.com/login'); // absolute urls are also ok
544
- * ```
545
- *
546
- * @param {string} url value to check.
547
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
548
- *
549
- */
550
- async dontSeeCurrentUrlEquals(url) {
551
- const currentUrl = await this.browser.url();
552
- urlEquals(this.options.url).negate(url, currentUrl);
553
- }
554
-
555
- /**
556
- * Checks that a page contains a visible text.
557
- * Use context parameter to narrow down the search.
558
- *
559
- * ```js
560
- * I.see('Welcome'); // text welcome on a page
561
- * I.see('Welcome', '.content'); // text inside .content div
562
- * I.see('Register', {css: 'form.register'}); // use strict locator
563
- * ```
564
- * @param {string} text expected on page.
565
- * @param {?CodeceptJS.LocatorOrString} [context=null] (optional, `null` by default) element located by CSS|Xpath|strict locator in which to search for text.
566
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
567
- *
568
- */
569
- async see(text, context = null) {
570
- return proceedSee.call(this, 'assert', text, context);
571
- }
572
-
573
- /**
574
- * Opposite to `see`. Checks that a text is not present on a page.
575
- * Use context parameter to narrow down the search.
576
- *
577
- * ```js
578
- * I.dontSee('Login'); // assume we are already logged in.
579
- * I.dontSee('Login', '.nav'); // no login inside .nav element
580
- * ```
581
- *
582
- * @param {string} text which is not present.
583
- * @param {CodeceptJS.LocatorOrString} [context] (optional) element located by CSS|XPath|strict locator in which to perfrom search.
584
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
585
- *
586
- */
587
- dontSee(text, context = null) {
588
- return proceedSee.call(this, 'negate', text, context);
589
- }
590
-
591
- /**
592
- * Checks that a given Element is visible
593
- * Element is located by CSS or XPath.
594
- *
595
- * ```js
596
- * I.seeElement('#modal');
597
- * ```
598
- * @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|strict locator.
599
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
600
- *
601
- */
602
- async seeElement(locator) {
603
- locator = new Locator(locator, 'css');
604
- const num = await this.browser.evaluate((by, locator) => {
605
- return window.codeceptjs.findElements(by, locator).filter(e => e.offsetWidth > 0 && e.offsetHeight > 0).length;
606
- }, locator.type, locator.value);
607
- equals('number of elements on a page').negate(0, num);
608
- }
609
-
610
- /**
611
- * Opposite to `seeElement`. Checks that element is not visible (or in DOM)
612
- *
613
- * ```js
614
- * I.dontSeeElement('.modal'); // modal is not shown
615
- * ```
616
- *
617
- * @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|Strict locator.
618
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
619
- *
620
- */
621
- async dontSeeElement(locator) {
622
- locator = new Locator(locator, 'css');
623
- locator = new Locator(locator, 'css');
624
- const num = await this.browser.evaluate((by, locator) => {
625
- return window.codeceptjs.findElements(by, locator).filter(e => e.offsetWidth > 0 && e.offsetHeight > 0).length;
626
- }, locator.type, locator.value);
627
- equals('number of elements on a page').assert(0, num);
628
- }
629
-
630
- /**
631
- * Checks that a given Element is present in the DOM
632
- * Element is located by CSS or XPath.
633
- *
634
- * ```js
635
- * I.seeElementInDOM('#modal');
636
- * ```
637
- * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
638
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
639
- *
640
- */
641
- async seeElementInDOM(locator) {
642
- locator = new Locator(locator, 'css');
643
- const els = await this.browser.findElements(locator.toStrict());
644
- empty('elements').negate(els.fill('ELEMENT'));
645
- }
646
-
647
- /**
648
- * Opposite to `seeElementInDOM`. Checks that element is not on page.
649
- *
650
- * ```js
651
- * I.dontSeeElementInDOM('.nav'); // checks that element is not on page visible or not
652
- * ```
653
- *
654
- * @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|Strict locator.
655
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
656
- *
657
- */
658
- async dontSeeElementInDOM(locator) {
659
- locator = new Locator(locator, 'css');
660
- const els = await this.browser.findElements(locator.toStrict());
661
- empty('elements').assert(els.fill('ELEMENT'));
662
- }
663
-
664
- /**
665
- * Checks that the current page contains the given string in its raw source code.
666
- *
667
- * ```js
668
- * I.seeInSource('<h1>Green eggs &amp; ham</h1>');
669
- * ```
670
- * @param {string} text value to check.
671
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
672
- *
673
- */
674
- async seeInSource(text) {
675
- const source = await this.browser.evaluate(() => document.documentElement.outerHTML);
676
- stringIncludes('HTML source of a page').assert(text, source);
677
- }
678
-
679
- /**
680
- * Checks that the current page does not contains the given string in its raw source code.
681
- *
682
- * ```js
683
- * I.dontSeeInSource('<!--'); // no comments in source
684
- * ```
685
- *
686
- * @param {string} value to check.
687
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
688
- *
689
- */
690
- async dontSeeInSource(text) {
691
- const source = await this.browser.evaluate(() => document.documentElement.outerHTML);
692
- stringIncludes('HTML source of a page').negate(text, source);
693
- }
694
-
695
- /**
696
- * Asserts that an element appears a given number of times in the DOM.
697
- * Element is located by label or name or CSS or XPath.
698
- *
699
- *
700
- * ```js
701
- * I.seeNumberOfElements('#submitBtn', 1);
702
- * ```
703
- *
704
- * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
705
- * @param {number} num number of elements.
706
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
707
- *
708
- */
709
- async seeNumberOfElements(locator, num) {
710
- const elements = await this._locate(locator);
711
- return equals(`expected number of elements (${(new Locator(locator))}) is ${num}, but found ${elements.length}`).assert(elements.length, num);
712
- }
713
-
714
- /**
715
- * Asserts that an element is visible a given number of times.
716
- * Element is located by CSS or XPath.
717
- *
718
- * ```js
719
- * I.seeNumberOfVisibleElements('.buttons', 3);
720
- * ```
721
- *
722
- * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
723
- * @param {number} num number of elements.
724
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
725
- *
726
- */
727
- async seeNumberOfVisibleElements(locator, num) {
728
- const res = await this.grabNumberOfVisibleElements(locator);
729
- return equals(`expected number of visible elements (${(new Locator(locator))}) is ${num}, but found ${res}`).assert(res, num);
730
- }
731
-
732
- /**
733
- * Grab number of visible elements by locator.
734
- * Resumes test execution, so **should be used inside async function with `await`** operator.
735
- *
736
- * ```js
737
- * let numOfElements = await I.grabNumberOfVisibleElements('p');
738
- * ```
739
- *
740
- * @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|strict locator.
741
- * @returns {Promise<number>} number of visible elements
742
- */
743
- async grabNumberOfVisibleElements(locator) {
744
- locator = new Locator(locator, 'css');
745
-
746
- const num = await this.browser.evaluate((by, locator) => {
747
- return window.codeceptjs.findElements(by, locator)
748
- .filter(e => e.offsetWidth > 0 && e.offsetHeight > 0).length;
749
- }, locator.type, locator.value);
750
-
751
- return num;
752
- }
753
-
754
- /**
755
- * Perform a click on a link or a button, given by a locator.
756
- * If a fuzzy locator is given, the page will be searched for a button, link, or image matching the locator string.
757
- * For buttons, the "value" attribute, "name" attribute, and inner text are searched. For links, the link text is searched.
758
- * For images, the "alt" attribute and inner text of any parent links are searched.
759
- *
760
- * The second parameter is a context (CSS or XPath locator) to narrow the search.
761
- *
762
- * ```js
763
- * // simple link
764
- * I.click('Logout');
765
- * // button of form
766
- * I.click('Submit');
767
- * // CSS button
768
- * I.click('#form input[type=submit]');
769
- * // XPath
770
- * I.click('//form/*[@type=submit]');
771
- * // link in context
772
- * I.click('Logout', '#nav');
773
- * // using strict locator
774
- * I.click({css: 'nav a.login'});
775
- * ```
776
- *
777
- * @param {CodeceptJS.LocatorOrString} locator clickable link or button located by text, or any element located by CSS|XPath|strict locator.
778
- * @param {?CodeceptJS.LocatorOrString | null} [context=null] (optional, `null` by default) element to search in CSS|XPath|Strict locator.
779
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
780
- *
781
- */
782
- async click(locator, context = null) {
783
- const el = await findClickable.call(this, locator, context);
784
- assertElementExists(el, locator, 'Clickable');
785
- return this.browser.evaluate(el => window.codeceptjs.clickEl(el), el)
786
- .wait(this.options.waitForAction);
787
- }
788
-
789
- /**
790
- * Performs a double-click on an element matched by link|button|label|CSS or XPath.
791
- * Context can be specified as second parameter to narrow search.
792
- *
793
- * ```js
794
- * I.doubleClick('Edit');
795
- * I.doubleClick('Edit', '.actions');
796
- * I.doubleClick({css: 'button.accept'});
797
- * I.doubleClick('.btn.edit');
798
- * ```
799
- *
800
- * @param {CodeceptJS.LocatorOrString} locator clickable link or button located by text, or any element located by CSS|XPath|strict locator.
801
- * @param {?CodeceptJS.LocatorOrString} [context=null] (optional, `null` by default) element to search in CSS|XPath|Strict locator.
802
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
803
- *
804
- */
805
- async doubleClick(locator, context = null) {
806
- const el = await findClickable.call(this, locator, context);
807
- assertElementExists(el, locator, 'Clickable');
808
- return this.browser.evaluate(el => window.codeceptjs.doubleClickEl(el), el)
809
- .wait(this.options.waitForAction);
810
- }
811
-
812
- /**
813
- * Performs right click on a clickable element matched by semantic locator, CSS or XPath.
814
- *
815
- * ```js
816
- * // right click element with id el
817
- * I.rightClick('#el');
818
- * // right click link or button with text "Click me"
819
- * I.rightClick('Click me');
820
- * // right click button with text "Click me" inside .context
821
- * I.rightClick('Click me', '.context');
822
- * ```
823
- *
824
- * @param {CodeceptJS.LocatorOrString} locator clickable element located by CSS|XPath|strict locator.
825
- * @param {?CodeceptJS.LocatorOrString} [context=null] (optional, `null` by default) element located by CSS|XPath|strict locator.
826
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
827
- *
828
- */
829
- async rightClick(locator, context = null) {
830
- const el = await findClickable.call(this, locator, context);
831
- assertElementExists(el, locator, 'Clickable');
832
- return this.browser.evaluate(el => window.codeceptjs.rightClickEl(el), el)
833
- .wait(this.options.waitForAction);
834
- }
835
-
836
- /**
837
- * Moves cursor to element matched by locator.
838
- * Extra shift can be set with offsetX and offsetY options.
839
- *
840
- * ```js
841
- * I.moveCursorTo('.tooltip');
842
- * I.moveCursorTo('#submit', 5,5);
843
- * ```
844
- *
845
- * @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|strict locator.
846
- * @param {number} [offsetX=0] (optional, `0` by default) X-axis offset.
847
- * @param {number} [offsetY=0] (optional, `0` by default) Y-axis offset.
848
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
849
- *
850
- */
851
- async moveCursorTo(locator, offsetX = 0, offsetY = 0) {
852
- locator = new Locator(locator, 'css');
853
- const el = await this.browser.findElement(locator.toStrict());
854
- assertElementExists(el, locator);
855
- return this.browser.evaluate((el, x, y) => window.codeceptjs.hoverEl(el, x, y), el, offsetX, offsetY)
856
- .wait(this.options.waitForAction); // wait for hover event to happen
857
- }
858
-
859
- /**
860
- * Executes sync script on a page.
861
- * Pass arguments to function as additional parameters.
862
- * Will return execution result to a test.
863
- * In this case you should use async function and await to receive results.
864
- *
865
- * Example with jQuery DatePicker:
866
- *
867
- * ```js
868
- * // change date of jQuery DatePicker
869
- * I.executeScript(function() {
870
- * // now we are inside browser context
871
- * $('date').datetimepicker('setDate', new Date());
872
- * });
873
- * ```
874
- * Can return values. Don't forget to use `await` to get them.
875
- *
876
- * ```js
877
- * let date = await I.executeScript(function(el) {
878
- * // only basic types can be returned
879
- * return $(el).datetimepicker('getDate').toString();
880
- * }, '#date'); // passing jquery selector
881
- * ```
882
- *
883
- * @param {string|function} fn function to be executed in browser context.
884
- * @param {...any} args to be passed to function.
885
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
886
- *
887
- *
888
- * Wrapper for synchronous [evaluate](https://github.com/segmentio/nightmare#evaluatefn-arg1-arg2)
889
- */
890
- async executeScript(...args) {
891
- return this.browser.evaluate.apply(this.browser, args)
892
- .catch(err => err); // Nightmare's first argument is error :(
893
- }
894
-
895
- /**
896
- * Executes async script on page.
897
- * Provided function should execute a passed callback (as first argument) to signal it is finished.
898
- *
899
- * Example: In Vue.js to make components completely rendered we are waiting for [nextTick](https://vuejs.org/v2/api/#Vue-nextTick).
900
- *
901
- * ```js
902
- * I.executeAsyncScript(function(done) {
903
- * Vue.nextTick(done); // waiting for next tick
904
- * });
905
- * ```
906
- *
907
- * By passing value to `done()` function you can return values.
908
- * Additional arguments can be passed as well, while `done` function is always last parameter in arguments list.
909
- *
910
- * ```js
911
- * let val = await I.executeAsyncScript(function(url, done) {
912
- * // in browser context
913
- * $.ajax(url, { success: (data) => done(data); }
914
- * }, 'http://ajax.callback.url/');
915
- * ```
916
- *
917
- * @param {string|function} fn function to be executed in browser context.
918
- * @param {...any} args to be passed to function.
919
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
920
- *
921
- *
922
- * Wrapper for asynchronous [evaluate](https://github.com/segmentio/nightmare#evaluatefn-arg1-arg2).
923
- * Unlike NightmareJS implementation calling `done` will return its first argument.
924
- */
925
- async executeAsyncScript(...args) {
926
- return this.browser.evaluate.apply(this.browser, args)
927
- .catch(err => err); // Nightmare's first argument is error :(
928
- }
929
-
930
- /**
931
- * Resize the current window to provided width and height.
932
- * First parameter can be set to `maximize`.
933
- *
934
- * @param {number} width width in pixels or `maximize`.
935
- * @param {number} height height in pixels.
936
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
937
- *
938
- */
939
- async resizeWindow(width, height) {
940
- if (width === 'maximize') {
941
- throw new Error('Nightmare doesn\'t support resizeWindow to maximum!');
942
- }
943
- return this.browser.viewport(width, height).wait(this.options.waitForAction);
944
- }
945
-
946
- /**
947
- * Selects a checkbox or radio button.
948
- * Element is located by label or name or CSS or XPath.
949
- *
950
- * The second parameter is a context (CSS or XPath locator) to narrow the search.
951
- *
952
- * ```js
953
- * I.checkOption('#agree');
954
- * I.checkOption('I Agree to Terms and Conditions');
955
- * I.checkOption('agree', '//form');
956
- * ```
957
- * @param {CodeceptJS.LocatorOrString} field checkbox located by label | name | CSS | XPath | strict locator.
958
- * @param {?CodeceptJS.LocatorOrString} [context=null] (optional, `null` by default) element located by CSS | XPath | strict locator.
959
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
960
- *
961
- */
962
- async checkOption(field, context = null) {
963
- const els = await findCheckable.call(this, field, context);
964
- assertElementExists(els[0], field, 'Checkbox or radio');
965
- return this.browser.evaluate(els => window.codeceptjs.checkEl(els[0]), els)
966
- .wait(this.options.waitForAction);
967
- }
968
-
969
- /**
970
- * Unselects a checkbox or radio button.
971
- * Element is located by label or name or CSS or XPath.
972
- *
973
- * The second parameter is a context (CSS or XPath locator) to narrow the search.
974
- *
975
- * ```js
976
- * I.uncheckOption('#agree');
977
- * I.uncheckOption('I Agree to Terms and Conditions');
978
- * I.uncheckOption('agree', '//form');
979
- * ```
980
- * @param {CodeceptJS.LocatorOrString} field checkbox located by label | name | CSS | XPath | strict locator.
981
- * @param {?CodeceptJS.LocatorOrString} [context=null] (optional, `null` by default) element located by CSS | XPath | strict locator.
982
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
983
- *
984
- */
985
- async uncheckOption(field, context = null) {
986
- const els = await findCheckable.call(this, field, context);
987
- assertElementExists(els[0], field, 'Checkbox or radio');
988
- return this.browser.evaluate(els => window.codeceptjs.unCheckEl(els[0]), els)
989
- .wait(this.options.waitForAction);
990
- }
991
-
992
- /**
993
- * Fills a text field or textarea, after clearing its value, with the given string.
994
- * Field is located by name, label, CSS, or XPath.
995
- *
996
- * ```js
997
- * // by label
998
- * I.fillField('Email', 'hello@world.com');
999
- * // by name
1000
- * I.fillField('password', secret('123456'));
1001
- * // by CSS
1002
- * I.fillField('form#login input[name=username]', 'John');
1003
- * // or by strict locator
1004
- * I.fillField({css: 'form#login input[name=username]'}, 'John');
1005
- * ```
1006
- * @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator.
1007
- * @param {CodeceptJS.StringOrSecret} value text value to fill.
1008
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1009
- *
1010
- */
1011
- async fillField(field, value) {
1012
- const el = await findField.call(this, field);
1013
- assertElementExists(el, field, 'Field');
1014
- return this.browser.enterText(el, value.toString(), true)
1015
- .wait(this.options.waitForAction);
1016
- }
1017
-
1018
- /**
1019
- * Clears a `<textarea>` or text `<input>` element's value.
1020
- *
1021
- * ```js
1022
- * I.clearField('Email');
1023
- * I.clearField('user[email]');
1024
- * I.clearField('#email');
1025
- * ```
1026
- * @param {LocatorOrString} editable field located by label|name|CSS|XPath|strict locator.
1027
- * ⚠️ returns a _promise_ which is synchronized internally by recorder.
1028
- *
1029
- */
1030
- async clearField(field) {
1031
- return this.fillField(field, '');
1032
- }
1033
-
1034
- /**
1035
- * Appends text to a input field or textarea.
1036
- * Field is located by name, label, CSS or XPath
1037
- *
1038
- * ```js
1039
- * I.appendField('#myTextField', 'appended');
1040
- * // typing secret
1041
- * I.appendField('password', secret('123456'));
1042
- * ```
1043
- * @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator
1044
- * @param {string} value text value to append.
1045
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1046
- *
1047
- */
1048
- async appendField(field, value) {
1049
- const el = await findField.call(this, field);
1050
- assertElementExists(el, field, 'Field');
1051
- return this.browser.enterText(el, value.toString(), false)
1052
- .wait(this.options.waitForAction);
1053
- }
1054
-
1055
- /**
1056
- * Checks that the given input field or textarea equals to given value.
1057
- * For fuzzy locators, fields are matched by label text, the "name" attribute, CSS, and XPath.
1058
- *
1059
- * ```js
1060
- * I.seeInField('Username', 'davert');
1061
- * I.seeInField({css: 'form textarea'},'Type your comment here');
1062
- * I.seeInField('form input[type=hidden]','hidden_value');
1063
- * I.seeInField('#searchform input','Search');
1064
- * ```
1065
- * @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator.
1066
- * @param {string} value value to check.
1067
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1068
- *
1069
- */
1070
- async seeInField(field, value) {
1071
- return proceedSeeInField.call(this, 'assert', field, value);
1072
- }
1073
-
1074
- /**
1075
- * Checks that value of input field or textarea doesn't equal to given value
1076
- * Opposite to `seeInField`.
1077
- *
1078
- * ```js
1079
- * I.dontSeeInField('email', 'user@user.com'); // field by name
1080
- * I.dontSeeInField({ css: 'form input.email' }, 'user@user.com'); // field by CSS
1081
- * ```
1082
- *
1083
- * @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator.
1084
- * @param {string} value value to check.
1085
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1086
- *
1087
- */
1088
- async dontSeeInField(field, value) {
1089
- return proceedSeeInField.call(this, 'negate', field, value);
1090
- }
1091
-
1092
- /**
1093
- * Sends [input event](http://electron.atom.io/docs/api/web-contents/#webcontentssendinputeventevent) on a page.
1094
- * Can submit special keys like 'Enter', 'Backspace', etc
1095
- */
1096
- async pressKey(key) {
1097
- if (Array.isArray(key)) {
1098
- key = key.join('+'); // should work with accelerators...
1099
- }
1100
- if (Object.keys(specialKeys).indexOf(key) >= 0) {
1101
- key = specialKeys[key];
1102
- }
1103
- return this.browser.pressKey(key).wait(this.options.waitForAction);
1104
- }
1105
-
1106
- /**
1107
- * Sends [input event](http://electron.atom.io/docs/api/web-contents/#contentssendinputeventevent) on a page.
1108
- * Should be a mouse event like:
1109
- * {
1110
- type: 'mouseDown',
1111
- x: args.x,
1112
- y: args.y,
1113
- button: "left"
1114
- }
1115
- */
1116
- async triggerMouseEvent(event) {
1117
- return this.browser.triggerMouseEvent(event).wait(this.options.waitForAction);
1118
- }
1119
-
1120
- /**
1121
- * Verifies that the specified checkbox is checked.
1122
- *
1123
- * ```js
1124
- * I.seeCheckboxIsChecked('Agree');
1125
- * I.seeCheckboxIsChecked('#agree'); // I suppose user agreed to terms
1126
- * I.seeCheckboxIsChecked({css: '#signup_form input[type=checkbox]'});
1127
- * ```
1128
- *
1129
- * @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator.
1130
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1131
- *
1132
- */
1133
- async seeCheckboxIsChecked(field) {
1134
- return proceedIsChecked.call(this, 'assert', field);
1135
- }
1136
-
1137
- /**
1138
- * Verifies that the specified checkbox is not checked.
1139
- *
1140
- * ```js
1141
- * I.dontSeeCheckboxIsChecked('#agree'); // located by ID
1142
- * I.dontSeeCheckboxIsChecked('I agree to terms'); // located by label
1143
- * I.dontSeeCheckboxIsChecked('agree'); // located by name
1144
- * ```
1145
- *
1146
- * @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator.
1147
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1148
- *
1149
- */
1150
- async dontSeeCheckboxIsChecked(field) {
1151
- return proceedIsChecked.call(this, 'negate', field);
1152
- }
1153
-
1154
- /**
1155
- * Attaches a file to element located by label, name, CSS or XPath
1156
- * Path to file is relative current codecept directory (where codecept.conf.ts or codecept.conf.js is located).
1157
- * File will be uploaded to remote system (if tests are running remotely).
1158
- *
1159
- * ```js
1160
- * I.attachFile('Avatar', 'data/avatar.jpg');
1161
- * I.attachFile('form input[name=avatar]', 'data/avatar.jpg');
1162
- * ```
1163
- *
1164
- * @param {CodeceptJS.LocatorOrString} locator field located by label|name|CSS|XPath|strict locator.
1165
- * @param {string} pathToFile local file path relative to codecept.conf.ts or codecept.conf.js config file.
1166
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1167
- *
1168
- *
1169
- * Doesn't work if the Chromium DevTools panel is open (as Chromium allows only one attachment to the debugger at a time. [See more](https://github.com/rosshinkley/nightmare-upload#important-note-about-setting-file-upload-inputs))
1170
- */
1171
- async attachFile(locator, pathToFile) {
1172
- const file = path.join(global.codecept_dir, pathToFile);
1173
-
1174
- locator = new Locator(locator, 'css');
1175
- if (!locator.isCSS()) {
1176
- throw new Error('Only CSS locator allowed for attachFile in Nightmare helper');
1177
- }
1178
-
1179
- if (!fileExists(file)) {
1180
- throw new Error(`File at ${file} can not be found on local system`);
1181
- }
1182
- return this.browser.upload(locator.value, file);
1183
- }
1184
-
1185
- /**
1186
- * Retrieves all texts from an element located by CSS or XPath and returns it to test.
1187
- * Resumes test execution, so **should be used inside async with `await`** operator.
1188
- *
1189
- * ```js
1190
- * let pins = await I.grabTextFromAll('#pin li');
1191
- * ```
1192
- *
1193
- * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1194
- * @returns {Promise<string[]>} attribute value
1195
- *
1196
- */
1197
- async grabTextFromAll(locator) {
1198
- locator = new Locator(locator, 'css');
1199
- const els = await this.browser.findElements(locator.toStrict());
1200
- const texts = [];
1201
- const getText = el => window.codeceptjs.fetchElement(el).innerText;
1202
- for (const el of els) {
1203
- texts.push(await this.browser.evaluate(getText, el));
1204
- }
1205
- return texts;
1206
- }
1207
-
1208
- /**
1209
- * Retrieves a text from an element located by CSS or XPath and returns it to test.
1210
- * Resumes test execution, so **should be used inside async with `await`** operator.
1211
- *
1212
- * ```js
1213
- * let pin = await I.grabTextFrom('#pin');
1214
- * ```
1215
- * If multiple elements found returns first element.
1216
- *
1217
- * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1218
- * @returns {Promise<string>} attribute value
1219
- *
1220
- */
1221
- async grabTextFrom(locator) {
1222
- locator = new Locator(locator, 'css');
1223
- const els = await this.browser.findElement(locator.toStrict());
1224
- assertElementExists(els, locator);
1225
- const texts = await this.grabTextFromAll(locator);
1226
- if (texts.length > 1) {
1227
- this.debugSection('GrabText', `Using first element out of ${texts.length}`);
1228
- }
1229
-
1230
- return texts[0];
1231
- }
1232
-
1233
- /**
1234
- * Retrieves an array of value from a form located by CSS or XPath and returns it to test.
1235
- * Resumes test execution, so **should be used inside async function with `await`** operator.
1236
- *
1237
- * ```js
1238
- * let inputs = await I.grabValueFromAll('//form/input');
1239
- * ```
1240
- * @param {CodeceptJS.LocatorOrString} locator field located by label|name|CSS|XPath|strict locator.
1241
- * @returns {Promise<string[]>} attribute value
1242
- *
1243
- */
1244
- async grabValueFromAll(locator) {
1245
- locator = new Locator(locator, 'css');
1246
- const els = await this.browser.findElements(locator.toStrict());
1247
- const values = [];
1248
- const getValues = el => window.codeceptjs.fetchElement(el).value;
1249
- for (const el of els) {
1250
- values.push(await this.browser.evaluate(getValues, el));
1251
- }
1252
-
1253
- return values;
1254
- }
1255
-
1256
- /**
1257
- * Retrieves a value from a form element located by CSS or XPath and returns it to test.
1258
- * Resumes test execution, so **should be used inside async function with `await`** operator.
1259
- * If more than one element is found - value of first element is returned.
1260
- *
1261
- * ```js
1262
- * let email = await I.grabValueFrom('input[name=email]');
1263
- * ```
1264
- * @param {CodeceptJS.LocatorOrString} locator field located by label|name|CSS|XPath|strict locator.
1265
- * @returns {Promise<string>} attribute value
1266
- *
1267
- */
1268
- async grabValueFrom(locator) {
1269
- const el = await findField.call(this, locator);
1270
- assertElementExists(el, locator, 'Field');
1271
- const values = await this.grabValueFromAll(locator);
1272
- if (values.length > 1) {
1273
- this.debugSection('GrabValue', `Using first element out of ${values.length}`);
1274
- }
1275
-
1276
- return values[0];
1277
- }
1278
-
1279
- /**
1280
- * Retrieves an array of attributes from elements located by CSS or XPath and returns it to test.
1281
- * Resumes test execution, so **should be used inside async with `await`** operator.
1282
- *
1283
- * ```js
1284
- * let hints = await I.grabAttributeFromAll('.tooltip', 'title');
1285
- * ```
1286
- * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1287
- * @param {string} attr attribute name.
1288
- * @returns {Promise<string[]>} attribute value
1289
- *
1290
- */
1291
- async grabAttributeFromAll(locator, attr) {
1292
- locator = new Locator(locator, 'css');
1293
- const els = await this.browser.findElements(locator.toStrict());
1294
- const array = [];
1295
-
1296
- for (let index = 0; index < els.length; index++) {
1297
- const el = els[index];
1298
- array.push(await this.browser.evaluate((el, attr) => window.codeceptjs.fetchElement(el).getAttribute(attr), el, attr));
1299
- }
1300
-
1301
- return array;
1302
- }
1303
-
1304
- /**
1305
- * Retrieves an attribute from an element located by CSS or XPath and returns it to test.
1306
- * Resumes test execution, so **should be used inside async with `await`** operator.
1307
- * If more than one element is found - attribute of first element is returned.
1308
- *
1309
- * ```js
1310
- * let hint = await I.grabAttributeFrom('#tooltip', 'title');
1311
- * ```
1312
- * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1313
- * @param {string} attr attribute name.
1314
- * @returns {Promise<string>} attribute value
1315
- *
1316
- */
1317
- async grabAttributeFrom(locator, attr) {
1318
- locator = new Locator(locator, 'css');
1319
- const els = await this.browser.findElement(locator.toStrict());
1320
- assertElementExists(els, locator);
1321
-
1322
- const attrs = await this.grabAttributeFromAll(locator, attr);
1323
- if (attrs.length > 1) {
1324
- this.debugSection('GrabAttribute', `Using first element out of ${attrs.length}`);
1325
- }
1326
-
1327
- return attrs[0];
1328
- }
1329
-
1330
- /**
1331
- * Retrieves all the innerHTML from elements located by CSS or XPath and returns it to test.
1332
- * Resumes test execution, so **should be used inside async function with `await`** operator.
1333
- *
1334
- * ```js
1335
- * let postHTMLs = await I.grabHTMLFromAll('.post');
1336
- * ```
1337
- *
1338
- * @param {CodeceptJS.LocatorOrString} element located by CSS|XPath|strict locator.
1339
- * @returns {Promise<string[]>} HTML code for an element
1340
- *
1341
- */
1342
- async grabHTMLFromAll(locator) {
1343
- locator = new Locator(locator, 'css');
1344
- const els = await this.browser.findElements(locator.toStrict());
1345
- const array = [];
1346
-
1347
- for (let index = 0; index < els.length; index++) {
1348
- const el = els[index];
1349
- array.push(await this.browser.evaluate(el => window.codeceptjs.fetchElement(el).innerHTML, el));
1350
- }
1351
- this.debugSection('GrabHTML', array);
1352
-
1353
- return array;
1354
- }
1355
-
1356
- /**
1357
- * Retrieves the innerHTML from an element located by CSS or XPath and returns it to test.
1358
- * Resumes test execution, so **should be used inside async function with `await`** operator.
1359
- * If more than one element is found - HTML of first element is returned.
1360
- *
1361
- * ```js
1362
- * let postHTML = await I.grabHTMLFrom('#post');
1363
- * ```
1364
- *
1365
- * @param {CodeceptJS.LocatorOrString} element located by CSS|XPath|strict locator.
1366
- * @returns {Promise<string>} HTML code for an element
1367
- *
1368
- */
1369
- async grabHTMLFrom(locator) {
1370
- locator = new Locator(locator, 'css');
1371
- const els = await this.browser.findElement(locator.toStrict());
1372
- assertElementExists(els, locator);
1373
- const html = await this.grabHTMLFromAll(locator);
1374
- if (html.length > 1) {
1375
- this.debugSection('GrabHTML', `Using first element out of ${html.length}`);
1376
- }
1377
-
1378
- return html[0];
1379
- }
1380
-
1381
- /**
1382
- * Grab CSS property for given locator
1383
- * Resumes test execution, so **should be used inside an async function with `await`** operator.
1384
- * If more than one element is found - value of first element is returned.
1385
- *
1386
- * ```js
1387
- * const value = await I.grabCssPropertyFrom('h3', 'font-weight');
1388
- * ```
1389
- *
1390
- * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1391
- * @param {string} cssProperty CSS property name.
1392
- * @returns {Promise<string>} CSS value
1393
- *
1394
- */
1395
- async grabCssPropertyFrom(locator, cssProperty) {
1396
- locator = new Locator(locator, 'css');
1397
- const els = await this.browser.findElements(locator.toStrict());
1398
- const array = [];
1399
-
1400
- const getCssPropForElement = async (el, prop) => {
1401
- return (await this.browser.evaluate((el) => {
1402
- return window.getComputedStyle(window.codeceptjs.fetchElement(el));
1403
- }, el))[toCamelCase(prop)];
1404
- };
1405
-
1406
- for (const el of els) {
1407
- assertElementExists(el, locator);
1408
- const cssValue = await getCssPropForElement(el, cssProperty);
1409
- array.push(cssValue);
1410
- }
1411
- this.debugSection('HTML', array);
1412
-
1413
- return array.length > 1 ? array : array[0];
1414
- }
1415
-
1416
- _injectClientScripts() {
1417
- return this.browser.inject('js', path.join(__dirname, 'clientscripts', 'nightmare.js'));
1418
- }
1419
-
1420
- /**
1421
- * Selects an option in a drop-down select.
1422
- * Field is searched by label | name | CSS | XPath.
1423
- * Option is selected by visible text or by value.
1424
- *
1425
- * ```js
1426
- * I.selectOption('Choose Plan', 'Monthly'); // select by label
1427
- * I.selectOption('subscription', 'Monthly'); // match option by text
1428
- * I.selectOption('subscription', '0'); // or by value
1429
- * I.selectOption('//form/select[@name=account]','Premium');
1430
- * I.selectOption('form select[name=account]', 'Premium');
1431
- * I.selectOption({css: 'form select[name=account]'}, 'Premium');
1432
- * ```
1433
- *
1434
- * Provide an array for the second argument to select multiple options.
1435
- *
1436
- * ```js
1437
- * I.selectOption('Which OS do you use?', ['Android', 'iOS']);
1438
- * ```
1439
- * @param {LocatorOrString} select field located by label|name|CSS|XPath|strict locator.
1440
- * @param {string|Array<*>} option visible text or value of option.
1441
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1442
- *
1443
- */
1444
- async selectOption(select, option) {
1445
- const fetchAndCheckOption = function (el, locator) {
1446
- el = window.codeceptjs.fetchElement(el);
1447
- const found = document.evaluate(locator, el, null, 5, null);
1448
- let current = null;
1449
- const items = [];
1450
- while (current = found.iterateNext()) {
1451
- items.push(current);
1452
- }
1453
- for (let i = 0; i < items.length; i++) {
1454
- current = items[i];
1455
- if (current instanceof HTMLOptionElement) {
1456
- current.selected = true;
1457
- if (!el.multiple) el.value = current.value;
1458
- }
1459
- const event = document.createEvent('HTMLEvents');
1460
- event.initEvent('change', true, true);
1461
- el.dispatchEvent(event);
1462
- }
1463
- return !!current;
1464
- };
1465
-
1466
- const el = await findField.call(this, select);
1467
- assertElementExists(el, select, 'Selectable field');
1468
- if (!Array.isArray(option)) {
1469
- option = [option];
1470
- }
1471
-
1472
- for (const key in option) {
1473
- const opt = xpathLocator.literal(option[key]);
1474
- const checked = await this.browser.evaluate(fetchAndCheckOption, el, Locator.select.byVisibleText(opt));
1475
-
1476
- if (!checked) {
1477
- await this.browser.evaluate(fetchAndCheckOption, el, Locator.select.byValue(opt));
1478
- }
1479
- }
1480
- return this.browser.wait(this.options.waitForAction);
1481
- }
1482
-
1483
- /**
1484
- * Sets cookie(s).
1485
- *
1486
- * Can be a single cookie object or an array of cookies:
1487
- *
1488
- * ```js
1489
- * I.setCookie({name: 'auth', value: true});
1490
- *
1491
- * // as array
1492
- * I.setCookie([
1493
- * {name: 'auth', value: true},
1494
- * {name: 'agree', value: true}
1495
- * ]);
1496
- * ```
1497
- *
1498
- * @param {Cookie|Array<Cookie>} cookie a cookie object or array of cookie objects.
1499
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1500
- *
1501
- *
1502
- * Wrapper for `.cookies.set(cookie)`.
1503
- * [See more](https://github.com/segmentio/nightmare/blob/master/Readme.md#cookiessetcookie)
1504
- */
1505
- async setCookie(cookie) {
1506
- return this.browser.cookies.set(cookie);
1507
- }
1508
-
1509
- /**
1510
- * Checks that cookie with given name exists.
1511
- *
1512
- * ```js
1513
- * I.seeCookie('Auth');
1514
- * ```
1515
- *
1516
- * @param {string} name cookie name.
1517
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1518
- *
1519
- *
1520
- */
1521
- async seeCookie(name) {
1522
- const res = await this.browser.cookies.get(name);
1523
- truth(`cookie ${name}`, 'to be set').assert(res);
1524
- }
1525
-
1526
- /**
1527
- * Checks that cookie with given name does not exist.
1528
- *
1529
- * ```js
1530
- * I.dontSeeCookie('auth'); // no auth cookie
1531
- * ```
1532
- *
1533
- * @param {string} name cookie name.
1534
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1535
- *
1536
- */
1537
- async dontSeeCookie(name) {
1538
- const res = await this.browser.cookies.get(name);
1539
- truth(`cookie ${name}`, 'to be set').negate(res);
1540
- }
1541
-
1542
- /**
1543
- * Gets a cookie object by name.
1544
- * If none provided gets all cookies.
1545
- * Resumes test execution, so **should be used inside async function with `await`** operator.
1546
- *
1547
- * ```js
1548
- * let cookie = await I.grabCookie('auth');
1549
- * assert(cookie.value, '123456');
1550
- * ```
1551
- *
1552
- * @param {?string} [name=null] cookie name.
1553
- * @returns {Promise<string>|Promise<string[]>} attribute value
1554
- *
1555
- *
1556
- * Cookie in JSON format. If name not passed returns all cookies for this domain.
1557
- *
1558
- * Multiple cookies can be received by passing query object `I.grabCookie({ secure: true});`. If you'd like get all cookies for all urls, use: `.grabCookie({ url: null }).`
1559
- */
1560
- async grabCookie(name) {
1561
- return this.browser.cookies.get(name);
1562
- }
1563
-
1564
- /**
1565
- * Clears a cookie by name,
1566
- * if none provided clears all cookies.
1567
- *
1568
- * ```js
1569
- * I.clearCookie();
1570
- * I.clearCookie('test');
1571
- * ```
1572
- *
1573
- * @param {?string} [cookie=null] (optional, `null` by default) cookie name
1574
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1575
- *
1576
- */
1577
- async clearCookie(cookie) {
1578
- if (!cookie) {
1579
- return this.browser.cookies.clearAll();
1580
- }
1581
- return this.browser.cookies.clear(cookie);
1582
- }
1583
-
1584
- /**
1585
- * Waits for a function to return true (waits for 1 sec by default).
1586
- * Running in browser context.
1587
- *
1588
- * ```js
1589
- * I.waitForFunction(fn[, [args[, timeout]])
1590
- * ```
1591
- *
1592
- * ```js
1593
- * I.waitForFunction(() => window.requests == 0);
1594
- * I.waitForFunction(() => window.requests == 0, 5); // waits for 5 sec
1595
- * I.waitForFunction((count) => window.requests == count, [3], 5) // pass args and wait for 5 sec
1596
- * ```
1597
- *
1598
- * @param {string|function} fn to be executed in browser context.
1599
- * @param {any[]|number} [argsOrSec] (optional, `1` by default) arguments for function or seconds.
1600
- * @param {number} [sec] (optional, `1` by default) time in seconds to wait
1601
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1602
- *
1603
- */
1604
- async waitForFunction(fn, argsOrSec = null, sec = null) {
1605
- let args = [];
1606
- if (argsOrSec) {
1607
- if (Array.isArray(argsOrSec)) {
1608
- args = argsOrSec;
1609
- } else if (typeof argsOrSec === 'number') {
1610
- sec = argsOrSec;
1611
- }
1612
- }
1613
- this.browser.options.waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
1614
- return this.browser.wait(fn, ...args);
1615
- }
1616
-
1617
- /**
1618
- * Pauses execution for a number of seconds.
1619
- *
1620
- * ```js
1621
- * I.wait(2); // wait 2 secs
1622
- * ```
1623
- *
1624
- * @param {number} sec number of second to wait.
1625
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1626
- *
1627
- */
1628
- async wait(sec) {
1629
- return new Promise(((done) => {
1630
- setTimeout(done, sec * 1000);
1631
- }));
1632
- }
1633
-
1634
- /**
1635
- * Waits for a text to appear (by default waits for 1sec).
1636
- * Element can be located by CSS or XPath.
1637
- * Narrow down search results by providing context.
1638
- *
1639
- * ```js
1640
- * I.waitForText('Thank you, form has been submitted');
1641
- * I.waitForText('Thank you, form has been submitted', 5, '#modal');
1642
- * ```
1643
- *
1644
- * @param {string }text to wait for.
1645
- * @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
1646
- * @param {CodeceptJS.LocatorOrString} [context] (optional) element located by CSS|XPath|strict locator.
1647
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1648
- *
1649
- */
1650
- async waitForText(text, sec, context = null) {
1651
- if (!context) {
1652
- context = this.context;
1653
- }
1654
- const locator = new Locator(context, 'css');
1655
- this.browser.options.waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
1656
- return this.browser.wait((by, locator, text) => {
1657
- return window.codeceptjs.findElement(by, locator).innerText.indexOf(text) > -1;
1658
- }, locator.type, locator.value, text).catch((err) => {
1659
- if (err.message.indexOf('Cannot read property') > -1) {
1660
- throw new Error(`element (${JSON.stringify(context)}) is not in DOM. Unable to wait text.`);
1661
- } else if (err.message && err.message.indexOf('.wait() timed out after') > -1) {
1662
- throw new Error(`there is no element(${JSON.stringify(context)}) with text "${text}" after ${sec} sec`);
1663
- } else throw err;
1664
- });
1665
- }
1666
-
1667
- /**
1668
- * Waits for an element to become visible on a page (by default waits for 1sec).
1669
- * Element can be located by CSS or XPath.
1670
- *
1671
- * ```js
1672
- * I.waitForVisible('#popup');
1673
- * ```
1674
- *
1675
- * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1676
- * @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
1677
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1678
- *
1679
- */
1680
- waitForVisible(locator, sec) {
1681
- this.browser.options.waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
1682
- locator = new Locator(locator, 'css');
1683
-
1684
- return this.browser.wait((by, locator) => {
1685
- const el = window.codeceptjs.findElement(by, locator);
1686
- if (!el) return false;
1687
- return el.offsetWidth > 0 && el.offsetHeight > 0;
1688
- }, locator.type, locator.value).catch((err) => {
1689
- if (err.message && err.message.indexOf('.wait() timed out after') > -1) {
1690
- throw new Error(`element (${JSON.stringify(locator)}) still not visible on page after ${sec} sec`);
1691
- } else throw err;
1692
- });
1693
- }
1694
-
1695
- /**
1696
- * Waits for an element to hide (by default waits for 1sec).
1697
- * Element can be located by CSS or XPath.
1698
- *
1699
- * ```js
1700
- * I.waitToHide('#popup');
1701
- * ```
1702
- *
1703
- * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1704
- * @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
1705
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1706
- *
1707
- */
1708
- async waitToHide(locator, sec = null) {
1709
- return this.waitForInvisible(locator, sec);
1710
- }
1711
-
1712
- /**
1713
- * Waits for an element to be removed or become invisible on a page (by default waits for 1sec).
1714
- * Element can be located by CSS or XPath.
1715
- *
1716
- * ```js
1717
- * I.waitForInvisible('#popup');
1718
- * ```
1719
- *
1720
- * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1721
- * @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
1722
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1723
- *
1724
- */
1725
- waitForInvisible(locator, sec) {
1726
- this.browser.options.waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
1727
- locator = new Locator(locator, 'css');
1728
-
1729
- return this.browser.wait((by, locator) => {
1730
- const el = window.codeceptjs.findElement(by, locator);
1731
- if (!el) return true;
1732
- return !(el.offsetWidth > 0 && el.offsetHeight > 0);
1733
- }, locator.type, locator.value).catch((err) => {
1734
- if (err.message && err.message.indexOf('.wait() timed out after') > -1) {
1735
- throw new Error(`element (${JSON.stringify(locator)}) still visible after ${sec} sec`);
1736
- } else throw err;
1737
- });
1738
- }
1739
-
1740
- /**
1741
- * Waits for element to be present on page (by default waits for 1sec).
1742
- * Element can be located by CSS or XPath.
1743
- *
1744
- * ```js
1745
- * I.waitForElement('.btn.continue');
1746
- * I.waitForElement('.btn.continue', 5); // wait for 5 secs
1747
- * ```
1748
- *
1749
- * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1750
- * @param {number} [sec] (optional, `1` by default) time in seconds to wait
1751
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1752
- *
1753
- */
1754
- async waitForElement(locator, sec) {
1755
- this.browser.options.waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
1756
- locator = new Locator(locator, 'css');
1757
-
1758
- return this.browser.wait((by, locator) => window.codeceptjs.findElement(by, locator) !== null, locator.type, locator.value).catch((err) => {
1759
- if (err.message && err.message.indexOf('.wait() timed out after') > -1) {
1760
- throw new Error(`element (${JSON.stringify(locator)}) still not present on page after ${sec} sec`);
1761
- } else throw err;
1762
- });
1763
- }
1764
-
1765
- async waitUntilExists(locator, sec) {
1766
- console.log(`waitUntilExists deprecated:
1767
- * use 'waitForElement' to wait for element to be attached
1768
- * use 'waitForDetached to wait for element to be removed'`);
1769
- return this.waitForDetached(locator, sec);
1770
- }
1771
-
1772
- /**
1773
- * Waits for an element to become not attached to the DOM on a page (by default waits for 1sec).
1774
- * Element can be located by CSS or XPath.
1775
- *
1776
- * ```js
1777
- * I.waitForDetached('#popup');
1778
- * ```
1779
- *
1780
- * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1781
- * @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
1782
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1783
- *
1784
- */
1785
- async waitForDetached(locator, sec) {
1786
- this.browser.options.waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
1787
- sec = this.browser.options.waitForTimeout / 1000;
1788
- locator = new Locator(locator, 'css');
1789
-
1790
- return this.browser.wait((by, locator) => window.codeceptjs.findElement(by, locator) === null, locator.type, locator.value).catch((err) => {
1791
- if (err.message && err.message.indexOf('.wait() timed out after') > -1) {
1792
- throw new Error(`element (${JSON.stringify(locator)}) still on page after ${sec} sec`);
1793
- } else throw err;
1794
- });
1795
- }
1796
-
1797
- /**
1798
- * Reload the current page.
1799
- *
1800
- * ```js
1801
- * I.refreshPage();
1802
- * ```
1803
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1804
- *
1805
- */
1806
- async refreshPage() {
1807
- return this.browser.refresh();
1808
- }
1809
-
1810
- /**
1811
- * Reload the page
1812
- */
1813
- refresh() {
1814
- console.log('Deprecated in favor of refreshPage');
1815
- return this.browser.refresh();
1816
- }
1817
-
1818
- /**
1819
- * Saves screenshot of the specified locator to ouput folder (set in codecept.conf.ts or codecept.conf.js).
1820
- * Filename is relative to output folder.
1821
- *
1822
- * ```js
1823
- * I.saveElementScreenshot(`#submit`,'debug.png');
1824
- * ```
1825
- *
1826
- * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1827
- * @param {string} fileName file name to save.
1828
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1829
- *
1830
- *
1831
- */
1832
- async saveElementScreenshot(locator, fileName) {
1833
- const outputFile = screenshotOutputFolder(fileName);
1834
-
1835
- const rect = await this.grabElementBoundingRect(locator);
1836
-
1837
- const button_clip = {
1838
- x: Math.floor(rect.x),
1839
- y: Math.floor(rect.y),
1840
- width: Math.floor(rect.width),
1841
- height: Math.floor(rect.height),
1842
- };
1843
-
1844
- this.debug(`Screenshot of ${(new Locator(locator))} element has been saved to ${outputFile}`);
1845
- // take the screenshot
1846
- await this.browser.screenshot(outputFile, button_clip);
1847
- }
1848
-
1849
- /**
1850
- * Grab the width, height, location of given locator.
1851
- * Provide `width` or `height`as second param to get your desired prop.
1852
- * Resumes test execution, so **should be used inside an async function with `await`** operator.
1853
- *
1854
- * Returns an object with `x`, `y`, `width`, `height` keys.
1855
- *
1856
- * ```js
1857
- * const value = await I.grabElementBoundingRect('h3');
1858
- * // value is like { x: 226.5, y: 89, width: 527, height: 220 }
1859
- * ```
1860
- *
1861
- * To get only one metric use second parameter:
1862
- *
1863
- * ```js
1864
- * const width = await I.grabElementBoundingRect('h3', 'width');
1865
- * // width == 527
1866
- * ```
1867
- * @param {LocatorOrString} locator element located by CSS|XPath|strict locator.
1868
- * @param {string=} elementSize x, y, width or height of the given element.
1869
- * @returns {Promise<DOMRect>|Promise<number>} Element bounding rectangle
1870
- *
1871
- */
1872
- async grabElementBoundingRect(locator, prop) {
1873
- locator = new Locator(locator, 'css');
1874
-
1875
- const rect = await this.browser.evaluate(async (by, locator) => {
1876
- // store the button in a variable
1877
-
1878
- const build_cluster_btn = await window.codeceptjs.findElement(by, locator);
1879
-
1880
- // use the getClientRects() function on the button to determine
1881
- // the size and location
1882
- const rect = build_cluster_btn.getBoundingClientRect();
1883
-
1884
- // convert the rectangle to a clip object and return it
1885
- return {
1886
- x: rect.left,
1887
- y: rect.top,
1888
- width: rect.width,
1889
- height: rect.height,
1890
- };
1891
- }, locator.type, locator.value);
1892
-
1893
- if (prop) return rect[prop];
1894
- return rect;
1895
- }
1896
-
1897
- /**
1898
- * Saves a screenshot to ouput folder (set in codecept.conf.ts or codecept.conf.js).
1899
- * Filename is relative to output folder.
1900
- * 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.
1901
- *
1902
- * ```js
1903
- * I.saveScreenshot('debug.png');
1904
- * I.saveScreenshot('debug.png', true) //resizes to available scrollHeight and scrollWidth before taking screenshot
1905
- * ```
1906
- *
1907
- * @param {string} fileName file name to save.
1908
- * @param {boolean} [fullPage=false] (optional, `false` by default) flag to enable fullscreen screenshot mode.
1909
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1910
- *
1911
- */
1912
- async saveScreenshot(fileName, fullPage = this.options.fullPageScreenshots) {
1913
- const outputFile = screenshotOutputFolder(fileName);
1914
-
1915
- this.debug(`Screenshot is saving to ${outputFile}`);
1916
-
1917
- if (!fullPage) {
1918
- return this.browser.screenshot(outputFile);
1919
- }
1920
- const { height, width } = await this.browser.evaluate(() => {
1921
- return { height: document.body.scrollHeight, width: document.body.scrollWidth };
1922
- });
1923
- await this.browser.viewport(width, height);
1924
- return this.browser.screenshot(outputFile);
1925
- }
1926
-
1927
- async _failed() {
1928
- if (withinStatus !== false) await this._withinEnd();
1929
- }
1930
-
1931
- /**
1932
- * Scrolls to element matched by locator.
1933
- * Extra shift can be set with offsetX and offsetY options.
1934
- *
1935
- * ```js
1936
- * I.scrollTo('footer');
1937
- * I.scrollTo('#submit', 5, 5);
1938
- * ```
1939
- *
1940
- * @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|strict locator.
1941
- * @param {number} [offsetX=0] (optional, `0` by default) X-axis offset.
1942
- * @param {number} [offsetY=0] (optional, `0` by default) Y-axis offset.
1943
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1944
- *
1945
- */
1946
- async scrollTo(locator, offsetX = 0, offsetY = 0) {
1947
- if (typeof locator === 'number' && typeof offsetX === 'number') {
1948
- offsetY = offsetX;
1949
- offsetX = locator;
1950
- locator = null;
1951
- }
1952
- if (locator) {
1953
- locator = new Locator(locator, 'css');
1954
- return this.browser.evaluate((by, locator, offsetX, offsetY) => {
1955
- const el = window.codeceptjs.findElement(by, locator);
1956
- if (!el) throw new Error(`Element not found ${by}: ${locator}`);
1957
- const rect = el.getBoundingClientRect();
1958
- window.scrollTo(rect.left + offsetX, rect.top + offsetY);
1959
- }, locator.type, locator.value, offsetX, offsetY);
1960
- }
1961
- // eslint-disable-next-line prefer-arrow-callback
1962
- return this.executeScript(function (x, y) { return window.scrollTo(x, y); }, offsetX, offsetY);
1963
- }
1964
-
1965
- /**
1966
- * Scroll page to the top.
1967
- *
1968
- * ```js
1969
- * I.scrollPageToTop();
1970
- * ```
1971
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1972
- *
1973
- */
1974
- async scrollPageToTop() {
1975
- return this.executeScript(() => window.scrollTo(0, 0));
1976
- }
1977
-
1978
- /**
1979
- * Scroll page to the bottom.
1980
- *
1981
- * ```js
1982
- * I.scrollPageToBottom();
1983
- * ```
1984
- * ⚠️ returns a _promise_ which is synchronized internally by recorder
1985
- *
1986
- */
1987
- async scrollPageToBottom() {
1988
- /* eslint-disable prefer-arrow-callback, comma-dangle */
1989
- return this.executeScript(function () {
1990
- const body = document.body;
1991
- const html = document.documentElement;
1992
- window.scrollTo(0, Math.max(
1993
- body.scrollHeight, body.offsetHeight,
1994
- html.clientHeight, html.scrollHeight, html.offsetHeight
1995
- ));
1996
- });
1997
- /* eslint-enable */
1998
- }
1999
-
2000
- /**
2001
- * Retrieves a page scroll position and returns it to test.
2002
- * Resumes test execution, so **should be used inside an async function with `await`** operator.
2003
- *
2004
- * ```js
2005
- * let { x, y } = await I.grabPageScrollPosition();
2006
- * ```
2007
- *
2008
- * @returns {Promise<PageScrollPosition>} scroll position
2009
- *
2010
- */
2011
- async grabPageScrollPosition() {
2012
- /* eslint-disable comma-dangle */
2013
- function getScrollPosition() {
2014
- return {
2015
- x: window.pageXOffset,
2016
- y: window.pageYOffset
2017
- };
2018
- }
2019
- /* eslint-enable comma-dangle */
2020
- return this.executeScript(getScrollPosition);
2021
- }
2022
- }
2023
-
2024
- module.exports = Nightmare;
2025
-
2026
- async function proceedSee(assertType, text, context) {
2027
- let description;
2028
- let locator;
2029
- if (!context) {
2030
- if (this.context === this.options.rootElement) {
2031
- locator = new Locator(this.context, 'css');
2032
- description = 'web application';
2033
- } else {
2034
- description = `current context ${this.context}`;
2035
- locator = new Locator({ xpath: './/*' });
2036
- }
2037
- } else {
2038
- locator = new Locator(context, 'css');
2039
- description = `element ${locator.toString()}`;
2040
- }
2041
-
2042
- const texts = await this.browser.evaluate((by, locator) => {
2043
- return window.codeceptjs.findElements(by, locator).map(el => el.innerText);
2044
- }, locator.type, locator.value);
2045
- const allText = texts.join(' | ');
2046
- return stringIncludes(description)[assertType](text, allText);
2047
- }
2048
-
2049
- async function proceedSeeInField(assertType, field, value) {
2050
- const el = await findField.call(this, field);
2051
- assertElementExists(el, field, 'Field');
2052
- const tag = await this.browser.evaluate(el => window.codeceptjs.fetchElement(el).tagName, el);
2053
- const fieldVal = await this.browser.evaluate(el => window.codeceptjs.fetchElement(el).value, el);
2054
- if (tag === 'select') {
2055
- // locate option by values and check them
2056
- const text = await this.browser.evaluate((el, val) => {
2057
- return el.querySelector(`option[value="${val}"]`).innerText;
2058
- }, el, xpathLocator.literal(fieldVal));
2059
- return equals(`select option by ${field}`)[assertType](value, text);
2060
- }
2061
- return stringIncludes(`field by ${field}`)[assertType](value, fieldVal);
2062
- }
2063
-
2064
- async function proceedIsChecked(assertType, option) {
2065
- const els = await findCheckable.call(this, option);
2066
- assertElementExists(els, option, 'Checkable');
2067
- const selected = await this.browser.evaluate((els) => {
2068
- return els.map(el => window.codeceptjs.fetchElement(el).checked).reduce((prev, cur) => prev || cur);
2069
- }, els);
2070
- return truth(`checkable ${option}`, 'to be checked')[assertType](selected);
2071
- }
2072
-
2073
- async function findCheckable(locator, context) {
2074
- let contextEl = null;
2075
- if (context) {
2076
- contextEl = await this.browser.findElement((new Locator(context, 'css')).toStrict());
2077
- }
2078
-
2079
- const matchedLocator = new Locator(locator);
2080
- if (!matchedLocator.isFuzzy()) {
2081
- return this.browser.findElements(matchedLocator.toStrict(), contextEl);
2082
- }
2083
-
2084
- const literal = xpathLocator.literal(locator);
2085
- let els = await this.browser.findElements({ xpath: Locator.checkable.byText(literal) }, contextEl);
2086
- if (els.length) {
2087
- return els;
2088
- }
2089
- els = await this.browser.findElements({ xpath: Locator.checkable.byName(literal) }, contextEl);
2090
- if (els.length) {
2091
- return els;
2092
- }
2093
- return this.browser.findElements({ css: locator }, contextEl);
2094
- }
2095
-
2096
- async function findClickable(locator, context) {
2097
- let contextEl = null;
2098
- if (context) {
2099
- contextEl = await this.browser.findElement((new Locator(context, 'css')).toStrict());
2100
- }
2101
-
2102
- const matchedLocator = new Locator(locator);
2103
- if (!matchedLocator.isFuzzy()) {
2104
- return this.browser.findElement(matchedLocator.toStrict(), contextEl);
2105
- }
2106
-
2107
- const literal = xpathLocator.literal(locator);
2108
-
2109
- let els = await this.browser.findElements({ xpath: Locator.clickable.narrow(literal) }, contextEl);
2110
- if (els.length) {
2111
- return els[0];
2112
- }
2113
-
2114
- els = await this.browser.findElements({ xpath: Locator.clickable.wide(literal) }, contextEl);
2115
- if (els.length) {
2116
- return els[0];
2117
- }
2118
-
2119
- return this.browser.findElement({ css: locator }, contextEl);
2120
- }
2121
-
2122
- async function findField(locator) {
2123
- const matchedLocator = new Locator(locator);
2124
- if (!matchedLocator.isFuzzy()) {
2125
- return this.browser.findElements(matchedLocator.toStrict());
2126
- }
2127
- const literal = xpathLocator.literal(locator);
2128
-
2129
- let els = await this.browser.findElements({ xpath: Locator.field.labelEquals(literal) });
2130
- if (els.length) {
2131
- return els[0];
2132
- }
2133
-
2134
- els = await this.browser.findElements({ xpath: Locator.field.labelContains(literal) });
2135
- if (els.length) {
2136
- return els[0];
2137
- }
2138
- els = await this.browser.findElements({ xpath: Locator.field.byName(literal) });
2139
- if (els.length) {
2140
- return els[0];
2141
- }
2142
- return this.browser.findElement({ css: locator });
2143
- }
2144
-
2145
- function assertElementExists(el, locator, prefix, suffix) {
2146
- if (el === null) throw new ElementNotFound(locator, prefix, suffix);
2147
- }