codeceptjs 3.5.12-beta.3 → 3.5.12-beta.5

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 (209) hide show
  1. package/docs/advanced.md +351 -0
  2. package/docs/ai.md +248 -0
  3. package/docs/api.md +323 -0
  4. package/docs/basics.md +979 -0
  5. package/docs/bdd.md +539 -0
  6. package/docs/best.md +237 -0
  7. package/docs/books.md +37 -0
  8. package/docs/bootstrap.md +135 -0
  9. package/docs/build/ApiDataFactory.js +410 -0
  10. package/docs/build/Appium.js +2027 -0
  11. package/docs/build/Expect.js +422 -0
  12. package/docs/build/FileSystem.js +228 -0
  13. package/docs/build/GraphQL.js +229 -0
  14. package/docs/build/GraphQLDataFactory.js +309 -0
  15. package/docs/build/JSONResponse.js +338 -0
  16. package/docs/build/Mochawesome.js +71 -0
  17. package/docs/build/Nightmare.js +2152 -0
  18. package/docs/build/OpenAI.js +126 -0
  19. package/docs/build/Playwright.js +5110 -0
  20. package/docs/build/Protractor.js +2706 -0
  21. package/docs/build/Puppeteer.js +3905 -0
  22. package/docs/build/REST.js +344 -0
  23. package/docs/build/TestCafe.js +2125 -0
  24. package/docs/build/WebDriver.js +4240 -0
  25. package/docs/changelog.md +2572 -0
  26. package/docs/commands.md +266 -0
  27. package/docs/community-helpers.md +58 -0
  28. package/docs/configuration.md +157 -0
  29. package/docs/continuous-integration.md +22 -0
  30. package/docs/custom-helpers.md +306 -0
  31. package/docs/data.md +379 -0
  32. package/docs/detox.md +235 -0
  33. package/docs/docker.md +136 -0
  34. package/docs/email.md +183 -0
  35. package/docs/examples.md +149 -0
  36. package/docs/helpers/ApiDataFactory.md +266 -0
  37. package/docs/helpers/Appium.md +1374 -0
  38. package/docs/helpers/Detox.md +586 -0
  39. package/docs/helpers/Expect.md +275 -0
  40. package/docs/helpers/FileSystem.md +152 -0
  41. package/docs/helpers/GraphQL.md +151 -0
  42. package/docs/helpers/GraphQLDataFactory.md +226 -0
  43. package/docs/helpers/JSONResponse.md +254 -0
  44. package/docs/helpers/Mochawesome.md +8 -0
  45. package/docs/helpers/MockRequest.md +377 -0
  46. package/docs/helpers/Nightmare.md +1305 -0
  47. package/docs/helpers/OpenAI.md +70 -0
  48. package/docs/helpers/Playwright.md +2759 -0
  49. package/docs/helpers/Polly.md +44 -0
  50. package/docs/helpers/Protractor.md +1769 -0
  51. package/docs/helpers/Puppeteer-firefox.md +86 -0
  52. package/docs/helpers/Puppeteer.md +2317 -0
  53. package/docs/helpers/REST.md +218 -0
  54. package/docs/helpers/TestCafe.md +1321 -0
  55. package/docs/helpers/WebDriver.md +2547 -0
  56. package/docs/hooks.md +340 -0
  57. package/docs/index.md +111 -0
  58. package/docs/installation.md +75 -0
  59. package/docs/internal-api.md +266 -0
  60. package/docs/locators.md +339 -0
  61. package/docs/mobile-react-native-locators.md +67 -0
  62. package/docs/mobile.md +338 -0
  63. package/docs/pageobjects.md +291 -0
  64. package/docs/parallel.md +400 -0
  65. package/docs/playwright.md +632 -0
  66. package/docs/plugins.md +1259 -0
  67. package/docs/puppeteer.md +316 -0
  68. package/docs/quickstart.md +162 -0
  69. package/docs/react.md +70 -0
  70. package/docs/reports.md +392 -0
  71. package/docs/secrets.md +36 -0
  72. package/docs/shadow.md +68 -0
  73. package/docs/shared/keys.mustache +31 -0
  74. package/docs/shared/react.mustache +1 -0
  75. package/docs/testcafe.md +174 -0
  76. package/docs/translation.md +247 -0
  77. package/docs/tutorial.md +271 -0
  78. package/docs/typescript.md +180 -0
  79. package/docs/ui.md +59 -0
  80. package/docs/videos.md +28 -0
  81. package/docs/visual.md +202 -0
  82. package/docs/vue.md +143 -0
  83. package/docs/webapi/amOnPage.mustache +11 -0
  84. package/docs/webapi/appendField.mustache +11 -0
  85. package/docs/webapi/attachFile.mustache +12 -0
  86. package/docs/webapi/blur.mustache +18 -0
  87. package/docs/webapi/checkOption.mustache +13 -0
  88. package/docs/webapi/clearCookie.mustache +9 -0
  89. package/docs/webapi/clearField.mustache +9 -0
  90. package/docs/webapi/click.mustache +25 -0
  91. package/docs/webapi/clickLink.mustache +8 -0
  92. package/docs/webapi/closeCurrentTab.mustache +7 -0
  93. package/docs/webapi/closeOtherTabs.mustache +8 -0
  94. package/docs/webapi/dontSee.mustache +11 -0
  95. package/docs/webapi/dontSeeCheckboxIsChecked.mustache +10 -0
  96. package/docs/webapi/dontSeeCookie.mustache +8 -0
  97. package/docs/webapi/dontSeeCurrentUrlEquals.mustache +10 -0
  98. package/docs/webapi/dontSeeElement.mustache +8 -0
  99. package/docs/webapi/dontSeeElementInDOM.mustache +8 -0
  100. package/docs/webapi/dontSeeInCurrentUrl.mustache +4 -0
  101. package/docs/webapi/dontSeeInField.mustache +11 -0
  102. package/docs/webapi/dontSeeInSource.mustache +8 -0
  103. package/docs/webapi/dontSeeInTitle.mustache +8 -0
  104. package/docs/webapi/doubleClick.mustache +13 -0
  105. package/docs/webapi/downloadFile.mustache +12 -0
  106. package/docs/webapi/dragAndDrop.mustache +9 -0
  107. package/docs/webapi/dragSlider.mustache +11 -0
  108. package/docs/webapi/executeAsyncScript.mustache +24 -0
  109. package/docs/webapi/executeScript.mustache +26 -0
  110. package/docs/webapi/fillField.mustache +16 -0
  111. package/docs/webapi/focus.mustache +13 -0
  112. package/docs/webapi/forceClick.mustache +28 -0
  113. package/docs/webapi/forceRightClick.mustache +18 -0
  114. package/docs/webapi/grabAllWindowHandles.mustache +7 -0
  115. package/docs/webapi/grabAttributeFrom.mustache +10 -0
  116. package/docs/webapi/grabAttributeFromAll.mustache +9 -0
  117. package/docs/webapi/grabBrowserLogs.mustache +9 -0
  118. package/docs/webapi/grabCookie.mustache +11 -0
  119. package/docs/webapi/grabCssPropertyFrom.mustache +11 -0
  120. package/docs/webapi/grabCssPropertyFromAll.mustache +10 -0
  121. package/docs/webapi/grabCurrentUrl.mustache +9 -0
  122. package/docs/webapi/grabCurrentWindowHandle.mustache +6 -0
  123. package/docs/webapi/grabDataFromPerformanceTiming.mustache +20 -0
  124. package/docs/webapi/grabElementBoundingRect.mustache +20 -0
  125. package/docs/webapi/grabGeoLocation.mustache +8 -0
  126. package/docs/webapi/grabHTMLFrom.mustache +10 -0
  127. package/docs/webapi/grabHTMLFromAll.mustache +9 -0
  128. package/docs/webapi/grabNumberOfOpenTabs.mustache +8 -0
  129. package/docs/webapi/grabNumberOfVisibleElements.mustache +9 -0
  130. package/docs/webapi/grabPageScrollPosition.mustache +8 -0
  131. package/docs/webapi/grabPopupText.mustache +5 -0
  132. package/docs/webapi/grabSource.mustache +8 -0
  133. package/docs/webapi/grabTextFrom.mustache +10 -0
  134. package/docs/webapi/grabTextFromAll.mustache +9 -0
  135. package/docs/webapi/grabTitle.mustache +8 -0
  136. package/docs/webapi/grabValueFrom.mustache +9 -0
  137. package/docs/webapi/grabValueFromAll.mustache +8 -0
  138. package/docs/webapi/grabWebElement.mustache +9 -0
  139. package/docs/webapi/grabWebElements.mustache +9 -0
  140. package/docs/webapi/moveCursorTo.mustache +12 -0
  141. package/docs/webapi/openNewTab.mustache +7 -0
  142. package/docs/webapi/pressKey.mustache +12 -0
  143. package/docs/webapi/pressKeyDown.mustache +12 -0
  144. package/docs/webapi/pressKeyUp.mustache +12 -0
  145. package/docs/webapi/pressKeyWithKeyNormalization.mustache +60 -0
  146. package/docs/webapi/refreshPage.mustache +6 -0
  147. package/docs/webapi/resizeWindow.mustache +6 -0
  148. package/docs/webapi/rightClick.mustache +14 -0
  149. package/docs/webapi/saveElementScreenshot.mustache +10 -0
  150. package/docs/webapi/saveScreenshot.mustache +12 -0
  151. package/docs/webapi/say.mustache +10 -0
  152. package/docs/webapi/scrollIntoView.mustache +11 -0
  153. package/docs/webapi/scrollPageToBottom.mustache +6 -0
  154. package/docs/webapi/scrollPageToTop.mustache +6 -0
  155. package/docs/webapi/scrollTo.mustache +12 -0
  156. package/docs/webapi/see.mustache +11 -0
  157. package/docs/webapi/seeAttributesOnElements.mustache +9 -0
  158. package/docs/webapi/seeCheckboxIsChecked.mustache +10 -0
  159. package/docs/webapi/seeCookie.mustache +8 -0
  160. package/docs/webapi/seeCssPropertiesOnElements.mustache +9 -0
  161. package/docs/webapi/seeCurrentUrlEquals.mustache +11 -0
  162. package/docs/webapi/seeElement.mustache +8 -0
  163. package/docs/webapi/seeElementInDOM.mustache +8 -0
  164. package/docs/webapi/seeInCurrentUrl.mustache +8 -0
  165. package/docs/webapi/seeInField.mustache +12 -0
  166. package/docs/webapi/seeInPopup.mustache +8 -0
  167. package/docs/webapi/seeInSource.mustache +7 -0
  168. package/docs/webapi/seeInTitle.mustache +8 -0
  169. package/docs/webapi/seeNumberOfElements.mustache +11 -0
  170. package/docs/webapi/seeNumberOfVisibleElements.mustache +10 -0
  171. package/docs/webapi/seeTextEquals.mustache +9 -0
  172. package/docs/webapi/seeTitleEquals.mustache +8 -0
  173. package/docs/webapi/selectOption.mustache +21 -0
  174. package/docs/webapi/setCookie.mustache +16 -0
  175. package/docs/webapi/setGeoLocation.mustache +12 -0
  176. package/docs/webapi/switchTo.mustache +9 -0
  177. package/docs/webapi/switchToNextTab.mustache +10 -0
  178. package/docs/webapi/switchToPreviousTab.mustache +10 -0
  179. package/docs/webapi/type.mustache +21 -0
  180. package/docs/webapi/uncheckOption.mustache +13 -0
  181. package/docs/webapi/wait.mustache +8 -0
  182. package/docs/webapi/waitForClickable.mustache +11 -0
  183. package/docs/webapi/waitForDetached.mustache +10 -0
  184. package/docs/webapi/waitForElement.mustache +11 -0
  185. package/docs/webapi/waitForEnabled.mustache +6 -0
  186. package/docs/webapi/waitForFunction.mustache +17 -0
  187. package/docs/webapi/waitForInvisible.mustache +10 -0
  188. package/docs/webapi/waitForNumberOfTabs.mustache +9 -0
  189. package/docs/webapi/waitForText.mustache +13 -0
  190. package/docs/webapi/waitForValue.mustache +10 -0
  191. package/docs/webapi/waitForVisible.mustache +10 -0
  192. package/docs/webapi/waitInUrl.mustache +9 -0
  193. package/docs/webapi/waitNumberOfVisibleElements.mustache +10 -0
  194. package/docs/webapi/waitToHide.mustache +10 -0
  195. package/docs/webapi/waitUrlEquals.mustache +10 -0
  196. package/docs/webdriver.md +701 -0
  197. package/docs/wiki/Books-&-Posts.md +27 -0
  198. package/docs/wiki/Community-Helpers-&-Plugins.md +53 -0
  199. package/docs/wiki/Converting-Playwright-to-Istanbul-Coverage.md +61 -0
  200. package/docs/wiki/Examples.md +145 -0
  201. package/docs/wiki/Google-Summer-of-Code-(GSoC)-2020.md +68 -0
  202. package/docs/wiki/Home.md +16 -0
  203. package/docs/wiki/Migration-to-Appium-v2---CodeceptJS.md +83 -0
  204. package/docs/wiki/Release-Process.md +24 -0
  205. package/docs/wiki/Roadmap.md +23 -0
  206. package/docs/wiki/Tests.md +1393 -0
  207. package/docs/wiki/Upgrading-to-CodeceptJS-3.md +153 -0
  208. package/docs/wiki/Videos.md +19 -0
  209. package/package.json +2 -1
