UNPKG

19.5 kBPlain TextView Raw
1import { expectError, expectType } from 'tsd';
2import { ExtendDescribeThis, NightwatchAPI } from '..';
3
4type Metrics = { [metricName: string]: number };
5
6//
7// .setGeolocation
8//
9describe('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 }) // sets the geolocation to Tokyo, Japan
17 .navigateTo('https://www.gps-coordinates.net/my-location')
18 .pause(3000)
19 .setGeolocation() // resets the geolocation
20 .navigateTo('https://www.gps-coordinates.net/my-location')
21 .pause(3000);
22 });
23
24 it('tests different ways of using setGeolocation', () => {
25 // with all parameters
26 browser.setGeolocation(
27 {
28 latitude: 35.689487,
29 longitude: 139.691706,
30 accuracy: 100,
31 },
32 function (result) {
33 expectType<NightwatchAPI>(this);
34 // without any parameter (resets the geolocation)
35 this.setGeolocation();
36 console.log(result.value);
37 }
38 );
39
40 // with only latitude and longitude
41 browser.setGeolocation({
42 latitude: 35.689487,
43 longitude: 139.691706,
44 });
45
46 // with just one parameter
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// .captureNetworkRequests
64//
65describe('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 // with all parameters
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 // without any parameter
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 // with all parameters
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 // without any parameter
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// .mockNetworkResponse
139//
140describe('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 // with all parameters
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 // without any parameter (invalid)
168 expectError(this.mockNetworkResponse())
169 console.log(result.value);
170 }
171 );
172
173 // with no response
174 browser.mockNetworkResponse('https://www.google.com/');
175
176 // with empty response
177 browser.mockNetworkResponse('https://www.google.com/', {});
178
179 // with just one parameter
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 // with all parameters
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 // without any parameter (invalid)
218 expectError(this.network.mockResponse())
219 console.log(result.value);
220 }
221 );
222
223 // with no response
224 browser.network.mockResponse('https://www.google.com/');
225
226 // with empty response
227 browser.network.mockResponse('https://www.google.com/', {});
228
229 // with just one parameter
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//.setNetworkConditions
244//
245describe('set network conditions', function () {
246 it('sets the network conditions', function () {
247 browser
248 .setNetworkConditions({
249 offline: false,
250 latency: 3000, // Additional latency (ms).
251 download_throughput: 500 * 1024, // Maximal aggregated download throughput.
252 upload_throughput: 500 * 1024, // Maximal aggregated upload throughput.
253 })
254 .navigateTo('https://www.google.com')
255 .pause(2000)
256 });
257
258 it('tests different ways of using setNetworkConditions', () => {
259 // with all parameters
260 browser.setNetworkConditions(
261 {
262 offline: false,
263 latency: 3000, // Additional latency (ms).
264 download_throughput: 500 * 1024, // Maximal aggregated download throughput.
265 upload_throughput: 500 * 1024, // Maximal aggregated upload throughput.
266 },
267 function (result) {
268 expectType<NightwatchAPI>(this);
269 // without any parameter (resets the network conditions)
270 // without any parameter (invalid)
271 expectError(this.setNetworkConditions())
272 // missing 'offline' parameter
273 expectError(this.setNetworkConditions({
274 latency: 3000,
275 download_throughput: 500 * 1024,
276 upload_throughput: 500 * 1024,
277 }));
278 // missing 'latency' parameter
279 expectError(this.setNetworkConditions({
280 offline: false,
281 download_throughput: 500 * 1024,
282 upload_throughput: 500 * 1024,
283 }));
284 // missing 'download_throughput' parameter
285 expectError(this.setNetworkConditions({
286 offline: false,
287 latency: 3000,
288 upload_throughput: 500 * 1024,
289 }));
290 // missing 'upload_throughput' parameter
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, // Additional latency (ms).
307 download_throughput: 500 * 1024, // Maximal aggregated download throughput.
308 upload_throughput: 500 * 1024, // Maximal aggregated upload throughput.
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, // Additional latency (ms).
319 download_throughput: 500 * 1024, // Maximal aggregated download throughput.
320 upload_throughput: 500 * 1024, // Maximal aggregated upload throughput.
321 })
322 .navigateTo('https://www.google.com')
323 .pause(2000)
324 });
325
326 it('tests different ways of using setNetworkConditions', () => {
327 // with all parameters
328 browser.network.setConditions(
329 {
330 offline: false,
331 latency: 3000, // Additional latency (ms).
332 download_throughput: 500 * 1024, // Maximal aggregated download throughput.
333 upload_throughput: 500 * 1024, // Maximal aggregated upload throughput.
334 },
335 function (result) {
336 expectType<NightwatchAPI>(this);
337 // without any parameter (resets the network conditions)
338 // without any parameter (invalid)
339 expectError(this.network.setConditions())
340 // missing 'offline' parameter
341 expectError(this.network.setConditions({
342 latency: 3000,
343 download_throughput: 500 * 1024,
344 upload_throughput: 500 * 1024,
345 }));
346 // missing 'latency' parameter
347 expectError(this.network.setConditions({
348 offline: false,
349 download_throughput: 500 * 1024,
350 upload_throughput: 500 * 1024,
351 }));
352 // missing 'download_throughput' parameter
353 expectError(this.network.setConditions({
354 offline: false,
355 latency: 3000,
356 upload_throughput: 500 * 1024,
357 }));
358 // missing 'upload_throughput' parameter
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, // Additional latency (ms).
375 download_throughput: 500 * 1024, // Maximal aggregated download throughput.
376 upload_throughput: 500 * 1024, // Maximal aggregated upload throughput.
377 });
378
379 expectType<null>(result);
380 });
381});
382
383//
384// .setDeviceDimensions
385//
386describe('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() // resets the device dimensions
398 .navigateTo('https://www.google.com')
399 .pause(1000);
400 });
401
402 it('tests different ways of using setDeviceDimensions', () => {
403 // with all parameters
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 // without any parameter (resets the dimensions)
414 this.setDeviceDimensions();
415 console.log(result.value);
416 }
417 );
418
419 // with only width and deviceScaleFactor
420 browser.setDeviceDimensions({
421 width: 400,
422 deviceScaleFactor: 50,
423 });
424
425 // with just one parameter
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// .getPerformanceMetrics
440//
441describe('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 // with all parameters
456 browser.getPerformanceMetrics(function (result) {
457 expectType<NightwatchAPI>(this);
458 // without any parameter
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// .enablePerformanceMetrics
482//
483describe('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 // with all parameters
498 browser.enablePerformanceMetrics(false, function (result) {
499 expectType<NightwatchAPI>(this);
500 // without any parameter
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// .takeHeapSnapshot
517//
518describe('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 // with all parameters
525 browser.takeHeapSnapshot('snap.heapsnapshot', function (result) {
526 expectType<NightwatchAPI>(this);
527 // without any parameter
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// .captureBrowserConsoleLogs
548//
549describe('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 // with all parameters
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 // without any parameter
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// .captureBrowserExceptions
598//
599describe('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 // with all parameters
638 browser.captureBrowserExceptions(
639 (event) => {
640 console.log('>>> Exception:', event.timestamp, event.exceptionDetails);
641 },
642 function (result) {
643 expectType<NightwatchAPI>(this);
644 // without any parameter
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});