How to use toButtonNumber method in Playwright Internal

Best JavaScript code snippet using playwright-internal

Run Playwright Internal automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

ffInput.js

Source: ffInput.js Github

copy
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.RawTouchscreenImpl = exports.RawMouseImpl = exports.RawKeyboardImpl = void 0;
7
8/**
9 * Copyright 2017 Google Inc. All rights reserved.
10 * Modifications copyright (c) Microsoft Corporation.
11 *
12 * Licensed under the Apache License, Version 2.0 (the 'License');
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 *     http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an 'AS IS' BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 */
24function toModifiersMask(modifiers) {
25  let mask = 0;
26  if (modifiers.has('Alt')) mask |= 1;
27  if (modifiers.has('Control')) mask |= 2;
28  if (modifiers.has('Shift')) mask |= 4;
29  if (modifiers.has('Meta')) mask |= 8;
30  return mask;
31}
32
33function toButtonNumber(button) {
34  if (button === 'left') return 0;
35  if (button === 'middle') return 1;
36  if (button === 'right') return 2;
37  return 0;
38}
39
40function toButtonsMask(buttons) {
41  let mask = 0;
42  if (buttons.has('left')) mask |= 1;
43  if (buttons.has('right')) mask |= 2;
44  if (buttons.has('middle')) mask |= 4;
45  return mask;
46}
47
48class RawKeyboardImpl {
49  constructor(client) {
50    this._client = void 0;
51    this._client = client;
52  }
53
54  async keydown(modifiers, code, keyCode, keyCodeWithoutLocation, key, location, autoRepeat, text) {
55    if (code === 'MetaLeft') code = 'OSLeft';
56    if (code === 'MetaRight') code = 'OSRight'; // Firefox will figure out Enter by itself
57
58    if (text === '\r') text = '';
59    await this._client.send('Page.dispatchKeyEvent', {
60      type: 'keydown',
61      keyCode: keyCodeWithoutLocation,
62      code,
63      key,
64      repeat: autoRepeat,
65      location,
66      text
67    });
68  }
69
70  async keyup(modifiers, code, keyCode, keyCodeWithoutLocation, key, location) {
71    if (code === 'MetaLeft') code = 'OSLeft';
72    if (code === 'MetaRight') code = 'OSRight';
73    await this._client.send('Page.dispatchKeyEvent', {
74      type: 'keyup',
75      key,
76      keyCode: keyCodeWithoutLocation,
77      code,
78      location,
79      repeat: false
80    });
81  }
82
83  async sendText(text) {
84    await this._client.send('Page.insertText', {
85      text
86    });
87  }
88
89}
90
91exports.RawKeyboardImpl = RawKeyboardImpl;
92
93class RawMouseImpl {
94  constructor(client) {
95    this._client = void 0;
96    this._client = client;
97  }
98
99  async move(x, y, button, buttons, modifiers) {
100    await this._client.send('Page.dispatchMouseEvent', {
101      type: 'mousemove',
102      button: 0,
103      buttons: toButtonsMask(buttons),
104      x,
105      y,
106      modifiers: toModifiersMask(modifiers)
107    });
108  }
109
110  async down(x, y, button, buttons, modifiers, clickCount) {
111    await this._client.send('Page.dispatchMouseEvent', {
112      type: 'mousedown',
113      button: toButtonNumber(button),
114      buttons: toButtonsMask(buttons),
115      x,
116      y,
117      modifiers: toModifiersMask(modifiers),
118      clickCount
119    });
120  }
121
122  async up(x, y, button, buttons, modifiers, clickCount) {
123    await this._client.send('Page.dispatchMouseEvent', {
124      type: 'mouseup',
125      button: toButtonNumber(button),
126      buttons: toButtonsMask(buttons),
127      x,
128      y,
129      modifiers: toModifiersMask(modifiers),
130      clickCount
131    });
132  }
133
134}
135
136exports.RawMouseImpl = RawMouseImpl;
137
138class RawTouchscreenImpl {
139  constructor(client) {
140    this._client = void 0;
141    this._client = client;
142  }
143
144  async tap(x, y, modifiers) {
145    await this._client.send('Page.dispatchTapEvent', {
146      x,
147      y,
148      modifiers: toModifiersMask(modifiers)
149    });
150  }
151
152}
153
154exports.RawTouchscreenImpl = RawTouchscreenImpl;
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Internal on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)