codeceptjs 3.5.15 → 3.6.0-beta.1.ai-healers
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/bin/codecept.js +66 -30
- package/docs/advanced.md +351 -0
- package/docs/ai.md +365 -0
- package/docs/api.md +323 -0
- package/docs/basics.md +979 -0
- package/docs/bdd.md +539 -0
- package/docs/best.md +237 -0
- package/docs/books.md +37 -0
- package/docs/bootstrap.md +135 -0
- package/docs/build/AI.js +124 -0
- package/docs/build/ApiDataFactory.js +410 -0
- package/docs/build/Appium.js +2027 -0
- package/docs/build/Expect.js +422 -0
- package/docs/build/FileSystem.js +228 -0
- package/docs/build/GraphQL.js +229 -0
- package/docs/build/GraphQLDataFactory.js +309 -0
- package/docs/build/JSONResponse.js +338 -0
- package/docs/build/Mochawesome.js +71 -0
- package/docs/build/Nightmare.js +2152 -0
- package/docs/build/Playwright.js +5110 -0
- package/docs/build/Protractor.js +2706 -0
- package/docs/build/Puppeteer.js +3905 -0
- package/docs/build/REST.js +344 -0
- package/docs/build/TestCafe.js +2125 -0
- package/docs/build/WebDriver.js +4240 -0
- package/docs/changelog.md +2572 -0
- package/docs/commands.md +266 -0
- package/docs/community-helpers.md +58 -0
- package/docs/configuration.md +157 -0
- package/docs/continuous-integration.md +22 -0
- package/docs/custom-helpers.md +306 -0
- package/docs/data.md +379 -0
- package/docs/detox.md +235 -0
- package/docs/docker.md +136 -0
- package/docs/email.md +183 -0
- package/docs/examples.md +149 -0
- package/docs/heal.md +186 -0
- package/docs/helpers/ApiDataFactory.md +266 -0
- package/docs/helpers/Appium.md +1374 -0
- package/docs/helpers/Detox.md +586 -0
- package/docs/helpers/Expect.md +275 -0
- package/docs/helpers/FileSystem.md +152 -0
- package/docs/helpers/GraphQL.md +151 -0
- package/docs/helpers/GraphQLDataFactory.md +226 -0
- package/docs/helpers/JSONResponse.md +254 -0
- package/docs/helpers/Mochawesome.md +8 -0
- package/docs/helpers/MockRequest.md +377 -0
- package/docs/helpers/Nightmare.md +1305 -0
- package/docs/helpers/OpenAI.md +70 -0
- package/docs/helpers/Playwright.md +2759 -0
- package/docs/helpers/Polly.md +44 -0
- package/docs/helpers/Protractor.md +1769 -0
- package/docs/helpers/Puppeteer-firefox.md +86 -0
- package/docs/helpers/Puppeteer.md +2317 -0
- package/docs/helpers/REST.md +218 -0
- package/docs/helpers/TestCafe.md +1321 -0
- package/docs/helpers/WebDriver.md +2547 -0
- package/docs/hooks.md +340 -0
- package/docs/index.md +111 -0
- package/docs/installation.md +75 -0
- package/docs/internal-api.md +266 -0
- package/docs/locators.md +339 -0
- package/docs/mobile-react-native-locators.md +67 -0
- package/docs/mobile.md +338 -0
- package/docs/pageobjects.md +291 -0
- package/docs/parallel.md +400 -0
- package/docs/playwright.md +632 -0
- package/docs/plugins.md +1247 -0
- package/docs/puppeteer.md +316 -0
- package/docs/quickstart.md +162 -0
- package/docs/react.md +70 -0
- package/docs/reports.md +392 -0
- package/docs/secrets.md +36 -0
- package/docs/shadow.md +68 -0
- package/docs/shared/keys.mustache +31 -0
- package/docs/shared/react.mustache +1 -0
- package/docs/testcafe.md +174 -0
- package/docs/translation.md +247 -0
- package/docs/tutorial.md +271 -0
- package/docs/typescript.md +180 -0
- package/docs/ui.md +59 -0
- package/docs/videos.md +28 -0
- package/docs/visual.md +202 -0
- package/docs/vue.md +143 -0
- package/docs/webdriver.md +701 -0
- package/docs/wiki/Books-&-Posts.md +27 -0
- package/docs/wiki/Community-Helpers-&-Plugins.md +53 -0
- package/docs/wiki/Converting-Playwright-to-Istanbul-Coverage.md +61 -0
- package/docs/wiki/Examples.md +145 -0
- package/docs/wiki/Google-Summer-of-Code-(GSoC)-2020.md +68 -0
- package/docs/wiki/Home.md +16 -0
- package/docs/wiki/Migration-to-Appium-v2---CodeceptJS.md +83 -0
- package/docs/wiki/Release-Process.md +24 -0
- package/docs/wiki/Roadmap.md +23 -0
- package/docs/wiki/Tests.md +1393 -0
- package/docs/wiki/Upgrading-to-CodeceptJS-3.md +153 -0
- package/docs/wiki/Videos.md +19 -0
- package/lib/actor.js +3 -6
- package/lib/ai.js +152 -80
- package/lib/cli.js +1 -0
- package/lib/command/dryRun.js +13 -44
- package/lib/command/generate.js +34 -0
- package/lib/command/run-workers.js +3 -0
- package/lib/command/run.js +3 -0
- package/lib/container.js +2 -0
- package/lib/heal.js +172 -0
- package/lib/helper/AI.js +124 -0
- package/lib/helper/Appium.js +12 -36
- package/lib/helper/Expect.js +8 -11
- package/lib/helper/JSONResponse.js +8 -8
- package/lib/helper/Playwright.js +240 -100
- package/lib/helper/Puppeteer.js +68 -182
- package/lib/helper/REST.js +1 -4
- package/lib/helper/WebDriver.js +10 -324
- package/lib/index.js +3 -0
- package/lib/listener/steps.js +0 -2
- package/lib/locator.js +4 -13
- package/lib/plugin/coverage.js +99 -112
- package/lib/plugin/heal.js +26 -117
- package/lib/recorder.js +11 -5
- package/lib/step.js +1 -3
- package/lib/store.js +2 -0
- package/lib/template/heal.js +39 -0
- package/package.json +35 -47
- package/typings/index.d.ts +0 -17
- package/typings/promiseBasedTypes.d.ts +57 -340
- package/typings/types.d.ts +73 -433
- package/docs/webapi/dontSeeTraffic.mustache +0 -13
- package/docs/webapi/flushNetworkTraffics.mustache +0 -5
- package/docs/webapi/grabRecordedNetworkTraffics.mustache +0 -10
- package/docs/webapi/seeTraffic.mustache +0 -36
- package/docs/webapi/startRecordingTraffic.mustache +0 -8
- package/docs/webapi/stopRecordingTraffic.mustache +0 -5
- package/docs/webapi/waitForCookie.mustache +0 -9
- package/lib/helper/MockServer.js +0 -221
- package/lib/helper/errors/ElementAssertion.js +0 -38
- package/lib/helper/networkTraffics/utils.js +0 -137
- /package/{lib/helper → docs/build}/OpenAI.js +0 -0
package/docs/mobile.md
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
---
|
|
2
|
+
permalink: /mobile
|
|
3
|
+
title: Mobile Testing with Appium
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Mobile Testing with Appium
|
|
7
|
+
|
|
8
|
+
CodeceptJS allows to test mobile and hybrid apps in a similar manner web applications are tested.
|
|
9
|
+
Such tests are executed using [Appium](https://appium.io) on emulated or physical devices. Also, Appium allows to test web application on mobile devices.
|
|
10
|
+
|
|
11
|
+
What makes CodeceptJS better for mobile testing?
|
|
12
|
+
Take a look. Here is the sample test for a native mobile application written in CodeceptJS:
|
|
13
|
+
|
|
14
|
+
```js
|
|
15
|
+
I.seeAppIsInstalled("io.super.app");
|
|
16
|
+
I.click('~startUserRegistrationCD');
|
|
17
|
+
I.fillField('~email of the customer', 'Nothing special');
|
|
18
|
+
I.see('davert@codecept.io', '~email of the customer');
|
|
19
|
+
I.clearField('~email of the customer');
|
|
20
|
+
I.dontSee('Nothing special', '~email of the customer');
|
|
21
|
+
I.seeElement({
|
|
22
|
+
android: 'android.widget.Button',
|
|
23
|
+
ios: '//UIAApplication[1]/UIAWindow[1]/UIAButton[1]'
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
This test is easy to read and write. Also, it will work both on iOS and Android devices.
|
|
28
|
+
Doesn't it sound cool?
|
|
29
|
+
|
|
30
|
+
## Setting Up
|
|
31
|
+
|
|
32
|
+
Ensure that you have [CodeceptJS installed](https://codecept.io/installation/).
|
|
33
|
+
You will also need to install [Appium](https://appium.io/docs/en/2.1/).
|
|
34
|
+
We suggest to use [appium-doctor](https://www.npmjs.com/package/appium-doctor) to check if your system is ready for mobile testing.
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
npm i -g appium-doctor
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
If everything is OK, continue with installing Appium. If not, consider using cloud based alternatives like [SauceLabs](https://saucelabs.com) or [BrowserStack](https://browserstack.com). Cloud services provide hosted appium with real and emulated mobile devices.
|
|
41
|
+
|
|
42
|
+
To install Appium use npm:
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
npm i -g appium
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Appium 2x reenvisions Appium as a platform where “drivers” and “plugins” can be easily created and shared independently.
|
|
49
|
+
|
|
50
|
+
** Note: ** Appium v1 is no longer maintained, so it's advised to migrate to Appium v2.
|
|
51
|
+
|
|
52
|
+
Install an Appium driver and its dependencies
|
|
53
|
+
To install the Appium driver and its dependencies, we'll be using the `uiautomator2` (Android), `XCUITest` (iOS) drivers.
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
appium driver install xcuitest
|
|
57
|
+
appium driver install uiautomator2
|
|
58
|
+
```
|
|
59
|
+
To make sure that all the drivers are installed successfully, run the following command:
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
appium driver list
|
|
63
|
+
|
|
64
|
+
tth~$appium driver list
|
|
65
|
+
✔ Listing available drivers
|
|
66
|
+
- espresso@2.17.0 [installed (NPM)]
|
|
67
|
+
- uiautomator2@2.12.6 [installed (NPM)]
|
|
68
|
+
- xcuitest@4.19.1 [installed (NPM)]
|
|
69
|
+
- mac2 [not installed]
|
|
70
|
+
- safari [not installed]
|
|
71
|
+
- gecko [not installed]
|
|
72
|
+
- chromium [not installed]
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Then you need to prepare application for execution.
|
|
76
|
+
It should be packed into apk (for Android) or .ipa (for iOS) or zip.
|
|
77
|
+
|
|
78
|
+
Next, is to launch the emulator or connect a physical device.
|
|
79
|
+
Once they are prepared, launch Appium:
|
|
80
|
+
|
|
81
|
+
```sh
|
|
82
|
+
tth~$npx appium --base-path=/wd/hub
|
|
83
|
+
[Appium] Welcome to Appium v2.0.0-beta.57 (REV 3e675c32ae71dc0b00749d5d29213e2ea5b53c5b)
|
|
84
|
+
[Appium] Non-default server args:
|
|
85
|
+
[Appium] {
|
|
86
|
+
[Appium] basePath: '/wd/hub'
|
|
87
|
+
[Appium] }
|
|
88
|
+
[Appium] Attempting to load driver espresso...
|
|
89
|
+
[debug] [Appium] Requiring driver at /Users/trung-thanh/Desktop/thanh-nguyen/task2/node_modules/appium-espresso-driver
|
|
90
|
+
[Appium] Attempting to load driver uiautomator2...
|
|
91
|
+
[debug] [Appium] Requiring driver at /Users/trung-thanh/Desktop/thanh-nguyen/task2/node_modules/appium-uiautomator2-driver
|
|
92
|
+
[Appium] Appium REST http interface listener started on 0.0.0.0:4723
|
|
93
|
+
[Appium] Available drivers:
|
|
94
|
+
[Appium] - espresso@2.17.0 (automationName 'Espresso')
|
|
95
|
+
[Appium] - uiautomator2@2.12.6 (automationName 'UiAutomator2')
|
|
96
|
+
[Appium] No plugins have been installed. Use the "appium plugin" command to install the one(s) you want to use.
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
** Note: ** Appium v2 doesn't use the same base path as Appium v1, hence if you want to use the same base path you should pass `--base-path=/wd/hub` when launching the Appium server.
|
|
100
|
+
|
|
101
|
+
To run mobile test you need either a device emulator (available with Android SDK or iOS) or real device connected for mobile testing. Alternatively, you may execute Appium with device emulator inside Docker container.
|
|
102
|
+
|
|
103
|
+
CodeceptJS should be installed with webdriverio support:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
npm install codeceptjs webdriverio@8.6.3 --save
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Configuring
|
|
110
|
+
|
|
111
|
+
Initialize CodeceptJS with `init` command:
|
|
112
|
+
|
|
113
|
+
```sh
|
|
114
|
+
npx codeceptjs init
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Select [Appium helper](https://codecept.io/helpers/Appium/) when asked.
|
|
118
|
+
|
|
119
|
+
```sh
|
|
120
|
+
? What helpers do you want to use?
|
|
121
|
+
◯ WebDriver
|
|
122
|
+
◯ Puppeteer
|
|
123
|
+
❯◉ Appium
|
|
124
|
+
◯ REST
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
You will also be asked for the platform and the application package.
|
|
128
|
+
|
|
129
|
+
```sh
|
|
130
|
+
? [Appium] Application package. Path to file or url
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Check the newly created `codecept.conf.js` configuration file.
|
|
134
|
+
You may want to set some additional Appium settings via [desiredCapabilities](https://appium.io/docs/en/2.1/guides/caps/)
|
|
135
|
+
|
|
136
|
+
```js
|
|
137
|
+
helpers: {
|
|
138
|
+
Appium: {
|
|
139
|
+
app: "my_app.apk",
|
|
140
|
+
platform: "Android",
|
|
141
|
+
desiredCapabilities: {}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Once you configured Appium, create the first test by running:
|
|
147
|
+
|
|
148
|
+
```sh
|
|
149
|
+
npx codeceptjs gt
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## BrowserStack Configuration
|
|
153
|
+
|
|
154
|
+
If you wish to use BrowserStack's [Automated Mobile App Testing](https://www.browserstack.com/app-automate) platform. Configure the Appium helper like this:
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
helpers: {
|
|
158
|
+
Appium: {
|
|
159
|
+
app: "bs://<hashed app-id>",
|
|
160
|
+
host: "hub-cloud.browserstack.com",
|
|
161
|
+
port: 4444,
|
|
162
|
+
platform: "ios",
|
|
163
|
+
user: "BROWSERSTACK_USER",
|
|
164
|
+
key: "BROWSERSTACK_KEY",
|
|
165
|
+
device: "iPhone 7"
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
Here is the full list of [capabilities](https://www.browserstack.com/app-automate/capabilities).
|
|
170
|
+
|
|
171
|
+
You need to upload your Android app (.apk) or iOS app (.ipa) to the BrowserStack servers using the REST API before running your tests. The App URL (`bs://hashed appid`) is returned in the response of this call.
|
|
172
|
+
|
|
173
|
+
```sh
|
|
174
|
+
curl -u "USERNAME:ACCESS_KEY" \
|
|
175
|
+
-X POST "https://api-cloud.browserstack.com/app-automate/upload" \
|
|
176
|
+
-F "file=@/path/to/app/file/Application-debug.apk"
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Writing a Test
|
|
180
|
+
|
|
181
|
+
A test is written in a scenario-driven manner, listing an actions taken by a user.
|
|
182
|
+
This is the sample test for a native mobile application:
|
|
183
|
+
|
|
184
|
+
```js
|
|
185
|
+
Scenario('test registration', ({ I }) => {
|
|
186
|
+
I.click('~startUserRegistrationCD');
|
|
187
|
+
I.fillField('~inputUsername', 'davert');
|
|
188
|
+
I.fillField('~inputEmail', 'davert@codecept.io');
|
|
189
|
+
I.fillField('~inputPassword', '123456');
|
|
190
|
+
I.hideDeviceKeyboard();
|
|
191
|
+
I.click('~input_preferredProgrammingLanguage');
|
|
192
|
+
I.click('Javascript');
|
|
193
|
+
I.checkOption('#io.demo.testapp:id/input_adds');
|
|
194
|
+
I.click('Register User (verify)');
|
|
195
|
+
I.swipeUp("#io.selendroid.testapp:id/LinearLayout1");
|
|
196
|
+
I.see('Javascript'); // see on the screen
|
|
197
|
+
I.see('davert', '~label_username_data'); // see in element
|
|
198
|
+
});
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Mobile test is pretty similar to a web test. And it is much the same, if you test hybrid app with a web view context inside.
|
|
202
|
+
However, mobile apps do not have URLs, Cookies, they have other features which may vary on a running platform.
|
|
203
|
+
|
|
204
|
+
There are mobile-only methods like:
|
|
205
|
+
|
|
206
|
+
* `swipeUp`, `swipeLeft`, ...
|
|
207
|
+
* `hideDeviceKeyboard`,
|
|
208
|
+
* `seeAppIsInstalled`, `installApp`, `removeApp`, `seeAppIsNotInstalled` - Android only
|
|
209
|
+
|
|
210
|
+
and [others](https://codecept.io/helpers/Appium/).
|
|
211
|
+
|
|
212
|
+
## Locating Elements
|
|
213
|
+
|
|
214
|
+
To start writing a test it is important to understand how to locate elements for native mobile applications.
|
|
215
|
+
In both Android and iPhone elements are defined in XML format and can be searched by XPath locators.
|
|
216
|
+
|
|
217
|
+
```js
|
|
218
|
+
I.seeElement('//android.widget.ScrollView/android.widget.LinearLayout')
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
> Despite showing XPath in this guide we **do not recommend using XPath for testing iOS native apps. XPath runs very slow on iOS. Consider using ID or Accessibility ID locators instead.
|
|
222
|
+
|
|
223
|
+
CSS locators are not supported in native mobile apps, you need to switch to web context to use them.
|
|
224
|
+
|
|
225
|
+
Elements can also be located by their accessability id, available both at Android and iOS.
|
|
226
|
+
Accessibility id is recommended to use for locating element, as it rarely changed.
|
|
227
|
+
|
|
228
|
+
* iOS uses [UIAccessibilityIdentification](https://developer.apple.com/documentation/uikit/uiaccessibilityidentification)
|
|
229
|
+
* Android `accessibility id` matches the content-description
|
|
230
|
+
* Web view uses `[aria-label]` attribute as accessibility id
|
|
231
|
+
* For [React Native for Android see our special guide](mobile-react-native-locators.md).
|
|
232
|
+
|
|
233
|
+
> If you test React Native application, consider using [Detox helper](/detox) for faster tests.
|
|
234
|
+
|
|
235
|
+
Add `~` prefix to search for element by its accessibility id:
|
|
236
|
+
|
|
237
|
+
```js
|
|
238
|
+
I.seeElement('~startUserRegistrationButton');
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Elements can also have ids, which can be located with `#` prefix.
|
|
242
|
+
On Android, it is important to keep full package name in id locator:
|
|
243
|
+
|
|
244
|
+
```js
|
|
245
|
+
I.seeElement('#io.selendroid.testapp:id/inputUsername');
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
Buttons can be matched by their visible text:
|
|
249
|
+
|
|
250
|
+
```js
|
|
251
|
+
I.tap('Click me!');
|
|
252
|
+
I.click('Click me!');
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
Native iOS/Android locators can be used with `android=` and `ios=` prefixes. [Learn more](https://webdriver.io/guide/usage/selectors.html#Mobile-Selectors).
|
|
256
|
+
|
|
257
|
+
But how to get all those locators? We recommend to use [Appium Inspector](https://github.com/appium/appium-inspector).
|
|
258
|
+
|
|
259
|
+
For Android, you can use **UI Automator Viewer** bundled with Android SDK:
|
|
260
|
+
|
|
261
|
+

|
|
262
|
+
|
|
263
|
+
## Hybrid Apps and Contexts
|
|
264
|
+
|
|
265
|
+
Mobile applications may have different contexts. For instance, there can be native view and web view with a browser instance in it.
|
|
266
|
+
|
|
267
|
+
To execute commands in context of a webview use `within('webview')` function:
|
|
268
|
+
|
|
269
|
+
```js
|
|
270
|
+
I.click('~startWebView');
|
|
271
|
+
within('webview', () => {
|
|
272
|
+
I.see('Preferred car');
|
|
273
|
+
I.click('Send me your name!');
|
|
274
|
+
});
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
It will locate first available webview, switch to it, and switch back to native application after.
|
|
278
|
+
Inside WebView all browser features are enabled: CSS locators, JavaScript, etc.
|
|
279
|
+
|
|
280
|
+
To set a specific context use `{ web: 'webview.context' }` instead:
|
|
281
|
+
|
|
282
|
+
```js
|
|
283
|
+
within({webview: 'MyWEBVIEW_com.my.app'}, () => {});
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
Alternatively use `switchToWeb` or `switchToNative` methods to switch between contexts.
|
|
287
|
+
|
|
288
|
+
```js
|
|
289
|
+
I.click('~startWebView');
|
|
290
|
+
I.switchToWeb();
|
|
291
|
+
I.see('Preferred car');
|
|
292
|
+
I.click('Send me your name!');
|
|
293
|
+
I.switchToNative();
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
To get a list of all contexts use `grabAllContexts` method:
|
|
297
|
+
|
|
298
|
+
```js
|
|
299
|
+
let contexts = await I.grabAllContexts();
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## Cross-Platform Testing
|
|
303
|
+
|
|
304
|
+
It is often happen that mobile applications behave similarly on different platforms. Can we build one test for them? Yes!
|
|
305
|
+
CodeceptJS provides a way to specify different locators for Android and iOS platforms:
|
|
306
|
+
|
|
307
|
+
```js
|
|
308
|
+
I.click({android: '//android.widget.Button', ios: '//UIAApplication[1]/UIAWindow[1]/UIAButton[1]'});
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
In case some code should be executed on one platform and ignored on others use `runOnAndroid` and `runOnIOS` methods:
|
|
312
|
+
|
|
313
|
+
```js
|
|
314
|
+
I.runOnAndroid(() => {
|
|
315
|
+
I.click('Hello Android');
|
|
316
|
+
});
|
|
317
|
+
I.runOnIOS(() => {
|
|
318
|
+
I.click('Hello iOS');
|
|
319
|
+
});
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
The same code can be shared for web applications as well. To execute some code in web browser only, use `I.runInWeb`:
|
|
323
|
+
|
|
324
|
+
```js
|
|
325
|
+
I.runInWeb(() => {
|
|
326
|
+
I.amOnPage('/login'); // not available for mobile
|
|
327
|
+
I.fillField('name', 'jon');
|
|
328
|
+
I.fillField('password', '123456');
|
|
329
|
+
I.click('Login');
|
|
330
|
+
I.waitForElement('#success'); // no available for mobile
|
|
331
|
+
});
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
Just as you can specify android, and ios-specific locators, you can do so for web:
|
|
335
|
+
|
|
336
|
+
```js
|
|
337
|
+
I.click({web: '#login', ios: '//UIAApplication[1]/UIAWindow[1]/UIAButton[1]'});
|
|
338
|
+
```
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
---
|
|
2
|
+
permalink: /pageobjects
|
|
3
|
+
title: Page Objects
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Page Objects
|
|
7
|
+
|
|
8
|
+
The UI of your web application has interaction areas which can be shared across different tests.
|
|
9
|
+
To avoid code duplication you can put common locators and methods in one place.
|
|
10
|
+
|
|
11
|
+
## Dependency Injection
|
|
12
|
+
|
|
13
|
+
All objects described here are injected via Dependency Injection, in a similar way AngularJS does. If you want an object to be injected in a scenario by its name, you can add it to the configuration:
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
include: {
|
|
17
|
+
I: "./custom_steps.js",
|
|
18
|
+
Smth: "./pages/Smth.js",
|
|
19
|
+
loginPage: "./pages/Login.js",
|
|
20
|
+
signinFragment: "./fragments/Signin.js"
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
These objects can now be retrieved by the name specified in the configuration.
|
|
25
|
+
|
|
26
|
+
Required objects can be obtained via parameters in tests or via a global `inject()` call.
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
// globally inject objects by name
|
|
30
|
+
const { I, myPage, mySteps } = inject();
|
|
31
|
+
|
|
32
|
+
// inject objects for a test by name
|
|
33
|
+
Scenario('sample test', ({ I, myPage, mySteps }) => {
|
|
34
|
+
// ...
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Actor
|
|
39
|
+
|
|
40
|
+
During initialization you were asked to create a custom steps file. If you accepted this option, you are now able to use the `custom_steps.js` file to extend `I`. See how the `login` method can be added to `I`:
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
module.exports = function() {
|
|
44
|
+
return actor({
|
|
45
|
+
|
|
46
|
+
login: function(email, password) {
|
|
47
|
+
this.fillField('Email', email);
|
|
48
|
+
this.fillField('Password', password);
|
|
49
|
+
this.click('Submit');
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
> ℹ Instead of `I` you should use `this` in the current context.
|
|
56
|
+
|
|
57
|
+
## PageObject
|
|
58
|
+
|
|
59
|
+
If an application has different pages (login, admin, etc) you should use a page object.
|
|
60
|
+
CodeceptJS can generate a template for it with the following command:
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
npx codeceptjs gpo
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
This will create a sample template for a page object and include it in the `codecept.json` config file.
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
const { I, otherPage } = inject();
|
|
70
|
+
|
|
71
|
+
module.exports = {
|
|
72
|
+
|
|
73
|
+
// insert your locators and methods here
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
As you see, the `I` object is available in scope, so you can use it just like you would do in tests.
|
|
78
|
+
A general page object for a login page could look like this:
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
// enable I and another page object
|
|
82
|
+
const { I, registerPage } = inject();
|
|
83
|
+
|
|
84
|
+
module.exports = {
|
|
85
|
+
|
|
86
|
+
// setting locators
|
|
87
|
+
fields: {
|
|
88
|
+
email: '#user_basic_email',
|
|
89
|
+
password: '#user_basic_password'
|
|
90
|
+
},
|
|
91
|
+
submitButton: {css: '#new_user_basic input[type=submit]'},
|
|
92
|
+
|
|
93
|
+
// introducing methods
|
|
94
|
+
sendForm(email, password) {
|
|
95
|
+
I.fillField(this.fields.email, email);
|
|
96
|
+
I.fillField(this.fields.password, password);
|
|
97
|
+
I.click(this.submitButton);
|
|
98
|
+
},
|
|
99
|
+
|
|
100
|
+
register(email, password) {
|
|
101
|
+
// use another page object inside current one
|
|
102
|
+
registerPage.registerUser({ email, password });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
You can include this pageobject in a test by its name (defined in `codecept.json`). If you created a `loginPage` object,
|
|
108
|
+
it should be added to the list of arguments to be included in the test:
|
|
109
|
+
|
|
110
|
+
```js
|
|
111
|
+
Scenario('login', ({ I, loginPage }) => {
|
|
112
|
+
loginPage.sendForm('john@doe.com','123456');
|
|
113
|
+
I.see('Hello, John');
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Also, you can use `async/await` inside a Page Object:
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
const { I } = inject();
|
|
121
|
+
|
|
122
|
+
module.exports = {
|
|
123
|
+
|
|
124
|
+
// setting locators
|
|
125
|
+
container: "//div[@class = 'numbers']",
|
|
126
|
+
mainItem: {
|
|
127
|
+
number: ".//div[contains(@class, 'numbers__main-number')]",
|
|
128
|
+
title: ".//div[contains(@class, 'numbers__main-title-block')]"
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
// introducing methods
|
|
132
|
+
async openMainArticle() {
|
|
133
|
+
I.waitForVisible(this.container)
|
|
134
|
+
let _this = this
|
|
135
|
+
let title;
|
|
136
|
+
await within(this.container, async () => {
|
|
137
|
+
title = await I.grabTextFrom(_this.mainItem.number);
|
|
138
|
+
let subtitle = await I.grabTextFrom(_this.mainItem.title);
|
|
139
|
+
title = title + " " + subtitle.charAt(0).toLowerCase() + subtitle.slice(1);
|
|
140
|
+
await I.click(_this.mainItem.title)
|
|
141
|
+
})
|
|
142
|
+
return title;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
and use them in your tests:
|
|
148
|
+
|
|
149
|
+
```js
|
|
150
|
+
Scenario('login2', async ({ I, loginPage, basePage }) => {
|
|
151
|
+
let title = await mainPage.openMainArticle()
|
|
152
|
+
basePage.pageShouldBeOpened(title)
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Page Objects can be be functions, arrays or classes. When declared as classes you can easily extend them in other page objects.
|
|
157
|
+
|
|
158
|
+
Here is an example of declaring page object as a class:
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
const { expect } = require('chai');
|
|
162
|
+
const { I } = inject();
|
|
163
|
+
|
|
164
|
+
class AttachFile {
|
|
165
|
+
constructor() {
|
|
166
|
+
this.inputFileField = 'input[name=fileUpload]';
|
|
167
|
+
this.fileSize = '.file-size';
|
|
168
|
+
this.fileName = '.file-name'
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async attachFileFrom(path) {
|
|
172
|
+
await I.waitForVisible(this.inputFileField)
|
|
173
|
+
await I.attachFile(this.inputFileField, path)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async hasFileSize(fileSizeText) {
|
|
177
|
+
await I.waitForElement(this.fileSize)
|
|
178
|
+
const size = await I.grabTextFrom(this.fileSize)
|
|
179
|
+
expect(size).toEqual(fileSizeText)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async hasFileSizeInPosition(fileNameText, position) {
|
|
183
|
+
await I.waitNumberOfVisibleElements(this.fileName, position)
|
|
184
|
+
const text = await I.grabTextFrom(this.fileName)
|
|
185
|
+
expect(text[position - 1]).toEqual(fileNameText)
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// For inheritance
|
|
190
|
+
module.exports = new AttachFile();
|
|
191
|
+
module.exports.AttachFile = AttachFile;
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
> ⚠ While building complex page objects it is important to keep all `async` functions to be called with `await`. While CodeceptJS allows to run commands synchronously if async function has `I.grab*` or any custom function that returns a promise it must be called with `await`. If you see `UnhandledPromiseRejectionWarning` it might be caused by async page object function that was called without `await`.
|
|
195
|
+
|
|
196
|
+
## Page Fragments
|
|
197
|
+
|
|
198
|
+
Similarly, CodeceptJS allows you to generate **PageFragments** and any other abstractions
|
|
199
|
+
by running the `go` command with `--type` (or `-t`) option:
|
|
200
|
+
|
|
201
|
+
```sh
|
|
202
|
+
npx codeceptjs go --type fragment
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Page Fragments represent autonomous parts of a page, like modal boxes, components, widgets.
|
|
206
|
+
Technically, they are the same as PageObject but conceptually they are a bit different.
|
|
207
|
+
For instance, it is recommended that Page Fragment includes a root locator of a component.
|
|
208
|
+
Methods of page fragments can use `within` block to narrow scope to a root locator:
|
|
209
|
+
|
|
210
|
+
```js
|
|
211
|
+
const { I } = inject();
|
|
212
|
+
// fragments/modal.js
|
|
213
|
+
module.exports = {
|
|
214
|
+
|
|
215
|
+
root: '#modal',
|
|
216
|
+
|
|
217
|
+
// we are clicking "Accept: inside a popup window
|
|
218
|
+
accept() {
|
|
219
|
+
within(this.root, function() {
|
|
220
|
+
I.click('Accept');
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
To use a Page Fragment within a Test Scenario, just inject it into your Scenario:
|
|
227
|
+
|
|
228
|
+
```js
|
|
229
|
+
Scenario('failed_login', async ({ I, loginPage, modal }) => {
|
|
230
|
+
loginPage.sendForm('john@doe.com','wrong password');
|
|
231
|
+
I.waitForVisible(modal.root);
|
|
232
|
+
within(modal.root, function () {
|
|
233
|
+
I.see('Login failed');
|
|
234
|
+
})
|
|
235
|
+
});
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
To use a Page Fragment within a Page Object, you can use `inject` method to get it by its name.
|
|
239
|
+
|
|
240
|
+
```js
|
|
241
|
+
const { I, modal } = inject();
|
|
242
|
+
|
|
243
|
+
module.exports = {
|
|
244
|
+
doStuff() {
|
|
245
|
+
...
|
|
246
|
+
modal.accept();
|
|
247
|
+
...
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
> PageObject and PageFragment names are declared inside `include` section of `codecept.conf.js`. See [Dependency Injection](#dependency-injection)
|
|
253
|
+
|
|
254
|
+
## StepObjects
|
|
255
|
+
|
|
256
|
+
StepObjects represent complex actions which involve the usage of multiple web pages. For instance, creating users in the backend, changing permissions, etc.
|
|
257
|
+
StepObject can be created similarly to PageObjects or PageFragments:
|
|
258
|
+
|
|
259
|
+
```sh
|
|
260
|
+
npx codeceptjs go --type step
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Technically, they are the same as PageObjects. StepObjects can inject PageObjects and use multiple POs to make a complex scenarios:
|
|
264
|
+
|
|
265
|
+
```js
|
|
266
|
+
const { I, userPage, permissionPage } = inject();
|
|
267
|
+
|
|
268
|
+
module.exports = {
|
|
269
|
+
|
|
270
|
+
createUser(name) {
|
|
271
|
+
// action composed from actions of page objects
|
|
272
|
+
userPage.open();
|
|
273
|
+
userPage.create(name);
|
|
274
|
+
permissionPage.activate(name);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
};
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## Dynamic Injection
|
|
281
|
+
|
|
282
|
+
You can inject objects per test by calling `injectDependencies` function in a Scenario:
|
|
283
|
+
|
|
284
|
+
```js
|
|
285
|
+
Scenario('search @grop', ({ I, Data }) => {
|
|
286
|
+
I.fillField('Username', Data.username);
|
|
287
|
+
I.pressKey('Enter');
|
|
288
|
+
}).injectDependencies({ Data: require('./data.js') });
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
This requires the `./data.js` module and assigns it to a `Data` argument in a test.
|