1 | import { expectError, expectType } from 'tsd';
|
2 | import { ExtendDescribeThis, NightwatchAPI } from '..';
|
3 |
|
4 | type Metrics = { [metricName: string]: number };
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | describe('mock geolocation', function () {
|
10 | it('sets the geolocation to Tokyo, Japan and then resets it', () => {
|
11 | browser
|
12 | .setGeolocation({
|
13 | latitude: 35.689487,
|
14 | longitude: 139.691706,
|
15 | accuracy: 100,
|
16 | })
|
17 | .navigateTo('https://www.gps-coordinates.net/my-location')
|
18 | .pause(3000)
|
19 | .setGeolocation()
|
20 | .navigateTo('https://www.gps-coordinates.net/my-location')
|
21 | .pause(3000);
|
22 | });
|
23 |
|
24 | it('tests different ways of using setGeolocation', () => {
|
25 |
|
26 | browser.setGeolocation(
|
27 | {
|
28 | latitude: 35.689487,
|
29 | longitude: 139.691706,
|
30 | accuracy: 100,
|
31 | },
|
32 | function (result) {
|
33 | expectType<NightwatchAPI>(this);
|
34 |
|
35 | this.setGeolocation();
|
36 | console.log(result.value);
|
37 | }
|
38 | );
|
39 |
|
40 |
|
41 | browser.setGeolocation({
|
42 | latitude: 35.689487,
|
43 | longitude: 139.691706,
|
44 | });
|
45 |
|
46 |
|
47 | expectError(browser.setGeolocation({
|
48 | latitude: 35.689487,
|
49 | }))
|
50 | });
|
51 |
|
52 | it('tests setGeolocation with async', async () => {
|
53 | const result = await browser.setGeolocation({
|
54 | latitude: 35.689487,
|
55 | longitude: 139.691706,
|
56 | });
|
57 |
|
58 | expectType<null>(result);
|
59 | });
|
60 | });
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | describe('capture network requests', function () {
|
66 | it('captures and logs network requests as they occur', function (this: ExtendDescribeThis<{ requestCount: number }>) {
|
67 | this.requestCount = 1;
|
68 | browser
|
69 | .captureNetworkRequests((requestParams) => {
|
70 | console.log('Request Number:', this.requestCount!++);
|
71 | console.log('Request URL:', requestParams.request.url);
|
72 | console.log('Request method:', requestParams.request.method);
|
73 | console.log('Request headers:', requestParams.request.headers);
|
74 | })
|
75 | .navigateTo('https://www.google.com');
|
76 | });
|
77 |
|
78 | it('tests different ways of using captureNetworkRequests', () => {
|
79 |
|
80 | browser.captureNetworkRequests(
|
81 | (requestParams) => {
|
82 | console.log('Request URL:', requestParams.request.url);
|
83 | console.log('Request method:', requestParams.request.method);
|
84 | console.log('Request headers:', requestParams.request.headers);
|
85 | },
|
86 | function (result) {
|
87 | expectType<NightwatchAPI>(this);
|
88 |
|
89 | expectError(this.captureNetworkRequests())
|
90 | console.log(result.value);
|
91 | }
|
92 | );
|
93 | });
|
94 |
|
95 | it('tests captureNetworkRequests with async', async () => {
|
96 | const result = await browser.captureNetworkRequests(() => {});
|
97 |
|
98 | expectType<null>(result);
|
99 | });
|
100 |
|
101 | it('captures and logs network requests as they occur', function (this: ExtendDescribeThis<{ requestCount: number }>) {
|
102 | this.requestCount = 1;
|
103 | browser
|
104 | .network.captureRequests((requestParams) => {
|
105 | console.log('Request Number:', this.requestCount!++);
|
106 | console.log('Request URL:', requestParams.request.url);
|
107 | console.log('Request method:', requestParams.request.method);
|
108 | console.log('Request headers:', requestParams.request.headers);
|
109 | })
|
110 | .navigateTo('https://www.google.com');
|
111 | });
|
112 |
|
113 | it('tests different ways of using captureRequests', () => {
|
114 |
|
115 | browser.network.captureRequests(
|
116 | (requestParams) => {
|
117 | console.log('Request URL:', requestParams.request.url);
|
118 | console.log('Request method:', requestParams.request.method);
|
119 | console.log('Request headers:', requestParams.request.headers);
|
120 | },
|
121 | function (result) {
|
122 | expectType<NightwatchAPI>(this);
|
123 |
|
124 | expectError(this.network.captureRequests())
|
125 | console.log(result.value);
|
126 | }
|
127 | );
|
128 | });
|
129 |
|
130 | it('tests captureRequests with async', async () => {
|
131 | const result = await browser.network.captureRequests(() => {});
|
132 |
|
133 | expectType<null>(result);
|
134 | });
|
135 | });
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | describe('mock network response', function () {
|
141 | it('intercepts the request made to Google search and mocks its response', function () {
|
142 | browser
|
143 | .mockNetworkResponse('https://www.google.com/', {
|
144 | status: 200,
|
145 | headers: {
|
146 | 'Content-Type': 'UTF-8',
|
147 | },
|
148 | body: 'Hello there!',
|
149 | })
|
150 | .navigateTo('https://www.google.com/')
|
151 | .pause(2000);
|
152 | });
|
153 |
|
154 | it('tests different ways of using mockNetworkResponse', () => {
|
155 |
|
156 | browser.mockNetworkResponse(
|
157 | 'https://www.google.com/',
|
158 | {
|
159 | status: 200,
|
160 | headers: {
|
161 | 'Content-Type': 'UTF-8',
|
162 | },
|
163 | body: 'Hello there!',
|
164 | },
|
165 | function (result) {
|
166 | expectType<NightwatchAPI>(this);
|
167 |
|
168 | expectError(this.mockNetworkResponse())
|
169 | console.log(result.value);
|
170 | }
|
171 | );
|
172 |
|
173 |
|
174 | browser.mockNetworkResponse('https://www.google.com/');
|
175 |
|
176 |
|
177 | browser.mockNetworkResponse('https://www.google.com/', {});
|
178 |
|
179 |
|
180 | browser.mockNetworkResponse('https://www.google.com/', {
|
181 | body: 'Hello there!',
|
182 | });
|
183 | });
|
184 |
|
185 | it('tests mockNetworkResponse with async', async () => {
|
186 | const result = await browser.mockNetworkResponse('https://www.google.com/');
|
187 |
|
188 | expectType<null>(result);
|
189 | });
|
190 |
|
191 | it('intercepts the request made to Google search and mocks its response', function () {
|
192 | browser
|
193 | .network.mockResponse('https://www.google.com/', {
|
194 | status: 200,
|
195 | headers: {
|
196 | 'Content-Type': 'UTF-8',
|
197 | },
|
198 | body: 'Hello there!',
|
199 | })
|
200 | .navigateTo('https://www.google.com/')
|
201 | .pause(2000);
|
202 | });
|
203 |
|
204 | it('tests different ways of using mockNetworkResponse', () => {
|
205 |
|
206 | browser.network.mockResponse(
|
207 | 'https://www.google.com/',
|
208 | {
|
209 | status: 200,
|
210 | headers: {
|
211 | 'Content-Type': 'UTF-8',
|
212 | },
|
213 | body: 'Hello there!',
|
214 | },
|
215 | function (result) {
|
216 | expectType<NightwatchAPI>(this);
|
217 |
|
218 | expectError(this.network.mockResponse())
|
219 | console.log(result.value);
|
220 | }
|
221 | );
|
222 |
|
223 |
|
224 | browser.network.mockResponse('https://www.google.com/');
|
225 |
|
226 |
|
227 | browser.network.mockResponse('https://www.google.com/', {});
|
228 |
|
229 |
|
230 | browser.network.mockResponse('https://www.google.com/', {
|
231 | body: 'Hello there!',
|
232 | });
|
233 | });
|
234 |
|
235 | it('tests mockResponse with async', async () => {
|
236 | const result = await browser.network.mockResponse('https://www.google.com/');
|
237 |
|
238 | expectType<null>(result);
|
239 | });
|
240 | });
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | describe('set network conditions', function () {
|
246 | it('sets the network conditions', function () {
|
247 | browser
|
248 | .setNetworkConditions({
|
249 | offline: false,
|
250 | latency: 3000,
|
251 | download_throughput: 500 * 1024,
|
252 | upload_throughput: 500 * 1024,
|
253 | })
|
254 | .navigateTo('https://www.google.com')
|
255 | .pause(2000)
|
256 | });
|
257 |
|
258 | it('tests different ways of using setNetworkConditions', () => {
|
259 |
|
260 | browser.setNetworkConditions(
|
261 | {
|
262 | offline: false,
|
263 | latency: 3000,
|
264 | download_throughput: 500 * 1024,
|
265 | upload_throughput: 500 * 1024,
|
266 | },
|
267 | function (result) {
|
268 | expectType<NightwatchAPI>(this);
|
269 |
|
270 |
|
271 | expectError(this.setNetworkConditions())
|
272 |
|
273 | expectError(this.setNetworkConditions({
|
274 | latency: 3000,
|
275 | download_throughput: 500 * 1024,
|
276 | upload_throughput: 500 * 1024,
|
277 | }));
|
278 |
|
279 | expectError(this.setNetworkConditions({
|
280 | offline: false,
|
281 | download_throughput: 500 * 1024,
|
282 | upload_throughput: 500 * 1024,
|
283 | }));
|
284 |
|
285 | expectError(this.setNetworkConditions({
|
286 | offline: false,
|
287 | latency: 3000,
|
288 | upload_throughput: 500 * 1024,
|
289 | }));
|
290 |
|
291 | expectError(this.setNetworkConditions({
|
292 | offline: false,
|
293 | latency: 3000,
|
294 | download_throughput: 500 * 1024,
|
295 | }));
|
296 |
|
297 | console.log(result.value);
|
298 | }
|
299 | );
|
300 |
|
301 | });
|
302 |
|
303 | it('tests setNetworkConditions with async', async () => {
|
304 | const result = await browser.setNetworkConditions({
|
305 | offline: false,
|
306 | latency: 3000,
|
307 | download_throughput: 500 * 1024,
|
308 | upload_throughput: 500 * 1024,
|
309 | });
|
310 |
|
311 | expectType<null>(result);
|
312 | });
|
313 |
|
314 | it('sets the network conditions', function () {
|
315 | browser
|
316 | .network.setConditions({
|
317 | offline: false,
|
318 | latency: 3000,
|
319 | download_throughput: 500 * 1024,
|
320 | upload_throughput: 500 * 1024,
|
321 | })
|
322 | .navigateTo('https://www.google.com')
|
323 | .pause(2000)
|
324 | });
|
325 |
|
326 | it('tests different ways of using setNetworkConditions', () => {
|
327 |
|
328 | browser.network.setConditions(
|
329 | {
|
330 | offline: false,
|
331 | latency: 3000,
|
332 | download_throughput: 500 * 1024,
|
333 | upload_throughput: 500 * 1024,
|
334 | },
|
335 | function (result) {
|
336 | expectType<NightwatchAPI>(this);
|
337 |
|
338 |
|
339 | expectError(this.network.setConditions())
|
340 |
|
341 | expectError(this.network.setConditions({
|
342 | latency: 3000,
|
343 | download_throughput: 500 * 1024,
|
344 | upload_throughput: 500 * 1024,
|
345 | }));
|
346 |
|
347 | expectError(this.network.setConditions({
|
348 | offline: false,
|
349 | download_throughput: 500 * 1024,
|
350 | upload_throughput: 500 * 1024,
|
351 | }));
|
352 |
|
353 | expectError(this.network.setConditions({
|
354 | offline: false,
|
355 | latency: 3000,
|
356 | upload_throughput: 500 * 1024,
|
357 | }));
|
358 |
|
359 | expectError(this.network.setConditions({
|
360 | offline: false,
|
361 | latency: 3000,
|
362 | download_throughput: 500 * 1024,
|
363 | }));
|
364 |
|
365 | console.log(result.value);
|
366 | }
|
367 | );
|
368 |
|
369 | });
|
370 |
|
371 | it('tests setConditions with async', async () => {
|
372 | const result = await browser.network.setConditions({
|
373 | offline: false,
|
374 | latency: 3000,
|
375 | download_throughput: 500 * 1024,
|
376 | upload_throughput: 500 * 1024,
|
377 | });
|
378 |
|
379 | expectType<null>(result);
|
380 | });
|
381 | });
|
382 |
|
383 |
|
384 |
|
385 |
|
386 | describe('modify device dimensions', function () {
|
387 | it('modifies the device dimensions and then resets it', function () {
|
388 | browser
|
389 | .setDeviceDimensions({
|
390 | width: 400,
|
391 | height: 600,
|
392 | deviceScaleFactor: 50,
|
393 | mobile: true,
|
394 | })
|
395 | .navigateTo('https://www.google.com')
|
396 | .pause(1000)
|
397 | .setDeviceDimensions()
|
398 | .navigateTo('https://www.google.com')
|
399 | .pause(1000);
|
400 | });
|
401 |
|
402 | it('tests different ways of using setDeviceDimensions', () => {
|
403 |
|
404 | browser.setDeviceDimensions(
|
405 | {
|
406 | width: 400,
|
407 | height: 600,
|
408 | deviceScaleFactor: 50,
|
409 | mobile: true,
|
410 | },
|
411 | function (result) {
|
412 | expectType<NightwatchAPI>(this);
|
413 |
|
414 | this.setDeviceDimensions();
|
415 | console.log(result.value);
|
416 | }
|
417 | );
|
418 |
|
419 |
|
420 | browser.setDeviceDimensions({
|
421 | width: 400,
|
422 | deviceScaleFactor: 50,
|
423 | });
|
424 |
|
425 |
|
426 | browser.setDeviceDimensions({
|
427 | mobile: true,
|
428 | });
|
429 | });
|
430 |
|
431 | it('tests setDeviceDimensions with async', async () => {
|
432 | const result = await browser.setDeviceDimensions();
|
433 |
|
434 | expectType<null>(result);
|
435 | });
|
436 | });
|
437 |
|
438 |
|
439 |
|
440 |
|
441 | describe('collect performance metrics', function () {
|
442 | it('enables the metrics collection, does some stuff and collects the metrics', function () {
|
443 | browser
|
444 | .enablePerformanceMetrics()
|
445 | .navigateTo('https://www.google.com')
|
446 | .getPerformanceMetrics((result) => {
|
447 | if (result.status === 0) {
|
448 | const metrics = result.value;
|
449 | console.log(metrics);
|
450 | }
|
451 | });
|
452 | });
|
453 |
|
454 | it('tests different ways of using getPerformanceMetrics', () => {
|
455 |
|
456 | browser.getPerformanceMetrics(function (result) {
|
457 | expectType<NightwatchAPI>(this);
|
458 |
|
459 | this.getPerformanceMetrics();
|
460 |
|
461 | if (result.status === 0) {
|
462 | const metrics = result.value;
|
463 | expectType<Metrics>(metrics);
|
464 | }
|
465 | });
|
466 | });
|
467 |
|
468 | it('tests getPerformanceMetrics with async', async () => {
|
469 | const result1 = await browser.getPerformanceMetrics();
|
470 | expectType<Metrics>(result1);
|
471 |
|
472 | const result2 = await browser
|
473 | .enablePerformanceMetrics()
|
474 | .navigateTo('https://www.google.com')
|
475 | .getPerformanceMetrics();
|
476 | expectType<Metrics>(result2);
|
477 | });
|
478 | });
|
479 |
|
480 |
|
481 |
|
482 |
|
483 | describe('collect performance metrics', function () {
|
484 | it('enables the metrics collection, does some stuff and collects the metrics', function () {
|
485 | browser
|
486 | .enablePerformanceMetrics()
|
487 | .navigateTo('https://www.google.com')
|
488 | .getPerformanceMetrics((result) => {
|
489 | if (result.status === 0) {
|
490 | const metrics = result.value;
|
491 | console.log(metrics);
|
492 | }
|
493 | });
|
494 | });
|
495 |
|
496 | it('tests different ways of using enablePerformanceMetrics', () => {
|
497 |
|
498 | browser.enablePerformanceMetrics(false, function (result) {
|
499 | expectType<NightwatchAPI>(this);
|
500 |
|
501 | this.enablePerformanceMetrics();
|
502 | console.log(result.value);
|
503 | });
|
504 | });
|
505 |
|
506 | it('tests enablePerformanceMetrics with async', async () => {
|
507 | const result1 = await browser.enablePerformanceMetrics();
|
508 | expectType<null>(result1);
|
509 |
|
510 | const result2 = await browser.enablePerformanceMetrics(false);
|
511 | expectType<null>(result2);
|
512 | });
|
513 | });
|
514 |
|
515 |
|
516 |
|
517 |
|
518 | describe('take heap snapshot', function () {
|
519 | it('takes heap snapshot and saves it as snap.heapsnapshot file', function () {
|
520 | browser.navigateTo('https://www.google.com').takeHeapSnapshot('./snap.heapsnapshot');
|
521 | });
|
522 |
|
523 | it('tests different ways of using takeHeapSnapshot', () => {
|
524 |
|
525 | browser.takeHeapSnapshot('snap.heapsnapshot', function (result) {
|
526 | expectType<NightwatchAPI>(this);
|
527 |
|
528 | this.takeHeapSnapshot();
|
529 |
|
530 | if (result.status === 0) {
|
531 | const snapshot = result.value;
|
532 | expectType<string>(snapshot);
|
533 | }
|
534 | });
|
535 | });
|
536 |
|
537 | it('tests takeHeapSnapshot with async', async () => {
|
538 | const result1 = await browser.takeHeapSnapshot();
|
539 | expectType<string>(result1);
|
540 |
|
541 | const result2 = await browser.navigateTo('https://www.google.com').takeHeapSnapshot('something.heapsnapshot');
|
542 | expectType<string>(result2);
|
543 | });
|
544 | });
|
545 |
|
546 |
|
547 |
|
548 |
|
549 | describe('capture console events', function () {
|
550 | it('captures and logs console.log event', function () {
|
551 | browser
|
552 | .captureBrowserConsoleLogs((event) => {
|
553 | console.log(event.type, event.timestamp, event.args[0].value);
|
554 | })
|
555 | .navigateTo('https://www.google.com')
|
556 | .executeScript(function () {
|
557 | console.log('here');
|
558 | }, []);
|
559 | });
|
560 |
|
561 | it('captures and logs console.log event using logs ns', function () {
|
562 | browser
|
563 | .logs.captureBrowserConsoleLogs((event) => {
|
564 | console.log(event.type, event.timestamp, event.args[0].value);
|
565 | })
|
566 | .navigateTo('https://www.google.com')
|
567 | .executeScript(function () {
|
568 | console.log('here');
|
569 | }, []);
|
570 | });
|
571 |
|
572 | it('tests different ways of using captureBrowserConsoleLogs', () => {
|
573 |
|
574 | browser.captureBrowserConsoleLogs(
|
575 | (event) => {
|
576 | console.log(event.type, event.timestamp, event.args[0].value);
|
577 | expectError(event.context);
|
578 | expectError(event.stackTrace);
|
579 | },
|
580 | function (result) {
|
581 | expectType<NightwatchAPI>(this);
|
582 |
|
583 | expectError(this.captureBrowserConsoleLogs())
|
584 | console.log(result.value);
|
585 | }
|
586 | );
|
587 | });
|
588 |
|
589 | it('tests captureBrowserConsoleLogs with async', async () => {
|
590 | const result = await browser.captureBrowserConsoleLogs(() => {});
|
591 |
|
592 | expectType<null>(result);
|
593 | });
|
594 | });
|
595 |
|
596 |
|
597 |
|
598 |
|
599 | describe('catch browser exceptions', function () {
|
600 | it('captures the js exceptions thrown in the browser', async function () {
|
601 | await browser.captureBrowserExceptions((event) => {
|
602 | console.log('>>> Exception:', event);
|
603 | });
|
604 |
|
605 | await browser.navigateTo('https://duckduckgo.com/');
|
606 |
|
607 | const searchBoxElement = await browser.findElement('input[name=q]');
|
608 | await browser.executeScript(
|
609 | function (_searchBoxElement) {
|
610 | expectError(_searchBoxElement.setAttribute('onclick', 'throw new Error("Hello world!")'))
|
611 | },
|
612 | [searchBoxElement]
|
613 | );
|
614 |
|
615 | await browser.elementIdClick(searchBoxElement.getId());
|
616 | });
|
617 |
|
618 | it('captures the js exceptions thrown in the browser ', async function () {
|
619 | await browser
|
620 | .logs.captureBrowserExceptions((event) => {
|
621 | console.log('>>> Exception:', event);
|
622 | })
|
623 | .navigateTo('https://duckduckgo.com/');
|
624 |
|
625 | const searchBoxElement = await browser.findElement('input[name=q]');
|
626 | await browser.executeScript(
|
627 | function (_searchBoxElement) {
|
628 | expectError(_searchBoxElement.setAttribute('onclick', 'throw new Error("Hello world!")'))
|
629 | },
|
630 | [searchBoxElement]
|
631 | );
|
632 |
|
633 | await browser.elementIdClick(searchBoxElement.getId());
|
634 | });
|
635 |
|
636 | it('tests different ways of using captureBrowserExceptions', () => {
|
637 |
|
638 | browser.captureBrowserExceptions(
|
639 | (event) => {
|
640 | console.log('>>> Exception:', event.timestamp, event.exceptionDetails);
|
641 | },
|
642 | function (result) {
|
643 | expectType<NightwatchAPI>(this);
|
644 |
|
645 | expectError(this.captureBrowserExceptions())
|
646 | console.log(result.value);
|
647 | }
|
648 | );
|
649 | });
|
650 |
|
651 | it('tests captureBrowserExceptions with async', async () => {
|
652 | const result = await browser.captureBrowserExceptions(() => {});
|
653 |
|
654 | expectType<null>(result);
|
655 | });
|
656 | });
|