How to use connectToWebView 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.

androidDispatcher.js

Source: androidDispatcher.js Github

copy
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.AndroidSocketDispatcher = exports.AndroidDispatcher = exports.AndroidDeviceDispatcher = void 0;
7
8var _dispatcher = require("./dispatcher");
9
10var _android = require("../server/android/android");
11
12var _browserContextDispatcher = require("./browserContextDispatcher");
13
14/**
15 * Copyright (c) Microsoft Corporation.
16 *
17 * Licensed under the Apache License, Version 2.0 (the 'License");
18 * you may not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS,
25 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
28 */
29class AndroidDispatcher extends _dispatcher.Dispatcher {
30  constructor(scope, android) {
31    super(scope, android, 'Android', {}, true);
32    this._type_Android = true;
33  }
34
35  async devices(params) {
36    const devices = await this._object.devices();
37    return {
38      devices: devices.map(d => AndroidDeviceDispatcher.from(this._scope, d))
39    };
40  }
41
42  async setDefaultTimeoutNoReply(params) {
43    this._object.setDefaultTimeout(params.timeout);
44  }
45
46}
47
48exports.AndroidDispatcher = AndroidDispatcher;
49
50class AndroidDeviceDispatcher extends _dispatcher.Dispatcher {
51  static from(scope, device) {
52    const result = (0, _dispatcher.existingDispatcher)(device);
53    return result || new AndroidDeviceDispatcher(scope, device);
54  }
55
56  constructor(scope, device) {
57    super(scope, device, 'AndroidDevice', {
58      model: device.model,
59      serial: device.serial
60    }, true);
61    this._type_EventTarget = true;
62    this._type_AndroidDevice = true;
63
64    for (const webView of device.webViews()) this._dispatchEvent('webViewAdded', {
65      webView
66    });
67
68    device.on(_android.AndroidDevice.Events.WebViewAdded, webView => this._dispatchEvent('webViewAdded', {
69      webView
70    }));
71    device.on(_android.AndroidDevice.Events.WebViewRemoved, pid => this._dispatchEvent('webViewRemoved', {
72      pid
73    }));
74  }
75
76  async wait(params) {
77    await this._object.send('wait', params);
78  }
79
80  async fill(params) {
81    await this._object.send('click', {
82      selector: params.selector
83    });
84    await this._object.send('fill', params);
85  }
86
87  async tap(params) {
88    await this._object.send('click', params);
89  }
90
91  async drag(params) {
92    await this._object.send('drag', params);
93  }
94
95  async fling(params) {
96    await this._object.send('fling', params);
97  }
98
99  async longTap(params) {
100    await this._object.send('longClick', params);
101  }
102
103  async pinchClose(params) {
104    await this._object.send('pinchClose', params);
105  }
106
107  async pinchOpen(params) {
108    await this._object.send('pinchOpen', params);
109  }
110
111  async scroll(params) {
112    await this._object.send('scroll', params);
113  }
114
115  async swipe(params) {
116    await this._object.send('swipe', params);
117  }
118
119  async info(params) {
120    return {
121      info: await this._object.send('info', params)
122    };
123  }
124
125  async inputType(params) {
126    const text = params.text;
127    const keyCodes = [];
128
129    for (let i = 0; i < text.length; ++i) {
130      const code = keyMap.get(text[i].toUpperCase());
131      if (code === undefined) throw new Error('No mapping for ' + text[i] + ' found');
132      keyCodes.push(code);
133    }
134
135    await Promise.all(keyCodes.map(keyCode => this._object.send('inputPress', {
136      keyCode
137    })));
138  }
139
140  async inputPress(params) {
141    if (!keyMap.has(params.key)) throw new Error('Unknown key: ' + params.key);
142    await this._object.send('inputPress', {
143      keyCode: keyMap.get(params.key)
144    });
145  }
146
147  async inputTap(params) {
148    await this._object.send('inputClick', params);
149  }
150
151  async inputSwipe(params) {
152    await this._object.send('inputSwipe', params);
153  }
154
155  async inputDrag(params) {
156    await this._object.send('inputDrag', params);
157  }
158
159  async screenshot(params) {
160    return {
161      binary: (await this._object.screenshot()).toString('base64')
162    };
163  }
164
165  async shell(params) {
166    return {
167      result: (await this._object.shell(params.command)).toString('base64')
168    };
169  }
170
171  async open(params, metadata) {
172    const socket = await this._object.open(params.command);
173    return {
174      socket: new AndroidSocketDispatcher(this._scope, socket)
175    };
176  }
177
178  async installApk(params) {
179    await this._object.installApk(Buffer.from(params.file, 'base64'), {
180      args: params.args
181    });
182  }
183
184  async push(params) {
185    await this._object.push(Buffer.from(params.file, 'base64'), params.path, params.mode);
186  }
187
188  async launchBrowser(params) {
189    const context = await this._object.launchBrowser(params.pkg, params);
190    return {
191      context: new _browserContextDispatcher.BrowserContextDispatcher(this._scope, context)
192    };
193  }
194
195  async close(params) {
196    await this._object.close();
197  }
198
199  async setDefaultTimeoutNoReply(params) {
200    this._object.setDefaultTimeout(params.timeout);
201  }
202
203  async connectToWebView(params) {
204    return {
205      context: new _browserContextDispatcher.BrowserContextDispatcher(this._scope, await this._object.connectToWebView(params.pid))
206    };
207  }
208
209}
210
211exports.AndroidDeviceDispatcher = AndroidDeviceDispatcher;
212
213class AndroidSocketDispatcher extends _dispatcher.Dispatcher {
214  constructor(scope, socket) {
215    super(scope, socket, 'AndroidSocket', {}, true);
216    this._type_AndroidSocket = true;
217    socket.on('data', data => this._dispatchEvent('data', {
218      data: data.toString('base64')
219    }));
220    socket.on('close', () => {
221      this._dispatchEvent('close');
222
223      this._dispose();
224    });
225  }
226
227  async write(params, metadata) {
228    await this._object.write(Buffer.from(params.data, 'base64'));
229  }
230
231  async close(params, metadata) {
232    this._object.close();
233  }
234
235}
236
237exports.AndroidSocketDispatcher = AndroidSocketDispatcher;
238const keyMap = new Map([['Unknown', 0], ['SoftLeft', 1], ['SoftRight', 2], ['Home', 3], ['Back', 4], ['Call', 5], ['EndCall', 6], ['0', 7], ['1', 8], ['2', 9], ['3', 10], ['4', 11], ['5', 12], ['6', 13], ['7', 14], ['8', 15], ['9', 16], ['Star', 17], ['*', 17], ['Pound', 18], ['#', 18], ['DialUp', 19], ['DialDown', 20], ['DialLeft', 21], ['DialRight', 22], ['DialCenter', 23], ['VolumeUp', 24], ['VolumeDown', 25], ['Power', 26], ['Camera', 27], ['Clear', 28], ['A', 29], ['B', 30], ['C', 31], ['D', 32], ['E', 33], ['F', 34], ['G', 35], ['H', 36], ['I', 37], ['J', 38], ['K', 39], ['L', 40], ['M', 41], ['N', 42], ['O', 43], ['P', 44], ['Q', 45], ['R', 46], ['S', 47], ['T', 48], ['U', 49], ['V', 50], ['W', 51], ['X', 52], ['Y', 53], ['Z', 54], ['Comma', 55], [',', 55], ['Period', 56], ['.', 56], ['AltLeft', 57], ['AltRight', 58], ['ShiftLeft', 59], ['ShiftRight', 60], ['Tab', 61], ['\t', 61], ['Space', 62], [' ', 62], ['Sym', 63], ['Explorer', 64], ['Envelop', 65], ['Enter', 66], ['Del', 67], ['Grave', 68], ['Minus', 69], ['-', 69], ['Equals', 70], ['=', 70], ['LeftBracket', 71], ['(', 71], ['RightBracket', 72], [')', 72], ['Backslash', 73], ['\\', 73], ['Semicolon', 74], [';', 74], ['Apostrophe', 75], ['`', 75], ['Slash', 76], ['/', 76], ['At', 77], ['@', 77], ['Num', 78], ['HeadsetHook', 79], ['Focus', 80], ['Plus', 81], ['Menu', 82], ['Notification', 83], ['Search', 84], ['AppSwitch', 187], ['Assist', 219], ['Cut', 277], ['Copy', 278], ['Paste', 279]]);
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)