Best JavaScript code snippet using qawolf
Connections.js
Source:Connections.js
1// Copyright (c) 2015 The Chromium Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4import * as Common from '../common/common.js';5import * as Host from '../host/host.js';6import * as ProtocolClient from '../protocol_client/protocol_client.js';7import {TargetManager} from './SDKModel.js';8/**9 * @implements {ProtocolClient.InspectorBackend.Connection}10 */11export class MainConnection {12 constructor() {13 this._onMessage = null;14 this._onDisconnect = null;15 this._messageBuffer = '';16 this._messageSize = 0;17 this._eventListeners = [18 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(19 Host.InspectorFrontendHostAPI.Events.DispatchMessage, this._dispatchMessage, this),20 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(21 Host.InspectorFrontendHostAPI.Events.DispatchMessageChunk, this._dispatchMessageChunk, this),22 ];23 }24 /**25 * @override26 * @param {function((!Object|string))} onMessage27 */28 setOnMessage(onMessage) {29 this._onMessage = onMessage;30 }31 /**32 * @override33 * @param {function(string)} onDisconnect34 */35 setOnDisconnect(onDisconnect) {36 this._onDisconnect = onDisconnect;37 }38 /**39 * @override40 * @param {string} message41 */42 sendRawMessage(message) {43 if (this._onMessage) {44 Host.InspectorFrontendHost.InspectorFrontendHostInstance.sendMessageToBackend(message);45 }46 }47 /**48 * @param {!Common.EventTarget.EventTargetEvent} event49 */50 _dispatchMessage(event) {51 if (this._onMessage) {52 this._onMessage.call(null, /** @type {string} */ (event.data));53 }54 }55 /**56 * @param {!Common.EventTarget.EventTargetEvent} event57 */58 _dispatchMessageChunk(event) {59 const messageChunk = /** @type {string} */ (event.data['messageChunk']);60 const messageSize = /** @type {number} */ (event.data['messageSize']);61 if (messageSize) {62 this._messageBuffer = '';63 this._messageSize = messageSize;64 }65 this._messageBuffer += messageChunk;66 if (this._messageBuffer.length === this._messageSize) {67 this._onMessage.call(null, this._messageBuffer);68 this._messageBuffer = '';69 this._messageSize = 0;70 }71 }72 /**73 * @override74 * @return {!Promise}75 */76 disconnect() {77 const onDisconnect = this._onDisconnect;78 Common.EventTarget.EventTarget.removeEventListeners(this._eventListeners);79 this._onDisconnect = null;80 this._onMessage = null;81 if (onDisconnect) {82 onDisconnect.call(null, 'force disconnect');83 }84 return Promise.resolve();85 }86}87/**88 * @implements {ProtocolClient.InspectorBackend.Connection}89 */90export class WebSocketConnection {91 /**92 * @param {string} url93 * @param {function()} onWebSocketDisconnect94 */95 constructor(url, onWebSocketDisconnect) {96 this._socket = new WebSocket(url);97 this._socket.onerror = this._onError.bind(this);98 this._socket.onopen = this._onOpen.bind(this);99 this._socket.onmessage = messageEvent => {100 if (this._onMessage) {101 this._onMessage.call(null, /** @type {string} */ (messageEvent.data));102 }103 };104 this._socket.onclose = this._onClose.bind(this);105 this._onMessage = null;106 this._onDisconnect = null;107 this._onWebSocketDisconnect = onWebSocketDisconnect;108 this._connected = false;109 this._messages = [];110 }111 /**112 * @override113 * @param {function((!Object|string))} onMessage114 */115 setOnMessage(onMessage) {116 this._onMessage = onMessage;117 }118 /**119 * @override120 * @param {function(string)} onDisconnect121 */122 setOnDisconnect(onDisconnect) {123 this._onDisconnect = onDisconnect;124 }125 _onError() {126 this._onWebSocketDisconnect.call(null);127 // This is called if error occurred while connecting.128 this._onDisconnect.call(null, 'connection failed');129 this._close();130 }131 _onOpen() {132 this._socket.onerror = console.error;133 this._connected = true;134 for (const message of this._messages) {135 this._socket.send(message);136 }137 this._messages = [];138 }139 _onClose() {140 this._onWebSocketDisconnect.call(null);141 this._onDisconnect.call(null, 'websocket closed');142 this._close();143 }144 /**145 * @param {function()=} callback146 */147 _close(callback) {148 this._socket.onerror = null;149 this._socket.onopen = null;150 this._socket.onclose = callback || null;151 this._socket.onmessage = null;152 this._socket.close();153 this._socket = null;154 this._onWebSocketDisconnect = null;155 }156 /**157 * @override158 * @param {string} message159 */160 sendRawMessage(message) {161 if (this._connected) {162 this._socket.send(message);163 } else {164 this._messages.push(message);165 }166 }167 /**168 * @override169 * @return {!Promise}170 */171 disconnect() {172 let fulfill;173 const promise = new Promise(f => fulfill = f);174 this._close(() => {175 if (this._onDisconnect) {176 this._onDisconnect.call(null, 'force disconnect');177 }178 fulfill();179 });180 return promise;181 }182}183/**184 * @implements {ProtocolClient.InspectorBackend.Connection}185 */186export class StubConnection {187 constructor() {188 this._onMessage = null;189 this._onDisconnect = null;190 }191 /**192 * @override193 * @param {function((!Object|string))} onMessage194 */195 setOnMessage(onMessage) {196 this._onMessage = onMessage;197 }198 /**199 * @override200 * @param {function(string)} onDisconnect201 */202 setOnDisconnect(onDisconnect) {203 this._onDisconnect = onDisconnect;204 }205 /**206 * @override207 * @param {string} message208 */209 sendRawMessage(message) {210 setTimeout(this._respondWithError.bind(this, message), 0);211 }212 /**213 * @param {string} message214 */215 _respondWithError(message) {216 const messageObject = JSON.parse(message);217 const error = {218 message: 'This is a stub connection, can\'t dispatch message.',219 code: ProtocolClient.InspectorBackend.DevToolsStubErrorCode,220 data: messageObject221 };222 if (this._onMessage) {223 this._onMessage.call(null, {id: messageObject.id, error: error});224 }225 }226 /**227 * @override228 * @return {!Promise}229 */230 disconnect() {231 if (this._onDisconnect) {232 this._onDisconnect.call(null, 'force disconnect');233 }234 this._onDisconnect = null;235 this._onMessage = null;236 return Promise.resolve();237 }238}239/**240 * @implements {ProtocolClient.InspectorBackend.Connection}241 */242export class ParallelConnection {243 /**244 * @param {!ProtocolClient.InspectorBackend.Connection} connection245 * @param {string} sessionId246 */247 constructor(connection, sessionId) {248 this._connection = connection;249 this._sessionId = sessionId;250 this._onMessage = null;251 this._onDisconnect = null;252 }253 /**254 * @override255 * @param {function(!Object)} onMessage256 */257 setOnMessage(onMessage) {258 this._onMessage = onMessage;259 }260 /**261 * @override262 * @param {function(string)} onDisconnect263 */264 setOnDisconnect(onDisconnect) {265 this._onDisconnect = onDisconnect;266 }267 /**268 * @override269 * @param {string} message270 */271 sendRawMessage(message) {272 const messageObject = JSON.parse(message);273 // If the message isn't for a specific session, it must be for the root session.274 if (!messageObject.sessionId) {275 messageObject.sessionId = this._sessionId;276 }277 this._connection.sendRawMessage(JSON.stringify(messageObject));278 }279 /**280 * @override281 * @return {!Promise}282 */283 disconnect() {284 if (this._onDisconnect) {285 this._onDisconnect.call(null, 'force disconnect');286 }287 this._onDisconnect = null;288 this._onMessage = null;289 return Promise.resolve();290 }291}292/**293 * @param {function():!Promise<undefined>} createMainTarget294 * @param {function()} websocketConnectionLost295 * @return {!Promise}296 */297export async function initMainConnection(createMainTarget, websocketConnectionLost) {298 ProtocolClient.InspectorBackend.Connection.setFactory(_createMainConnection.bind(null, websocketConnectionLost));299 await createMainTarget();300 Host.InspectorFrontendHost.InspectorFrontendHostInstance.connectionReady();301 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(302 Host.InspectorFrontendHostAPI.Events.ReattachMainTarget, () => {303 TargetManager.instance().mainTarget().router().connection().disconnect();304 createMainTarget();305 });306 return Promise.resolve();307}308/**309 * @param {function()} websocketConnectionLost310 * @return {!ProtocolClient.InspectorBackend.Connection}311 */312export function _createMainConnection(websocketConnectionLost) {313 const wsParam = Root.Runtime.queryParam('ws');314 const wssParam = Root.Runtime.queryParam('wss');315 if (wsParam || wssParam) {316 const ws = wsParam ? `ws://${wsParam}` : `wss://${wssParam}`;317 return new WebSocketConnection(ws, websocketConnectionLost);318 }319 if (Host.InspectorFrontendHost.InspectorFrontendHostInstance.isHostedMode()) {320 return new StubConnection();321 }322 return new MainConnection();...
Using AI Code Generation
1const qawolf = require('qawolf');2const { firefox } = require('playwright');3(async () => {4 const browser = await firefox.launch();5 const context = await browser.newContext();6 const page = await context.newPage();7 await page._onDisconnect();8 await qawolf.stopVideos();9 await browser.close();10})();11Your name to display (optional):12Your name to display (optional):13Hey @Shivani, the _onDisconnect() method is not available in the API of playwright. It is only available in the API of puppeteer. So, you can use the _onDisconnect() method o
Using AI Code Generation
1const { launch } = require("qawolf");2const { test, expect } = require("@playwright/test");3test("test", async ({ page }) => {4 await page.click("input[name=q]");5 await page.fill("input[name=q]", "qawolf");6 await page.press("input[name=q]", "Enter");7 await page.click("text=Create a free account");8 await page.click("input[name=email]");9 await page.fill("input[name=email]", "
Using AI Code Generation
1const qawolf = require("qawolf");2const browser = await qawolf.launch();3await qawolf.register(browser);4const qawolf = require("qawolf");5const browser = await qawolf.launch();6await qawolf.register(browser);7const qawolf = require("qawolf");8const browser = await qawolf.launch();9await qawolf.register(browser);10const context = browser.defaultBrowserContext();11const qawolf = require("qawolf");12const browser = await qawolf.launch();13await qawolf.register(browser);14const context = browser.defaultBrowserContext();
Using AI Code Generation
1const { _onDisconnect } = require('qawolf');2const { _onDisconnect } = require('qawolf');3const { _onDisconnect } = require('qawolf');4const { _onDisconnect } = require('qawolf');5const { _onDisconnect } = require('qawolf');6const { _onDisconnect } = require('qawolf');7const { _onDisconnect } = require('qawolf');8const { _onDisconnect } = require('qawolf');9const { _onDisconnect } = require('qawolf');10const { _onDisconnect } = require('qawolf');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!