codeceptjs 3.5.12-beta.1 → 3.5.12-beta.2
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/docs/advanced.md +351 -0
- package/docs/ai.md +248 -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/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/OpenAI.js +126 -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/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 +1259 -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/webapi/amOnPage.mustache +11 -0
- package/docs/webapi/appendField.mustache +11 -0
- package/docs/webapi/attachFile.mustache +12 -0
- package/docs/webapi/blur.mustache +18 -0
- package/docs/webapi/checkOption.mustache +13 -0
- package/docs/webapi/clearCookie.mustache +9 -0
- package/docs/webapi/clearField.mustache +9 -0
- package/docs/webapi/click.mustache +25 -0
- package/docs/webapi/clickLink.mustache +8 -0
- package/docs/webapi/closeCurrentTab.mustache +7 -0
- package/docs/webapi/closeOtherTabs.mustache +8 -0
- package/docs/webapi/dontSee.mustache +11 -0
- package/docs/webapi/dontSeeCheckboxIsChecked.mustache +10 -0
- package/docs/webapi/dontSeeCookie.mustache +8 -0
- package/docs/webapi/dontSeeCurrentUrlEquals.mustache +10 -0
- package/docs/webapi/dontSeeElement.mustache +8 -0
- package/docs/webapi/dontSeeElementInDOM.mustache +8 -0
- package/docs/webapi/dontSeeInCurrentUrl.mustache +4 -0
- package/docs/webapi/dontSeeInField.mustache +11 -0
- package/docs/webapi/dontSeeInSource.mustache +8 -0
- package/docs/webapi/dontSeeInTitle.mustache +8 -0
- package/docs/webapi/doubleClick.mustache +13 -0
- package/docs/webapi/downloadFile.mustache +12 -0
- package/docs/webapi/dragAndDrop.mustache +9 -0
- package/docs/webapi/dragSlider.mustache +11 -0
- package/docs/webapi/executeAsyncScript.mustache +24 -0
- package/docs/webapi/executeScript.mustache +26 -0
- package/docs/webapi/fillField.mustache +16 -0
- package/docs/webapi/focus.mustache +13 -0
- package/docs/webapi/forceClick.mustache +28 -0
- package/docs/webapi/forceRightClick.mustache +18 -0
- package/docs/webapi/grabAllWindowHandles.mustache +7 -0
- package/docs/webapi/grabAttributeFrom.mustache +10 -0
- package/docs/webapi/grabAttributeFromAll.mustache +9 -0
- package/docs/webapi/grabBrowserLogs.mustache +9 -0
- package/docs/webapi/grabCookie.mustache +11 -0
- package/docs/webapi/grabCssPropertyFrom.mustache +11 -0
- package/docs/webapi/grabCssPropertyFromAll.mustache +10 -0
- package/docs/webapi/grabCurrentUrl.mustache +9 -0
- package/docs/webapi/grabCurrentWindowHandle.mustache +6 -0
- package/docs/webapi/grabDataFromPerformanceTiming.mustache +20 -0
- package/docs/webapi/grabElementBoundingRect.mustache +20 -0
- package/docs/webapi/grabGeoLocation.mustache +8 -0
- package/docs/webapi/grabHTMLFrom.mustache +10 -0
- package/docs/webapi/grabHTMLFromAll.mustache +9 -0
- package/docs/webapi/grabNumberOfOpenTabs.mustache +8 -0
- package/docs/webapi/grabNumberOfVisibleElements.mustache +9 -0
- package/docs/webapi/grabPageScrollPosition.mustache +8 -0
- package/docs/webapi/grabPopupText.mustache +5 -0
- package/docs/webapi/grabSource.mustache +8 -0
- package/docs/webapi/grabTextFrom.mustache +10 -0
- package/docs/webapi/grabTextFromAll.mustache +9 -0
- package/docs/webapi/grabTitle.mustache +8 -0
- package/docs/webapi/grabValueFrom.mustache +9 -0
- package/docs/webapi/grabValueFromAll.mustache +8 -0
- package/docs/webapi/grabWebElement.mustache +9 -0
- package/docs/webapi/grabWebElements.mustache +9 -0
- package/docs/webapi/moveCursorTo.mustache +12 -0
- package/docs/webapi/openNewTab.mustache +7 -0
- package/docs/webapi/pressKey.mustache +12 -0
- package/docs/webapi/pressKeyDown.mustache +12 -0
- package/docs/webapi/pressKeyUp.mustache +12 -0
- package/docs/webapi/pressKeyWithKeyNormalization.mustache +60 -0
- package/docs/webapi/refreshPage.mustache +6 -0
- package/docs/webapi/resizeWindow.mustache +6 -0
- package/docs/webapi/rightClick.mustache +14 -0
- package/docs/webapi/saveElementScreenshot.mustache +10 -0
- package/docs/webapi/saveScreenshot.mustache +12 -0
- package/docs/webapi/say.mustache +10 -0
- package/docs/webapi/scrollIntoView.mustache +11 -0
- package/docs/webapi/scrollPageToBottom.mustache +6 -0
- package/docs/webapi/scrollPageToTop.mustache +6 -0
- package/docs/webapi/scrollTo.mustache +12 -0
- package/docs/webapi/see.mustache +11 -0
- package/docs/webapi/seeAttributesOnElements.mustache +9 -0
- package/docs/webapi/seeCheckboxIsChecked.mustache +10 -0
- package/docs/webapi/seeCookie.mustache +8 -0
- package/docs/webapi/seeCssPropertiesOnElements.mustache +9 -0
- package/docs/webapi/seeCurrentUrlEquals.mustache +11 -0
- package/docs/webapi/seeElement.mustache +8 -0
- package/docs/webapi/seeElementInDOM.mustache +8 -0
- package/docs/webapi/seeInCurrentUrl.mustache +8 -0
- package/docs/webapi/seeInField.mustache +12 -0
- package/docs/webapi/seeInPopup.mustache +8 -0
- package/docs/webapi/seeInSource.mustache +7 -0
- package/docs/webapi/seeInTitle.mustache +8 -0
- package/docs/webapi/seeNumberOfElements.mustache +11 -0
- package/docs/webapi/seeNumberOfVisibleElements.mustache +10 -0
- package/docs/webapi/seeTextEquals.mustache +9 -0
- package/docs/webapi/seeTitleEquals.mustache +8 -0
- package/docs/webapi/selectOption.mustache +21 -0
- package/docs/webapi/setCookie.mustache +16 -0
- package/docs/webapi/setGeoLocation.mustache +12 -0
- package/docs/webapi/switchTo.mustache +9 -0
- package/docs/webapi/switchToNextTab.mustache +10 -0
- package/docs/webapi/switchToPreviousTab.mustache +10 -0
- package/docs/webapi/type.mustache +21 -0
- package/docs/webapi/uncheckOption.mustache +13 -0
- package/docs/webapi/wait.mustache +8 -0
- package/docs/webapi/waitForClickable.mustache +11 -0
- package/docs/webapi/waitForDetached.mustache +10 -0
- package/docs/webapi/waitForElement.mustache +11 -0
- package/docs/webapi/waitForEnabled.mustache +6 -0
- package/docs/webapi/waitForFunction.mustache +17 -0
- package/docs/webapi/waitForInvisible.mustache +10 -0
- package/docs/webapi/waitForNumberOfTabs.mustache +9 -0
- package/docs/webapi/waitForText.mustache +13 -0
- package/docs/webapi/waitForValue.mustache +10 -0
- package/docs/webapi/waitForVisible.mustache +10 -0
- package/docs/webapi/waitInUrl.mustache +9 -0
- package/docs/webapi/waitNumberOfVisibleElements.mustache +10 -0
- package/docs/webapi/waitToHide.mustache +10 -0
- package/docs/webapi/waitUrlEquals.mustache +10 -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/css2xpath/js/css_to_xpath.js +20 -0
- package/lib/css2xpath/js/expression.js +23 -0
- package/lib/css2xpath/js/renderer.js +239 -0
- package/lib/locator.js +15 -1
- package/package.json +12 -9
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
---
|
|
2
|
+
permalink: /playwright
|
|
3
|
+
title: Testing with Playwright
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Testing with Playwright
|
|
7
|
+
|
|
8
|
+
Playwright is a Node library to automate the [Chromium](https://www.chromium.org/Home), [WebKit](https://webkit.org/) and [Firefox](https://www.mozilla.org/en-US/firefox/new/) browsers as well as [Electron](https://www.electronjs.org/) apps with a single API. It enables **cross-browser** web automation that is **ever-green**, **capable**, **reliable** and **fast**.
|
|
9
|
+
|
|
10
|
+
Playwright was built similarly to [Puppeteer](https://github.com/puppeteer/puppeteer), using its API and so is very different in usage. However, Playwright has cross browser support with better design for test automation.
|
|
11
|
+
|
|
12
|
+
Take a look at a sample test:
|
|
13
|
+
|
|
14
|
+
```js
|
|
15
|
+
I.amOnPage('https://github.com');
|
|
16
|
+
I.click('Sign in', '//html/body/div[1]/header');
|
|
17
|
+
I.see('Sign in to GitHub', 'h1');
|
|
18
|
+
I.fillField('Username or email address', 'something@totest.com');
|
|
19
|
+
I.fillField('Password', '123456');
|
|
20
|
+
I.click('Sign in');
|
|
21
|
+
I.see('Incorrect username or password.', '.flash-error');
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
It's readable and simple and working using Playwright API!
|
|
25
|
+
|
|
26
|
+
## Setup
|
|
27
|
+
|
|
28
|
+
To start you need CodeceptJS with Playwright packages installed
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install codeceptjs playwright --save
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Or see [alternative installation options](https://codecept.io/installation/)
|
|
35
|
+
|
|
36
|
+
> If you already have CodeceptJS project, just install `playwright` package and enable a helper it in config.
|
|
37
|
+
|
|
38
|
+
And a basic project initialized
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
npx codeceptjs init
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
You will be asked for a Helper to use, you should select Playwright and provide url of a website you are testing.
|
|
45
|
+
|
|
46
|
+
## Configuring
|
|
47
|
+
|
|
48
|
+
Make sure `Playwright` helper is enabled in `codecept.conf.js` config:
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
{ // ..
|
|
52
|
+
helpers: {
|
|
53
|
+
Playwright: {
|
|
54
|
+
url: "http://localhost",
|
|
55
|
+
show: true,
|
|
56
|
+
browser: 'chromium'
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// ..
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
> Turn off the `show` option if you want to run test in headless mode.
|
|
64
|
+
> If you don't specify the browser here, `chromium` will be used. Possible browsers are: `chromium`, `firefox` and `webkit`
|
|
65
|
+
|
|
66
|
+
Playwright uses different strategies to detect if a page is loaded. In configuration use `waitForNavigation` option for that:
|
|
67
|
+
|
|
68
|
+
When to consider navigation succeeded, defaults to `load`. Given an array of event strings, navigation is considered to be successful after all events have been fired. Events can be either:
|
|
69
|
+
- `load` - consider navigation to be finished when the load event is fired.
|
|
70
|
+
- `domcontentloaded` - consider navigation to be finished when the DOMContentLoaded event is fired.
|
|
71
|
+
- `networkidle` - consider navigation to be finished when there are no network connections for at least 500 ms.
|
|
72
|
+
|
|
73
|
+
```js
|
|
74
|
+
helpers: {
|
|
75
|
+
Playwright: {
|
|
76
|
+
url: "http://localhost",
|
|
77
|
+
show: true,
|
|
78
|
+
browser: 'chromium',
|
|
79
|
+
waitForNavigation: "networkidle0"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
When a test runs faster than application it is recommended to increase `waitForAction` config value.
|
|
85
|
+
It will wait for a small amount of time (100ms) by default after each user action is taken.
|
|
86
|
+
|
|
87
|
+
> ▶ More options are listed in [helper reference](https://codecept.io/helpers/Playwright/).
|
|
88
|
+
|
|
89
|
+
## Writing Tests
|
|
90
|
+
|
|
91
|
+
Additional CodeceptJS tests should be created with `gt` command:
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
npx codeceptjs gt
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
As an example we will use `ToDoMvc` app for testing.
|
|
98
|
+
|
|
99
|
+
### Actions
|
|
100
|
+
|
|
101
|
+
Tests consist with a scenario of user's action taken on a page. The most widely used ones are:
|
|
102
|
+
|
|
103
|
+
* `amOnPage` - to open a webpage (accepts relative or absolute url)
|
|
104
|
+
* `click` - to locate a button or link and click on it
|
|
105
|
+
* `fillField` - to enter a text inside a field
|
|
106
|
+
* `selectOption`, `checkOption` - to interact with a form
|
|
107
|
+
* `wait*` to wait for some parts of page to be fully rendered (important for testing SPA)
|
|
108
|
+
* `grab*` to get values from page sources
|
|
109
|
+
* `see`, `dontSee` - to check for a text on a page
|
|
110
|
+
* `seeElement`, `dontSeeElement` - to check for elements on a page
|
|
111
|
+
|
|
112
|
+
> ℹ All actions are listed in [Playwright helper reference](https://codecept.io/helpers/Playwright/).*
|
|
113
|
+
|
|
114
|
+
All actions which interact with elements can use **[CSS or XPath locators](https://codecept.io/locators/#css-and-xpath)**. Actions like `click` or `fillField` can locate elements by their name or value on a page:
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
// search for link or button
|
|
118
|
+
I.click('Login');
|
|
119
|
+
// locate field by its label
|
|
120
|
+
I.fillField('Name', 'Miles');
|
|
121
|
+
// we can use input name
|
|
122
|
+
I.fillField('user[email]','miles@davis.com');
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
You can also specify the exact locator type with strict locators:
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
I.click({css: 'button.red'});
|
|
129
|
+
I.fillField({name: 'user[email]'},'miles@davis.com');
|
|
130
|
+
I.seeElement({xpath: '//body/header'});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Interactive Pause
|
|
134
|
+
|
|
135
|
+
It's easy to start writing a test if you use [interactive pause](/basics#debug). Just open a web page and pause execution.
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
Feature('Sample Test');
|
|
139
|
+
|
|
140
|
+
Scenario('open my website', ({ I }) => {
|
|
141
|
+
I.amOnPage('http://todomvc.com/examples/react/');
|
|
142
|
+
pause();
|
|
143
|
+
});
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
This is just enough to run a test, open a browser, and think what to do next to write a test case.
|
|
147
|
+
|
|
148
|
+
When you execute such test with `codeceptjs run` command you may see the browser is started
|
|
149
|
+
|
|
150
|
+
```
|
|
151
|
+
npx codeceptjs run --steps
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
After a page is opened a full control of a browser is given to a terminal. Type in different commands such as `click`, `see`, `fillField` to write the test. A successful commands will be saved to `./output/cli-history` file and can be copied into a test.
|
|
155
|
+
|
|
156
|
+
A complete ToDo-MVC test may look like:
|
|
157
|
+
|
|
158
|
+
```js
|
|
159
|
+
Feature('ToDo');
|
|
160
|
+
|
|
161
|
+
Scenario('create todo item', ({ I }) => {
|
|
162
|
+
I.amOnPage('http://todomvc.com/examples/react/');
|
|
163
|
+
I.dontSeeElement('.todo-count');
|
|
164
|
+
I.fillField('What needs to be done?', 'Write a guide');
|
|
165
|
+
I.pressKey('Enter');
|
|
166
|
+
I.see('Write a guide', '.todo-list');
|
|
167
|
+
I.see('1 item left', '.todo-count');
|
|
168
|
+
});
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Grabbers
|
|
172
|
+
|
|
173
|
+
If you need to get element's value inside a test you can use `grab*` methods. They should be used with `await` operator inside `async` function:
|
|
174
|
+
|
|
175
|
+
```js
|
|
176
|
+
const assert = require('assert');
|
|
177
|
+
Scenario('get value of current tasks', async ({ I }) => {
|
|
178
|
+
I.fillField('.todo', 'my first item');
|
|
179
|
+
I.pressKey('Enter')
|
|
180
|
+
I.fillField('.todo', 'my second item');
|
|
181
|
+
I.pressKey('Enter')
|
|
182
|
+
let numTodos = await I.grabTextFrom('.todo-count strong');
|
|
183
|
+
assert.equal(2, numTodos);
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Within
|
|
188
|
+
|
|
189
|
+
In case some actions should be taken inside one element (a container or modal window or iframe) you can use `within` block to narrow the scope.
|
|
190
|
+
Please take a note that you can't use within inside another within in Playwright helper:
|
|
191
|
+
|
|
192
|
+
```js
|
|
193
|
+
await within('.todoapp', () => {
|
|
194
|
+
I.fillField('.todo', 'my new item');
|
|
195
|
+
I.pressKey('Enter')
|
|
196
|
+
I.see('1 item left', '.todo-count');
|
|
197
|
+
I.click('.todo-list input.toggle');
|
|
198
|
+
});
|
|
199
|
+
I.see('0 items left', '.todo-count');
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Each Element <Badge text="Since 3.3" type="warning"/>
|
|
203
|
+
|
|
204
|
+
Usually, CodeceptJS performs an action on the first matched element.
|
|
205
|
+
In case you want to do an action on each element found, use the special function `eachElement` which comes from [eachElement](https://codecept.io/plugins/#eachelement) plugin.
|
|
206
|
+
|
|
207
|
+
`eachElement` function matches all elements by locator and performs a callback on each of those element. A callback function receives [ElementHandle instance](https://playwright.dev/docs/api/class-elementhandle) from Playwright API. `eachElement` may perform arbitrary actions on a page, so the first argument should by a description of the actions performed. This description will be used for logging purposes.
|
|
208
|
+
|
|
209
|
+
Usage example
|
|
210
|
+
|
|
211
|
+
```js
|
|
212
|
+
await eachElement(
|
|
213
|
+
'tick all checkboxes',
|
|
214
|
+
'input.custom-checkbox',
|
|
215
|
+
async (el, index) => {
|
|
216
|
+
await el.check();
|
|
217
|
+
});
|
|
218
|
+
);
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
> ℹ Learn more about [eachElement plugin](/plugins/#eachelement)
|
|
222
|
+
|
|
223
|
+
## Multi Session Testing
|
|
224
|
+
|
|
225
|
+
To launch additional browser context (or incognito window) use `session` command.
|
|
226
|
+
|
|
227
|
+
```js
|
|
228
|
+
Scenario('I try to open this site as anonymous user', ({ I }) => {
|
|
229
|
+
I.amOnPage('/');
|
|
230
|
+
I.dontSee('Agree to cookies');
|
|
231
|
+
session('anonymous user', () => {
|
|
232
|
+
I.amOnPage('/');
|
|
233
|
+
I.see('Agree to cookies');
|
|
234
|
+
});
|
|
235
|
+
})
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
> ℹ Learn more about [multi-session testing](/basics/#multiple-sessions)
|
|
239
|
+
|
|
240
|
+
## Electron Testing
|
|
241
|
+
|
|
242
|
+
CodeceptJS allows you to make use of [Playwright's Electron flavor](https://github.com/microsoft/playwright/blob/master/packages/playwright-electron/README.md).
|
|
243
|
+
To use this functionality, all you need to do is set the browser to `electron` in the CodeceptJS configuration file and, according to the [Playwright BrowserType API](https://playwright.dev/docs/api/class-browsertype/#browsertypelaunchoptions), set the launch options to point to your Electron application.
|
|
244
|
+
|
|
245
|
+
`main.js` - main Electron application file
|
|
246
|
+
```js
|
|
247
|
+
const { app, BrowserWindow } = require("electron");
|
|
248
|
+
|
|
249
|
+
function createWindow() {
|
|
250
|
+
const window = new BrowserWindow({ width: 800, height: 600 });
|
|
251
|
+
window.loadURL("https://example.com");
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
app.whenReady().then(createWindow);
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
`codecept.conf.js` - CodeceptJS configuration file
|
|
258
|
+
```js
|
|
259
|
+
const path = require("path");
|
|
260
|
+
|
|
261
|
+
exports.config = {
|
|
262
|
+
helpers: {
|
|
263
|
+
Playwright: {
|
|
264
|
+
browser: "electron",
|
|
265
|
+
electron: {
|
|
266
|
+
executablePath: require("electron"),
|
|
267
|
+
args: [path.join(__dirname, "main.js")],
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
},
|
|
271
|
+
// rest of config
|
|
272
|
+
}
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
Sometimes, the Electron app is built with [electron-forge](https://www.electronforge.io/), then configuring may a bit different.
|
|
276
|
+
|
|
277
|
+
- First, you should run your electron-forge command to build and pack your app.
|
|
278
|
+
- Then, you would find `index.js` file inside `.webpack/main/index.js`
|
|
279
|
+
|
|
280
|
+
`codecept.conf.js` - CodeceptJS configuration file
|
|
281
|
+
```js
|
|
282
|
+
const path = require("path");
|
|
283
|
+
|
|
284
|
+
exports.config = {
|
|
285
|
+
helpers: {
|
|
286
|
+
Playwright: {
|
|
287
|
+
browser: "electron",
|
|
288
|
+
electron: {
|
|
289
|
+
executablePath: require("electron"),
|
|
290
|
+
args: [path.join(__dirname, ".webpack/main/index.js")],
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
},
|
|
294
|
+
// rest of config
|
|
295
|
+
}
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### Headless Mode
|
|
299
|
+
|
|
300
|
+
With Electron, headless mode must be set when creating the window. Therefore, CodeceptJS's `show` configuration parameter will not work. However, you can set it in the `main.js` file as shown below:
|
|
301
|
+
|
|
302
|
+
```js
|
|
303
|
+
function createWindow() {
|
|
304
|
+
const window = new BrowserWindow({ width: 800, height: 600, show: false });
|
|
305
|
+
window.loadURL("https://example.com");
|
|
306
|
+
}
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
## Device Emulation
|
|
311
|
+
|
|
312
|
+
Playwright can emulate browsers of mobile devices. Instead of paying for expensive devices for mobile tests you can adjust Playwright settings so it could emulate mobile browsers on iPhone, Samsung Galaxy, etc.
|
|
313
|
+
|
|
314
|
+
Device emulation can be enabled in CodeceptJS globally in a config or per session.
|
|
315
|
+
|
|
316
|
+
Playwright contains a [list of predefined devices](https://github.com/microsoft/playwright/blob/master/src/server/deviceDescriptors.js) to emulate, for instance this is how you can enable iPhone 6 emulation for all tests:
|
|
317
|
+
|
|
318
|
+
```js
|
|
319
|
+
const { devices } = require('playwright');
|
|
320
|
+
|
|
321
|
+
helpers: {
|
|
322
|
+
Playwright: {
|
|
323
|
+
// regular config goes here
|
|
324
|
+
emulate: devices['iPhone 6'],
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
```
|
|
328
|
+
To adjust browser settings you can pass [custom options](https://github.com/microsoft/playwright/blob/master/docs/src/api/class-browsercontext.md)
|
|
329
|
+
|
|
330
|
+
```js
|
|
331
|
+
helpers: {
|
|
332
|
+
Playwright: {
|
|
333
|
+
// regular config goes here
|
|
334
|
+
// put on mobile device
|
|
335
|
+
emulate: { isMobile: true, deviceScaleFactor: 2 }
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
To enable device emulation for a specific test, create an additional browser session and pass in config as a second parameter:
|
|
341
|
+
|
|
342
|
+
```js
|
|
343
|
+
const { devices } = require('playwright');
|
|
344
|
+
|
|
345
|
+
Scenario('website looks nice on iPhone', () => {
|
|
346
|
+
session('mobile user', devices['iPhone 6'], () => {
|
|
347
|
+
I.amOnPage('/');
|
|
348
|
+
I.see('Hello, iPhone user!')
|
|
349
|
+
})
|
|
350
|
+
});
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
## API Requests
|
|
354
|
+
|
|
355
|
+
CodeceptJS has [REST](/helpers/REST) and [GraphQL]((/helpers/GraphQL)) helpers to perform requests to external APIs. This may be helpful to implement [data management](https://codecept.io/data/) strategy.
|
|
356
|
+
|
|
357
|
+
However, Playwright since 1.18 has its own [API for making request](https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-get). It uses cookies from browser session to authenticate requests. So you can use it via [`makeApiRequest`](/helpers/Playwright#makeApiRequest) method:
|
|
358
|
+
|
|
359
|
+
```js
|
|
360
|
+
I.makeApiRequest('GET', '/users')
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
It is also possible to test JSON responses by adding [`JSONResponse`](/helpers/JSONResponse) and connecting it to Playwright:
|
|
364
|
+
|
|
365
|
+
```js
|
|
366
|
+
// inside codecept.conf.js
|
|
367
|
+
{
|
|
368
|
+
helpers: {
|
|
369
|
+
Playwright: {
|
|
370
|
+
// current config
|
|
371
|
+
},
|
|
372
|
+
JSONResponse: {
|
|
373
|
+
requestHelper: 'Playwright',
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
```
|
|
378
|
+
This helper provides you methods for [API testing](/api). For instance, you can check for status code, data inclusion and structure:
|
|
379
|
+
|
|
380
|
+
```js
|
|
381
|
+
I.makeApiRequest('GET', '/users/1');
|
|
382
|
+
I.seeResponseCodeIs(200);
|
|
383
|
+
I.seeResponseContainsKeys(['user']);
|
|
384
|
+
```
|
|
385
|
+
This way you can do full fledged API testing via Playwright.
|
|
386
|
+
|
|
387
|
+
## Accessing Playwright API
|
|
388
|
+
|
|
389
|
+
To get [Playwright API](https://playwright.dev/docs/api/class-playwright) inside a test use `I.usePlaywrightTo` method with a callback.
|
|
390
|
+
|
|
391
|
+
`usePlaywrightTo` passes in an instance of Playwright helper from which you can obtain access to main Playwright classes:
|
|
392
|
+
|
|
393
|
+
* [`browser`](https://playwright.dev/docs/api/class-browser)
|
|
394
|
+
* [`browserContext`](https://playwright.dev/docs/api/class-browsercontext)
|
|
395
|
+
* [`page`](https://playwright.dev/docs/api/class-page)
|
|
396
|
+
|
|
397
|
+
To keep test readable provide a description of a callback inside the first parameter.
|
|
398
|
+
|
|
399
|
+
```js
|
|
400
|
+
I.usePlaywrightTo('emulate offline mode', async ({ browser, browserContext, page }) => {
|
|
401
|
+
// use browser, page, context objects inside this function
|
|
402
|
+
await browserContext.setOffline(true);
|
|
403
|
+
});
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
Playwright commands are asynchronous so a callback function must be async.
|
|
407
|
+
|
|
408
|
+
A Playwright helper is passed as argument for callback, so you can combine Playwright API with CodeceptJS API:
|
|
409
|
+
|
|
410
|
+
```js
|
|
411
|
+
I.usePlaywrightTo('emulate offline mode', async (Playwright) => {
|
|
412
|
+
// access internal objects browser, page, context of helper
|
|
413
|
+
await Playwright.browserContext.setOffline(true);
|
|
414
|
+
// call a method of helper, await is required here
|
|
415
|
+
await Playwright.click('Reload');
|
|
416
|
+
});
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
## Mocking Network Requests <Badge text="Since 3.1" type="warning"/>
|
|
420
|
+
|
|
421
|
+
Network requests & responses can be mocked and modified. Use `mockRequest` which strictly follows [Playwright's `route` API](https://playwright.dev/docs/api/class-browsercontext#browser-context-route).
|
|
422
|
+
|
|
423
|
+
```js
|
|
424
|
+
I.mockRoute('/api/**', route => {
|
|
425
|
+
if (route.request().postData().includes('my-string'))
|
|
426
|
+
route.fulfill({ body: 'mocked-data' });
|
|
427
|
+
else
|
|
428
|
+
route.continue();
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
I.mockRoute('**/*.{png,jpg,jpeg}', route => route.abort());
|
|
432
|
+
|
|
433
|
+
// To disable mocking for a route call `stopMockingRoute`
|
|
434
|
+
// for previously mocked URL
|
|
435
|
+
I.stopMockingRoute('**/*.{png,jpg,jpeg}'
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
To master request intercepting [use `route` object](https://playwright.dev/docs/api/class-route) object passed into mock request handler.
|
|
439
|
+
|
|
440
|
+
## Video <Badge text="Since 3.1" type="warning"/>
|
|
441
|
+
|
|
442
|
+
Playwright may record videos for failed tests. This can be enabled in a config with `video: true` option:
|
|
443
|
+
|
|
444
|
+
```js
|
|
445
|
+
exports.config = {
|
|
446
|
+
helpers: {
|
|
447
|
+
Playwright: {
|
|
448
|
+
// ...
|
|
449
|
+
video: true
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
```
|
|
454
|
+
When a test fails and video was enabled a video file is shown under the `artifacts` section in the error message:
|
|
455
|
+
|
|
456
|
+
```
|
|
457
|
+
-- FAILURES:
|
|
458
|
+
|
|
459
|
+
1) GitHub
|
|
460
|
+
open:
|
|
461
|
+
|
|
462
|
+
Scenario Steps:
|
|
463
|
+
- I.amOnPage("https://gothub11.com/search") at Test.<anonymous> (./github_test.js:16:5)
|
|
464
|
+
|
|
465
|
+
Artifacts:
|
|
466
|
+
- screenshot: /home/davert/projects/codeceptjs/examples/output/open.failed.png
|
|
467
|
+
- video: /home/davert/projects/codeceptjs/examples/output/videos/5ecf6aaa78865bce14d271b55de964fd.webm
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
Open video and use it to debug a failed test case. Video helps when running tests on CI. Configure your CI system to enable artifacts storage for `output/video` and review videos of failed test case to understand failures.
|
|
471
|
+
|
|
472
|
+
It is recommended to enable [subtitles](https://codecept.io/plugins/#subtitles) plugin which will generate subtitles from steps in `.srt` format. Subtitles file will be saved into after a video file so video player (like VLC) would load them automatically:
|
|
473
|
+
|
|
474
|
+

|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
## Trace <Badge text="Since 3.1" type="warning"/>
|
|
478
|
+
|
|
479
|
+
If video is not enough to descover why a test failed a [trace](https://playwright.dev/docs/trace-viewer/) can be recorded.
|
|
480
|
+
|
|
481
|
+

|
|
482
|
+
|
|
483
|
+
Inside a trace you get screenshots, DOM snapshots, console logs, network requests and playwright commands logged and showed on a timeline. This may help for a deep debug of a failed test cases. Trace file is saved into ZIP archive and can be viewed with Trace Viewer built into Playwright.
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
Enable trace with `trace: true` option in a config:
|
|
487
|
+
|
|
488
|
+
```js
|
|
489
|
+
exports.config = {
|
|
490
|
+
helpers: {
|
|
491
|
+
Playwright: {
|
|
492
|
+
// ...
|
|
493
|
+
trace: true
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
When a test fails and trace was enabled, a trace file is shown under the `artifacts` section in the error message:
|
|
500
|
+
|
|
501
|
+
```
|
|
502
|
+
-- FAILURES:
|
|
503
|
+
|
|
504
|
+
1) GitHub
|
|
505
|
+
open:
|
|
506
|
+
|
|
507
|
+
Scenario Steps:
|
|
508
|
+
- I.amOnPage("https://gothub11.com/search") at Test.<anonymous> (./github_test.js:16:5)
|
|
509
|
+
|
|
510
|
+
Artifacts:
|
|
511
|
+
- screenshot: /home/davert/projects/codeceptjs/examples/output/open.failed.png
|
|
512
|
+
- trace: /home/davert/projects/codeceptjs/examples/output/trace/open.zip
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
Use Playwright's trace viewer to analyze the trace:
|
|
516
|
+
|
|
517
|
+
```
|
|
518
|
+
npx playwright show-trace {path-to-trace-file}
|
|
519
|
+
```
|
|
520
|
+
|
|
521
|
+
For instance, this is how you can read a trace for a failed test from an example:
|
|
522
|
+
|
|
523
|
+
```
|
|
524
|
+
npx playwright show-trace /home/davert/projects/codeceptjs/examples/output/trace/open.zip
|
|
525
|
+
```
|
|
526
|
+
|
|
527
|
+
## Capturing Code Coverage
|
|
528
|
+
|
|
529
|
+
Code coverage can be captured, by enabling the `coverage` plugin in `codecept.config.js`.
|
|
530
|
+
|
|
531
|
+
```js
|
|
532
|
+
{
|
|
533
|
+
plugins: {
|
|
534
|
+
coverage: {
|
|
535
|
+
enabled: true
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
Once all the tests are completed, `codecept` will create and store coverage in `output/coverage` folder, as shown below.
|
|
542
|
+
|
|
543
|
+

|
|
544
|
+
|
|
545
|
+
Then you need to [convert code coverage from Playwright's format into Istanbul format](https://github.com/codeceptjs/CodeceptJS/wiki/Converting-Playwright-to-Istanbul-Coverage).
|
|
546
|
+
|
|
547
|
+
Once the istanbul compatible coverage is generated, use [`nyc`](https://www.npmjs.com/package/nyc) to generate your coverage report in your desired format.
|
|
548
|
+
|
|
549
|
+
```
|
|
550
|
+
npx nyc report --reporter html -t coverage
|
|
551
|
+
```
|
|
552
|
+
|
|
553
|
+
The above command will generate will generate coverage in an interactive html format. It should generate `html` files in the directory where your code coverage is present, something like shown below.
|
|
554
|
+
|
|
555
|
+

|
|
556
|
+
|
|
557
|
+
Open `index.html` in your browser to view the full interactive coverage report.
|
|
558
|
+
|
|
559
|
+

|
|
560
|
+
|
|
561
|
+

|
|
562
|
+
|
|
563
|
+
## Extending Helper
|
|
564
|
+
|
|
565
|
+
To create custom `I.*` commands using Playwright API you need to create a custom helper.
|
|
566
|
+
|
|
567
|
+
Start with creating an `MyPlaywright` helper using `generate:helper` or `gh` command:
|
|
568
|
+
|
|
569
|
+
```sh
|
|
570
|
+
npx codeceptjs gh
|
|
571
|
+
```
|
|
572
|
+
|
|
573
|
+
Then inside a Helper you can access `Playwright` helper of CodeceptJS.
|
|
574
|
+
Let's say you want to create `I.grabDimensionsOfCurrentPage` action. In this case you need to call `evaluate` method of `page` object
|
|
575
|
+
|
|
576
|
+
```js
|
|
577
|
+
// inside a MyPlaywright helper
|
|
578
|
+
async grabDimensionsOfCurrentPage() {
|
|
579
|
+
const { page } = this.helpers.Playwright;
|
|
580
|
+
await page.goto('https://www.example.com/');
|
|
581
|
+
return page.evaluate(() => {
|
|
582
|
+
return {
|
|
583
|
+
width: document.documentElement.clientWidth,
|
|
584
|
+
height: document.documentElement.clientHeight,
|
|
585
|
+
deviceScaleFactor: window.devicePixelRatio
|
|
586
|
+
}
|
|
587
|
+
})
|
|
588
|
+
}
|
|
589
|
+
```
|
|
590
|
+
|
|
591
|
+
The same way you can also access `browser` object to implement more actions or handle events. For instance, you want to set the permissions, you can approach it with:
|
|
592
|
+
|
|
593
|
+
```js
|
|
594
|
+
// inside a MyPlaywright helper
|
|
595
|
+
async setPermissions() {
|
|
596
|
+
const { browser } = this.helpers.Playwright;
|
|
597
|
+
const context = browser.defaultContext()
|
|
598
|
+
return context.setPermissions('https://html5demos.com', ['geolocation']);
|
|
599
|
+
}
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
> [▶ Learn more about BrowserContext](https://github.com/microsoft/playwright/blob/master/docs/src/api/class-browsercontext.md)
|
|
603
|
+
|
|
604
|
+
> [▶ Learn more about Helpers](https://codecept.io/helpers/)
|
|
605
|
+
|
|
606
|
+
## Timezone change
|
|
607
|
+
|
|
608
|
+
Sometimes it's useful to test browser in different timezones. You can change timezone this way:
|
|
609
|
+
|
|
610
|
+
```js
|
|
611
|
+
Scenario("Test in a different timezone", ({ I }) => {
|
|
612
|
+
I.restartBrowser({ timezoneId: 'America/Phoenix' });
|
|
613
|
+
I.amOnPage("/");
|
|
614
|
+
// ...
|
|
615
|
+
// Reset timezone to default one (taken from OS)
|
|
616
|
+
I.restartBrowser();
|
|
617
|
+
});
|
|
618
|
+
```
|
|
619
|
+
Other context options: https://playwright.dev/docs/api/class-browser#browser-new-context
|
|
620
|
+
|
|
621
|
+
## Configuring CI
|
|
622
|
+
|
|
623
|
+
### GitHub Actions
|
|
624
|
+
|
|
625
|
+
Playwright can be added to GitHub Actions using [official action](https://github.com/microsoft/playwright-github-action). Use it before starting CodeceptJS tests to install all dependencies. It is important to run tests in headless mode ([otherwise you will need to enable xvfb to emulate desktop](https://github.com/microsoft/playwright-github-action#run-in-headful-mode)).
|
|
626
|
+
|
|
627
|
+
```yml
|
|
628
|
+
# from workflows/tests.yml
|
|
629
|
+
- uses: microsoft/playwright-github-action@v1
|
|
630
|
+
- name: run CodeceptJS tests
|
|
631
|
+
run: npx codeceptjs run
|
|
632
|
+
```
|