summaryrefslogtreecommitdiffstats
path: root/remote/test/puppeteer/test/src/click.spec.ts
blob: 8e253f30857597d4e771a4d9167fddb129492d4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/**
 * Copyright 2018 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import expect from 'expect';
import {KnownDevices} from 'puppeteer';

import {
  getTestState,
  setupTestBrowserHooks,
  setupTestPageAndContextHooks,
} from './mocha-utils.js';
import {attachFrame} from './utils.js';

describe('Page.click', function () {
  setupTestBrowserHooks();
  setupTestPageAndContextHooks();
  it('should click the button', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/input/button.html');
    await page.click('button');
    expect(
      await page.evaluate(() => {
        return (globalThis as any).result;
      })
    ).toBe('Clicked');
  });
  it('should click svg', async () => {
    const {page} = getTestState();

    await page.setContent(`
        <svg height="100" width="100">
          <circle onclick="javascript:window.__CLICKED=42" cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
        </svg>
      `);
    await page.click('circle');
    expect(
      await page.evaluate(() => {
        return (globalThis as any).__CLICKED;
      })
    ).toBe(42);
  });
  it('should click the button if window.Node is removed', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/input/button.html');
    await page.evaluate(() => {
      // @ts-expect-error Expected.
      return delete window.Node;
    });
    await page.click('button');
    expect(
      await page.evaluate(() => {
        return (globalThis as any).result;
      })
    ).toBe('Clicked');
  });
  // @see https://github.com/puppeteer/puppeteer/issues/4281
  it('should click on a span with an inline element inside', async () => {
    const {page} = getTestState();

    await page.setContent(`
        <style>
        span::before {
          content: 'q';
        }
        </style>
        <span onclick='javascript:window.CLICKED=42'></span>
      `);
    await page.click('span');
    expect(
      await page.evaluate(() => {
        return (globalThis as any).CLICKED;
      })
    ).toBe(42);
  });
  it('should not throw UnhandledPromiseRejection when page closes', async () => {
    const {page} = getTestState();

    const newPage = await page.browser().newPage();
    await Promise.all([newPage.close(), newPage.mouse.click(1, 2)]).catch(
      () => {}
    );
  });
  it('should click the button after navigation', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/input/button.html');
    await page.click('button');
    await page.goto(server.PREFIX + '/input/button.html');
    await page.click('button');
    expect(
      await page.evaluate(() => {
        return (globalThis as any).result;
      })
    ).toBe('Clicked');
  });
  it('should click with disabled javascript', async () => {
    const {page, server} = getTestState();

    await page.setJavaScriptEnabled(false);
    await page.goto(server.PREFIX + '/wrappedlink.html');
    await Promise.all([page.click('a'), page.waitForNavigation()]);
    expect(page.url()).toBe(server.PREFIX + '/wrappedlink.html#clicked');
  });
  it('should scroll and click with disabled javascript', async () => {
    const {page, server} = getTestState();

    await page.setJavaScriptEnabled(false);
    await page.goto(server.PREFIX + '/wrappedlink.html');
    const body = await page.waitForSelector('body');
    await body!.evaluate(el => {
      el.style.paddingTop = '3000px';
    });
    await Promise.all([page.click('a'), page.waitForNavigation()]);
    expect(page.url()).toBe(server.PREFIX + '/wrappedlink.html#clicked');
  });
  it('should click when one of inline box children is outside of viewport', async () => {
    const {page} = getTestState();

    await page.setContent(`
        <style>
        i {
          position: absolute;
          top: -1000px;
        }
        </style>
        <span onclick='javascript:window.CLICKED = 42;'><i>woof</i><b>doggo</b></span>
      `);
    await page.click('span');
    expect(
      await page.evaluate(() => {
        return (globalThis as any).CLICKED;
      })
    ).toBe(42);
  });
  it('should select the text by triple clicking', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/input/textarea.html');
    await page.focus('textarea');
    const text =
      "This is the text that we are going to try to select. Let's see how it goes.";
    await page.keyboard.type(text);
    await page.evaluate(() => {
      (window as any).clicks = [];
      window.addEventListener('click', event => {
        return (window as any).clicks.push(event.detail);
      });
    });
    await page.click('textarea', {count: 3});
    expect(
      await page.evaluate(() => {
        return (window as any).clicks;
      })
    ).toMatchObject({0: 1, 1: 2, 2: 3});
    expect(
      await page.evaluate(() => {
        const textarea = document.querySelector('textarea');
        return textarea!.value.substring(
          textarea!.selectionStart,
          textarea!.selectionEnd
        );
      })
    ).toBe(text);
  });
  it('should click offscreen buttons', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/offscreenbuttons.html');
    const messages: any[] = [];
    page.on('console', msg => {
      if (msg.type() === 'log') {
        return messages.push(msg.text());
      }
      return;
    });
    for (let i = 0; i < 11; ++i) {
      // We might've scrolled to click a button - reset to (0, 0).
      await page.evaluate(() => {
        return window.scrollTo(0, 0);
      });
      await page.click(`#btn${i}`);
    }
    expect(messages).toEqual([
      'button #0 clicked',
      'button #1 clicked',
      'button #2 clicked',
      'button #3 clicked',
      'button #4 clicked',
      'button #5 clicked',
      'button #6 clicked',
      'button #7 clicked',
      'button #8 clicked',
      'button #9 clicked',
      'button #10 clicked',
    ]);
  });

  it('should click wrapped links', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/wrappedlink.html');
    await page.click('a');
    expect(
      await page.evaluate(() => {
        return (globalThis as any).__clicked;
      })
    ).toBe(true);
  });

  it('should click on checkbox input and toggle', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/input/checkbox.html');
    expect(
      await page.evaluate(() => {
        return (globalThis as any).result.check;
      })
    ).toBe(null);
    await page.click('input#agree');
    expect(
      await page.evaluate(() => {
        return (globalThis as any).result.check;
      })
    ).toBe(true);
    expect(
      await page.evaluate(() => {
        return (globalThis as any).result.events;
      })
    ).toEqual([
      'mouseover',
      'mouseenter',
      'mousemove',
      'mousedown',
      'mouseup',
      'click',
      'input',
      'change',
    ]);
    await page.click('input#agree');
    expect(
      await page.evaluate(() => {
        return (globalThis as any).result.check;
      })
    ).toBe(false);
  });

  it('should click on checkbox label and toggle', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/input/checkbox.html');
    expect(
      await page.evaluate(() => {
        return (globalThis as any).result.check;
      })
    ).toBe(null);
    await page.click('label[for="agree"]');
    expect(
      await page.evaluate(() => {
        return (globalThis as any).result.check;
      })
    ).toBe(true);
    expect(
      await page.evaluate(() => {
        return (globalThis as any).result.events;
      })
    ).toEqual(['click', 'input', 'change']);
    await page.click('label[for="agree"]');
    expect(
      await page.evaluate(() => {
        return (globalThis as any).result.check;
      })
    ).toBe(false);
  });

  it('should fail to click a missing button', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/input/button.html');
    let error!: Error;
    await page.click('button.does-not-exist').catch(error_ => {
      return (error = error_);
    });
    expect(error.message).toBe(
      'No element found for selector: button.does-not-exist'
    );
  });
  // @see https://github.com/puppeteer/puppeteer/issues/161
  it('should not hang with touch-enabled viewports', async () => {
    const {page} = getTestState();

    await page.setViewport(KnownDevices['iPhone 6'].viewport);
    await page.mouse.down();
    await page.mouse.move(100, 10);
    await page.mouse.up();
  });
  it('should scroll and click the button', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/input/scrollable.html');
    await page.click('#button-5');
    expect(
      await page.evaluate(() => {
        return document.querySelector('#button-5')!.textContent;
      })
    ).toBe('clicked');
    await page.click('#button-80');
    expect(
      await page.evaluate(() => {
        return document.querySelector('#button-80')!.textContent;
      })
    ).toBe('clicked');
  });
  it('should double click the button', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/input/button.html');
    await page.evaluate(() => {
      (globalThis as any).double = false;
      const button = document.querySelector('button');
      button!.addEventListener('dblclick', () => {
        (globalThis as any).double = true;
      });
    });
    const button = (await page.$('button'))!;
    await button!.click({count: 2});
    expect(await page.evaluate('double')).toBe(true);
    expect(await page.evaluate('result')).toBe('Clicked');
  });
  it('should click a partially obscured button', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/input/button.html');
    await page.evaluate(() => {
      const button = document.querySelector('button');
      button!.textContent = 'Some really long text that will go offscreen';
      button!.style.position = 'absolute';
      button!.style.left = '368px';
    });
    await page.click('button');
    expect(
      await page.evaluate(() => {
        return (globalThis as any).result;
      })
    ).toBe('Clicked');
  });
  it('should click a rotated button', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/input/rotatedButton.html');
    await page.click('button');
    expect(
      await page.evaluate(() => {
        return (globalThis as any).result;
      })
    ).toBe('Clicked');
  });
  it('should fire contextmenu event on right click', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/input/scrollable.html');
    await page.click('#button-8', {button: 'right'});
    expect(
      await page.evaluate(() => {
        return document.querySelector('#button-8')!.textContent;
      })
    ).toBe('context menu');
  });
  it('should fire aux event on middle click', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/input/scrollable.html');
    await page.click('#button-8', {button: 'middle'});
    expect(
      await page.evaluate(() => {
        return document.querySelector('#button-8')!.textContent;
      })
    ).toBe('aux click');
  });
  it('should fire back click', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/input/scrollable.html');
    await page.click('#button-8', {button: 'back'});
    expect(
      await page.evaluate(() => {
        return document.querySelector('#button-8')!.textContent;
      })
    ).toBe('back click');
  });
  it('should fire forward click', async () => {
    const {page, server} = getTestState();

    await page.goto(server.PREFIX + '/input/scrollable.html');
    await page.click('#button-8', {button: 'forward'});
    expect(
      await page.evaluate(() => {
        return document.querySelector('#button-8')!.textContent;
      })
    ).toBe('forward click');
  });
  // @see https://github.com/puppeteer/puppeteer/issues/206
  it('should click links which cause navigation', async () => {
    const {page, server} = getTestState();

    await page.setContent(`<a href="${server.EMPTY_PAGE}">empty.html</a>`);
    // This await should not hang.
    await page.click('a');
  });
  it('should click the button inside an iframe', async () => {
    const {page, server} = getTestState();

    await page.goto(server.EMPTY_PAGE);
    await page.setContent('<div style="width:100px;height:100px">spacer</div>');
    await attachFrame(
      page,
      'button-test',
      server.PREFIX + '/input/button.html'
    );
    const frame = page.frames()[1];
    const button = await frame!.$('button');
    await button!.click();
    expect(
      await frame!.evaluate(() => {
        return (globalThis as any).result;
      })
    ).toBe('Clicked');
  });
  // @see https://github.com/puppeteer/puppeteer/issues/4110
  it('should click the button with fixed position inside an iframe', async () => {
    const {page, server} = getTestState();

    await page.goto(server.EMPTY_PAGE);
    await page.setViewport({width: 500, height: 500});
    await page.setContent(
      '<div style="width:100px;height:2000px">spacer</div>'
    );
    await attachFrame(
      page,
      'button-test',
      server.CROSS_PROCESS_PREFIX + '/input/button.html'
    );
    const frame = page.frames()[1];
    await frame!.$eval('button', (button: Element) => {
      return (button as HTMLElement).style.setProperty('position', 'fixed');
    });
    await frame!.click('button');
    expect(
      await frame!.evaluate(() => {
        return (globalThis as any).result;
      })
    ).toBe('Clicked');
  });
  it('should click the button with deviceScaleFactor set', async () => {
    const {page, server} = getTestState();

    await page.setViewport({width: 400, height: 400, deviceScaleFactor: 5});
    expect(
      await page.evaluate(() => {
        return window.devicePixelRatio;
      })
    ).toBe(5);
    await page.setContent('<div style="width:100px;height:100px">spacer</div>');
    await attachFrame(
      page,
      'button-test',
      server.PREFIX + '/input/button.html'
    );
    const frame = page.frames()[1];
    const button = await frame!.$('button');
    await button!.click();
    expect(
      await frame!.evaluate(() => {
        return (globalThis as any).result;
      })
    ).toBe('Clicked');
  });
});