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,228 +0,0 @@
1
- const assert = require('assert');
2
- const path = require('path');
3
- const fs = require('fs');
4
-
5
- const Helper = require('@codeceptjs/helper');
6
- const { fileExists } = require('../utils');
7
- const { fileIncludes } = require('../assert/include');
8
- const { fileEquals } = require('../assert/equal');
9
-
10
- /**
11
- * Helper for testing filesystem.
12
- * Can be easily used to check file structures:
13
- *
14
- * ```js
15
- * I.amInPath('test');
16
- * I.seeFile('codecept.js');
17
- * I.seeInThisFile('FileSystem');
18
- * I.dontSeeInThisFile("WebDriver");
19
- * ```
20
- *
21
- * ## Configuration
22
- *
23
- * Enable helper in config file:
24
- *
25
- * ```js
26
- * helpers: {
27
- * FileSystem: {},
28
- * }
29
- * ```
30
- *
31
- * ## Methods
32
- */
33
- class FileSystem extends Helper {
34
- constructor() {
35
- super();
36
- this.dir = global.codecept_dir;
37
- this.file = '';
38
- }
39
-
40
- _before() {
41
- this.debugSection('Dir', this.dir);
42
- }
43
-
44
- /**
45
- * Enters a directory In local filesystem.
46
- * Starts from a current directory
47
- * @param {string} openPath
48
- */
49
- amInPath(openPath) {
50
- this.dir = path.join(global.codecept_dir, openPath);
51
- this.debugSection('Dir', this.dir);
52
- }
53
-
54
- /**
55
- * Writes test to file
56
- * @param {string} name
57
- * @param {string} text
58
- */
59
- writeToFile(name, text) {
60
- fs.writeFileSync(path.join(this.dir, name), text);
61
- }
62
-
63
- /**
64
- * Checks that file exists
65
- * @param {string} name
66
- */
67
- seeFile(name) {
68
- this.file = path.join(this.dir, name);
69
- this.debugSection('File', this.file);
70
- assert.ok(fileExists(this.file), `File ${name} not found in ${this.dir}`);
71
- }
72
-
73
- /**
74
- * Waits for the file to be present in the current directory.
75
- *
76
- * ```js
77
- * I.handleDownloads('downloads/largeFilesName.txt');
78
- * I.click('Download large File');
79
- * I.amInPath('output/downloads');
80
- * I.waitForFile('largeFilesName.txt', 10); // wait 10 seconds for file
81
- * ```
82
- * @param {string} name
83
- * @param {number} [sec=1] seconds to wait
84
- */
85
- async waitForFile(name, sec = 1) {
86
- if (sec === 0) assert.fail('Use `seeFile` instead of waiting 0 seconds!');
87
- const waitTimeout = sec * 1000;
88
- this.file = path.join(this.dir, name);
89
- this.debugSection('File', this.file);
90
- return isFileExists(this.file, waitTimeout).catch(() => {
91
- throw new Error(`file (${name}) still not present in directory ${this.dir} after ${waitTimeout / 1000} sec`);
92
- });
93
- }
94
-
95
- /**
96
- * Checks that file with a name including given text exists in the current directory.
97
- *
98
- *```js
99
- * I.handleDownloads();
100
- * I.click('Download as PDF');
101
- * I.amInPath('output/downloads');
102
- * I.seeFileNameMatching('.pdf');
103
- * ```
104
- * @param {string} text
105
- */
106
- seeFileNameMatching(text) {
107
- assert.ok(
108
- this.grabFileNames().some(file => file.includes(text)),
109
- `File name which contains ${text} not found in ${this.dir}`,
110
- );
111
- }
112
-
113
- /**
114
- * Checks that file found by `seeFile` includes a text.
115
- * @param {string} text
116
- * @param {string} [encoding='utf8']
117
- */
118
- seeInThisFile(text, encoding = 'utf8') {
119
- const content = getFileContents(this.file, encoding);
120
- fileIncludes(this.file).assert(text, content);
121
- }
122
-
123
- /**
124
- * Checks that file found by `seeFile` doesn't include text.
125
- * @param {string} text
126
- * @param {string} [encoding='utf8']
127
- */
128
- dontSeeInThisFile(text, encoding = 'utf8') {
129
- const content = getFileContents(this.file, encoding);
130
- fileIncludes(this.file).negate(text, content);
131
- }
132
-
133
- /**
134
- * Checks that contents of file found by `seeFile` equal to text.
135
- * @param {string} text
136
- * @param {string} [encoding='utf8']
137
- */
138
- seeFileContentsEqual(text, encoding = 'utf8') {
139
- const content = getFileContents(this.file, encoding);
140
- fileEquals(this.file).assert(text, content);
141
- }
142
-
143
- /**
144
- * Checks that contents of the file found by `seeFile` equal to contents of the file at `pathToReferenceFile`.
145
- * @param {string} pathToReferenceFile
146
- * @param {string} [encoding='utf8']
147
- * @param {string} [encodingReference='utf8']
148
- */
149
- seeFileContentsEqualReferenceFile(pathToReferenceFile, encoding = 'utf8', encodingReference = '') {
150
- const content = getFileContents(this.file, encoding);
151
- assert.ok(fileExists(pathToReferenceFile), `Reference file ${pathToReferenceFile} not found.`);
152
- encodingReference = encodingReference || encoding;
153
- const expectedContent = getFileContents(pathToReferenceFile, encodingReference);
154
- fileEquals(this.file).assert(expectedContent, content);
155
- }
156
-
157
- /**
158
- * Checks that contents of file found by `seeFile` doesn't equal to text.
159
- * @param {string} text
160
- * @param {string} [encoding='utf8']
161
- */
162
- dontSeeFileContentsEqual(text, encoding = 'utf8') {
163
- const content = getFileContents(this.file, encoding);
164
- fileEquals(this.file).negate(text, content);
165
- }
166
-
167
- /**
168
- * Returns file names in current directory.
169
- *
170
- * ```js
171
- * I.handleDownloads();
172
- * I.click('Download Files');
173
- * I.amInPath('output/downloads');
174
- * const downloadedFileNames = I.grabFileNames();
175
- * ```
176
- */
177
- grabFileNames() {
178
- return fs.readdirSync(this.dir)
179
- .filter(item => !fs.lstatSync(path.join(this.dir, item)).isDirectory());
180
- }
181
- }
182
-
183
- module.exports = FileSystem;
184
-
185
- /**
186
- * @param {string} file
187
- * @param {string} [encoding='utf8']
188
- * @private
189
- * @returns {string}
190
- */
191
- function getFileContents(file, encoding = 'utf8') {
192
- if (!file) assert.fail('No files were opened, please use seeFile action');
193
- if (encoding === '') assert.fail('Encoding is an empty string, please set a valid encoding');
194
- return fs.readFileSync(file, encoding);
195
- }
196
-
197
- /**
198
- * @param {string} file
199
- * @param {number} timeout
200
- * @private
201
- * @returns {Promise<any>}
202
- */
203
- function isFileExists(file, timeout) {
204
- return new Promise(((resolve, reject) => {
205
- const timer = setTimeout(() => {
206
- watcher.close();
207
- reject(new Error('File did not exists and was not created during the timeout.'));
208
- }, timeout);
209
-
210
- const dir = path.dirname(file);
211
- const basename = path.basename(file);
212
- const watcher = fs.watch(dir, (eventType, filename) => {
213
- if (eventType === 'rename' && filename === basename) {
214
- clearTimeout(timer);
215
- watcher.close();
216
- resolve();
217
- }
218
- });
219
-
220
- fs.access(file, fs.constants.R_OK, (err) => {
221
- if (!err) {
222
- clearTimeout(timer);
223
- watcher.close();
224
- resolve();
225
- }
226
- });
227
- }));
228
- }
@@ -1,204 +0,0 @@
1
- const axios = require('axios').default;
2
- const Helper = require('@codeceptjs/helper');
3
-
4
- /**
5
- * GraphQL helper allows to send additional requests to a GraphQl endpoint during acceptance tests.
6
- * [Axios](https://github.com/axios/axios) library is used to perform requests.
7
- *
8
- * ## Configuration
9
- *
10
- * * endpoint: GraphQL base URL
11
- * * timeout: timeout for requests in milliseconds. 10000ms by default
12
- * * defaultHeaders: a list of default headers
13
- * * onRequest: a async function which can update request object.
14
- *
15
- * ## Example
16
- *
17
- * ```js
18
- * GraphQL: {
19
- * endpoint: 'http://site.com/graphql/',
20
- * onRequest: (request) => {
21
- * request.headers.auth = '123';
22
- * }
23
- * }
24
- * ```
25
- *
26
- * ## Access From Helpers
27
- *
28
- * Send GraphQL requests by accessing `_executeQuery` method:
29
- *
30
- * ```js
31
- * this.helpers['GraphQL']._executeQuery({
32
- * url,
33
- * data,
34
- * });
35
- * ```
36
- *
37
- * ## Methods
38
- */
39
- class GraphQL extends Helper {
40
- constructor(config) {
41
- super(config);
42
- this.axios = axios.create();
43
- this.headers = {};
44
- this.options = {
45
- timeout: 10000,
46
- defaultHeaders: {},
47
- endpoint: '',
48
- };
49
- this.options = Object.assign(this.options, config);
50
- this.headers = { ...this.options.defaultHeaders };
51
- this.axios.defaults.headers = this.options.defaultHeaders;
52
- }
53
-
54
- static _checkRequirements() {
55
- try {
56
- require('axios');
57
- } catch (e) {
58
- return ['axios'];
59
- }
60
- }
61
-
62
- static _config() {
63
- return [
64
- { name: 'endpoint', message: 'Endpoint of API you are going to test', default: 'http://localhost:3000/graphql' },
65
- ];
66
- }
67
-
68
- /**
69
- * Executes query via axios call
70
- *
71
- * @param {object} request
72
- */
73
- async _executeQuery(request) {
74
- this.axios.defaults.timeout = request.timeout || this.options.timeout;
75
-
76
- if (this.headers && this.headers.auth) {
77
- request.auth = this.headers.auth;
78
- }
79
-
80
- request.headers = Object.assign(request.headers, {
81
- 'Content-Type': 'application/json',
82
- });
83
-
84
- if (this.config.onRequest) {
85
- await this.config.onRequest(request);
86
- }
87
-
88
- this.debugSection('Request', JSON.stringify(request));
89
-
90
- let response;
91
- try {
92
- response = await this.axios(request);
93
- } catch (err) {
94
- if (!err.response) throw err;
95
- this.debugSection(
96
- 'Response',
97
- `Response error. Status code: ${err.response.status}`,
98
- );
99
- response = err.response;
100
- }
101
-
102
- if (this.config.onResponse) {
103
- await this.config.onResponse(response);
104
- }
105
-
106
- this.debugSection('Response', JSON.stringify(response.data));
107
- return response;
108
- }
109
-
110
- /**
111
- * Prepares request for axios call
112
- *
113
- * @param {object} operation
114
- * @param {object} headers
115
- * @return {object} graphQLRequest
116
- */
117
- _prepareGraphQLRequest(operation, headers) {
118
- return {
119
- baseURL: this.options.endpoint,
120
- method: 'POST',
121
- data: operation,
122
- headers,
123
- };
124
- }
125
-
126
- /**
127
- * Send query to GraphQL endpoint over http.
128
- * Returns a response as a promise.
129
- *
130
- * ```js
131
- *
132
- * const response = await I.sendQuery('{ users { name email }}');
133
- * // with variables
134
- * const response = await I.sendQuery(
135
- * 'query getUser($id: ID) { user(id: $id) { name email }}',
136
- * { id: 1 },
137
- * )
138
- * const user = response.data.data;
139
- * ```
140
- *
141
- * @param {String} query
142
- * @param {object} [variables] that may go along with the query
143
- * @param {object} [options] are additional query options
144
- * @param {object} [headers]
145
- * @return Promise<any>
146
- */
147
- async sendQuery(query, variables, options = {}, headers = {}) {
148
- if (typeof query !== 'string') {
149
- throw new Error(`query expected to be a String, instead received ${typeof query}`);
150
- }
151
- const operation = {
152
- query,
153
- variables,
154
- ...options,
155
- };
156
- const request = this._prepareGraphQLRequest(operation, headers);
157
- return this._executeQuery(request);
158
- }
159
-
160
- /**
161
- * Send query to GraphQL endpoint over http
162
- *
163
- * ```js
164
- * I.sendMutation(`
165
- * mutation createUser($user: UserInput!) {
166
- * createUser(user: $user) {
167
- * id
168
- * name
169
- * email
170
- * }
171
- * }
172
- * `,
173
- * { user: {
174
- * name: 'John Doe',
175
- * email: 'john@xmail.com'
176
- * }
177
- * },
178
- * });
179
- * ```
180
- *
181
- * @param {String} mutation
182
- * @param {object} [variables] that may go along with the mutation
183
- * @param {object} [options] are additional query options
184
- * @param {object} [headers]
185
- * @return Promise<any>
186
- */
187
- async sendMutation(mutation, variables, options = {}, headers = {}) {
188
- if (typeof mutation !== 'string') {
189
- throw new Error(`mutation expected to be a String, instead received ${typeof mutation}`);
190
- }
191
- const operation = {
192
- query: mutation,
193
- variables,
194
- ...options,
195
- };
196
- const request = this._prepareGraphQLRequest(operation, headers);
197
- return this._executeQuery(request);
198
- }
199
-
200
- _setRequestTimeout(newTimeout) {
201
- this.options.timeout = newTimeout;
202
- }
203
- }
204
- module.exports = GraphQL;