Skip to content

Commit eb24967

Browse files
committed
docs: fix test examples
1 parent 386b083 commit eb24967

15 files changed

+154
-131
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
.env.development.local
1717
.env.test.local
1818
.env.production.local
19+
.vscode/
1920

2021
npm-debug.log*
2122

docs/commands/overview.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ However, the command descriptions on the [WebDriverIO][webdriverio-api] website
2222

2323
```javascript
2424
it("should test something", async function () {
25-
const browser = this.browser;
25+
await this.browser.url("https://testplane.io");
2626
// test code...
2727
});
2828
```
@@ -31,6 +31,7 @@ or get the `browser` object from the function argument (note that the `browser`
3131

3232
```javascript
3333
it("should test something", async ({ browser }) => {
34+
await browser.url("https://testplane.io");
3435
// test code...
3536
});
3637
```

docs/guides/how-to-check-accessibility.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ To obtain such a tree, _puppeteer_ has a special [Accessibility class][puppeteer
2020
Here's an example of how to use it:
2121

2222
```javascript
23-
it("should get accessibility tree of yandex.ru", async function () {
23+
it("should get accessibility tree of yandex.ru", async function ({browser}) {
2424
// Get puppeteer instance
25-
const puppeteer = await this.browser.getPuppeteer();
25+
const puppeteer = await browser.getPuppeteer();
2626

2727
// Get the first open page (considering it to be currently active)
2828
const [page] = await puppeteer.pages();
2929

30-
await this.browser.url("https://yandex.ru");
30+
await browser.url("https://yandex.ru");
3131

3232
// Get the current state of the accessibility tree
3333
const snapshot = await page.accessibility.snapshot();

docs/guides/how-to-hide-scrollbars-by-cdp.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ CDP has a special method [Emulation.setScrollbarsHidden][set-scrollbars-hidden]
2020
Here's how it looks:
2121

2222
```javascript
23-
it("should hide scrollbar", async function () {
23+
it("should hide scrollbar", async function ({browser}) {
2424
// Get puppeteer instance
25-
const puppeteer = await this.browser.getPuppeteer();
25+
const puppeteer = await browser.getPuppeteer();
2626

2727
// Get the first open page (considering it to be currently active)
2828
const [page] = await puppeteer.pages();
@@ -33,7 +33,7 @@ it("should hide scrollbar", async function () {
3333
// Hide the scrollbar
3434
await client.send("Emulation.setScrollbarsHidden", { hidden: true });
3535

36-
await this.browser.url("https://yandex.ru");
36+
await browser.url("https://yandex.ru");
3737
});
3838
```
3939

docs/guides/how-to-intercept-requests-and-responses.mdx

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,25 @@ Let's try writing tests using this API and cover different cases. To clarify, al
3030
## Example 1: Mocking a Request to google.com and Returning Our Own Response {#example_1}
3131

3232
```javascript
33-
it("should mock google.com", async function () {
33+
it("should mock google.com", async function ({browser}) {
3434
// Mocking the request to google.com
35-
const mock = await this.browser.mock("https://google.com");
35+
const mock = await browser.mock("https://google.com");
3636

3737
// Returning the string "Hello, world!" instead of the response from the site.
3838
// The "fetchResponse" option dictates whether the request should be made
3939
// to the mocked resource; default is true.
4040
mock.respond("Hello, world!", { fetchResponse: false });
4141

42-
await this.browser.url("https://google.com");
42+
await browser.url("https://google.com");
4343
});
4444
```
4545

4646
From the graphical representation, you can see that we returned our text, although the browser's address bar shows we navigated to _google.com._ Also, it's clear that we didn't mock the favicon, which was fetched from an external source. We can write this same example using the puppeteer API. For this, _webdriverio_ has the [getPuppeteer()][get-puppeteer] command:
4747

4848
```javascript
49-
it("should mock google.com using puppeteer api", async function () {
49+
it("should mock google.com using puppeteer api", async function ({browser}) {
5050
// Get puppeteer instance
51-
const puppeteer = await this.browser.getPuppeteer();
51+
const puppeteer = await browser.getPuppeteer();
5252

5353
// Get the first open page (considering it to be currently active)
5454
const [page] = await puppeteer.pages();
@@ -69,7 +69,7 @@ it("should mock google.com using puppeteer api", async function () {
6969
// Here, we could call "page.goto('https://google.com')", but it's better to call "url",
7070
// because most plugins have wrappers for the "url" command adding additional logic.
7171
// For example, in testplane, the URL is added to the meta.
72-
await this.browser.url("https://google.com");
72+
await browser.url("https://google.com");
7373
});
7474
```
7575

@@ -78,9 +78,9 @@ it("should mock google.com using puppeteer api", async function () {
7878
Now, let's imagine that puppeteer doesn't yet have an API for mocking requests, but this is already implemented in the [Fetch][fetch] domain of CDP. In this case, we will use this domain's method by interacting with the CDP session directly. For this, puppeteer has the [CDPSession.send()][cdp-session-send] method:
7979

8080
```javascript
81-
it("should mock google.com using cdp fetch domain", async function () {
81+
it("should mock google.com using cdp fetch domain", async function ({browser}) {
8282
// Get puppeteer instance
83-
const puppeteer = await this.browser.getPuppeteer();
83+
const puppeteer = await browser.getPuppeteer();
8484

8585
// Get the first open page (considering it to be currently active)
8686
const [page] = await puppeteer.pages();
@@ -113,7 +113,7 @@ it("should mock google.com using cdp fetch domain", async function () {
113113
});
114114
});
115115

116-
await this.browser.url("https://google.com");
116+
await browser.url("https://google.com");
117117
});
118118
```
119119

@@ -122,14 +122,14 @@ Obviously, when using the _webdriverio_ API for mocking requests, the code is mu
122122
## Example 2: Canceling the Request for Google's Logo {#example_2}
123123

124124
```javascript
125-
it("should abort request to logo on google.com", async function () {
125+
it("should abort request to logo on google.com", async function ({browser}) {
126126
// You can use a mask for the URL
127-
const mock = await this.browser.mock("https://www.google.com/images/**/*.png");
127+
const mock = await browser.mock("https://www.google.com/images/**/*.png");
128128

129129
// Throw an error "ERR_FAILED" when loading a resource that matches the mask
130130
mock.abort("Failed");
131131

132-
await this.browser.url("https://google.com");
132+
await browser.url("https://google.com");
133133
});
134134
```
135135

@@ -138,15 +138,15 @@ From the graphical representation, it is clear that the logo is not displayed, a
138138
## Example 3: Loading google.com Using a Fixture for the Response {#example_3}
139139

140140
```javascript
141-
it("should mock google.com and return answer from fixture", async function () {
141+
it("should mock google.com and return answer from fixture", async function ({browser}) {
142142
// Mocking the request to google.com
143-
const mock = await this.browser.mock("https://google.com");
143+
const mock = await browser.mock("https://google.com");
144144

145145
// Specify the path from which to take the fixture, and with
146146
// "fetchResponse: false", indicate that the real request should not be made
147147
mock.respond("./fixtures/my-google.com.html", { fetchResponse: false });
148148

149-
await this.browser.url("https://google.com");
149+
await browser.url("https://google.com");
150150
});
151151
```
152152

@@ -155,14 +155,14 @@ From the graphical representation, it is clear that instead of google.com's cont
155155
## Example 4: Redirecting the Request from google.com to yandex.ru {#example_4}
156156

157157
```javascript
158-
it("should redirect from google.com to yandex.ru", async function () {
158+
it("should redirect from google.com to yandex.ru", async function ({browser}) {
159159
// Mocking the request to google.com
160-
const mock = await this.browser.mock("https://google.com");
160+
const mock = await browser.mock("https://google.com");
161161

162162
// For redirection, simply specify the URL
163163
mock.respond("https://yandex.ru");
164164

165-
await this.browser.url("https://google.com");
165+
await browser.url("https://google.com");
166166
});
167167
```
168168

@@ -173,26 +173,26 @@ Puppeteer still does not have an API for conveniently modifying responses. There
173173
Replace all occurrences of the string `Google` with `Yandex` in google.com's response:
174174

175175
```javascript
176-
it("should modify response from google.com", async function () {
176+
it("should modify response from google.com", async function ({browser}) {
177177
// Here, you need to mock with www because navigating to google.com
178178
// returns a 301 response without a body and redirects to www
179-
const mock = await this.browser.mock("https://www.google.com");
179+
const mock = await browser.mock("https://www.google.com");
180180

181181
mock.respond(req => {
182182
// Replace "Google" with "Yandex" using a regular expression
183183
return req.body.replace(/Google/g, "Yandex");
184184
});
185185

186-
await this.browser.url("https://google.com");
186+
await browser.url("https://google.com");
187187
});
188188
```
189189

190190
Additionally, we can modify responses from unknown sources in advance. For example, let's modify all scripts loaded on _google.com:_
191191

192192
```javascript
193-
it("should modify response from google.com", async function () {
193+
it("should modify response from google.com", async function ({browser}) {
194194
// The first argument specifies that we will intercept all requests
195-
const mock = await this.browser.mock("**", {
195+
const mock = await browser.mock("**", {
196196
headers: headers => {
197197
// Filter only the requests where the "content-type"
198198
// header contains values "text/javascript" or "application/javascript"
@@ -208,7 +208,7 @@ it("should modify response from google.com", async function () {
208208
return (req.body += `\nconsole.log("This script was modified in real time.");`);
209209
});
210210

211-
await this.browser.url("https://google.com");
211+
await browser.url("https://google.com");
212212
});
213213
```
214214

@@ -217,11 +217,11 @@ it("should modify response from google.com", async function () {
217217
Let's say we need to collect a list of all URLs loaded on the page. Using this information, we could determine if we have requests for external resources or neighboring services that we do not control. This means they could fail at any time and break our tests. Here's what our code might look like:
218218

219219
```javascript
220-
it("should mock yandex.ru and log all loaded urls", async function () {
220+
it("should mock yandex.ru and log all loaded urls", async function ({browser}) {
221221
// Intercept absolutely all requests
222-
const mock = await this.browser.mock("**");
222+
const mock = await browser.mock("**");
223223

224-
await this.browser.url("https://yandex.ru");
224+
await browser.url("https://yandex.ru");
225225

226226
// mock.calls contains not only the visited URL information
227227
// but also the response from the source, the request headers, response headers, etc.
@@ -235,7 +235,7 @@ it("should mock yandex.ru and log all loaded urls", async function () {
235235
Most likely, your tests are more complex than these examples and involve various clicks on elements that open in new tabs. In such cases, the previous code will not capture the opening of new tabs or that URLs need to be collected there as well. Therefore, in such cases, you need to use puppeteer's API:
236236

237237
```javascript
238-
it("should mock yandex.ru and log all loaded urls (using puppeteer)", async function () {
238+
it("should mock yandex.ru and log all loaded urls (using puppeteer)", async function ({browser}) {
239239
// Accumulative list of all URLs
240240
const urls = [];
241241

@@ -249,7 +249,7 @@ it("should mock yandex.ru and log all loaded urls (using puppeteer)", async func
249249
}
250250

251251
// Get puppeteer instance
252-
const puppeteer = await this.browser.getPuppeteer();
252+
const puppeteer = await browser.getPuppeteer();
253253

254254
// Get all open pages at the current moment
255255
const pages = await puppeteer.pages();
@@ -274,10 +274,10 @@ it("should mock yandex.ru and log all loaded urls (using puppeteer)", async func
274274
urlsHandler(page);
275275
});
276276

277-
await this.browser.url("https://yandex.ru");
277+
await browser.url("https://yandex.ru");
278278

279279
// Find the first element in the list of services (at that time it was a football page)
280-
const elem = await this.browser.$(".services-new__list-item > a");
280+
const elem = await browser.$(".services-new__list-item > a");
281281

282282
// Click the service that opens in a new tab
283283
await elem.click();
@@ -298,20 +298,20 @@ module.exports = {
298298
"testplane-global-hook": {
299299
enabled: true,
300300

301-
beforeEach: async function () {
301+
beforeEach: async function ({browser}) {
302302
// Check that the browser name starts with "chrome"
303-
if (!/^chrome$/i.test(this.browser.capabilities.browserName)) {
303+
if (!/^chrome$/i.test(browser.capabilities.browserName)) {
304304
return;
305305
}
306306

307307
// Mocking the request to google.com
308-
const mock = await this.browser.mock("https://google.com");
308+
const mock = await browser.mock("https://google.com");
309309
mock.respond("hello world", { fetchResponse: false });
310310
},
311311

312-
afterEach: function () {
312+
afterEach: function ({browser}) {
313313
// Clear all mocks in the current session
314-
this.browser.mockRestoreAll();
314+
browser.mockRestoreAll();
315315
},
316316
},
317317

@@ -327,8 +327,8 @@ The test code will now only contain the URL transition:
327327
```javascript
328328
// Explicitly indicate that the test is only executed in browsers whose name starts with chrome
329329
testplane.only.in(/^chrome/);
330-
it("should mock google.com inside global before each", async function () {
331-
await this.browser.url("https://google.com");
330+
it("should mock google.com inside global before each", async function ({browser}) {
331+
await browser.url("https://google.com");
332332
});
333333
```
334334

docs/guides/how-to-manage-cpu-performance.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ The CPU speed on mobile devices is significantly slower than on computers. There
1818
Let's use this method to slow down CPU speed by 8 times:
1919

2020
```javascript
21-
it("should open yandex.ru with emulation 8x slower CPU", async function () {
21+
it("should open yandex.ru with emulation 8x slower CPU", async function ({browser}) {
2222
// Get puppeteer instance
23-
const puppeteer = await this.browser.getPuppeteer();
23+
const puppeteer = await browser.getPuppeteer();
2424

2525
// Get the first open page (considering it to be currently active)
2626
const [page] = await puppeteer.pages();
2727

2828
// Slow down the CPU speed by 8 times
2929
await page.emulateCPUThrottling(8);
3030

31-
await this.browser.url("https://yandex.ru");
31+
await browser.url("https://yandex.ru");
3232
});
3333
```
3434

@@ -39,9 +39,9 @@ Initially, _webdriverio_ did not support the `page.emulateCPUThrottling` method
3939
However, this limitation could be bypassed using puppeteer's [CDPSession.send()][cdp-session-send] method by sending the browser the [Emulation.setCPUThrottlingRate][emulation-set-cpu-throttling-rate] command via CDP:
4040

4141
```javascript
42-
it("should open yandex.ru with emulation 8x slower CPU", async function () {
42+
it("should open yandex.ru with emulation 8x slower CPU", async function ({browser}) {
4343
// Get puppeteer instance
44-
const puppeteer = await this.browser.getPuppeteer();
44+
const puppeteer = await browser.getPuppeteer();
4545

4646
// Get the first open page (considering it to be currently active)
4747
const [page] = await puppeteer.pages();
@@ -52,7 +52,7 @@ it("should open yandex.ru with emulation 8x slower CPU", async function () {
5252
// Slow down the CPU speed by 8 times
5353
await client.send("Emulation.setCPUThrottlingRate", { rate: 8 });
5454

55-
await this.browser.url("https://yandex.ru");
55+
await browser.url("https://yandex.ru");
5656
});
5757
```
5858

docs/guides/how-to-manage-network-bandwidth.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ Besides custom settings, the [throttle][throttle] method supports the following
2828
Let's emulate a 2G connection and open yandex.ru in Chrome with phone emulation:
2929

3030
```javascript
31-
it("should open yandex.ru with emulation of 2G-connection", async function () {
31+
it("should open yandex.ru with emulation of 2G-connection", async function ({browser}) {
3232
// Emulate a 2G connection
33-
await this.browser.throttle("Good2G");
33+
await browser.throttle("Good2G");
3434

35-
await this.browser.url("https://yandex.ru");
35+
await browser.url("https://yandex.ru");
3636
});
3737
```
3838

@@ -41,16 +41,16 @@ it("should open yandex.ru with emulation of 2G-connection", async function () {
4141
We can also emulate a connection with specific characteristics:
4242

4343
```javascript
44-
it("should open yandex.ru with emulation of custom connection", async function () {
44+
it("should open yandex.ru with emulation of custom connection", async function ({browser}) {
4545
// Emulate a network connection with specified characteristics
46-
await this.browser.throttle({
46+
await browser.throttle({
4747
offline: false, // emulate offline state
4848
downloadThroughput: (10 * 1024) / 8, // max download bandwidth (byte/sec)
4949
uploadThroughput: (10 * 1024) / 8, // max upload bandwidth (byte/sec)
5050
latency: 10, // min latency from sending the request to receiving the response headers
5151
});
5252

53-
await this.browser.url("https://yandex.ru");
53+
await browser.url("https://yandex.ru");
5454
});
5555
```
5656

docs/plugins/testplane-global-hook.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ Add the plugin to the `plugins` section of the `testplane` config:
3030
module.exports = {
3131
plugins: {
3232
"testplane-global-hook": {
33-
beforeEach: async function () {
34-
await this.browser.deleteCookie(); // Say, we want to always clear cookies before running a test
33+
beforeEach: async function ({browser}) {
34+
await browser.deleteCookie(); // Say, we want to always clear cookies before running a test
3535
},
36-
afterEach: async function () {
37-
await this.browser.execute(function () {
36+
afterEach: async function ({browser}) {
37+
await browser.execute(function () {
3838
try {
3939
localStorage.clear(); // And always clean up the localStorage after the test is completed
4040
} catch (e) {}

0 commit comments

Comments
 (0)