Best JavaScript code snippet using wpt
browser_RemotePageManager.js
Source:browser_RemotePageManager.js
...5var { RemotePages, RemotePageManager } = Cu.import("resource://gre/modules/RemotePageManager.jsm", {});6function failOnMessage(message) {7 ok(false, "Should not have seen message " + message.name);8}9function waitForMessage(port, message, expectedPort = port) {10 return new Promise((resolve) => {11 function listener(message) {12 is(message.target, expectedPort, "Message should be from the right port.");13 port.removeMessageListener(listener);14 resolve(message);15 }16 port.addMessageListener(message, listener);17 });18}19function waitForPort(url, createTab = true) {20 return new Promise((resolve) => {21 RemotePageManager.addRemotePageListener(url, (port) => {22 RemotePageManager.removeRemotePageListener(url);23 waitForMessage(port, "RemotePage:Load").then(() => resolve(port));24 });25 if (createTab)26 gBrowser.selectedTab = gBrowser.addTab(url);27 });28}29function waitForPage(pages) {30 return new Promise((resolve) => {31 function listener({ target }) {32 pages.removeMessageListener("RemotePage:Init", listener);33 waitForMessage(target, "RemotePage:Load").then(() => resolve(target));34 }35 pages.addMessageListener("RemotePage:Init", listener);36 gBrowser.selectedTab = gBrowser.addTab(TEST_URL);37 });38}39function swapDocShells(browser1, browser2) {40 // Swap frameLoaders.41 browser1.swapDocShells(browser2);42 // Swap permanentKeys.43 let tmp = browser1.permanentKey;44 browser1.permanentKey = browser2.permanentKey;45 browser2.permanentKey = tmp;46}47// Test that opening a page creates a port, sends the load event and then48// navigating to a new page sends the unload event. Going back should create a49// new port50add_task(function* init_navigate() {51 let port = yield waitForPort(TEST_URL);52 is(port.browser, gBrowser.selectedBrowser, "Port is for the correct browser");53 let loaded = new Promise(resolve => {54 function listener() {55 gBrowser.selectedBrowser.removeEventListener("load", listener, true);56 resolve();57 }58 gBrowser.selectedBrowser.addEventListener("load", listener, true);59 gBrowser.loadURI("about:blank");60 });61 yield waitForMessage(port, "RemotePage:Unload");62 // Port should be destroyed now63 try {64 port.addMessageListener("Foo", failOnMessage);65 ok(false, "Should have seen exception");66 }67 catch (e) {68 ok(true, "Should have seen exception");69 }70 try {71 port.sendAsyncMessage("Foo");72 ok(false, "Should have seen exception");73 }74 catch (e) {75 ok(true, "Should have seen exception");76 }77 yield loaded;78 gBrowser.goBack();79 port = yield waitForPort(TEST_URL, false);80 port.sendAsyncMessage("Ping2");81 let message = yield waitForMessage(port, "Pong2");82 port.destroy();83 gBrowser.removeCurrentTab();84});85// Test that opening a page creates a port, sends the load event and then86// closing the tab sends the unload event87add_task(function* init_close() {88 let port = yield waitForPort(TEST_URL);89 is(port.browser, gBrowser.selectedBrowser, "Port is for the correct browser");90 let unloadPromise = waitForMessage(port, "RemotePage:Unload");91 gBrowser.removeCurrentTab();92 yield unloadPromise;93 // Port should be destroyed now94 try {95 port.addMessageListener("Foo", failOnMessage);96 ok(false, "Should have seen exception");97 }98 catch (e) {99 ok(true, "Should have seen exception");100 }101 try {102 port.sendAsyncMessage("Foo");103 ok(false, "Should have seen exception");104 }105 catch (e) {106 ok(true, "Should have seen exception");107 }108});109// Tests that we can send messages to individual pages even when more than one110// is open111add_task(function* multiple_ports() {112 let port1 = yield waitForPort(TEST_URL);113 is(port1.browser, gBrowser.selectedBrowser, "Port is for the correct browser");114 let port2 = yield waitForPort(TEST_URL);115 is(port2.browser, gBrowser.selectedBrowser, "Port is for the correct browser");116 port2.addMessageListener("Pong", failOnMessage);117 port1.sendAsyncMessage("Ping", { str: "foobar", counter: 0 });118 let message = yield waitForMessage(port1, "Pong");119 port2.removeMessageListener("Pong", failOnMessage);120 is(message.data.str, "foobar", "String should pass through");121 is(message.data.counter, 1, "Counter should be incremented");122 port1.addMessageListener("Pong", failOnMessage);123 port2.sendAsyncMessage("Ping", { str: "foobaz", counter: 5 });124 message = yield waitForMessage(port2, "Pong");125 port1.removeMessageListener("Pong", failOnMessage);126 is(message.data.str, "foobaz", "String should pass through");127 is(message.data.counter, 6, "Counter should be incremented");128 let unloadPromise = waitForMessage(port2, "RemotePage:Unload");129 gBrowser.removeTab(gBrowser.getTabForBrowser(port2.browser));130 yield unloadPromise;131 try {132 port2.addMessageListener("Pong", failOnMessage);133 ok(false, "Should not have been able to add a new message listener to a destroyed port.");134 }135 catch (e) {136 ok(true, "Should not have been able to add a new message listener to a destroyed port.");137 }138 port1.sendAsyncMessage("Ping", { str: "foobar", counter: 0 });139 message = yield waitForMessage(port1, "Pong");140 is(message.data.str, "foobar", "String should pass through");141 is(message.data.counter, 1, "Counter should be incremented");142 unloadPromise = waitForMessage(port1, "RemotePage:Unload");143 gBrowser.removeTab(gBrowser.getTabForBrowser(port1.browser));144 yield unloadPromise;145});146// Tests that swapping browser docshells doesn't break the ports147add_task(function* browser_switch() {148 let port1 = yield waitForPort(TEST_URL);149 is(port1.browser, gBrowser.selectedBrowser, "Port is for the correct browser");150 let browser1 = gBrowser.selectedBrowser;151 port1.sendAsyncMessage("SetCookie", { value: "om nom" });152 let port2 = yield waitForPort(TEST_URL);153 is(port2.browser, gBrowser.selectedBrowser, "Port is for the correct browser");154 let browser2 = gBrowser.selectedBrowser;155 port2.sendAsyncMessage("SetCookie", { value: "om nom nom" });156 port2.addMessageListener("Cookie", failOnMessage);157 port1.sendAsyncMessage("GetCookie");158 let message = yield waitForMessage(port1, "Cookie");159 port2.removeMessageListener("Cookie", failOnMessage);160 is(message.data.value, "om nom", "Should have the right cookie");161 port1.addMessageListener("Cookie", failOnMessage);162 port2.sendAsyncMessage("GetCookie", { str: "foobaz", counter: 5 });163 message = yield waitForMessage(port2, "Cookie");164 port1.removeMessageListener("Cookie", failOnMessage);165 is(message.data.value, "om nom nom", "Should have the right cookie");166 swapDocShells(browser1, browser2);167 is(port1.browser, browser2, "Should have noticed the swap");168 is(port2.browser, browser1, "Should have noticed the swap");169 // Cookies should have stayed the same170 port2.addMessageListener("Cookie", failOnMessage);171 port1.sendAsyncMessage("GetCookie");172 message = yield waitForMessage(port1, "Cookie");173 port2.removeMessageListener("Cookie", failOnMessage);174 is(message.data.value, "om nom", "Should have the right cookie");175 port1.addMessageListener("Cookie", failOnMessage);176 port2.sendAsyncMessage("GetCookie", { str: "foobaz", counter: 5 });177 message = yield waitForMessage(port2, "Cookie");178 port1.removeMessageListener("Cookie", failOnMessage);179 is(message.data.value, "om nom nom", "Should have the right cookie");180 swapDocShells(browser1, browser2);181 is(port1.browser, browser1, "Should have noticed the swap");182 is(port2.browser, browser2, "Should have noticed the swap");183 // Cookies should have stayed the same184 port2.addMessageListener("Cookie", failOnMessage);185 port1.sendAsyncMessage("GetCookie");186 message = yield waitForMessage(port1, "Cookie");187 port2.removeMessageListener("Cookie", failOnMessage);188 is(message.data.value, "om nom", "Should have the right cookie");189 port1.addMessageListener("Cookie", failOnMessage);190 port2.sendAsyncMessage("GetCookie", { str: "foobaz", counter: 5 });191 message = yield waitForMessage(port2, "Cookie");192 port1.removeMessageListener("Cookie", failOnMessage);193 is(message.data.value, "om nom nom", "Should have the right cookie");194 let unloadPromise = waitForMessage(port2, "RemotePage:Unload");195 gBrowser.removeTab(gBrowser.getTabForBrowser(browser2));196 yield unloadPromise;197 unloadPromise = waitForMessage(port1, "RemotePage:Unload");198 gBrowser.removeTab(gBrowser.getTabForBrowser(browser1));199 yield unloadPromise;200});201// Tests that removeMessageListener in chrome works202add_task(function* remove_chrome_listener() {203 let port = yield waitForPort(TEST_URL);204 is(port.browser, gBrowser.selectedBrowser, "Port is for the correct browser");205 // This relies on messages sent arriving in the same order. Pong will be206 // sent back before Pong2 so if removeMessageListener fails the test will fail207 port.addMessageListener("Pong", failOnMessage);208 port.removeMessageListener("Pong", failOnMessage);209 port.sendAsyncMessage("Ping", { str: "remove_listener", counter: 27 });210 port.sendAsyncMessage("Ping2");211 yield waitForMessage(port, "Pong2");212 let unloadPromise = waitForMessage(port, "RemotePage:Unload");213 gBrowser.removeCurrentTab();214 yield unloadPromise;215});216// Tests that removeMessageListener in content works217add_task(function* remove_content_listener() {218 let port = yield waitForPort(TEST_URL);219 is(port.browser, gBrowser.selectedBrowser, "Port is for the correct browser");220 // This relies on messages sent arriving in the same order. Pong3 would be221 // sent back before Pong2 so if removeMessageListener fails the test will fail222 port.addMessageListener("Pong3", failOnMessage);223 port.sendAsyncMessage("Ping3");224 port.sendAsyncMessage("Ping2");225 yield waitForMessage(port, "Pong2");226 let unloadPromise = waitForMessage(port, "RemotePage:Unload");227 gBrowser.removeCurrentTab();228 yield unloadPromise;229});230// Test RemotePages works231add_task(function* remote_pages_basic() {232 let pages = new RemotePages(TEST_URL);233 let port = yield waitForPage(pages);234 is(port.browser, gBrowser.selectedBrowser, "Port is for the correct browser");235 // Listening to global messages should work236 let unloadPromise = waitForMessage(pages, "RemotePage:Unload", port);237 gBrowser.removeCurrentTab();238 yield unloadPromise;239 pages.destroy();240 // RemotePages should be destroyed now241 try {242 pages.addMessageListener("Foo", failOnMessage);243 ok(false, "Should have seen exception");244 }245 catch (e) {246 ok(true, "Should have seen exception");247 }248 try {249 pages.sendAsyncMessage("Foo");250 ok(false, "Should have seen exception");251 }252 catch (e) {253 ok(true, "Should have seen exception");254 }255});256// Test sending messages to all remote pages works257add_task(function* remote_pages_multiple() {258 let pages = new RemotePages(TEST_URL);259 let port1 = yield waitForPage(pages);260 let port2 = yield waitForPage(pages);261 let pongPorts = [];262 yield new Promise((resolve) => {263 function listener({ name, target, data }) {264 is(name, "Pong", "Should have seen the right response.");265 is(data.str, "remote_pages", "String should pass through");266 is(data.counter, 43, "Counter should be incremented");267 pongPorts.push(target);268 if (pongPorts.length == 2)269 resolve();270 }271 pages.addMessageListener("Pong", listener);272 pages.sendAsyncMessage("Ping", { str: "remote_pages", counter: 42 });273 });274 // We don't make any guarantees about which order messages are sent to known275 // pages so the pongs could have come back in any order.276 isnot(pongPorts[0], pongPorts[1], "Should have received pongs from different ports");277 ok(pongPorts.indexOf(port1) >= 0, "Should have seen a pong from port1");278 ok(pongPorts.indexOf(port2) >= 0, "Should have seen a pong from port2");279 // After destroy we should see no messages280 pages.addMessageListener("RemotePage:Unload", failOnMessage);281 pages.destroy();282 gBrowser.removeTab(gBrowser.getTabForBrowser(port1.browser));283 gBrowser.removeTab(gBrowser.getTabForBrowser(port2.browser));284});285// Test sending various types of data across the boundary286add_task(function* send_data() {287 let port = yield waitForPort(TEST_URL);288 is(port.browser, gBrowser.selectedBrowser, "Port is for the correct browser");289 let data = {290 integer: 45,291 real: 45.78,292 str: "foobar",293 array: [1, 2, 3, 5, 27]294 };295 port.sendAsyncMessage("SendData", data);296 let message = yield waitForMessage(port, "ReceivedData");297 ok(message.data.result, message.data.status);298 gBrowser.removeCurrentTab();299});300// Test sending an object of data across the boundary301add_task(function* send_data2() {302 let port = yield waitForPort(TEST_URL);303 is(port.browser, gBrowser.selectedBrowser, "Port is for the correct browser");304 let data = {305 integer: 45,306 real: 45.78,307 str: "foobar",308 array: [1, 2, 3, 5, 27]309 };310 port.sendAsyncMessage("SendData2", {data});311 let message = yield waitForMessage(port, "ReceivedData2");312 ok(message.data.result, message.data.status);313 gBrowser.removeCurrentTab();...
start.js
Source:start.js
...17 opts.command,18 opts.options19 );20 if (opts.waitForMessage) {21 proc.stdout.on('data', function waitForMessage(data) {22 if (data.toString().match(opts.waitForMessage)) {23 if (callback) {24 callback();25 }26 }27 });28 }29 if (!opts.silent) {30 proc.stdout.pipe(process.stdout);31 proc.stderr.pipe(process.stderr);32 }33 if (opts.logFile) {34 var logStream = fs.createWriteStream(opts.logFile, {flags: 'a'});35 proc.stdout.pipe(logStream);...
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var waitForMessage = wptoolkit.waitForMessage;3var send = wptoolkit.send;4var sendSync = wptoolkit.sendSync;5var sendAsync = wptoolkit.sendAsync;6var sendAsyncWithCallback = wptoolkit.sendAsyncWithCallback;7var sendAsyncWithPromise = wptoolkit.sendAsyncWithPromise;8var worker = new Worker('worker.js');9waitForMessage(worker, 'message', function(e) {10 console.log('message received in main.js: ' + e.data);11});12worker.postMessage('hello from main.js');13send(worker, 'message', 'hello from main.js');14sendSync(worker, 'message', 'hello from main.js');15sendAsync(worker, 'message', 'hello from main.js');16sendAsyncWithCallback(worker, 'message', 'hello from main.js', function(e) {17 console.log('message received in main.js: ' + e.data);18});19sendAsyncWithPromise(worker, 'message', 'hello from main.js').then(function(e) {20 console.log('message received in main.js: ' + e.data);21});22onmessage = function(e) {23 console.log('message received in worker.js: ' + e.data);24 postMessage('hello from worker.js');25};26addEventListener('message', function(e) {27 console.log('message received in worker.js: ' + e.data);28 postMessage('hello from worker.js');29});30addEventListener('message', function(e) {31 console.log('message received in worker.js: ' + e.data);32 postMessage('hello from worker.js');33}, false);34addEventListener('message', function(e) {35 console.log('message received in worker.js: ' + e.data);
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2wp.waitForMessage(function (msg) {3 console.log(msg);4});5var wptoolkit = require('wptoolkit');6wp.send("Hello World");7Copyright (c) 2013, Srinivas Rajagopal
Using AI Code Generation
1var wpt = require('wptapi');2var wpt = new wpt('API_KEY');3wpt.waitForMessage('test', 10000, function(err, data) {4 if (err) {5 console.log('Error: ' + err);6 } else {7 console.log('Message: ' + data);8 }9});10var wpt = require('wptapi');11var wpt = new wpt('API_KEY');12wpt.waitForMessage('test', 10000, function(err, data) {13 if (err) {14 console.log('Error: ' + err);15 } else {16 console.log('Message: ' + data);17 }18});19var wpt = require('wptapi');20var wpt = new wpt('API_KEY');21wpt.waitForMessage('test', 10000, function(err, data) {22 if (err) {23 console.log('Error: ' + err);24 } else {25 console.log('Message: ' + data);26 }27});28var wpt = require('wptapi');29var wpt = new wpt('API_KEY');30wpt.waitForMessage('test', 10000, function(err, data) {31 if (err) {32 console.log('Error: ' + err);33 } else {34 console.log('Message: ' + data);35 }36});37var wpt = require('wptapi');38var wpt = new wpt('API_KEY');39wpt.waitForMessage('test', 10000, function(err, data) {40 if (err) {41 console.log('Error: ' + err);42 } else {43 console.log('Message: ' + data);44 }45});46var wpt = require('wptapi');47var wpt = new wpt('API_KEY');48wpt.waitForMessage('test', 10000, function(err, data) {49 if (err) {50 console.log('Error: ' +
Using AI Code Generation
1var wptoolkit = require("wptoolkit");2var wp = new wptoolkit();3wp.waitForMessage("test", function(){4 console.log("Message received");5});6$wptoolkit = new Wptoolkit();7$wptoolkit->sendMessage("test", "test");
Using AI Code Generation
1wpt.waitForMessage('message to wait for');2wpt.waitForMessage('message to wait for');3wpt.waitForMessage('message to wait for');4wpt.waitForMessage('message to wait for');5wpt.waitForMessage('message to wait for');6wpt.waitForMessage('message to wait for');7wpt.waitForMessage('message to wait for');8wpt.waitForMessage('message to wait for');9wpt.waitForMessage('message to wait for');10wpt.waitForMessage('message to wait for');11wpt.waitForMessage('message to wait for');
Using AI Code Generation
1var wpt = require('./wpt');2wpt.waitForMessage(function(msg) {3 wpt.sendMessage("closeTab");4});5I have a test that is supposed to run a script and then close the tab. The script is a simple one that just sends a message to the browser to close the tab. I'm using the waitForMessage() method to wait for the message from the browser, and then I send a message back to the browser to close the tab. The problem is that the test never finishes. I've tried using the waitForMessage() method with a timeout, but that doesn't seem to work either. Is there a way to get the test to finish when I send the message to the browser to close the tab?6I am having the same problem. I am trying to run a test that waits for a message from the browser, and then sends a message back to the browser to close the tab. The problem is that the test never finishes. I've tried using the waitForMessage() method with a timeout, but that doesn't seem to work either. Is there a way to get the test to finish when I send the message to the browser to close the tab?7I am having the same problem. I am trying to run a test that waits for a message from the browser, and then sends a message back to the browser to close the tab. The problem is that the test never finishes. I've tried using the waitForMessage() method with a timeout, but that doesn't seem to work either. Is there a way to get the test to finish when I send the message to the browser to close the tab?8I am having the same problem. I am trying to run a test that waits for a message from the browser, and then sends a message back to the browser to close the tab. The problem is that the test never finishes. I've tried using the waitForMessage() method with a timeout, but that doesn't seem to work either. Is there a way to get the test to finish when
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.9c9b6b8d8d0f6e5f5b5c5c5d5d5f5f5f5');3var options = { location: 'Dulles:Chrome', runs: 1, pollResults: 5, video: true };4var testId;5wpt.runTest(url, options, function(err, data) {6 if (err) return console.log(err);7 testId = data.data.testId;8 console.log('Test started: ' + testId);9 wpt.waitForMessage(testId, 'Test Complete', 600, function(err, data) {10 if (err) return console.log(err);11 console.log('Test Complete!');12 wpt.getTestResults(testId, function(err, data) {13 if (err) return console.log(err);14 console.log(data.data.runs[1].firstView.videoFrames);15 });16 });17});18var wpt = require('webpagetest');19var wpt = new WebPageTest('www.webpagetest.org', 'A.9c9b6b8d8d0f6e5f5b5c5c5d5d5f5f5f5');20var options = { location: 'Dulles:Chrome', runs: 1, pollResults: 5, video: true };21var testId;22wpt.runTest(url, options, function(err, data) {23 if (err) return console.log(err);24 testId = data.data.testId;25 console.log('Test started: ' + testId);26 wpt.waitForTestStart(testId, 600, function(err, data) {27 if (err) return console.log(err);28 console.log('Test Started!');29 wpt.waitForMessage(testId, 'Test Complete', 600, function(err, data) {30 if (err) return console.log(err);31 console.log('Test Complete!');32 wpt.getTestResults(testId, 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!!