Best JavaScript code snippet using wpt
test_ext_webSocket.js
Source:test_ext_webSocket.js
1"use strict";2const HOSTS = new Set(["example.com"]);3Services.prefs.setBoolPref("extensions.manifestV3.enabled", true);4const server = createHttpServer({ hosts: HOSTS });5const BASE_URL = `http://example.com`;6const pageURL = `${BASE_URL}/plain.html`;7server.registerPathHandler("/plain.html", (request, response) => {8 response.setStatusLine(request.httpVersion, 200, "OK");9 response.setHeader("Content-Type", "text/html");10 response.setHeader("Content-Security-Policy", "upgrade-insecure-requests;");11 response.write("<!DOCTYPE html><html></html>");12});13async function testWebSocketInFrameUpgraded() {14 const frame = document.createElement("iframe");15 frame.src = browser.runtime.getURL("frame.html");16 document.documentElement.appendChild(frame);17}18// testIframe = true: open WebSocket from iframe (original test case).19// testIframe = false: open WebSocket from content script.20async function test_webSocket({ manifest_version, useIframe }) {21 let extension = ExtensionTestUtils.loadExtension({22 manifest: {23 manifest_version,24 permissions: ["webRequest", "webRequestBlocking"],25 host_permissions: ["<all_urls>"],26 granted_host_permissions: true,27 content_scripts: [28 {29 matches: ["http://*/plain.html"],30 run_at: "document_idle",31 js: [useIframe ? "content_script.js" : "load_WebSocket.js"],32 },33 ],34 },35 temporarilyInstalled: true,36 background() {37 browser.webRequest.onBeforeSendHeaders.addListener(38 details => {39 let header = details.requestHeaders.find(h => h.name === "Origin");40 browser.test.sendMessage("ws_request", {41 ws_scheme: new URL(details.url).protocol,42 originHeader: header?.value,43 });44 },45 { urls: ["wss://example.com/*", "ws://example.com/*"] },46 ["requestHeaders", "blocking"]47 );48 },49 files: {50 "frame.html": `51<html>52 <head>53 <meta charset="utf-8"/>54 <script src="load_WebSocket.js"></script>55 </head>56 <body>57 </body>58</html>59 `,60 "load_WebSocket.js": `new WebSocket("ws://example.com/ws_dummy");`,61 "content_script.js": `62 (${testWebSocketInFrameUpgraded})()63 `,64 },65 });66 await extension.startup();67 let contentPage = await ExtensionTestUtils.loadContentPage(pageURL);68 let { ws_scheme, originHeader } = await extension.awaitMessage("ws_request");69 if (useIframe || manifest_version == 2) {70 Assert.equal(ws_scheme, "ws:", "ws:-request should not have been upgraded");71 } else {72 Assert.equal(ws_scheme, "wss:", "WebSocket affected by page CSP in MV3");73 }74 if (useIframe) {75 Assert.equal(76 originHeader,77 `moz-extension://${extension.uuid}`,78 "Origin header of WebSocket request from extension page"79 );80 } else {81 Assert.equal(82 originHeader,83 manifest_version == 2 ? "null" : "http://example.com",84 "Origin header of WebSocket request from content script"85 );86 }87 await contentPage.close();88 await extension.unload();89}90add_task(async function test_webSocket_upgrade_iframe_mv2() {91 await test_webSocket({ manifest_version: 2, useIframe: true });92});93add_task(async function test_webSocket_upgrade_iframe_mv3() {94 await test_webSocket({ manifest_version: 3, useIframe: true });95});96add_task(async function test_webSocket_upgrade_in_contentscript_mv2() {97 await test_webSocket({ manifest_version: 2, useIframe: false });98});99add_task(async function test_webSocket_upgrade_in_contentscript_mv3() {100 await test_webSocket({ manifest_version: 3, useIframe: false });...
engineTest.js
Source:engineTest.js
1class EngineTest{2 constructor(){3 this.test_websocket =null;4 this.callback=null;5 }6 testEngine(){7 let access_token=null;8 let x=this;9 let url = "wss://wt.gaocloud.com";10 let hasResult = false;11 if(localStorage.hasOwnProperty("access_token")){12 access_token=localStorage.access_token;13 console.log('access_token:'+access_token);14 }15 this.test_websocket = new WebSocket(url,"browser."+access_token);16 this.test_websocket.onopen = () => {17 if(x.callback && !hasResult ){18 x.callback(true);19 hasResult = true;20 };21 }22 this.test_websocket.onError = () => {23 if(x.callback && !hasResult ){24 x.callback(false);25 hasResult = true;26 }27 }28 // æè¿æ£æµæ¶é´ä¸º10s29 setTimeout(()=>{30 if(x.callback && !hasResult ){31 x.callback(false);32 hasResult = true;33 }34 },3000);35 }36 onTestEngine(callback) {37 if (typeof callback === 'function') {38 this.callback = callback;39 }40 }41 disconnect(params){42 //this._websocket.close();43 this._websocket = null;44 this._buf_cmd_data=[];//æ¸
空ç¼åä¿¡æ¯45 }46}...
Using AI Code Generation
1var WebSocket = require('ws');2ws.on('open', function() {3 ws.send('something');4});5ws.on('message', function(message) {6 console.log('received: %s', message);7});8 at WebSocket.send (/home/username/workspace/learn/websocket/node_modules/ws/lib/WebSocket.js:249:14)9 at WebSocket.onOpen (/home/username/workspace/learn/websocket/test.js:6:7)10 at WebSocket.emit (events.js:98:17)11 at WebSocket.onHandshake (/home/username/workspace/learn/websocket/node_modules/ws/lib/WebSocket.js:468:10)12 at ClientRequest.<anonymous> (/home/username/workspace/learn/websocket/node_modules/ws/lib/WebSocket.js:452:18)13 at ClientRequest.g (events.js:180:16)14 at ClientRequest.emit (events.js:117:20)15 at HTTPParser.parserOnIncomingClient [as onIncoming] (http.js:1488:21)16 at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:111:23)17 at CleartextStream.socketOnData [as ondata] (http.js:1576:20)
Using AI Code Generation
1var WebSocket = require('ws');2ws.on('open', function open() {3 ws.send('test_websocket');4});5ws.on('message', function(data, flags) {6 console.log(data);7 ws.close();8 process.exit(0);9});10ws.on('close', function() {11 console.log('disconnected');12});13ws.on('error', function() {14 console.log('error');15});16ws.on('unexpected-response', function(request, response) {17 console.log('unexpected-response');18});
Using AI Code Generation
1var wptools = require('wptools');2wptools.test_websocket(function(err, data) {3 if (err) {4 console.log(err);5 } else {6 console.log(data);7 }8});9var wptools = require('wptools');10wptools.websocket(function(err, data) {11 if (err) {12 console.log(err);13 } else {14 console.log(data);15 }16});17var wptools = require('wptools');18wptools.websocket(function(err, data) {19 if (err) {20 console.log(err);21 } else {22 console.log(data);23 }24});25var wptools = require('wptools');26wptools.websocket(function(err, data) {27 if (err) {28 console.log(err);29 } else {30 console.log(data);31 }32});33var wptools = require('wptools');34wptools.websocket(function(err, data) {35 if (err) {36 console.log(err);37 } else {38 console.log(data);39 }40});41var wptools = require('wptools');42wptools.websocket(function(err, data) {43 if (err) {44 console.log(err);45 } else {46 console.log(data);47 }48});49var wptools = require('wptools');50wptools.websocket(function(err, data) {51 if (err) {52 console.log(err);53 } else {54 console.log(data);55 }56});57var wptools = require('wptools');58wptools.websocket(function(err, data) {59 if (err) {60 console.log(err);61 } else {62 console.log(data);63 }64});65var wptools = require('wptools');66wptools.websocket(function(err, data) {
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!!