You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
47
47
48
48
```javascript
49
-
it("should mock google.com using puppeteer api", asyncfunction () {
49
+
it("should mock google.com using puppeteer api", asyncfunction ({browser}) {
50
50
// Get puppeteer instance
51
-
constpuppeteer=awaitthis.browser.getPuppeteer();
51
+
constpuppeteer=awaitbrowser.getPuppeteer();
52
52
53
53
// Get the first open page (considering it to be currently active)
54
54
const [page] =awaitpuppeteer.pages();
@@ -69,7 +69,7 @@ it("should mock google.com using puppeteer api", async function () {
69
69
// Here, we could call "page.goto('https://google.com')", but it's better to call "url",
70
70
// because most plugins have wrappers for the "url" command adding additional logic.
71
71
// For example, in testplane, the URL is added to the meta.
72
-
awaitthis.browser.url("https://google.com");
72
+
awaitbrowser.url("https://google.com");
73
73
});
74
74
```
75
75
@@ -78,9 +78,9 @@ it("should mock google.com using puppeteer api", async function () {
78
78
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:
79
79
80
80
```javascript
81
-
it("should mock google.com using cdp fetch domain", asyncfunction () {
81
+
it("should mock google.com using cdp fetch domain", asyncfunction ({browser}) {
82
82
// Get puppeteer instance
83
-
constpuppeteer=awaitthis.browser.getPuppeteer();
83
+
constpuppeteer=awaitbrowser.getPuppeteer();
84
84
85
85
// Get the first open page (considering it to be currently active)
86
86
const [page] =awaitpuppeteer.pages();
@@ -113,7 +113,7 @@ it("should mock google.com using cdp fetch domain", async function () {
113
113
});
114
114
});
115
115
116
-
awaitthis.browser.url("https://google.com");
116
+
awaitbrowser.url("https://google.com");
117
117
});
118
118
```
119
119
@@ -122,14 +122,14 @@ Obviously, when using the _webdriverio_ API for mocking requests, the code is mu
122
122
## Example 2: Canceling the Request for Google's Logo {#example_2}
123
123
124
124
```javascript
125
-
it("should abort request to logo on google.com", asyncfunction () {
125
+
it("should abort request to logo on google.com", asyncfunction ({browser}) {
// Replace "Google" with "Yandex" using a regular expression
183
183
returnreq.body.replace(/Google/g, "Yandex");
184
184
});
185
185
186
-
awaitthis.browser.url("https://google.com");
186
+
awaitbrowser.url("https://google.com");
187
187
});
188
188
```
189
189
190
190
Additionally, we can modify responses from unknown sources in advance. For example, let's modify all scripts loaded on _google.com:_
191
191
192
192
```javascript
193
-
it("should modify response from google.com", asyncfunction () {
193
+
it("should modify response from google.com", asyncfunction ({browser}) {
194
194
// The first argument specifies that we will intercept all requests
195
-
constmock=awaitthis.browser.mock("**", {
195
+
constmock=awaitbrowser.mock("**", {
196
196
headers:headers=> {
197
197
// Filter only the requests where the "content-type"
198
198
// header contains values "text/javascript" or "application/javascript"
@@ -208,7 +208,7 @@ it("should modify response from google.com", async function () {
208
208
return (req.body+=`\nconsole.log("This script was modified in real time.");`);
209
209
});
210
210
211
-
awaitthis.browser.url("https://google.com");
211
+
awaitbrowser.url("https://google.com");
212
212
});
213
213
```
214
214
@@ -217,11 +217,11 @@ it("should modify response from google.com", async function () {
217
217
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:
218
218
219
219
```javascript
220
-
it("should mock yandex.ru and log all loaded urls", asyncfunction () {
220
+
it("should mock yandex.ru and log all loaded urls", asyncfunction ({browser}) {
221
221
// Intercept absolutely all requests
222
-
constmock=awaitthis.browser.mock("**");
222
+
constmock=awaitbrowser.mock("**");
223
223
224
-
awaitthis.browser.url("https://yandex.ru");
224
+
awaitbrowser.url("https://yandex.ru");
225
225
226
226
// mock.calls contains not only the visited URL information
227
227
// 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 () {
235
235
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:
236
236
237
237
```javascript
238
-
it("should mock yandex.ru and log all loaded urls (using puppeteer)", asyncfunction () {
238
+
it("should mock yandex.ru and log all loaded urls (using puppeteer)", asyncfunction ({browser}) {
239
239
// Accumulative list of all URLs
240
240
consturls= [];
241
241
@@ -249,7 +249,7 @@ it("should mock yandex.ru and log all loaded urls (using puppeteer)", async func
249
249
}
250
250
251
251
// Get puppeteer instance
252
-
constpuppeteer=awaitthis.browser.getPuppeteer();
252
+
constpuppeteer=awaitbrowser.getPuppeteer();
253
253
254
254
// Get all open pages at the current moment
255
255
constpages=awaitpuppeteer.pages();
@@ -274,10 +274,10 @@ it("should mock yandex.ru and log all loaded urls (using puppeteer)", async func
274
274
urlsHandler(page);
275
275
});
276
276
277
-
awaitthis.browser.url("https://yandex.ru");
277
+
awaitbrowser.url("https://yandex.ru");
278
278
279
279
// Find the first element in the list of services (at that time it was a football page)
Copy file name to clipboardExpand all lines: docs/guides/how-to-manage-cpu-performance.mdx
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,17 +18,17 @@ The CPU speed on mobile devices is significantly slower than on computers. There
18
18
Let's use this method to slow down CPU speed by 8 times:
19
19
20
20
```javascript
21
-
it("should open yandex.ru with emulation 8x slower CPU", asyncfunction () {
21
+
it("should open yandex.ru with emulation 8x slower CPU", asyncfunction ({browser}) {
22
22
// Get puppeteer instance
23
-
constpuppeteer=awaitthis.browser.getPuppeteer();
23
+
constpuppeteer=awaitbrowser.getPuppeteer();
24
24
25
25
// Get the first open page (considering it to be currently active)
26
26
const [page] =awaitpuppeteer.pages();
27
27
28
28
// Slow down the CPU speed by 8 times
29
29
awaitpage.emulateCPUThrottling(8);
30
30
31
-
awaitthis.browser.url("https://yandex.ru");
31
+
awaitbrowser.url("https://yandex.ru");
32
32
});
33
33
```
34
34
@@ -39,9 +39,9 @@ Initially, _webdriverio_ did not support the `page.emulateCPUThrottling` method
39
39
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:
40
40
41
41
```javascript
42
-
it("should open yandex.ru with emulation 8x slower CPU", asyncfunction () {
42
+
it("should open yandex.ru with emulation 8x slower CPU", asyncfunction ({browser}) {
43
43
// Get puppeteer instance
44
-
constpuppeteer=awaitthis.browser.getPuppeteer();
44
+
constpuppeteer=awaitbrowser.getPuppeteer();
45
45
46
46
// Get the first open page (considering it to be currently active)
47
47
const [page] =awaitpuppeteer.pages();
@@ -52,7 +52,7 @@ it("should open yandex.ru with emulation 8x slower CPU", async function () {
0 commit comments