codeceptjs 3.3.8-beta.1 → 3.4.0
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/CHANGELOG.md +47 -0
- package/README.md +31 -32
- package/docs/advanced.md +48 -24
- package/docs/basics.md +115 -40
- package/docs/best.md +2 -2
- package/docs/build/ApiDataFactory.js +14 -9
- package/docs/build/Appium.js +2 -19
- package/docs/build/FileSystem.js +3 -3
- package/docs/build/GraphQL.js +1 -1
- package/docs/build/GraphQLDataFactory.js +3 -3
- package/docs/build/JSONResponse.js +1 -1
- package/docs/build/Mochawesome.js +1 -1
- package/docs/build/Nightmare.js +1 -1
- package/docs/build/Playwright.js +4 -3
- package/docs/build/Protractor.js +1 -1
- package/docs/build/Puppeteer.js +1 -1
- package/docs/build/REST.js +1 -1
- package/docs/build/TestCafe.js +5 -5
- package/docs/build/WebDriver.js +30 -165
- package/docs/changelog.md +49 -2
- package/docs/helpers/ApiDataFactory.md +6 -6
- package/docs/helpers/FileSystem.md +2 -2
- package/docs/helpers/GraphQLDataFactory.md +2 -2
- package/docs/helpers/Playwright.md +2 -1
- package/docs/index.md +1 -1
- package/docs/plugins.md +42 -125
- package/docs/reports.md +0 -56
- package/docs/tutorial.md +271 -0
- package/docs/typescript.md +2 -8
- package/lib/actor.js +2 -1
- package/lib/cli.js +3 -3
- package/lib/codecept.js +2 -1
- package/lib/command/generate.js +3 -1
- package/lib/command/gherkin/snippets.js +8 -4
- package/lib/command/init.js +0 -8
- package/lib/command/run-workers.js +3 -6
- package/lib/command/utils.js +0 -10
- package/lib/command/workers/runTests.js +2 -2
- package/lib/config.js +5 -1
- package/lib/helper/ApiDataFactory.js +14 -9
- package/lib/helper/Appium.js +2 -19
- package/lib/helper/FileSystem.js +3 -3
- package/lib/helper/GraphQL.js +1 -1
- package/lib/helper/GraphQLDataFactory.js +3 -3
- package/lib/helper/JSONResponse.js +1 -1
- package/lib/helper/Mochawesome.js +1 -1
- package/lib/helper/Nightmare.js +1 -1
- package/lib/helper/Playwright.js +4 -3
- package/lib/helper/Protractor.js +1 -1
- package/lib/helper/Puppeteer.js +1 -1
- package/lib/helper/REST.js +1 -1
- package/lib/helper/TestCafe.js +5 -5
- package/lib/helper/WebDriver.js +30 -165
- package/lib/helper.js +0 -2
- package/lib/interfaces/bdd.js +1 -1
- package/lib/interfaces/featureConfig.js +1 -0
- package/lib/interfaces/gherkin.js +38 -25
- package/lib/listener/exit.js +2 -2
- package/lib/listener/retry.js +67 -0
- package/lib/listener/steps.js +1 -1
- package/lib/listener/timeout.js +47 -10
- package/lib/mochaFactory.js +3 -3
- package/lib/plugin/allure.js +14 -323
- package/lib/plugin/fakerTransform.js +2 -2
- package/lib/recorder.js +1 -1
- package/lib/scenario.js +25 -18
- package/lib/utils.js +6 -0
- package/lib/workers.js +4 -7
- package/package.json +14 -18
- package/typings/index.d.ts +76 -1
- package/typings/promiseBasedTypes.d.ts +12 -12
- package/typings/types.d.ts +95 -262
package/docs/plugins.md
CHANGED
|
@@ -7,83 +7,6 @@ title: Plugins
|
|
|
7
7
|
|
|
8
8
|
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
|
9
9
|
|
|
10
|
-
## allure
|
|
11
|
-
|
|
12
|
-
Allure reporter
|
|
13
|
-
|
|
14
|
-
![][1]
|
|
15
|
-
|
|
16
|
-
Enables Allure reporter.
|
|
17
|
-
|
|
18
|
-
#### Usage
|
|
19
|
-
|
|
20
|
-
To start please install `allure-commandline` package (which requires Java 8)
|
|
21
|
-
|
|
22
|
-
npm install -g allure-commandline --save-dev
|
|
23
|
-
|
|
24
|
-
Add this plugin to config file:
|
|
25
|
-
|
|
26
|
-
```js
|
|
27
|
-
"plugins": {
|
|
28
|
-
"allure": {}
|
|
29
|
-
}
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
Run tests with allure plugin enabled:
|
|
33
|
-
|
|
34
|
-
npx codeceptjs run --plugins allure
|
|
35
|
-
|
|
36
|
-
By default, allure reports are saved to `output` directory.
|
|
37
|
-
Launch Allure server and see the report like on a screenshot above:
|
|
38
|
-
|
|
39
|
-
allure serve output
|
|
40
|
-
|
|
41
|
-
#### Configuration
|
|
42
|
-
|
|
43
|
-
- `outputDir` - a directory where allure reports should be stored. Standard output directory is set by default.
|
|
44
|
-
- `enableScreenshotDiffPlugin` - a boolean flag for add screenshot diff to report.
|
|
45
|
-
To attach, tou need to attach three files to the report - "diff.png", "actual.png", "expected.png".
|
|
46
|
-
See [Allure Screenshot Plugin][2]
|
|
47
|
-
|
|
48
|
-
#### Public API
|
|
49
|
-
|
|
50
|
-
There are few public API methods which can be accessed from other plugins.
|
|
51
|
-
|
|
52
|
-
```js
|
|
53
|
-
const allure = codeceptjs.container.plugins('allure');
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
`allure` object has following methods:
|
|
57
|
-
|
|
58
|
-
- `addAttachment(name, buffer, type)` - add an attachment to current test / suite
|
|
59
|
-
- `addLabel(name, value)` - adds a label to current test
|
|
60
|
-
- `addParameter(kind, name, value)` - adds a parameter to current test
|
|
61
|
-
- `createStep(name, stepFunc)` - create a step, stepFunc could consist an attachment
|
|
62
|
-
Example of usage:
|
|
63
|
-
|
|
64
|
-
```js
|
|
65
|
-
allure.createStep('New created step', () => {
|
|
66
|
-
allure.addAttachment(
|
|
67
|
-
'Request params',
|
|
68
|
-
'{"clientId":123, "name":"Tom", "age":29}',
|
|
69
|
-
'application/json'
|
|
70
|
-
);
|
|
71
|
-
});
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
![Created Step Image][3]
|
|
75
|
-
|
|
76
|
-
- `severity(value)` - adds severity label
|
|
77
|
-
- `epic(value)` - adds epic label
|
|
78
|
-
- `feature(value)` - adds feature label
|
|
79
|
-
- `story(value)` - adds story label
|
|
80
|
-
- `issue(value)` - adds issue label
|
|
81
|
-
- `setDescription(description, type)` - sets a description
|
|
82
|
-
|
|
83
|
-
### Parameters
|
|
84
|
-
|
|
85
|
-
- `config`
|
|
86
|
-
|
|
87
10
|
## autoDelay
|
|
88
11
|
|
|
89
12
|
Sometimes it takes some time for a page to respond to user's actions.
|
|
@@ -449,7 +372,7 @@ Possible config options:
|
|
|
449
372
|
|
|
450
373
|
## customLocator
|
|
451
374
|
|
|
452
|
-
Creates a [custom locator][
|
|
375
|
+
Creates a [custom locator][1] by using special attributes in HTML.
|
|
453
376
|
|
|
454
377
|
If you have a convention to use `data-test-id` or `data-qa` attributes to mark active elements for e2e tests,
|
|
455
378
|
you can enable this plugin to simplify matching elements with these attributes:
|
|
@@ -599,9 +522,9 @@ This method works with WebDriver, Playwright, Puppeteer, Appium helpers.
|
|
|
599
522
|
Function parameter `el` represents a matched element.
|
|
600
523
|
Depending on a helper API of `el` can be different. Refer to API of corresponding browser testing engine for a complete API list:
|
|
601
524
|
|
|
602
|
-
- [Playwright ElementHandle][
|
|
603
|
-
- [Puppeteer][
|
|
604
|
-
- [webdriverio element][
|
|
525
|
+
- [Playwright ElementHandle][2]
|
|
526
|
+
- [Puppeteer][3]
|
|
527
|
+
- [webdriverio element][4]
|
|
605
528
|
|
|
606
529
|
#### Configuration
|
|
607
530
|
|
|
@@ -615,17 +538,17 @@ const eachElement = codeceptjs.container.plugins('eachElement');
|
|
|
615
538
|
|
|
616
539
|
### Parameters
|
|
617
540
|
|
|
618
|
-
- `purpose` **[string][
|
|
541
|
+
- `purpose` **[string][5]**
|
|
619
542
|
- `locator` **CodeceptJS.LocatorOrString**
|
|
620
|
-
- `fn` **[Function][
|
|
543
|
+
- `fn` **[Function][6]**
|
|
621
544
|
|
|
622
|
-
Returns **([Promise][
|
|
545
|
+
Returns **([Promise][7]<any> | [undefined][8])**
|
|
623
546
|
|
|
624
547
|
## fakerTransform
|
|
625
548
|
|
|
626
|
-
Use the [faker.js][
|
|
549
|
+
Use the [faker.js][9] package to generate fake data inside examples on your gherkin tests
|
|
627
550
|
|
|
628
|
-
![Faker.js][
|
|
551
|
+
![Faker.js][10]
|
|
629
552
|
|
|
630
553
|
#### Usage
|
|
631
554
|
|
|
@@ -663,7 +586,7 @@ Scenario Outline: ...
|
|
|
663
586
|
|
|
664
587
|
## pauseOnFail
|
|
665
588
|
|
|
666
|
-
Automatically launches [interactive pause][
|
|
589
|
+
Automatically launches [interactive pause][11] when a test fails.
|
|
667
590
|
|
|
668
591
|
Useful for debugging flaky tests on local environment.
|
|
669
592
|
Add this plugin to config file:
|
|
@@ -846,14 +769,14 @@ Possible config options:
|
|
|
846
769
|
|
|
847
770
|
## selenoid
|
|
848
771
|
|
|
849
|
-
[Selenoid][
|
|
772
|
+
[Selenoid][12] plugin automatically starts browsers and video recording.
|
|
850
773
|
Works with WebDriver helper.
|
|
851
774
|
|
|
852
775
|
### Prerequisite
|
|
853
776
|
|
|
854
777
|
This plugin **requires Docker** to be installed.
|
|
855
778
|
|
|
856
|
-
> If you have issues starting Selenoid with this plugin consider using the official [Configuration Manager][
|
|
779
|
+
> If you have issues starting Selenoid with this plugin consider using the official [Configuration Manager][13] tool from Selenoid
|
|
857
780
|
|
|
858
781
|
### Usage
|
|
859
782
|
|
|
@@ -882,7 +805,7 @@ plugins: {
|
|
|
882
805
|
}
|
|
883
806
|
```
|
|
884
807
|
|
|
885
|
-
When `autoCreate` is enabled it will pull the [latest Selenoid from DockerHub][
|
|
808
|
+
When `autoCreate` is enabled it will pull the [latest Selenoid from DockerHub][14] and start Selenoid automatically.
|
|
886
809
|
It will also create `browsers.json` file required by Selenoid.
|
|
887
810
|
|
|
888
811
|
In automatic mode the latest version of browser will be used for tests. It is recommended to specify exact version of each browser inside `browsers.json` file.
|
|
@@ -894,10 +817,10 @@ In automatic mode the latest version of browser will be used for tests. It is re
|
|
|
894
817
|
While this plugin can create containers for you for better control it is recommended to create and launch containers manually.
|
|
895
818
|
This is especially useful for Continous Integration server as you can configure scaling for Selenoid containers.
|
|
896
819
|
|
|
897
|
-
> Use [Selenoid Configuration Manager][
|
|
820
|
+
> Use [Selenoid Configuration Manager][13] to create and start containers semi-automatically.
|
|
898
821
|
|
|
899
822
|
1. Create `browsers.json` file in the same directory `codecept.conf.js` is located
|
|
900
|
-
[Refer to Selenoid documentation][
|
|
823
|
+
[Refer to Selenoid documentation][15] to know more about browsers.json.
|
|
901
824
|
|
|
902
825
|
_Sample browsers.json_
|
|
903
826
|
|
|
@@ -922,7 +845,7 @@ _Sample browsers.json_
|
|
|
922
845
|
|
|
923
846
|
2. Create Selenoid container
|
|
924
847
|
|
|
925
|
-
Run the following command to create a container. To know more [refer here][
|
|
848
|
+
Run the following command to create a container. To know more [refer here][16]
|
|
926
849
|
|
|
927
850
|
```bash
|
|
928
851
|
docker create \
|
|
@@ -955,7 +878,7 @@ When `allure` plugin is enabled a video is attached to report automatically.
|
|
|
955
878
|
| enableVideo | Enable video recording and use `video` folder of output (default: false) |
|
|
956
879
|
| enableLog | Enable log recording and use `logs` folder of output (default: false) |
|
|
957
880
|
| deletePassed | Delete video and logs of passed tests (default : true) |
|
|
958
|
-
| additionalParams | example: `additionalParams: '--env TEST=test'` [Refer here][
|
|
881
|
+
| additionalParams | example: `additionalParams: '--env TEST=test'` [Refer here][17] to know more |
|
|
959
882
|
|
|
960
883
|
### Parameters
|
|
961
884
|
|
|
@@ -963,7 +886,7 @@ When `allure` plugin is enabled a video is attached to report automatically.
|
|
|
963
886
|
|
|
964
887
|
## stepByStepReport
|
|
965
888
|
|
|
966
|
-
![step-by-step-report][
|
|
889
|
+
![step-by-step-report][18]
|
|
967
890
|
|
|
968
891
|
Generates step by step report for a test.
|
|
969
892
|
After each step in a test a screenshot is created. After test executed screenshots are combined into slideshow.
|
|
@@ -1144,7 +1067,7 @@ This plugin allows to run webdriverio services like:
|
|
|
1144
1067
|
- browserstack
|
|
1145
1068
|
- appium
|
|
1146
1069
|
|
|
1147
|
-
A complete list of all available services can be found on [webdriverio website][
|
|
1070
|
+
A complete list of all available services can be found on [webdriverio website][19].
|
|
1148
1071
|
|
|
1149
1072
|
#### Setup
|
|
1150
1073
|
|
|
@@ -1156,7 +1079,7 @@ See examples below:
|
|
|
1156
1079
|
|
|
1157
1080
|
#### Selenium Standalone Service
|
|
1158
1081
|
|
|
1159
|
-
Install `@wdio/selenium-standalone-service` package, as [described here][
|
|
1082
|
+
Install `@wdio/selenium-standalone-service` package, as [described here][20].
|
|
1160
1083
|
It is important to make sure it is compatible with current webdriverio version.
|
|
1161
1084
|
|
|
1162
1085
|
Enable `wdio` plugin in plugins list and add `selenium-standalone` service:
|
|
@@ -1175,7 +1098,7 @@ Please note, this service can be used with Protractor helper as well!
|
|
|
1175
1098
|
|
|
1176
1099
|
#### Sauce Service
|
|
1177
1100
|
|
|
1178
|
-
Install `@wdio/sauce-service` package, as [described here][
|
|
1101
|
+
Install `@wdio/sauce-service` package, as [described here][21].
|
|
1179
1102
|
It is important to make sure it is compatible with current webdriverio version.
|
|
1180
1103
|
|
|
1181
1104
|
Enable `wdio` plugin in plugins list and add `sauce` service:
|
|
@@ -1205,50 +1128,44 @@ In the same manner additional services from webdriverio can be installed, enable
|
|
|
1205
1128
|
|
|
1206
1129
|
- `config`
|
|
1207
1130
|
|
|
1208
|
-
[1]: https://
|
|
1209
|
-
|
|
1210
|
-
[2]: https://github.com/allure-framework/allure2/blob/master/plugins/screen-diff-plugin/README.md
|
|
1211
|
-
|
|
1212
|
-
[3]: https://user-images.githubusercontent.com/63167966/139339384-e6e70a62-3638-406d-a224-f32473071428.png
|
|
1213
|
-
|
|
1214
|
-
[4]: https://codecept.io/locators#custom-locators
|
|
1131
|
+
[1]: https://codecept.io/locators#custom-locators
|
|
1215
1132
|
|
|
1216
|
-
[
|
|
1133
|
+
[2]: https://playwright.dev/docs/api/class-elementhandle
|
|
1217
1134
|
|
|
1218
|
-
[
|
|
1135
|
+
[3]: https://pptr.dev/#?product=Puppeteer&show=api-class-elementhandle
|
|
1219
1136
|
|
|
1220
|
-
[
|
|
1137
|
+
[4]: https://webdriver.io/docs/api
|
|
1221
1138
|
|
|
1222
|
-
[
|
|
1139
|
+
[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
|
|
1223
1140
|
|
|
1224
|
-
[
|
|
1141
|
+
[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
|
|
1225
1142
|
|
|
1226
|
-
[
|
|
1143
|
+
[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise
|
|
1227
1144
|
|
|
1228
|
-
[
|
|
1145
|
+
[8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined
|
|
1229
1146
|
|
|
1230
|
-
[
|
|
1147
|
+
[9]: https://www.npmjs.com/package/faker
|
|
1231
1148
|
|
|
1232
|
-
[
|
|
1149
|
+
[10]: https://raw.githubusercontent.com/Marak/faker.js/master/logo.png
|
|
1233
1150
|
|
|
1234
|
-
[
|
|
1151
|
+
[11]: /basics/#pause
|
|
1235
1152
|
|
|
1236
|
-
[
|
|
1153
|
+
[12]: https://aerokube.com/selenoid/
|
|
1237
1154
|
|
|
1238
|
-
[
|
|
1155
|
+
[13]: https://aerokube.com/cm/latest/
|
|
1239
1156
|
|
|
1240
|
-
[
|
|
1157
|
+
[14]: https://hub.docker.com/u/selenoid
|
|
1241
1158
|
|
|
1242
|
-
[
|
|
1159
|
+
[15]: https://aerokube.com/selenoid/latest/#_prepare_configuration
|
|
1243
1160
|
|
|
1244
|
-
[
|
|
1161
|
+
[16]: https://aerokube.com/selenoid/latest/#_option_2_start_selenoid_container
|
|
1245
1162
|
|
|
1246
|
-
[
|
|
1163
|
+
[17]: https://docs.docker.com/engine/reference/commandline/create/
|
|
1247
1164
|
|
|
1248
|
-
[
|
|
1165
|
+
[18]: https://codecept.io/img/codeceptjs-slideshow.gif
|
|
1249
1166
|
|
|
1250
|
-
[
|
|
1167
|
+
[19]: https://webdriver.io
|
|
1251
1168
|
|
|
1252
|
-
[
|
|
1169
|
+
[20]: https://webdriver.io/docs/selenium-standalone-service.html
|
|
1253
1170
|
|
|
1254
|
-
[
|
|
1171
|
+
[21]: https://webdriver.io/docs/sauce-service.html
|
package/docs/reports.md
CHANGED
|
@@ -183,62 +183,6 @@ plugins: {
|
|
|
183
183
|
[Testomat.io](https://testomat.io) reporter works in the cloud, so it doesn't require you to install additional software. It can be integrated with your CI service to rerun only failed tests, launch new runs from UI, and send report notifications by email or in Slack, MS Teams, or create issue in Jira.
|
|
184
184
|
|
|
185
185
|
|
|
186
|
-
## Allure
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
[Allure reporter](https://allure.qatools.ru/#) is a tool to store and display test reports.
|
|
190
|
-
It provides nice web UI which contains all important information on test execution.
|
|
191
|
-
CodeceptJS has built-in support for Allure reports. Inside reports you will have all steps, substeps and screenshots.
|
|
192
|
-
|
|
193
|
-

|
|
194
|
-
|
|
195
|
-
> ▶ Allure is a standalone tool. Please refer to [Allure documentation](https://docs.qameta.io/allure/) to learn more about using Allure reports.
|
|
196
|
-
|
|
197
|
-
Allure requires **Java 8** to work. Then Allure can be installed via NPM:
|
|
198
|
-
|
|
199
|
-
```
|
|
200
|
-
npm install -g allure-commandline --save-dev
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
Add [Allure plugin](/plugins/#allure) in config under `plugins` section.
|
|
204
|
-
|
|
205
|
-
```js
|
|
206
|
-
plugins: {
|
|
207
|
-
allure: {
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
Run tests with allure plugin enabled:
|
|
213
|
-
|
|
214
|
-
```
|
|
215
|
-
npx codeceptjs run --plugins allure
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
(optionally) To enable allure plugin permanently include `"enabled": true` into plugin config:
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
```js
|
|
222
|
-
"plugins": {
|
|
223
|
-
"allure": {
|
|
224
|
-
"enabled": true
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
Launch Allure server and see the report like on a screenshot above:
|
|
230
|
-
|
|
231
|
-
```
|
|
232
|
-
allure serve output
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
Allure reporter aggregates data from other plugins like [*stepByStepReport*](/plugins/#stepByStepReport) and [*screenshotOnFail*](/plugins/#screenshotOnFail)
|
|
236
|
-
|
|
237
|
-
Allure reports can also be generated for `dry-run` command. So you can get the first report with no tests actually being executed. Enable allure plugin in dry-run options, and pass `--debug` option to print all tests on screen.
|
|
238
|
-
|
|
239
|
-
```
|
|
240
|
-
npx codeceptjs dry-run --debug -p allure
|
|
241
|
-
```
|
|
242
186
|
|
|
243
187
|
## ReportPortal
|
|
244
188
|
|
package/docs/tutorial.md
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
---
|
|
2
|
+
permalink: /tutorial
|
|
3
|
+
title: CoeceptJS Complete Tutorial
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
**[CodeceptJS](https://codecept.io) is a popular open-source testing framework** for JavaScript. It is designed to simplify writing and maintain end-to-end tests for web applications, using a readable and intuitive syntax. To run tests in browser it uses **[Playwright](https://playwright.dev)** library from Microsoft.
|
|
7
|
+
|
|
8
|
+
CodeceptJS was started in 2015 and is widely used by organizations of all sizes, from startups to large enterprises.
|
|
9
|
+
|
|
10
|
+
## Let's get CodeceptJS installed!
|
|
11
|
+
|
|
12
|
+
To install CodeceptJS, you will need to have Node.js and npm (the Node.js package manager) installed on your system. You can check if you already have these tools installed by running the following commands in a terminal:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
node --version
|
|
16
|
+
npm --version
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
If either of these commands return an error, you will need to install Node.js and npm before you can install CodeceptJS. You can download and install the latest version of Node.js from the official website, which includes npm.
|
|
20
|
+
|
|
21
|
+
To install CodeceptJS create a new folder and run command form terminal:
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
npx create-codeceptjs .
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
If you run the npx create-codeceptjs . command, it will install CodeceptJS with Playwright in the current directory.
|
|
28
|
+
|
|
29
|
+
> The `npx` command is a tool that comes with npm (the Node.js package manager) and it allows you to run npm packages without having to install them globally on your system.
|
|
30
|
+
|
|
31
|
+
It may take some time as it downloads browsers: Chrome, Firefox and Safari and creates a demo project.
|
|
32
|
+
|
|
33
|
+
But we are here to write a checkout test, right?
|
|
34
|
+
|
|
35
|
+
Let's initialize a new project for that!
|
|
36
|
+
|
|
37
|
+
Run
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
npx codeceptjs init
|
|
41
|
+
```
|
|
42
|
+
Agree on defaults (press Enter for every question asked). When asked for base site URL, provide a URL of a ecommerce website you are testing. For instance, it could be: `https://myshop.com` if you test already published website or `http://localhost` if you run the website locally.
|
|
43
|
+
|
|
44
|
+
When asked for a test name and suite name write "Checkout". It will create the following dirctory structure:
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
.
|
|
48
|
+
├── codecept.conf.js
|
|
49
|
+
├── package.json
|
|
50
|
+
└── Checkout_test.js
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The `codecept.conf.js` file in the root of the project directory contains the global configuration settings for CodeceptJS.
|
|
54
|
+
|
|
55
|
+
Now open a test:
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
Feature('Checkout');
|
|
59
|
+
|
|
60
|
+
Scenario('test something', ({ I }) => {
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
Inside the Scenario block you write a test.
|
|
64
|
+
|
|
65
|
+
Add `I.amOnPage('/')` into it. It will open the browser on a URL you specified as a base.
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
Feature('Checkout');
|
|
69
|
+
|
|
70
|
+
Scenario('test something', ({ I }) => {
|
|
71
|
+
I.amOnPage('/')
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
But you may want to ask...
|
|
75
|
+
|
|
76
|
+
## What is I?
|
|
77
|
+
|
|
78
|
+
Glad you asked!
|
|
79
|
+
|
|
80
|
+
In CodeceptJS, the `I` object is used to represent the user performing actions in a test scenario. It provides a number of methods (also known as actions) that can be used to simulate user interactions with the application under test.
|
|
81
|
+
|
|
82
|
+
Some of the most popular actions of the I object are:
|
|
83
|
+
|
|
84
|
+
* `I.amOnPage(url)`: This action navigates the user to the specified URL.
|
|
85
|
+
* `I.click(locator)`: This action simulates a click on the element identified by the given locator.
|
|
86
|
+
* `I.fillField(field, value)`: This action fills the specified field with the given value.
|
|
87
|
+
* `I.see(text, context)`: This action checks that the given text is visible on the page (or in the specified context).
|
|
88
|
+
* `I.selectOption(select, option)`: This action selects the specified option from the given select dropdown.
|
|
89
|
+
* `I.waitForElement(locator, timeout)`: This action waits for the specified element to appear on the page, up to the given timeout.
|
|
90
|
+
* `I.waitForText(text, timeout, context)`: This action waits for the given text to appear on the page (or in the specified context), up to the given timeout.
|
|
91
|
+
|
|
92
|
+
We will need to use them to navigate into Checkout process. How do we navigate web? Sure by clicking on links!
|
|
93
|
+
|
|
94
|
+
Let's use `I.click()` for that.
|
|
95
|
+
|
|
96
|
+
But how we can access elements on a webpage?
|
|
97
|
+
|
|
98
|
+
CodeceptJS is smart enough to locate clickable elements by their visible text. For instance, if on your ecommerce website you have a product 'Coffee Cup' with that exact name you can use
|
|
99
|
+
|
|
100
|
+
```js
|
|
101
|
+
I.click('Coffee Cup');
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
But sometimes elements are not as easy to locate, so you can use CSS or XPath locators to locate them.
|
|
105
|
+
|
|
106
|
+
For instance, locating Coffee Cup via CSS can take into accont HTML structure of a page and element attributes. For instance, it can be like this:
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
I.click('div.products a.product-name[title="Coffee Cup"]');
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
In this example, the `div.products` part of the selector specifies a div element with the `products` class, and the `a.product-name[title="Coffee Cup"]` part specifies an a element with `the product-name` class and the `title` attribute set to Coffee Cup.
|
|
113
|
+
|
|
114
|
+
You can read more about HTML and CSS locators, and basically that's all what you need to know to start writing a checkout test!
|
|
115
|
+
|
|
116
|
+
## Get back to Checkout
|
|
117
|
+
|
|
118
|
+
Let's see how a regular checkout script may look in CodeceptJS:
|
|
119
|
+
|
|
120
|
+
```js
|
|
121
|
+
Scenario('test the checkout form', async ({ I }) => {
|
|
122
|
+
// we select one product and switched to checkout project
|
|
123
|
+
I.amOnPage('/');
|
|
124
|
+
I.click('Coffee Cup');
|
|
125
|
+
I.click('Purchase');
|
|
126
|
+
I.click('Checkout');
|
|
127
|
+
|
|
128
|
+
// fill in the shipping address
|
|
129
|
+
I.fillField('First Name', 'John');
|
|
130
|
+
I.fillField('Last Name', 'Doe');
|
|
131
|
+
I.fillField('Address', '123 Main St.');
|
|
132
|
+
I.fillField('City', 'New York');
|
|
133
|
+
I.selectOption('State', 'New York');
|
|
134
|
+
I.fillField('Zip Code', '10001');
|
|
135
|
+
|
|
136
|
+
// select a payment method
|
|
137
|
+
I.click('#credit-card-option');
|
|
138
|
+
I.fillField('Card Number', '1234-5678-9012-3456');
|
|
139
|
+
I.fillField('Expiration Date', '12/22');
|
|
140
|
+
I.fillField('Security Code', '123');
|
|
141
|
+
|
|
142
|
+
// click the checkout button
|
|
143
|
+
I.click('Checkout');
|
|
144
|
+
|
|
145
|
+
// verify that the checkout was successful
|
|
146
|
+
I.see('Your order has been placed successfully!');
|
|
147
|
+
});
|
|
148
|
+
```
|
|
149
|
+
Sure, in relaity your script might be more complicated. As you have noticed, we used CSS locator `'#credit-card-option'` to get select a payment option. However, the test is simple and you can follow user steps through it.
|
|
150
|
+
|
|
151
|
+
Please note, that you shouldn't use a real credit card number here. Good news, you don't need to. Payment providers like Strip provide dummy card numbers for testing purposes.
|
|
152
|
+
|
|
153
|
+
Run the test with next command:
|
|
154
|
+
|
|
155
|
+
```
|
|
156
|
+
npx codeceptjs run --debug -p pauseOnFail
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
What are special options here?
|
|
160
|
+
|
|
161
|
+
* `--debug` flag is used to output additional information to the console, such as the details of each step in the test, the values of variables, and the results of test assertions. This can help you to identify and fix any issues in your tests.
|
|
162
|
+
* `-p pauseOnFail` option is also used to keep the browser opened even if a test fails. It will help us to identify to which point test was executed and what can be improved.
|
|
163
|
+
|
|
164
|
+
Add more test steps if needed, update locators, and notify business owners that all that purchases are made by you so your collegues won't call you in the night asking when you want to get a coffee cup 😀 Also the good idea is to run tests on staging website, to not interfere with business process.
|
|
165
|
+
|
|
166
|
+
What a test is complete you can run it with:
|
|
167
|
+
|
|
168
|
+
```
|
|
169
|
+
npx codeceptjs run
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
If you are annoyed to see a browser window you can use `HEADLESS` environment variable:
|
|
173
|
+
|
|
174
|
+
```
|
|
175
|
+
HEADLESS=true codeceptjs run
|
|
176
|
+
```
|
|
177
|
+
for Windows users HEADLESS should be set in a different manner:
|
|
178
|
+
|
|
179
|
+
```
|
|
180
|
+
set HEADLESS=true&& codeceptjs run
|
|
181
|
+
```
|
|
182
|
+
The tests will pass but no browser is shown, so you can watch YouTube videos while it goes!
|
|
183
|
+
|
|
184
|
+
## Refactoring
|
|
185
|
+
|
|
186
|
+
What if you need to check more purchases? Should you copy paste your code for that?
|
|
187
|
+
|
|
188
|
+
No! You can use Page Object pattern to put repeating interactions into the reusable functions.
|
|
189
|
+
|
|
190
|
+
You can create a page object via next command:
|
|
191
|
+
|
|
192
|
+
```
|
|
193
|
+
npx codeceptjs gpo
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
Sure, we will call it `Checkout`. It will be created in `./pages/Checkout.js` file. You should enable it in `codecept.conf.js` inside `include` section:
|
|
197
|
+
|
|
198
|
+
```js
|
|
199
|
+
include: {
|
|
200
|
+
...
|
|
201
|
+
checkoutPage: './pages/Checkout.js',
|
|
202
|
+
},
|
|
203
|
+
|
|
204
|
+
```
|
|
205
|
+
Now open this file:
|
|
206
|
+
|
|
207
|
+
```js
|
|
208
|
+
const { I } = inject();
|
|
209
|
+
|
|
210
|
+
module.exports = {
|
|
211
|
+
|
|
212
|
+
// insert your locators and methods here
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Feels really empty. What should we do about it? Should we write more code? No, we already have it. Let's copy code blocks from a test we have it and place them under a corredponnding function names:
|
|
217
|
+
|
|
218
|
+
```js
|
|
219
|
+
connst { I } = inject();
|
|
220
|
+
|
|
221
|
+
module.exports = {
|
|
222
|
+
|
|
223
|
+
fillShippingAddress(name, address, city, state, zip) {
|
|
224
|
+
I.fillField('Name', name);
|
|
225
|
+
I.fillField('Address', address);
|
|
226
|
+
I.fillField('City', city);
|
|
227
|
+
I.fillField('State', state);
|
|
228
|
+
I.fillField('Zip', zip);
|
|
229
|
+
},
|
|
230
|
+
|
|
231
|
+
fillValidCreditCard() {
|
|
232
|
+
I.click('#credit-card-option');
|
|
233
|
+
I.fillField('Card Number', '1234-5678-9012-3456');
|
|
234
|
+
I.fillField('Expiration Date', '12/22');
|
|
235
|
+
I.fillField('Security Code', '123');
|
|
236
|
+
},
|
|
237
|
+
|
|
238
|
+
checkout() {
|
|
239
|
+
I.click('Checkout');
|
|
240
|
+
},
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
After that we can update our test to use the created page object. Note, that we import Checkout PageObject by its name `checkoutPage` we previously defined in a config.
|
|
245
|
+
|
|
246
|
+
```js
|
|
247
|
+
Scenario('test the checkout form', async ({I, checkoutPage}) => {
|
|
248
|
+
I.amOnPage('/');
|
|
249
|
+
I.click('Coffee Cup');
|
|
250
|
+
I.click('Purchase');
|
|
251
|
+
I.click('Checkout');
|
|
252
|
+
|
|
253
|
+
// fill in the shipping address using the page object
|
|
254
|
+
checkoutPage.fillShippingAddress('John', 'Doe', '123 Main St.', 'New York', 'New York', '10001');
|
|
255
|
+
checkoutPage.fillValidCreditCard();
|
|
256
|
+
checkoutPage.checkout();
|
|
257
|
+
|
|
258
|
+
// verify that the checkout was successful
|
|
259
|
+
I.see('Your order has been placed successfully!');
|
|
260
|
+
});
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
As you see the code of a test was reduced. And we can write the similar tests on the same manner.
|
|
264
|
+
|
|
265
|
+
By applying more and more cases you can test a website to all behaviors.
|
|
266
|
+
|
|
267
|
+
## Summary
|
|
268
|
+
|
|
269
|
+
This was a deep dive! If you think on just starting test automation, CodeceptJS is the best choice for you as it uses native language to pass commands to browser.
|
|
270
|
+
|
|
271
|
+
If you already skilled in JavaScript, with CodeceptJS you can focus on business level of your test, instead of writing code for browser. This way you can keep your tests stable and maintainable.
|
package/docs/typescript.md
CHANGED
|
@@ -41,16 +41,10 @@ Then select TypeScript as the first question:
|
|
|
41
41
|
|
|
42
42
|
Then a config file and new tests will be created in TypeScript format.
|
|
43
43
|
|
|
44
|
-
If a config file is set in TypeScript format (`codecept.conf.ts`) package `node
|
|
44
|
+
If a config file is set in TypeScript format (`codecept.conf.ts`) package `ts-node` will be used to run tests.
|
|
45
45
|
|
|
46
46
|
## Promise-Based Typings
|
|
47
47
|
|
|
48
|
-
If you plan to write tests in TypeScript you will probably want to enable "promise-based typings" as you will be asked in `init` command about it:
|
|
49
|
-
|
|
50
|
-
```js
|
|
51
|
-
? Would you prefer to use promise-based typings and explicitly use `await` for all I.* commands?
|
|
52
|
-
```
|
|
53
|
-
|
|
54
48
|
By default, CodeceptJS tests are written in synchronous mode. This is a regular CodeceptJS test:
|
|
55
49
|
|
|
56
50
|
```js
|
|
@@ -82,7 +76,7 @@ Otherwise they will still return promises but it won't be relfected in type defi
|
|
|
82
76
|
To introduce promise-based typings into a current project edit `codecept.conf.ts`:
|
|
83
77
|
|
|
84
78
|
```ts
|
|
85
|
-
|
|
79
|
+
fullPromiseBased: true;
|
|
86
80
|
```
|
|
87
81
|
|
|
88
82
|
and rebuild type definitions with
|
package/lib/actor.js
CHANGED
|
@@ -16,8 +16,9 @@ class Actor {
|
|
|
16
16
|
* add print comment method`
|
|
17
17
|
* @param {string} msg
|
|
18
18
|
* @param {string} color
|
|
19
|
-
* @return {Promise<any> | undefined}
|
|
20
19
|
* @inner
|
|
20
|
+
*
|
|
21
|
+
* ⚠️ returns a promise which is synchronized internally by recorder
|
|
21
22
|
*/
|
|
22
23
|
say(msg, color = 'cyan') {
|
|
23
24
|
return recorder.add(`say ${msg}`, () => {
|