@@ -0,0 +1,2027 @@
1
+ let webdriverio;
2
+
3
+ const fs = require('fs');
4
+ const axios = require('axios').default;
5
+
6
+ const Webdriver = require('./WebDriver');
7
+ const AssertionFailedError = require('../assert/error');
8
+ const { truth } = require('../assert/truth');
9
+ const recorder = require('../recorder');
10
+ const Locator = require('../locator');
11
+ const ConnectionRefused = require('./errors/ConnectionRefused');
12
+
13
+ const mobileRoot = '//*';
14
+ const webRoot = 'body';
15
+ const supportedPlatform = {
16
+ android: 'Android',
17
+ iOS: 'iOS',
18
+ };
19
+
20
+ const vendorPrefix = {
21
+ appium: 'appium',
22
+ };
23
+
24
+ /**
25
+ * Appium helper extends [Webdriver](http://codecept.io/helpers/WebDriver/) helper.
26
+ * It supports all browser methods and also includes special methods for mobile apps testing.
27
+ * You can use this helper to test Web on desktop and mobile devices and mobile apps.
28
+ *
29
+ * ## Appium Installation
30
+ *
31
+ * Appium is an open source test automation framework for use with native, hybrid and mobile web apps that implements the WebDriver protocol.
32
+ * It allows you to run Selenium tests on mobile devices and also test native, hybrid and mobile web apps.
33
+ *
34
+ * Download and install [Appium](https://appium.io/docs/en/2.1/)
35
+ *
36
+ * ```sh
37
+ * npm install -g appium
38
+ * ```
39
+ *
40
+ * Launch the daemon: `appium`
41
+ *
42
+ * ## Helper configuration
43
+ *
44
+ * This helper should be configured in codecept.conf.ts or codecept.conf.js
45
+ *
46
+ * * `appiumV2`: set this to true if you want to run tests with AppiumV2. See more how to setup [here](https://codecept.io/mobile/#setting-up)
47
+ * * `app`: Application path. Local path or remote URL to an .ipa or .apk file, or a .zip containing one of these. Alias to desiredCapabilities.appPackage
48
+ * * `host`: (default: 'localhost') Appium host
49
+ * * `port`: (default: '4723') Appium port
50
+ * * `platform`: (Android or IOS), which mobile OS to use; alias to desiredCapabilities.platformName
51
+ * * `restart`: restart browser or app between tests (default: true), if set to false cookies will be cleaned but browser window will be kept and for apps nothing will be changed.
52
+ * * `desiredCapabilities`: [], Appium capabilities, see below
53
+ * * `platformName` - Which mobile OS platform to use
54
+ * * `appPackage` - Java package of the Android app you want to run
55
+ * * `appActivity` - Activity name for the Android activity you want to launch from your package.
56
+ * * `deviceName`: The kind of mobile device or emulator to use
57
+ * * `platformVersion`: Mobile OS version
58
+ * * `app` - The absolute local path or remote http URL to an .ipa or .apk file, or a .zip containing one of these. Appium will attempt to install this app binary on the appropriate device first.
59
+ * * `browserName`: Name of mobile web browser to automate. Should be an empty string if automating an app instead.
60
+ *
61
+ * Example Android App:
62
+ *
63
+ * ```js
64
+ * {
65
+ * helpers: {
66
+ * Appium: {
67
+ * platform: "Android",
68
+ * desiredCapabilities: {
69
+ * appPackage: "com.example.android.myApp",
70
+ * appActivity: "MainActivity",
71
+ * deviceName: "OnePlus3",
72
+ * platformVersion: "6.0.1"
73
+ * }
74
+ * }
75
+ * }
76
+ * }
77
+ * ```
78
+ *
79
+ * Example iOS Mobile Web with local Appium:
80
+ *
81
+ * ```js
82
+ * {
83
+ * helpers: {
84
+ * Appium: {
85
+ * platform: "iOS",
86
+ * url: "https://the-internet.herokuapp.com/",
87
+ * desiredCapabilities: {
88
+ * deviceName: "iPhone X",
89
+ * platformVersion: "12.0",
90
+ * browserName: "safari"
91
+ * }
92
+ * }
93
+ * }
94
+ * }
95
+ * ```
96
+ *
97
+ * Example iOS Mobile Web on BrowserStack:
98
+ *
99
+ * ```js
100
+ * {
101
+ * helpers: {
102
+ * Appium: {
103
+ * host: "hub-cloud.browserstack.com",
104
+ * port: 4444,
105
+ * user: process.env.BROWSERSTACK_USER,
106
+ * key: process.env.BROWSERSTACK_KEY,
107
+ * platform: "iOS",
108
+ * url: "https://the-internet.herokuapp.com/",
109
+ * desiredCapabilities: {
110
+ * realMobile: "true",
111
+ * device: "iPhone 8",
112
+ * os_version: "12",
113
+ * browserName: "safari"
114
+ * }
115
+ * }
116
+ * }
117
+ * }
118
+ * ```
119
+ *
120
+ * Example Android App using AppiumV2 on BrowserStack:
121
+ *
122
+ * ```js
123
+ * {
124
+ * helpers: {
125
+ * Appium: {
126
+ * appiumV2: true,
127
+ * host: "hub-cloud.browserstack.com",
128
+ * port: 4444,
129
+ * user: process.env.BROWSERSTACK_USER,
130
+ * key: process.env.BROWSERSTACK_KEY,
131
+ * app: `bs://c700ce60cf1gjhgjh3ae8ed9770ghjg5a55b8e022f13c5827cg`,
132
+ * browser: '',
133
+ * desiredCapabilities: {
134
+ * 'appPackage': data.packageName,
135
+ * 'deviceName': process.env.DEVICE || 'Google Pixel 3',
136
+ * 'platformName': process.env.PLATFORM || 'android',
137
+ * 'platformVersion': process.env.OS_VERSION || '10.0',
138
+ * 'automationName': process.env.ENGINE || 'UIAutomator2',
139
+ * 'newCommandTimeout': 300000,
140
+ * 'androidDeviceReadyTimeout': 300000,
141
+ * 'androidInstallTimeout': 90000,
142
+ * 'appWaitDuration': 300000,
143
+ * 'autoGrantPermissions': true,
144
+ * 'gpsEnabled': true,
145
+ * 'isHeadless': false,
146
+ * 'noReset': false,
147
+ * 'noSign': true,
148
+ * 'bstack:options' : {
149
+ * "appiumVersion" : "2.0.1",
150
+ * },
151
+ * }
152
+ * }
153
+ * }
154
+ * }
155
+ * ```
156
+ *
157
+ * Additional configuration params can be used from <https://github.com/appium/appium/blob/master/packages/appium/docs/en/guides/caps.md>
158
+ *
159
+ * ## Access From Helpers
160
+ *
161
+ * Receive Appium client from a custom helper by accessing `browser` property:
162
+ *
163
+ * ```js
164
+ * let browser = this.helpers['Appium'].browser
165
+ * ```
166
+ *
167
+ * ## Methods
168
+ */
169
+ class Appium extends Webdriver {
170
+ /**
171
+ * Appium Special Methods for Mobile only
172
+ * @augments WebDriver
173
+ */
174
+
175
+ // @ts-ignore
176
+ constructor(config) {
177
+ super(config);
178
+
179
+ this.isRunning = false;
180
+ if (config.appiumV2 === true) {
181
+ this.appiumV2 = true;
182
+ }
183
+ this.axios = axios.create();
184
+
185
+ webdriverio = require('webdriverio');
186
+ if (!config.appiumV2) {
187
+ console.log('The Appium core team does not maintain Appium 1.x anymore since the 1st of January 2022. Please migrating to Appium 2.x by adding appiumV2: true to your config.');
188
+ console.log('More info: https://bit.ly/appium-v2-migration');
189
+ console.log('This Appium 1.x support will be removed in next major release.');
190
+ }
191
+ }
192
+
193
+ _validateConfig(config) {
194
+ if (!(config.app || config.platform) && !config.browser) {
195
+ throw new Error(`
196
+ Appium requires either platform and app or a browser to be set.
197
+ Check your codeceptjs config file to ensure these are set properly
198
+ {
199
+ "helpers": {
200
+ "Appium": {
201
+ "app": "/path/to/app/package"
202
+ "platform": "MOBILE_OS",
203
+ }
204
+ }
205
+ }
206
+ `);
207
+ }
208
+
209
+ // set defaults
210
+ const defaults = {
211
+ // webdriverio defaults
212
+ protocol: 'http',
213
+ hostname: '0.0.0.0', // webdriverio specs
214
+ port: 4723,
215
+ path: '/wd/hub',
216
+
217
+ // config
218
+ waitForTimeout: 1000, // ms
219
+ logLevel: 'error',
220
+ capabilities: {},
221
+ deprecationWarnings: false,
222
+ restart: true,
223
+ manualStart: false,
224
+ timeouts: {
225
+ script: 0, // ms
226
+ },
227
+ };
228
+
229
+ // override defaults with config
230
+ config = Object.assign(defaults, config);
231
+
232
+ config.baseUrl = config.url || config.baseUrl;
233
+ if (config.desiredCapabilities && Object.keys(config.desiredCapabilities).length) {
234
+ config.capabilities = this.appiumV2 === true ? this._convertAppiumV2Caps(config.desiredCapabilities) : config.desiredCapabilities;
235
+ }
236
+
237
+ if (this.appiumV2) {
238
+ config.capabilities[`${vendorPrefix.appium}:deviceName`] = config[`${vendorPrefix.appium}:device`] || config.capabilities[`${vendorPrefix.appium}:deviceName`];
239
+ config.capabilities[`${vendorPrefix.appium}:browserName`] = config[`${vendorPrefix.appium}:browser`] || config.capabilities[`${vendorPrefix.appium}:browserName`];
240
+ config.capabilities[`${vendorPrefix.appium}:app`] = config[`${vendorPrefix.appium}:app`] || config.capabilities[`${vendorPrefix.appium}:app`];
241
+ config.capabilities[`${vendorPrefix.appium}:tunnelIdentifier`] = config[`${vendorPrefix.appium}:tunnelIdentifier`] || config.capabilities[`${vendorPrefix.appium}:tunnelIdentifier`]; // Adding the code to connect to sauce labs via sauce tunnel
242
+ } else {
243
+ config.capabilities.deviceName = config.device || config.capabilities.deviceName;
244
+ config.capabilities.browserName = config.browser || config.capabilities.browserName;
245
+ config.capabilities.app = config.app || config.capabilities.app;
246
+ config.capabilities.tunnelIdentifier = config.tunnelIdentifier || config.capabilities.tunnelIdentifier; // Adding the code to connect to sauce labs via sauce tunnel
247
+ }
248
+
249
+ config.capabilities.platformName = config.platform || config.capabilities.platformName;
250
+ config.waitForTimeoutInSeconds = config.waitForTimeout / 1000; // convert to seconds
251
+
252
+ // [CodeceptJS compatible] transform host to hostname
253
+ config.hostname = config.host || config.hostname;
254
+
255
+ if (!config.app && config.capabilities.browserName) {
256
+ this.isWeb = true;
257
+ this.root = webRoot;
258
+ } else {
259
+ this.isWeb = false;
260
+ this.root = mobileRoot;
261
+ }
262
+
263
+ this.platform = null;
264
+ if (config.capabilities[`${vendorPrefix.appium}:platformName`]) {
265
+ this.platform = config.capabilities[`${vendorPrefix.appium}:platformName`].toLowerCase();
266
+ }
267
+
268
+ if (config.capabilities.platformName) {
269
+ this.platform = config.capabilities.platformName.toLowerCase();
270
+ }
271
+
272
+ return config;
273
+ }
274
+
275
+ _convertAppiumV2Caps(capabilities) {
276
+ const _convertedCaps = {};
277
+ for (const [key, value] of Object.entries(capabilities)) {
278
+ if (!key.startsWith(vendorPrefix.appium)) {
279
+ if (key !== 'platformName' && key !== 'bstack:options') {
280
+ _convertedCaps[`${vendorPrefix.appium}:${key}`] = value;
281
+ } else {
282
+ _convertedCaps[`${key}`] = value;
283
+ }
284
+ } else {
285
+ _convertedCaps[`${key}`] = value;
286
+ }
287
+ }
288
+ return _convertedCaps;
289
+ }
290
+
291
+ static _config() {
292
+ return [{
293
+ name: 'app',
294
+ message: 'Application package. Path to file or url',
295
+ default: 'http://localhost',
296
+ }, {
297
+ name: 'platform',
298
+ message: 'Mobile Platform',
299
+ type: 'list',
300
+ choices: ['iOS', supportedPlatform.android],
301
+ default: supportedPlatform.android,
302
+ }, {
303
+ name: 'device',
304
+ message: 'Device to run tests on',
305
+ default: 'emulator',
306
+ }];
307
+ }
308
+
309
+ async _startBrowser() {
310
+ if (this.appiumV2 === true) {
311
+ this.options.capabilities = this._convertAppiumV2Caps(this.options.capabilities);
312
+ this.options.desiredCapabilities = this._convertAppiumV2Caps(this.options.desiredCapabilities);
313
+ }
314
+
315
+ try {
316
+ if (this.options.multiremote) {
317
+ this.browser = await webdriverio.multiremote(this.options.multiremote);
318
+ } else {
319
+ this.browser = await webdriverio.remote(this.options);
320
+ }
321
+ } catch (err) {
322
+ if (err.toString().indexOf('ECONNREFUSED')) {
323
+ throw new ConnectionRefused(err);
324
+ }
325
+ throw err;
326
+ }
327
+ this.$$ = this.browser.$$.bind(this.browser);
328
+
329
+ this.isRunning = true;
330
+ if (this.options.timeouts && this.isWeb) {
331
+ await this.defineTimeout(this.options.timeouts);
332
+ }
333
+ if (this.options.windowSize === 'maximize' && !this.platform) {
334
+ const res = await this.browser.execute('return [screen.width, screen.height]');
335
+ return this.browser.windowHandleSize({
336
+ width: res.value[0],
337
+ height: res.value[1],
338
+ });
339
+ }
340
+ if (this.options.windowSize && this.options.windowSize.indexOf('x') > 0 && !this.platform) {
341
+ const dimensions = this.options.windowSize.split('x');
342
+ await this.browser.windowHandleSize({
343
+ width: dimensions[0],
344
+ height: dimensions[1],
345
+ });
346
+ }
347
+ }
348
+
349
+ async _after() {
350
+ if (!this.isRunning) return;
351
+ if (this.options.restart) {
352
+ this.isRunning = false;
353
+ return this.browser.deleteSession();
354
+ }
355
+ if (this.isWeb && !this.platform) {
356
+ return super._after();
357
+ }
358
+ }
359
+
360
+ async _withinBegin(context) {
361
+ if (this.isWeb) {
362
+ return super._withinBegin(context);
363
+ }
364
+ if (context === 'webview') {
365
+ return this.switchToWeb();
366
+ }
367
+ if (typeof context === 'object') {
368
+ if (context.web) return this.switchToWeb(context.web);
369
+ if (context.webview) return this.switchToWeb(context.webview);
370
+ }
371
+ return this.switchToContext(context);
372
+ }
373
+
374
+ _withinEnd() {
375
+ if (this.isWeb) {
376
+ return super._withinEnd();
377
+ }
378
+ return this.switchToNative();
379
+ }
380
+
381
+ _buildAppiumEndpoint() {
382
+ const {
383
+ protocol, port, hostname, path,
384
+ } = this.browser.options;
385
+ // Build path to Appium REST API endpoint
386
+ return `${protocol}://${hostname}:${port}${path}`;
387
+ }
388
+
389
+ /**
390
+ * Execute code only on iOS
391
+ *
392
+ * ```js
393
+ * I.runOnIOS(() => {
394
+ * I.click('//UIAApplication[1]/UIAWindow[1]/UIAButton[1]');
395
+ * I.see('Hi, IOS', '~welcome');
396
+ * });
397
+ * ```
398
+ *
399
+ * Additional filter can be applied by checking for capabilities.
400
+ * For instance, this code will be executed only on iPhone 5s:
401
+ *
402
+ *
403
+ * ```js
404
+ * I.runOnIOS({deviceName: 'iPhone 5s'},() => {
405
+ * // ...
406
+ * });
407
+ * ```
408
+ *
409
+ * Also capabilities can be checked by a function.
410
+ *
411
+ * ```js
412
+ * I.runOnAndroid((caps) => {
413
+ * // caps is current config of desiredCapabiliites
414
+ * return caps.platformVersion >= 6
415
+ * },() => {
416
+ * // ...
417
+ * });
418
+ * ```
419
+ *
420
+ * @param {*} caps
421
+ * @param {*} fn
422
+ */
423
+ async runOnIOS(caps, fn) {
424
+ if (this.platform !== 'ios') return;
425
+ recorder.session.start('iOS-only actions');
426
+ await this._runWithCaps(caps, fn);
427
+ await recorder.add('restore from iOS session', () => recorder.session.restore());
428
+ return recorder.promise();
429
+ }
430
+
431
+ /**
432
+ * Execute code only on Android
433
+ *
434
+ * ```js
435
+ * I.runOnAndroid(() => {
436
+ * I.click('io.selendroid.testapp:id/buttonTest');
437
+ * });
438
+ * ```
439
+ *
440
+ * Additional filter can be applied by checking for capabilities.
441
+ * For instance, this code will be executed only on Android 6.0:
442
+ *
443
+ *
444
+ * ```js
445
+ * I.runOnAndroid({platformVersion: '6.0'},() => {
446
+ * // ...
447
+ * });
448
+ * ```
449
+ *
450
+ * Also capabilities can be checked by a function.
451
+ * In this case, code will be executed only on Android >= 6.
452
+ *
453
+ * ```js
454
+ * I.runOnAndroid((caps) => {
455
+ * // caps is current config of desiredCapabiliites
456
+ * return caps.platformVersion >= 6
457
+ * },() => {
458
+ * // ...
459
+ * });
460
+ * ```
461
+ *
462
+ * @param {*} caps
463
+ * @param {*} fn
464
+ */
465
+ async runOnAndroid(caps, fn) {
466
+ if (this.platform !== 'android') return;
467
+ recorder.session.start('Android-only actions');
468
+ await this._runWithCaps(caps, fn);
469
+ await recorder.add('restore from Android session', () => recorder.session.restore());
470
+ return recorder.promise();
471
+ }
472
+
473
+ /**
474
+ * Execute code only in Web mode.
475
+ *
476
+ * ```js
477
+ * I.runInWeb(() => {
478
+ * I.waitForElement('#data');
479
+ * I.seeInCurrentUrl('/data');
480
+ * });
481
+ * ```
482
+ *
483
+ * @param {*} fn
484
+ */
485
+ /* eslint-disable */
486
+ async runInWeb(fn) {
487
+ if (!this.isWeb) return;
488
+ recorder.session.start('Web-only actions');
489
+
490
+ recorder.add('restore from Web session', () => recorder.session.restore(), true);
491
+ return recorder.promise();
492
+ }
493
+ /* eslint-enable */
494
+
495
+ async _runWithCaps(caps, fn) {
496
+ if (typeof caps === 'object') {
497
+ for (const key in caps) {
498
+ // skip if capabilities do not match
499
+ if (this.config.desiredCapabilities[key] !== caps[key]) {
500
+ return;
501
+ }
502
+ }
503
+ }
504
+ if (typeof caps === 'function') {
505
+ if (!fn) {
506
+ fn = caps;
507
+ } else {
508
+ // skip if capabilities are checked inside a function
509
+ const enabled = caps(this.config.desiredCapabilities);
510
+ if (!enabled) return;
511
+ }
512
+ }
513
+
514
+ fn();
515
+ }
516
+
517
+ /**
518
+ * Returns app installation status.
519
+ *
520
+ * ```js
521
+ * I.checkIfAppIsInstalled("com.example.android.apis");
522
+ * ```
523
+ *
524
+ * @param {string} bundleId String ID of bundled app
525
+ * @return {Promise<boolean>}
526
+ *
527
+ * Appium: support only Android
528
+ */
529
+ async checkIfAppIsInstalled(bundleId) {
530
+ onlyForApps.call(this, supportedPlatform.android);
531
+
532
+ return this.browser.isAppInstalled(bundleId);
533
+ }
534
+
535
+ /**
536
+ * Check if an app is installed.
537
+ *
538
+ * ```js
539
+ * I.seeAppIsInstalled("com.example.android.apis");
540
+ * ```
541
+ *
542
+ * @param {string} bundleId String ID of bundled app
543
+ * @return {Promise<void>}
544
+ *
545
+ * Appium: support only Android
546
+ */
547
+ async seeAppIsInstalled(bundleId) {
548
+ onlyForApps.call(this, supportedPlatform.android);
549
+ const res = await this.browser.isAppInstalled(bundleId);
550
+ return truth(`app ${bundleId}`, 'to be installed').assert(res);
551
+ }
552
+
553
+ /**
554
+ * Check if an app is not installed.
555
+ *
556
+ * ```js
557
+ * I.seeAppIsNotInstalled("com.example.android.apis");
558
+ * ```
559
+ *
560
+ * @param {string} bundleId String ID of bundled app
561
+ * @return {Promise<void>}
562
+ *
563
+ * Appium: support only Android
564
+ */
565
+ async seeAppIsNotInstalled(bundleId) {
566
+ onlyForApps.call(this, supportedPlatform.android);
567
+ const res = await this.browser.isAppInstalled(bundleId);
568
+ return truth(`app ${bundleId}`, 'not to be installed').negate(res);
569
+ }
570
+
571
+ /**
572
+ * Install an app on device.
573
+ *
574
+ * ```js
575
+ * I.installApp('/path/to/file.apk');
576
+ * ```
577
+ * @param {string} path path to apk file
578
+ * @return {Promise<void>}
579
+ *
580
+ * Appium: support only Android
581
+ */
582
+ async installApp(path) {
583
+ onlyForApps.call(this, supportedPlatform.android);
584
+ return this.browser.installApp(path);
585
+ }
586
+
587
+ /**
588
+ * Remove an app from the device.
589
+ *
590
+ * ```js
591
+ * I.removeApp('appName', 'com.example.android.apis');
592
+ * ```
593
+ *
594
+ * Appium: support only Android
595
+ *
596
+ * @param {string} appId
597
+ * @param {string} [bundleId] ID of bundle
598
+ */
599
+ async removeApp(appId, bundleId) {
600
+ onlyForApps.call(this, supportedPlatform.android);
601
+
602
+ return this.axios({
603
+ method: 'post',
604
+ url: `${this._buildAppiumEndpoint()}/session/${this.browser.sessionId}/appium/device/remove_app`,
605
+ data: { appId, bundleId },
606
+ });
607
+ }
608
+
609
+ /**
610
+ * Reset the currently running app for current session.
611
+ *
612
+ * ```js
613
+ * I.resetApp();
614
+ * ```
615
+ *
616
+ */
617
+ async resetApp() {
618
+ onlyForApps.call(this);
619
+ return this.axios({
620
+ method: 'post',
621
+ url: `${this._buildAppiumEndpoint()}/session/${this.browser.sessionId}/appium/app/reset`,
622
+ });
623
+ }
624
+
625
+ /**
626
+ * Check current activity on an Android device.
627
+ *
628
+ * ```js
629
+ * I.seeCurrentActivityIs(".HomeScreenActivity")
630
+ * ```
631
+ * @param {string} currentActivity
632
+ * @return {Promise<void>}
633
+ *
634
+ * Appium: support only Android
635
+ */
636
+ async seeCurrentActivityIs(currentActivity) {
637
+ onlyForApps.call(this, supportedPlatform.android);
638
+ const res = await this.browser.getCurrentActivity();
639
+ return truth('current activity', `to be ${currentActivity}`).assert(res === currentActivity);
640
+ }
641
+
642
+ /**
643
+ * Check whether the device is locked.
644
+ *
645
+ * ```js
646
+ * I.seeDeviceIsLocked();
647
+ * ```
648
+ *
649
+ * @return {Promise<void>}
650
+ *
651
+ * Appium: support only Android
652
+ */
653
+ async seeDeviceIsLocked() {
654
+ onlyForApps.call(this, supportedPlatform.android);
655
+ const res = await this.browser.isLocked();
656
+ return truth('device', 'to be locked').assert(res);
657
+ }
658
+
659
+ /**
660
+ * Check whether the device is not locked.
661
+ *
662
+ * ```js
663
+ * I.seeDeviceIsUnlocked();
664
+ * ```
665
+ *
666
+ * @return {Promise<void>}
667
+ *
668
+ * Appium: support only Android
669
+ */
670
+ async seeDeviceIsUnlocked() {
671
+ onlyForApps.call(this, supportedPlatform.android);
672
+ const res = await this.browser.isLocked();
673
+ return truth('device', 'to be locked').negate(res);
674
+ }
675
+
676
+ /**
677
+ * Check the device orientation
678
+ *
679
+ * ```js
680
+ * I.seeOrientationIs('PORTRAIT');
681
+ * I.seeOrientationIs('LANDSCAPE')
682
+ * ```
683
+ *
684
+ * @return {Promise<void>}
685
+ *
686
+ * @param {'LANDSCAPE'|'PORTRAIT'} orientation LANDSCAPE or PORTRAIT
687
+ *
688
+ * Appium: support Android and iOS
689
+ */
690
+ async seeOrientationIs(orientation) {
691
+ onlyForApps.call(this);
692
+
693
+ const res = await this.axios({
694
+ method: 'get',
695
+ url: `${this._buildAppiumEndpoint()}/session/${this.browser.sessionId}/orientation`,
696
+ });
697
+
698
+ const currentOrientation = res.data.value;
699
+ return truth('orientation', `to be ${orientation}`).assert(currentOrientation === orientation);
700
+ }
701
+
702
+ /**
703
+ * Set a device orientation. Will fail, if app will not set orientation
704
+ *
705
+ * ```js
706
+ * I.setOrientation('PORTRAIT');
707
+ * I.setOrientation('LANDSCAPE')
708
+ * ```
709
+ *
710
+ * @param {'LANDSCAPE'|'PORTRAIT'} orientation LANDSCAPE or PORTRAIT
711
+ *
712
+ * Appium: support Android and iOS
713
+ */
714
+ async setOrientation(orientation) {
715
+ onlyForApps.call(this);
716
+
717
+ return this.axios({
718
+ method: 'post',
719
+ url: `${this._buildAppiumEndpoint()}/session/${this.browser.sessionId}/orientation`,
720
+ data: { orientation },
721
+ });
722
+ }
723
+
724
+ /**
725
+ * Get list of all available contexts
726
+ *
727
+ * ```
728
+ * let contexts = await I.grabAllContexts();
729
+ * ```
730
+ *
731
+ * @return {Promise<string[]>}
732
+ *
733
+ * Appium: support Android and iOS
734
+ */
735
+ async grabAllContexts() {
736
+ onlyForApps.call(this);
737
+ return this.browser.getContexts();
738
+ }
739
+
740
+ /**
741
+ * Retrieve current context
742
+ *
743
+ * ```js
744
+ * let context = await I.grabContext();
745
+ * ```
746
+ *
747
+ * @return {Promise<string|null>}
748
+ *
749
+ * Appium: support Android and iOS
750
+ */
751
+ async grabContext() {
752
+ onlyForApps.call(this);
753
+ return this.browser.getContext();
754
+ }
755
+
756
+ /**
757
+ * Get current device activity.
758
+ *
759
+ * ```js
760
+ * let activity = await I.grabCurrentActivity();
761
+ * ```
762
+ *
763
+ * @return {Promise<string>}
764
+ *
765
+ * Appium: support only Android
766
+ */
767
+ async grabCurrentActivity() {
768
+ onlyForApps.call(this, supportedPlatform.android);
769
+ return this.browser.getCurrentActivity();
770
+ }
771
+
772
+ /**
773
+ * Get information about the current network connection (Data/WIFI/Airplane).
774
+ * The actual server value will be a number. However WebdriverIO additional
775
+ * properties to the response object to allow easier assertions.
776
+ *
777
+ * ```js
778
+ * let con = await I.grabNetworkConnection();
779
+ * ```
780
+ *
781
+ * @return {Promise<{}>}
782
+ *
783
+ * Appium: support only Android
784
+ */
785
+ async grabNetworkConnection() {
786
+ onlyForApps.call(this, supportedPlatform.android);
787
+ const res = await this.browser.getNetworkConnection();
788
+ return {
789
+ value: res,
790
+ inAirplaneMode: res.inAirplaneMode,
791
+ hasWifi: res.hasWifi,
792
+ hasData: res.hasData,
793
+ };
794
+ }
795
+
796
+ /**
797
+ * Get current orientation.
798
+ *
799
+ * ```js
800
+ * let orientation = await I.grabOrientation();
801
+ * ```
802
+ *
803
+ * @return {Promise<string>}
804
+ *
805
+ * Appium: support Android and iOS
806
+ */
807
+ async grabOrientation() {
808
+ onlyForApps.call(this);
809
+ const res = await this.browser.orientation();
810
+ this.debugSection('Orientation', res);
811
+ return res;
812
+ }
813
+
814
+ /**
815
+ * Get all the currently specified settings.
816
+ *
817
+ * ```js
818
+ * let settings = await I.grabSettings();
819
+ * ```
820
+ *
821
+ * @return {Promise<string>}
822
+ *
823
+ * Appium: support Android and iOS
824
+ */
825
+ async grabSettings() {
826
+ onlyForApps.call(this);
827
+ const res = await this.browser.getSettings();
828
+ this.debugSection('Settings', JSON.stringify(res));
829
+ return res;
830
+ }
831
+
832
+ /**
833
+ * Switch to the specified context.
834
+ *
835
+ * @param {*} context the context to switch to
836
+ */
837
+ async switchToContext(context) {
838
+ return this.browser.switchContext(context);
839
+ }
840
+
841
+ /**
842
+ * Switches to web context.
843
+ * If no context is provided switches to the first detected web context
844
+ *
845
+ * ```js
846
+ * // switch to first web context
847
+ * I.switchToWeb();
848
+ *
849
+ * // or set the context explicitly
850
+ * I.switchToWeb('WEBVIEW_io.selendroid.testapp');
851
+ * ```
852
+ *
853
+ * @return {Promise<void>}
854
+ *
855
+ * @param {string} [context]
856
+ */
857
+ async switchToWeb(context) {
858
+ this.isWeb = true;
859
+ this.defaultContext = 'body';
860
+
861
+ if (context) return this.switchToContext(context);
862
+ const contexts = await this.grabAllContexts();
863
+ this.debugSection('Contexts', contexts.toString());
864
+ for (const idx in contexts) {
865
+ if (contexts[idx].match(/^WEBVIEW/)) return this.switchToContext(contexts[idx]);
866
+ }
867
+
868
+ throw new Error('No WEBVIEW could be guessed, please specify one in params');
869
+ }
870
+
871
+ /**
872
+ * Switches to native context.
873
+ * By default switches to NATIVE_APP context unless other specified.
874
+ *
875
+ * ```js
876
+ * I.switchToNative();
877
+ *
878
+ * // or set context explicitly
879
+ * I.switchToNative('SOME_OTHER_CONTEXT');
880
+ * ```
881
+ * @param {*} [context]
882
+ * @return {Promise<void>}
883
+ */
884
+ async switchToNative(context = null) {
885
+ this.isWeb = false;
886
+ this.defaultContext = '//*';
887
+
888
+ if (context) return this.switchToContext(context);
889
+ return this.switchToContext('NATIVE_APP');
890
+ }
891
+
892
+ /**
893
+ * Start an arbitrary Android activity during a session.
894
+ *
895
+ * ```js
896
+ * I.startActivity('io.selendroid.testapp', '.RegisterUserActivity');
897
+ * ```
898
+ *
899
+ * Appium: support only Android
900
+ *
901
+ * @param {string} appPackage
902
+ * @param {string} appActivity
903
+ * @return {Promise<void>}
904
+ */
905
+ async startActivity(appPackage, appActivity) {
906
+ onlyForApps.call(this, supportedPlatform.android);
907
+ return this.browser.startActivity(appPackage, appActivity);
908
+ }
909
+
910
+ /**
911
+ * Set network connection mode.
912
+ *
913
+ * * airplane mode
914
+ * * wifi mode
915
+ * * data data
916
+ *
917
+ * ```js
918
+ * I.setNetworkConnection(0) // airplane mode off, wifi off, data off
919
+ * I.setNetworkConnection(1) // airplane mode on, wifi off, data off
920
+ * I.setNetworkConnection(2) // airplane mode off, wifi on, data off
921
+ * I.setNetworkConnection(4) // airplane mode off, wifi off, data on
922
+ * I.setNetworkConnection(6) // airplane mode off, wifi on, data on
923
+ * ```
924
+ * See corresponding [webdriverio reference](https://webdriver.io/docs/api/chromium/#setnetworkconnection).
925
+ *
926
+ * Appium: support only Android
927
+ *
928
+ * @param {number} value The network connection mode bitmask
929
+ * @return {Promise<number>}
930
+ */
931
+ async setNetworkConnection(value) {
932
+ onlyForApps.call(this, supportedPlatform.android);
933
+ return this.browser.setNetworkConnection(value);
934
+ }
935
+
936
+ /**
937
+ * Update the current setting on the device
938
+ *
939
+ * ```js
940
+ * I.setSettings({cyberdelia: 'open'});
941
+ * ```
942
+ *
943
+ * @param {object} settings object
944
+ *
945
+ * Appium: support Android and iOS
946
+ */
947
+ async setSettings(settings) {
948
+ onlyForApps.call(this);
949
+ return this.browser.settings(settings);
950
+ }
951
+
952
+ /**
953
+ * Hide the keyboard.
954
+ *
955
+ * ```js
956
+ * // taps outside to hide keyboard per default
957
+ * I.hideDeviceKeyboard();
958
+ * I.hideDeviceKeyboard('tapOutside');
959
+ *
960
+ * // or by pressing key
961
+ * I.hideDeviceKeyboard('pressKey', 'Done');
962
+ * ```
963
+ *
964
+ * Appium: support Android and iOS
965
+ *
966
+ * @param {'tapOutside' | 'pressKey'} [strategy] Desired strategy to close keyboard (‘tapOutside’ or ‘pressKey’)
967
+ * @param {string} [key] Optional key
968
+ */
969
+ async hideDeviceKeyboard(strategy, key) {
970
+ onlyForApps.call(this);
971
+ strategy = strategy || 'tapOutside';
972
+ return this.browser.hideKeyboard(strategy, key);
973
+ }
974
+
975
+ /**
976
+ * Send a key event to the device.
977
+ * List of keys: https://developer.android.com/reference/android/view/KeyEvent.html
978
+ *
979
+ * ```js
980
+ * I.sendDeviceKeyEvent(3);
981
+ * ```
982
+ *
983
+ * @param {number} keyValue Device specific key value
984
+ * @return {Promise<void>}
985
+ *
986
+ * Appium: support only Android
987
+ */
988
+ async sendDeviceKeyEvent(keyValue) {
989
+ onlyForApps.call(this, supportedPlatform.android);
990
+ return this.browser.pressKeyCode(keyValue);
991
+ }
992
+
993
+ /**
994
+ * Open the notifications panel on the device.
995
+ *
996
+ * ```js
997
+ * I.openNotifications();
998
+ * ```
999
+ *
1000
+ * @return {Promise<void>}
1001
+ *
1002
+ * Appium: support only Android
1003
+ */
1004
+ async openNotifications() {
1005
+ onlyForApps.call(this, supportedPlatform.android);
1006
+ return this.browser.openNotifications();
1007
+ }
1008
+
1009
+ /**
1010
+ * The Touch Action API provides the basis of all gestures that can be
1011
+ * automated in Appium. At its core is the ability to chain together ad hoc
1012
+ * individual actions, which will then be applied to an element in the
1013
+ * application on the device.
1014
+ * [See complete documentation](http://webdriver.io/api/mobile/touchAction.html)
1015
+ *
1016
+ * ```js
1017
+ * I.makeTouchAction("~buttonStartWebviewCD", 'tap');
1018
+ * ```
1019
+ *
1020
+ * @return {Promise<void>}
1021
+ *
1022
+ * Appium: support Android and iOS
1023
+ */
1024
+ async makeTouchAction(locator, action) {
1025
+ onlyForApps.call(this);
1026
+ const element = await this.browser.$(parseLocator.call(this, locator));
1027
+
1028
+ return this.browser.touchAction({
1029
+ action,
1030
+ element,
1031
+ });
1032
+ }
1033
+
1034
+ /**
1035
+ * Taps on element.
1036
+ *
1037
+ * ```js
1038
+ * I.tap("~buttonStartWebviewCD");
1039
+ * ```
1040
+ *
1041
+ * Shortcut for `makeTouchAction`
1042
+ *
1043
+ * @return {Promise<void>}
1044
+ *
1045
+ * @param {*} locator
1046
+ */
1047
+ async tap(locator) {
1048
+ return this.makeTouchAction(locator, 'tap');
1049
+ }
1050
+
1051
+ /**
1052
+ * Perform a swipe on the screen or an element.
1053
+ *
1054
+ * ```js
1055
+ * let locator = "#io.selendroid.testapp:id/LinearLayout1";
1056
+ * I.swipe(locator, 800, 1200, 1000);
1057
+ * ```
1058
+ *
1059
+ * [See complete reference](http://webdriver.io/api/mobile/swipe.html)
1060
+ *
1061
+ * @param {CodeceptJS.LocatorOrString} locator
1062
+ * @param {number} xoffset
1063
+ * @param {number} yoffset
1064
+ * @param {number} [speed=1000] (optional), 1000 by default
1065
+ * @return {Promise<void>}
1066
+ *
1067
+ * Appium: support Android and iOS
1068
+ */
1069
+ /* eslint-disable */
1070
+ async swipe(locator, xoffset, yoffset, speed = 1000) {
1071
+ onlyForApps.call(this);
1072
+ const res = await this.browser.$(parseLocator.call(this, locator));
1073
+ // if (!res.length) throw new ElementNotFound(locator, 'was not found in UI');
1074
+ return this.performSwipe(await res.getLocation(), { x: (await res.getLocation()).x + xoffset, y: (await res.getLocation()).y + yoffset });
1075
+ }
1076
+ /* eslint-enable */
1077
+
1078
+ /**
1079
+ * Perform a swipe on the screen.
1080
+ *
1081
+ * ```js
1082
+ * I.performSwipe({ x: 300, y: 100 }, { x: 200, y: 100 });
1083
+ * ```
1084
+ *
1085
+ * @param {object} from
1086
+ * @param {object} to
1087
+ *
1088
+ * Appium: support Android and iOS
1089
+ */
1090
+ async performSwipe(from, to) {
1091
+ await this.browser.touchPerform([{
1092
+ action: 'press',
1093
+ options: from,
1094
+ }, {
1095
+ action: 'wait',
1096
+ options: { ms: 1000 },
1097
+ }, {
1098
+ action: 'moveTo',
1099
+ options: to,
1100
+ }, {
1101
+ action: 'release',
1102
+ }]);
1103
+ await this.browser.pause(1000);
1104
+ }
1105
+
1106
+ /**
1107
+ * Perform a swipe down on an element.
1108
+ *
1109
+ * ```js
1110
+ * let locator = "#io.selendroid.testapp:id/LinearLayout1";
1111
+ * I.swipeDown(locator); // simple swipe
1112
+ * I.swipeDown(locator, 500); // set speed
1113
+ * I.swipeDown(locator, 1200, 1000); // set offset and speed
1114
+ * ```
1115
+ *
1116
+ * @param {CodeceptJS.LocatorOrString} locator
1117
+ * @param {number} [yoffset] (optional)
1118
+ * @param {number} [speed=1000] (optional), 1000 by default
1119
+ * @return {Promise<void>}
1120
+ *
1121
+ * Appium: support Android and iOS
1122
+ */
1123
+ async swipeDown(locator, yoffset = 1000, speed) {
1124
+ onlyForApps.call(this);
1125
+
1126
+ if (!speed) {
1127
+ speed = yoffset;
1128
+ yoffset = 100;
1129
+ }
1130
+
1131
+ return this.swipe(locator, 0, yoffset, speed);
1132
+ }
1133
+
1134
+ /**
1135
+ *
1136
+ * Perform a swipe left on an element.
1137
+ *
1138
+ * ```js
1139
+ * let locator = "#io.selendroid.testapp:id/LinearLayout1";
1140
+ * I.swipeLeft(locator); // simple swipe
1141
+ * I.swipeLeft(locator, 500); // set speed
1142
+ * I.swipeLeft(locator, 1200, 1000); // set offset and speed
1143
+ * ```
1144
+ *
1145
+ * @param {CodeceptJS.LocatorOrString} locator
1146
+ * @param {number} [xoffset] (optional)
1147
+ * @param {number} [speed=1000] (optional), 1000 by default
1148
+ * @return {Promise<void>}
1149
+ *
1150
+ * Appium: support Android and iOS
1151
+ */
1152
+ async swipeLeft(locator, xoffset = 1000, speed) {
1153
+ onlyForApps.call(this);
1154
+ if (!speed) {
1155
+ speed = xoffset;
1156
+ xoffset = 100;
1157
+ }
1158
+
1159
+ return this.swipe(parseLocator.call(this, locator), -xoffset, 0, speed);
1160
+ }
1161
+
1162
+ /**
1163
+ * Perform a swipe right on an element.
1164
+ *
1165
+ * ```js
1166
+ * let locator = "#io.selendroid.testapp:id/LinearLayout1";
1167
+ * I.swipeRight(locator); // simple swipe
1168
+ * I.swipeRight(locator, 500); // set speed
1169
+ * I.swipeRight(locator, 1200, 1000); // set offset and speed
1170
+ * ```
1171
+ *
1172
+ * @param {CodeceptJS.LocatorOrString} locator
1173
+ * @param {number} [xoffset] (optional)
1174
+ * @param {number} [speed=1000] (optional), 1000 by default
1175
+ * @return {Promise<void>}
1176
+ *
1177
+ * Appium: support Android and iOS
1178
+ */
1179
+ async swipeRight(locator, xoffset = 1000, speed) {
1180
+ onlyForApps.call(this);
1181
+ if (!speed) {
1182
+ speed = xoffset;
1183
+ xoffset = 100;
1184
+ }
1185
+
1186
+ return this.swipe(parseLocator.call(this, locator), xoffset, 0, speed);
1187
+ }
1188
+
1189
+ /**
1190
+ * Perform a swipe up on an element.
1191
+ *
1192
+ * ```js
1193
+ * let locator = "#io.selendroid.testapp:id/LinearLayout1";
1194
+ * I.swipeUp(locator); // simple swipe
1195
+ * I.swipeUp(locator, 500); // set speed
1196
+ * I.swipeUp(locator, 1200, 1000); // set offset and speed
1197
+ * ```
1198
+ *
1199
+ * @param {CodeceptJS.LocatorOrString} locator
1200
+ * @param {number} [yoffset] (optional)
1201
+ * @param {number} [speed=1000] (optional), 1000 by default
1202
+ * @return {Promise<void>}
1203
+ *
1204
+ * Appium: support Android and iOS
1205
+ */
1206
+ async swipeUp(locator, yoffset = 1000, speed) {
1207
+ onlyForApps.call(this);
1208
+
1209
+ if (!speed) {
1210
+ speed = yoffset;
1211
+ yoffset = 100;
1212
+ }
1213
+
1214
+ return this.swipe(parseLocator.call(this, locator), 0, -yoffset, speed);
1215
+ }
1216
+
1217
+ /**
1218
+ * Perform a swipe in selected direction on an element to searchable element.
1219
+ *
1220
+ * ```js
1221
+ * I.swipeTo(
1222
+ * "android.widget.CheckBox", // searchable element
1223
+ * "//android.widget.ScrollView/android.widget.LinearLayout", // scroll element
1224
+ * "up", // direction
1225
+ * 30,
1226
+ * 100,
1227
+ * 500);
1228
+ * ```
1229
+ *
1230
+ * @param {string} searchableLocator
1231
+ * @param {string} scrollLocator
1232
+ * @param {string} direction
1233
+ * @param {number} timeout
1234
+ * @param {number} offset
1235
+ * @param {number} speed
1236
+ * @return {Promise<void>}
1237
+ *
1238
+ * Appium: support Android and iOS
1239
+ */
1240
+ async swipeTo(searchableLocator, scrollLocator, direction, timeout, offset, speed) {
1241
+ onlyForApps.call(this);
1242
+ direction = direction || 'down';
1243
+ switch (direction) {
1244
+ case 'down':
1245
+ direction = 'swipeDown';
1246
+ break;
1247
+ case 'up':
1248
+ direction = 'swipeUp';
1249
+ break;
1250
+ case 'left':
1251
+ direction = 'swipeLeft';
1252
+ break;
1253
+ case 'right':
1254
+ direction = 'swipeRight';
1255
+ break;
1256
+ }
1257
+ timeout = timeout || this.options.waitForTimeoutInSeconds;
1258
+
1259
+ const errorMsg = `element ("${searchableLocator}") still not visible after ${timeout}seconds`;
1260
+ const browser = this.browser;
1261
+ let err = false;
1262
+ let currentSource;
1263
+ return browser.waitUntil(() => {
1264
+ if (err) {
1265
+ return new Error(`Scroll to the end and element ${searchableLocator} was not found`);
1266
+ }
1267
+ return browser.$$(parseLocator.call(this, searchableLocator))
1268
+ .then(els => els.length && els[0].isDisplayed())
1269
+ .then((res) => {
1270
+ if (res) {
1271
+ return true;
1272
+ }
1273
+ return this[direction](scrollLocator, offset, speed).getSource().then((source) => {
1274
+ if (source === currentSource) {
1275
+ err = true;
1276
+ } else {
1277
+ currentSource = source;
1278
+ return false;
1279
+ }
1280
+ });
1281
+ });
1282
+ }, timeout * 1000, errorMsg)
1283
+ .catch((e) => {
1284
+ if (e.message.indexOf('timeout') && e.type !== 'NoSuchElement') {
1285
+ throw new AssertionFailedError({ customMessage: `Scroll to the end and element ${searchableLocator} was not found` }, '');
1286
+ } else {
1287
+ throw e;
1288
+ }
1289
+ });
1290
+ }
1291
+
1292
+ /**
1293
+ * Performs a specific touch action.
1294
+ * The action object need to contain the action name, x/y coordinates
1295
+ *
1296
+ * ```js
1297
+ * I.touchPerform([{
1298
+ * action: 'press',
1299
+ * options: {
1300
+ * x: 100,
1301
+ * y: 200
1302
+ * }
1303
+ * }, {action: 'release'}])
1304
+ *
1305
+ * I.touchPerform([{
1306
+ * action: 'tap',
1307
+ * options: {
1308
+ * element: '1', // json web element was queried before
1309
+ * x: 10, // x offset
1310
+ * y: 20, // y offset
1311
+ * count: 1 // number of touches
1312
+ * }
1313
+ * }]);
1314
+ * ```
1315
+ *
1316
+ * Appium: support Android and iOS
1317
+ *
1318
+ * @param {Array} actions Array of touch actions
1319
+ */
1320
+ async touchPerform(actions) {
1321
+ onlyForApps.call(this);
1322
+ return this.browser.touchPerform(actions);
1323
+ }
1324
+
1325
+ /**
1326
+ * Pulls a file from the device.
1327
+ *
1328
+ * ```js
1329
+ * I.pullFile('/storage/emulated/0/DCIM/logo.png', 'my/path');
1330
+ * // save file to output dir
1331
+ * I.pullFile('/storage/emulated/0/DCIM/logo.png', output_dir);
1332
+ * ```
1333
+ *
1334
+ * @param {string} path
1335
+ * @param {string} dest
1336
+ * @return {Promise<string>}
1337
+ *
1338
+ * Appium: support Android and iOS
1339
+ */
1340
+ async pullFile(path, dest) {
1341
+ onlyForApps.call(this);
1342
+ return this.browser.pullFile(path).then(res => fs.writeFile(dest, Buffer.from(res, 'base64'), (err) => {
1343
+ if (err) {
1344
+ return false;
1345
+ }
1346
+ return true;
1347
+ }));
1348
+ }
1349
+
1350
+ /**
1351
+ * Perform a shake action on the device.
1352
+ *
1353
+ * ```js
1354
+ * I.shakeDevice();
1355
+ * ```
1356
+ *
1357
+ * @return {Promise<void>}
1358
+ *
1359
+ * Appium: support only iOS
1360
+ */
1361
+ async shakeDevice() {
1362
+ onlyForApps.call(this, 'iOS');
1363
+ return this.browser.shake();
1364
+ }
1365
+
1366
+ /**
1367
+ * Perform a rotation gesture centered on the specified element.
1368
+ *
1369
+ * ```js
1370
+ * I.rotate(120, 120)
1371
+ * ```
1372
+ *
1373
+ * See corresponding [webdriverio reference](http://webdriver.io/api/mobile/rotate.html).
1374
+ *
1375
+ * @return {Promise<void>}
1376
+ *
1377
+ * Appium: support only iOS
1378
+ */
1379
+ async rotate(x, y, duration, radius, rotation, touchCount) {
1380
+ onlyForApps.call(this, 'iOS');
1381
+ return this.browser.rotate(x, y, duration, radius, rotation, touchCount);
1382
+ }
1383
+
1384
+ /**
1385
+ * Set immediate value in app.
1386
+ *
1387
+ * See corresponding [webdriverio reference](http://webdriver.io/api/mobile/setImmediateValue.html).
1388
+ *
1389
+ * @return {Promise<void>}
1390
+ *
1391
+ * Appium: support only iOS
1392
+ */
1393
+ async setImmediateValue(id, value) {
1394
+ onlyForApps.call(this, 'iOS');
1395
+ return this.browser.setImmediateValue(id, value);
1396
+ }
1397
+
1398
+ /**
1399
+ * Simulate Touch ID with either valid (match == true) or invalid (match == false) fingerprint.
1400
+ *
1401
+ * ```js
1402
+ * I.touchId(); // simulates valid fingerprint
1403
+ * I.touchId(true); // simulates valid fingerprint
1404
+ * I.touchId(false); // simulates invalid fingerprint
1405
+ * ```
1406
+ *
1407
+ * @return {Promise<void>}
1408
+ *
1409
+ * Appium: support only iOS
1410
+ * TODO: not tested
1411
+ */
1412
+ async simulateTouchId(match) {
1413
+ onlyForApps.call(this, 'iOS');
1414
+ match = match || true;
1415
+ return this.browser.touchId(match);
1416
+ }
1417
+
1418
+ /**
1419
+ * Close the given application.
1420
+ *
1421
+ * ```js
1422
+ * I.closeApp();
1423
+ * ```
1424
+ *
1425
+ * @return {Promise<void>}
1426
+ *
1427
+ * Appium: support both Android and iOS
1428
+ */
1429
+ async closeApp() {
1430
+ onlyForApps.call(this);
1431
+ return this.browser.closeApp();
1432
+ }
1433
+
1434
+ /**
1435
+ * Appends text to a input field or textarea.
1436
+ * Field is located by name, label, CSS or XPath
1437
+ *
1438
+ * ```js
1439
+ * I.appendField('#myTextField', 'appended');
1440
+ * // typing secret
1441
+ * I.appendField('password', secret('123456'));
1442
+ * ```
1443
+ * @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator
1444
+ * @param {string} value text value to append.
1445
+ * @returns {void} automatically synchronized promise through #recorder
1446
+ *
1447
+ *
1448
+ */
1449
+ async appendField(field, value) {
1450
+ if (this.isWeb) return super.appendField(field, value);
1451
+ return super.appendField(parseLocator.call(this, field), value);
1452
+ }
1453
+
1454
+ /**
1455
+ * Selects a checkbox or radio button.
1456
+ * Element is located by label or name or CSS or XPath.
1457
+ *
1458
+ * The second parameter is a context (CSS or XPath locator) to narrow the search.
1459
+ *
1460
+ * ```js
1461
+ * I.checkOption('#agree');
1462
+ * I.checkOption('I Agree to Terms and Conditions');
1463
+ * I.checkOption('agree', '//form');
1464
+ * ```
1465
+ * @param {CodeceptJS.LocatorOrString} field checkbox located by label | name | CSS | XPath | strict locator.
1466
+ * @param {?CodeceptJS.LocatorOrString} [context=null] (optional, `null` by default) element located by CSS | XPath | strict locator.
1467
+ * @returns {void} automatically synchronized promise through #recorder
1468
+ *
1469
+ *
1470
+ */
1471
+ async checkOption(field) {
1472
+ if (this.isWeb) return super.checkOption(field);
1473
+ return super.checkOption(parseLocator.call(this, field));
1474
+ }
1475
+
1476
+ /**
1477
+ * Perform a click on a link or a button, given by a locator.
1478
+ * If a fuzzy locator is given, the page will be searched for a button, link, or image matching the locator string.
1479
+ * For buttons, the "value" attribute, "name" attribute, and inner text are searched. For links, the link text is searched.
1480
+ * For images, the "alt" attribute and inner text of any parent links are searched.
1481
+ *
1482
+ * The second parameter is a context (CSS or XPath locator) to narrow the search.
1483
+ *
1484
+ * ```js
1485
+ * // simple link
1486
+ * I.click('Logout');
1487
+ * // button of form
1488
+ * I.click('Submit');
1489
+ * // CSS button
1490
+ * I.click('#form input[type=submit]');
1491
+ * // XPath
1492
+ * I.click('//form/*[@type=submit]');
1493
+ * // link in context
1494
+ * I.click('Logout', '#nav');
1495
+ * // using strict locator
1496
+ * I.click({css: 'nav a.login'});
1497
+ * ```
1498
+ *
1499
+ * @param {CodeceptJS.LocatorOrString} locator clickable link or button located by text, or any element located by CSS|XPath|strict locator.
1500
+ * @param {?CodeceptJS.LocatorOrString | null} [context=null] (optional, `null` by default) element to search in CSS|XPath|Strict locator.
1501
+ * @returns {void} automatically synchronized promise through #recorder
1502
+ *
1503
+ *
1504
+ */
1505
+ async click(locator, context) {
1506
+ if (this.isWeb) return super.click(locator, context);
1507
+ return super.click(parseLocator.call(this, locator), parseLocator.call(this, context));
1508
+ }
1509
+
1510
+ /**
1511
+ * Verifies that the specified checkbox is not checked.
1512
+ *
1513
+ * ```js
1514
+ * I.dontSeeCheckboxIsChecked('#agree'); // located by ID
1515
+ * I.dontSeeCheckboxIsChecked('I agree to terms'); // located by label
1516
+ * I.dontSeeCheckboxIsChecked('agree'); // located by name
1517
+ * ```
1518
+ *
1519
+ * @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator.
1520
+ * @returns {void} automatically synchronized promise through #recorder
1521
+ *
1522
+ *
1523
+ */
1524
+ async dontSeeCheckboxIsChecked(field) {
1525
+ if (this.isWeb) return super.dontSeeCheckboxIsChecked(field);
1526
+ return super.dontSeeCheckboxIsChecked(parseLocator.call(this, field));
1527
+ }
1528
+
1529
+ /**
1530
+ * Opposite to `seeElement`. Checks that element is not visible (or in DOM)
1531
+ *
1532
+ * ```js
1533
+ * I.dontSeeElement('.modal'); // modal is not shown
1534
+ * ```
1535
+ *
1536
+ * @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|Strict locator.
1537
+ * @returns {void} automatically synchronized promise through #recorder
1538
+ *
1539
+ */
1540
+ async dontSeeElement(locator) {
1541
+ if (this.isWeb) return super.dontSeeElement(locator);
1542
+ return super.dontSeeElement(parseLocator.call(this, locator));
1543
+ }
1544
+
1545
+ /**
1546
+ * Checks that value of input field or textarea doesn't equal to given value
1547
+ * Opposite to `seeInField`.
1548
+ *
1549
+ * ```js
1550
+ * I.dontSeeInField('email', 'user@user.com'); // field by name
1551
+ * I.dontSeeInField({ css: 'form input.email' }, 'user@user.com'); // field by CSS
1552
+ * ```
1553
+ *
1554
+ * @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator.
1555
+ * @param {CodeceptJS.StringOrSecret} value value to check.
1556
+ * @returns {void} automatically synchronized promise through #recorder
1557
+ *
1558
+ *
1559
+ */
1560
+ async dontSeeInField(field, value) {
1561
+ const _value = (typeof value === 'boolean') ? value : value.toString();
1562
+ if (this.isWeb) return super.dontSeeInField(field, _value);
1563
+ return super.dontSeeInField(parseLocator.call(this, field), _value);
1564
+ }
1565
+
1566
+ /**
1567
+ * Opposite to `see`. Checks that a text is not present on a page.
1568
+ * Use context parameter to narrow down the search.
1569
+ *
1570
+ * ```js
1571
+ * I.dontSee('Login'); // assume we are already logged in.
1572
+ * I.dontSee('Login', '.nav'); // no login inside .nav element
1573
+ * ```
1574
+ *
1575
+ * @param {string} text which is not present.
1576
+ * @param {CodeceptJS.LocatorOrString} [context] (optional) element located by CSS|XPath|strict locator in which to perfrom search.
1577
+ * @returns {void} automatically synchronized promise through #recorder
1578
+ *
1579
+ */
1580
+ async dontSee(text, context = null) {
1581
+ if (this.isWeb) return super.dontSee(text, context);
1582
+ return super.dontSee(text, parseLocator.call(this, context));
1583
+ }
1584
+
1585
+ /**
1586
+ * Fills a text field or textarea, after clearing its value, with the given string.
1587
+ * Field is located by name, label, CSS, or XPath.
1588
+ *
1589
+ * ```js
1590
+ * // by label
1591
+ * I.fillField('Email', 'hello@world.com');
1592
+ * // by name
1593
+ * I.fillField('password', secret('123456'));
1594
+ * // by CSS
1595
+ * I.fillField('form#login input[name=username]', 'John');
1596
+ * // or by strict locator
1597
+ * I.fillField({css: 'form#login input[name=username]'}, 'John');
1598
+ * ```
1599
+ * @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator.
1600
+ * @param {CodeceptJS.StringOrSecret} value text value to fill.
1601
+ * @returns {void} automatically synchronized promise through #recorder
1602
+ *
1603
+ *
1604
+ */
1605
+ async fillField(field, value) {
1606
+ value = value.toString();
1607
+ if (this.isWeb) return super.fillField(field, value);
1608
+ return super.fillField(parseLocator.call(this, field), value);
1609
+ }
1610
+
1611
+ /**
1612
+ * Retrieves all texts from an element located by CSS or XPath and returns it to test.
1613
+ * Resumes test execution, so **should be used inside async with `await`** operator.
1614
+ *
1615
+ * ```js
1616
+ * let pins = await I.grabTextFromAll('#pin li');
1617
+ * ```
1618
+ *
1619
+ * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1620
+ * @returns {Promise<string[]>} attribute value
1621
+ *
1622
+ *
1623
+ */
1624
+ async grabTextFromAll(locator) {
1625
+ if (this.isWeb) return super.grabTextFromAll(locator);
1626
+ return super.grabTextFromAll(parseLocator.call(this, locator));
1627
+ }
1628
+
1629
+ /**
1630
+ * Retrieves a text from an element located by CSS or XPath and returns it to test.
1631
+ * Resumes test execution, so **should be used inside async with `await`** operator.
1632
+ *
1633
+ * ```js
1634
+ * let pin = await I.grabTextFrom('#pin');
1635
+ * ```
1636
+ * If multiple elements found returns first element.
1637
+ *
1638
+ * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1639
+ * @returns {Promise<string>} attribute value
1640
+ *
1641
+ *
1642
+ */
1643
+ async grabTextFrom(locator) {
1644
+ if (this.isWeb) return super.grabTextFrom(locator);
1645
+ return super.grabTextFrom(parseLocator.call(this, locator));
1646
+ }
1647
+
1648
+ /**
1649
+ * Grab number of visible elements by locator.
1650
+ * Resumes test execution, so **should be used inside async function with `await`** operator.
1651
+ *
1652
+ * ```js
1653
+ * let numOfElements = await I.grabNumberOfVisibleElements('p');
1654
+ * ```
1655
+ *
1656
+ * @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|strict locator.
1657
+ * @returns {Promise<number>} number of visible elements
1658
+ */
1659
+ async grabNumberOfVisibleElements(locator) {
1660
+ if (this.isWeb) return super.grabNumberOfVisibleElements(locator);
1661
+ return super.grabNumberOfVisibleElements(parseLocator.call(this, locator));
1662
+ }
1663
+
1664
+ /**
1665
+ * Can be used for apps only with several values ("contentDescription", "text", "className", "resourceId")
1666
+ *
1667
+ * Retrieves an attribute from an element located by CSS or XPath and returns it to test.
1668
+ * Resumes test execution, so **should be used inside async with `await`** operator.
1669
+ * If more than one element is found - attribute of first element is returned.
1670
+ *
1671
+ * ```js
1672
+ * let hint = await I.grabAttributeFrom('#tooltip', 'title');
1673
+ * ```
1674
+ * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1675
+ * @param {string} attr attribute name.
1676
+ * @returns {Promise<string>} attribute value
1677
+ *
1678
+ */
1679
+ async grabAttributeFrom(locator, attr) {
1680
+ if (this.isWeb) return super.grabAttributeFrom(locator, attr);
1681
+ return super.grabAttributeFrom(parseLocator.call(this, locator), attr);
1682
+ }
1683
+
1684
+ /**
1685
+ * Can be used for apps only with several values ("contentDescription", "text", "className", "resourceId")
1686
+ * Retrieves an array of attributes from elements located by CSS or XPath and returns it to test.
1687
+ * Resumes test execution, so **should be used inside async with `await`** operator.
1688
+ *
1689
+ * ```js
1690
+ * let hints = await I.grabAttributeFromAll('.tooltip', 'title');
1691
+ * ```
1692
+ * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1693
+ * @param {string} attr attribute name.
1694
+ * @returns {Promise<string[]>} attribute value
1695
+ *
1696
+ */
1697
+ async grabAttributeFromAll(locator, attr) {
1698
+ if (this.isWeb) return super.grabAttributeFromAll(locator, attr);
1699
+ return super.grabAttributeFromAll(parseLocator.call(this, locator), attr);
1700
+ }
1701
+
1702
+ /**
1703
+ * Retrieves an array of value from a form located by CSS or XPath and returns it to test.
1704
+ * Resumes test execution, so **should be used inside async function with `await`** operator.
1705
+ *
1706
+ * ```js
1707
+ * let inputs = await I.grabValueFromAll('//form/input');
1708
+ * ```
1709
+ * @param {CodeceptJS.LocatorOrString} locator field located by label|name|CSS|XPath|strict locator.
1710
+ * @returns {Promise<string[]>} attribute value
1711
+ *
1712
+ *
1713
+ */
1714
+ async grabValueFromAll(locator) {
1715
+ if (this.isWeb) return super.grabValueFromAll(locator);
1716
+ return super.grabValueFromAll(parseLocator.call(this, locator));
1717
+ }
1718
+
1719
+ /**
1720
+ * Retrieves a value from a form element located by CSS or XPath and returns it to test.
1721
+ * Resumes test execution, so **should be used inside async function with `await`** operator.
1722
+ * If more than one element is found - value of first element is returned.
1723
+ *
1724
+ * ```js
1725
+ * let email = await I.grabValueFrom('input[name=email]');
1726
+ * ```
1727
+ * @param {CodeceptJS.LocatorOrString} locator field located by label|name|CSS|XPath|strict locator.
1728
+ * @returns {Promise<string>} attribute value
1729
+ *
1730
+ *
1731
+ */
1732
+ async grabValueFrom(locator) {
1733
+ if (this.isWeb) return super.grabValueFrom(locator);
1734
+ return super.grabValueFrom(parseLocator.call(this, locator));
1735
+ }
1736
+
1737
+ /**
1738
+ * Saves a screenshot to ouput folder (set in codecept.conf.ts or codecept.conf.js).
1739
+ * Filename is relative to output folder.
1740
+ *
1741
+ * ```js
1742
+ * I.saveScreenshot('debug.png');
1743
+ * ```
1744
+ *
1745
+ * @param {string} fileName file name to save.
1746
+ * @return {Promise<void>}
1747
+ */
1748
+ async saveScreenshot(fileName) {
1749
+ return super.saveScreenshot(fileName, false);
1750
+ }
1751
+
1752
+ /**
1753
+ * Scroll element into viewport.
1754
+ *
1755
+ * ```js
1756
+ * I.scrollIntoView('#submit');
1757
+ * I.scrollIntoView('#submit', true);
1758
+ * I.scrollIntoView('#submit', { behavior: "smooth", block: "center", inline: "center" });
1759
+ * ```
1760
+ *
1761
+ * @param {LocatorOrString} locator located by CSS|XPath|strict locator.
1762
+ * @param {ScrollIntoViewOptions|boolean} scrollIntoViewOptions either alignToTop=true|false or scrollIntoViewOptions. See https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView.
1763
+ * @returns {void} automatically synchronized promise through #recorder
1764
+ *
1765
+ *
1766
+ * Supported only for web testing
1767
+ */
1768
+ async scrollIntoView(locator, scrollIntoViewOptions) {
1769
+ if (this.isWeb) return super.scrollIntoView(locator, scrollIntoViewOptions);
1770
+ }
1771
+
1772
+ /**
1773
+ * Verifies that the specified checkbox is checked.
1774
+ *
1775
+ * ```js
1776
+ * I.seeCheckboxIsChecked('Agree');
1777
+ * I.seeCheckboxIsChecked('#agree'); // I suppose user agreed to terms
1778
+ * I.seeCheckboxIsChecked({css: '#signup_form input[type=checkbox]'});
1779
+ * ```
1780
+ *
1781
+ * @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator.
1782
+ * @returns {void} automatically synchronized promise through #recorder
1783
+ *
1784
+ *
1785
+ */
1786
+ async seeCheckboxIsChecked(field) {
1787
+ if (this.isWeb) return super.seeCheckboxIsChecked(field);
1788
+ return super.seeCheckboxIsChecked(parseLocator.call(this, field));
1789
+ }
1790
+
1791
+ /**
1792
+ * Checks that a given Element is visible
1793
+ * Element is located by CSS or XPath.
1794
+ *
1795
+ * ```js
1796
+ * I.seeElement('#modal');
1797
+ * ```
1798
+ * @param {CodeceptJS.LocatorOrString} locator located by CSS|XPath|strict locator.
1799
+ * @returns {void} automatically synchronized promise through #recorder
1800
+ *
1801
+ *
1802
+ */
1803
+ async seeElement(locator) {
1804
+ if (this.isWeb) return super.seeElement(locator);
1805
+ return super.seeElement(parseLocator.call(this, locator));
1806
+ }
1807
+
1808
+ /**
1809
+ * Checks that the given input field or textarea equals to given value.
1810
+ * For fuzzy locators, fields are matched by label text, the "name" attribute, CSS, and XPath.
1811
+ *
1812
+ * ```js
1813
+ * I.seeInField('Username', 'davert');
1814
+ * I.seeInField({css: 'form textarea'},'Type your comment here');
1815
+ * I.seeInField('form input[type=hidden]','hidden_value');
1816
+ * I.seeInField('#searchform input','Search');
1817
+ * ```
1818
+ * @param {CodeceptJS.LocatorOrString} field located by label|name|CSS|XPath|strict locator.
1819
+ * @param {CodeceptJS.StringOrSecret} value value to check.
1820
+ * @returns {void} automatically synchronized promise through #recorder
1821
+ *
1822
+ *
1823
+ */
1824
+ async seeInField(field, value) {
1825
+ const _value = (typeof value === 'boolean') ? value : value.toString();
1826
+ if (this.isWeb) return super.seeInField(field, _value);
1827
+ return super.seeInField(parseLocator.call(this, field), _value);
1828
+ }
1829
+
1830
+ /**
1831
+ * Checks that a page contains a visible text.
1832
+ * Use context parameter to narrow down the search.
1833
+ *
1834
+ * ```js
1835
+ * I.see('Welcome'); // text welcome on a page
1836
+ * I.see('Welcome', '.content'); // text inside .content div
1837
+ * I.see('Register', {css: 'form.register'}); // use strict locator
1838
+ * ```
1839
+ * @param {string} text expected on page.
1840
+ * @param {?CodeceptJS.LocatorOrString} [context=null] (optional, `null` by default) element located by CSS|Xpath|strict locator in which to search for text.
1841
+ * @returns {void} automatically synchronized promise through #recorder
1842
+ *
1843
+ *
1844
+ */
1845
+ async see(text, context) {
1846
+ if (this.isWeb) return super.see(text, context);
1847
+ return super.see(text, parseLocator.call(this, context));
1848
+ }
1849
+
1850
+ /**
1851
+ * Selects an option in a drop-down select.
1852
+ * Field is searched by label | name | CSS | XPath.
1853
+ * Option is selected by visible text or by value.
1854
+ *
1855
+ * ```js
1856
+ * I.selectOption('Choose Plan', 'Monthly'); // select by label
1857
+ * I.selectOption('subscription', 'Monthly'); // match option by text
1858
+ * I.selectOption('subscription', '0'); // or by value
1859
+ * I.selectOption('//form/select[@name=account]','Premium');
1860
+ * I.selectOption('form select[name=account]', 'Premium');
1861
+ * I.selectOption({css: 'form select[name=account]'}, 'Premium');
1862
+ * ```
1863
+ *
1864
+ * Provide an array for the second argument to select multiple options.
1865
+ *
1866
+ * ```js
1867
+ * I.selectOption('Which OS do you use?', ['Android', 'iOS']);
1868
+ * ```
1869
+ * @param {LocatorOrString} select field located by label|name|CSS|XPath|strict locator.
1870
+ * @param {string|Array<*>} option visible text or value of option.
1871
+ * @returns {void} automatically synchronized promise through #recorder
1872
+ *
1873
+ *
1874
+ * Supported only for web testing
1875
+ */
1876
+ async selectOption(select, option) {
1877
+ if (this.isWeb) return super.selectOption(select, option);
1878
+ throw new Error('Should be used only in Web context. In native context use \'click\' method instead');
1879
+ }
1880
+
1881
+ /**
1882
+ * Waits for element to be present on page (by default waits for 1sec).
1883
+ * Element can be located by CSS or XPath.
1884
+ *
1885
+ * ```js
1886
+ * I.waitForElement('.btn.continue');
1887
+ * I.waitForElement('.btn.continue', 5); // wait for 5 secs
1888
+ * ```
1889
+ *
1890
+ * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1891
+ * @param {number} [sec] (optional, `1` by default) time in seconds to wait
1892
+ * @returns {void} automatically synchronized promise through #recorder
1893
+ *
1894
+ *
1895
+ */
1896
+ async waitForElement(locator, sec = null) {
1897
+ if (this.isWeb) return super.waitForElement(locator, sec);
1898
+ return super.waitForElement(parseLocator.call(this, locator), sec);
1899
+ }
1900
+
1901
+ /**
1902
+ * Waits for an element to become visible on a page (by default waits for 1sec).
1903
+ * Element can be located by CSS or XPath.
1904
+ *
1905
+ * ```js
1906
+ * I.waitForVisible('#popup');
1907
+ * ```
1908
+ *
1909
+ * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1910
+ * @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
1911
+ * @returns {void} automatically synchronized promise through #recorder
1912
+ *
1913
+ *
1914
+ */
1915
+ async waitForVisible(locator, sec = null) {
1916
+ if (this.isWeb) return super.waitForVisible(locator, sec);
1917
+ return super.waitForVisible(parseLocator.call(this, locator), sec);
1918
+ }
1919
+
1920
+ /**
1921
+ * Waits for an element to be removed or become invisible on a page (by default waits for 1sec).
1922
+ * Element can be located by CSS or XPath.
1923
+ *
1924
+ * ```js
1925
+ * I.waitForInvisible('#popup');
1926
+ * ```
1927
+ *
1928
+ * @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
1929
+ * @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
1930
+ * @returns {void} automatically synchronized promise through #recorder
1931
+ *
1932
+ *
1933
+ */
1934
+ async waitForInvisible(locator, sec = null) {
1935
+ if (this.isWeb) return super.waitForInvisible(locator, sec);
1936
+ return super.waitForInvisible(parseLocator.call(this, locator), sec);
1937
+ }
1938
+
1939
+ /**
1940
+ * Waits for a text to appear (by default waits for 1sec).
1941
+ * Element can be located by CSS or XPath.
1942
+ * Narrow down search results by providing context.
1943
+ *
1944
+ * ```js
1945
+ * I.waitForText('Thank you, form has been submitted');
1946
+ * I.waitForText('Thank you, form has been submitted', 5, '#modal');
1947
+ * ```
1948
+ *
1949
+ * @param {string }text to wait for.
1950
+ * @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
1951
+ * @param {CodeceptJS.LocatorOrString} [context] (optional) element located by CSS|XPath|strict locator.
1952
+ * @returns {void} automatically synchronized promise through #recorder
1953
+ *
1954
+ *
1955
+ */
1956
+ async waitForText(text, sec = null, context = null) {
1957
+ if (this.isWeb) return super.waitForText(text, sec, context);
1958
+ return super.waitForText(text, sec, parseLocator.call(this, context));
1959
+ }
1960
+ }
1961
+
1962
+ function parseLocator(locator) {
1963
+ if (!locator) return null;
1964
+
1965
+ if (typeof locator === 'object') {
1966
+ if (locator.web && this.isWeb) {
1967
+ return parseLocator.call(this, locator.web);
1968
+ }
1969
+
1970
+ if (locator.android && this.platform === 'android') {
1971
+ if (typeof locator.android === 'string') {
1972
+ return parseLocator.call(this, locator.android);
1973
+ }
1974
+ // The locator is an Android DataMatcher or ViewMatcher locator so return as is
1975
+ return locator.android;
1976
+ }
1977
+
1978
+ if (locator.ios && this.platform === 'ios') {
1979
+ return parseLocator.call(this, locator.ios);
1980
+ }
1981
+ }
1982
+
1983
+ if (typeof locator === 'string') {
1984
+ if (locator[0] === '~') return locator;
1985
+ if (locator.substr(0, 2) === '//') return locator;
1986
+ if (locator[0] === '#' && !this.isWeb) {
1987
+ // hook before webdriverio supports native # locators
1988
+ return parseLocator.call(this, { id: locator.slice(1) });
1989
+ }
1990
+
1991
+ if (this.platform === 'android' && !this.isWeb) {
1992
+ const isNativeLocator = /^\-?android=?/.exec(locator);
1993
+ return isNativeLocator
1994
+ ? locator
1995
+ : `android=new UiSelector().text("${locator}")`;
1996
+ }
1997
+ }
1998
+
1999
+ locator = new Locator(locator, 'xpath');
2000
+ if (locator.type === 'css' && !this.isWeb) throw new Error('Unable to use css locators in apps. Locator strategies for this request: xpath, id, class name or accessibility id');
2001
+ if (locator.type === 'name' && !this.isWeb) throw new Error("Can't locate element by name in Native context. Use either ID, class name or accessibility id");
2002
+ if (locator.type === 'id' && !this.isWeb && this.platform === 'android') return `//*[@resource-id='${locator.value}']`;
2003
+ return locator.simplify();
2004
+ }
2005
+
2006
+ // in the end of a file
2007
+ function onlyForApps(expectedPlatform) {
2008
+ const stack = new Error().stack || '';
2009
+ const re = /Appium.(\w+)/g;
2010
+ const caller = stack.split('\n')[2].trim();
2011
+ const m = re.exec(caller);
2012
+
2013
+ if (!m) {
2014
+ throw new Error(`Invalid caller ${caller}`);
2015
+ }
2016
+
2017
+ const callerName = m[1] || m[2];
2018
+ if (!expectedPlatform) {
2019
+ if (!this.platform) {
2020
+ throw new Error(`${callerName} method can be used only with apps`);
2021
+ }
2022
+ } else if (this.platform !== expectedPlatform.toLowerCase()) {
2023
+ throw new Error(`${callerName} method can be used only with ${expectedPlatform} apps`);
2024
+ }
2025
+ }
2026
+
2027
+ module.exports = Appium;