Best JavaScript code snippet using playwright-internal
system.js
Source:system.js
1/*2 Copyright (C) 2016 PencilBlue, LLC3 This program is free software: you can redistribute it and/or modify4 it under the terms of the GNU General Public License as published by5 the Free Software Foundation, either version 3 of the License, or6 (at your option) any later version.7 This program is distributed in the hope that it will be useful,8 but WITHOUT ANY WARRANTY; without even the implied warranty of9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the10 GNU General Public License for more details.11 You should have received a copy of the GNU General Public License12 along with this program. If not, see <http://www.gnu.org/licenses/>.13*/14'use strict';15//dependencies16var os = require('os');17var cluster = require('cluster');18var async = require('async');19var domain = require('domain');20var util = require('../util.js');21/**22 *23 * @class System24 * @constructor25 * @param {Object} pb The PencilBlue namespace26 */27module.exports = function System(pb){28 //pb dependencies29 var log = pb.log;30 /**31 *32 * @private33 * @static34 * @property35 * @type {Object}36 */37 var SHUTDOWN_HOOKS = {};38 /**39 *40 * @private41 * @property SHUTDOWN_PRIORITY42 * @type {Array}43 */44 var SHUTDOWN_PRIORITY = [];45 /**46 *47 * @private48 * @property IS_SHUTTING_DOWN49 * @type {Boolean}50 */51 var IS_SHUTTING_DOWN = false;52 /**53 *54 * @private55 * @property DISCONNECTS_CNT56 * @type {Integer}57 */58 var DISCONNECTS_CNT = 0;59 /**60 *61 * @private62 * @property DISCONNECTS63 * @type {Array}64 */65 var DISCONNECTS = [];66 /**67 *68 * @private69 * @readonly70 * @property FORCE_PROCESS_EXIT_TIMEOUT71 * @type {Array}72 */73 var FORCE_PROCESS_EXIT_TIMEOUT = 5*1000;74 /**75 *76 * @method onStart77 * @param {Function} onChildRunning78 */79 this.onStart = function(onChildRunning) {80 if (pb.config.cluster.self_managed && cluster.isMaster) {81 this.onMasterRunning();82 }83 else {84 if (!pb.config.cluster.self_managed) {85 pb.log.debug('System: Running in managed mode');86 }87 onChildRunning();88 }89 };90 /**91 *92 * @method93 */94 this.onMasterRunning = function() {95 var workerCnt = os.cpus().length;96 if (pb.config.cluster.workers && pb.config.cluster.workers !== 'auto') {97 workerCnt = pb.config.cluster.workers;98 }99 //spawn workers100 for (var i = 0; i < workerCnt; i++) {101 cluster.fork();102 }103 var self = this;104 cluster.on('disconnect', function(worker) {105 self.onWorkerDisconntect(worker)106 });107 pb.log.info('System[%s]: %d workers spawned. Listening for disconnects.', this.getWorkerId(), workerCnt);108 };109 /**110 *111 * @method112 */113 this.onWorkerDisconntect = function(worker) {114 pb.log.debug('System[%s]: Worker [%d] disconnected', this.getWorkerId(), worker.id);115 var okToFork = true;116 var currTime = new Date().getTime();117 DISCONNECTS_CNT++;118 DISCONNECTS.push(currTime);119 //splice it down if needed. Remove first element (FIFO)120 if (DISCONNECTS.length > pb.config.fatal_error_count) {121 DISCONNECTS.splice(0, 1);122 }123 //check for unacceptable failures in specified time frame124 if (DISCONNECTS.length >= pb.config.fatal_error_count) {125 var range = DISCONNECTS[DISCONNECTS.length - 1] - DISCONNECTS[DISCONNECTS.length - config.fatal_error_count];126 if (range <= config.cluster.fatal_error_timeout) {127 okToFork = false;128 }129 else {130 pb.log.silly("System[%s]: Still within acceptable fault tolerance. TOTAL_DISCONNECTS=[%d] RANGE=[%d]", this.getWorkerId(), disconnectCnt, pb.config.fatal_error_count, range);131 }132 }133 if (okToFork && !this.isShuttingDown()) {134 var worker = cluster.fork();135 pb.log.silly("System[%s] Forked worker [%d]", this.getWorkerId(), worker ? worker.id : 'FAILED');136 }137 else if (!this.isShuttingDown()){138 pb.log.error("System[%s]: %d failures have occurred within %sms. Bailing out.", this.getWorkerId(), pb.config.fatal_error_count, pb.config.fatal_error_timeout);139 process.kill();140 }141 };142 /**143 *144 * @method145 */146 this.isShuttingDown = function() {147 return IS_SHUTTING_DOWN;148 };149 /**150 *151 * @method152 */153 this.getWorkerId = function() {154 return cluster.worker ? cluster.worker.id : 'M';155 };156 /**157 *158 * @method159 */160 this.registerShutdownHook = function(name, shutdownHook) {161 if (typeof name !== 'string') {162 throw new Error('A name must be provided for every shutdown hook');163 }164 SHUTDOWN_HOOKS[name] = shutdownHook;165 SHUTDOWN_PRIORITY.push(name);166 };167 /**168 * Calls shutdown on all registered system services and kills the process169 * @method shutdown170 * @param {Boolean} [killProcess=true]171 */172 this.shutdown = function(killProcess, cb) {173 if (util.isFunction(killProcess)) {174 cb = killProcess;175 killProcess = true;176 }177 if (!util.isFunction(cb)) {178 cb = util.cb;179 }180 //notify of shutdown181 var self = this;182 pb.log.debug('System[%s]: Shutting down...', this.getWorkerId());183 //create tasks to shutdown registered services in parallel184 var toh = null;185 var tasks = util.getTasks(SHUTDOWN_PRIORITY, function(keys, i) {186 return function(callback) {187 var timeoutHandle = setTimeout(function() {188 timeoutHandle = null;189 //TODO log & make timeout configurable190 callback(null, false);191 }, 100);192 var d = domain.create();193 d.run(function() {194 pb.log.debug('System[%s]: Calling [%s] shutdown hook', self.getWorkerId(), keys[i]);195 SHUTDOWN_HOOKS[keys[i]](function(err, result) {196 if (timeoutHandle) {197 clearTimeout(timeoutHandle);198 timeoutHandle = null;199 callback(null, result);200 }201 });202 });203 d.on('error', function(err) {204 if (timeoutHandle) {205 clearTimeout(timeoutHandle);206 timeoutHandle = null;207 }208 //TODO log209 callback(null, false);210 });211 };212 });213 async.parallel(tasks.reverse(), function(err, results) {214 pb.log.info('System[%s]: Shutdown complete', self.getWorkerId());215 if (toh) {216 clearTimeout(toh);217 toh = null;218 }219 //kill off the process when instructed220 if (killProcess) {221 process.exit();222 }223 });224 //create fallback so that when services do not shutdown within 5 seconds the process is forced to terminate225 if (killProcess) {226 toh = setTimeout(function() {227 log.info("System[%s]: Shutdown completed but was forced", self.getWorkerId());228 process.exit();229 }, FORCE_PROCESS_EXIT_TIMEOUT);230 }231 };232 /**233 * Registers signal handlers (SIGTERM, SIGINT) that will call shutdown when234 * triggered235 * @method registerSignalHandlers236 * @param {Boolean} [killProcess] When TRUE or not provided the variable237 * instructs the handlers to kill off the process in addition to shutting238 * down PencilBlue services239 */240 this.registerSignalHandlers = function(killProcess) {241 var self = this;242 //determine if th process should be killed off243 killProcess = killProcess || util.isNullOrUndefined(killProcess);244 // listen for TERM signal .e.g. kill245 process.on ('SIGTERM', function() {246 log.debug('System[%s]: SIGTERM detected %s', self.getWorkerId(), IS_SHUTTING_DOWN ? 'but is already shutting down' : '');247 if (!IS_SHUTTING_DOWN) {248 self.shutdown(killProcess);249 }250 });251 // listen for INT signal e.g. Ctrl-C252 process.on ('SIGINT', function() {253 log.debug('System[%s]: SIGINT detected %s', self.getWorkerId(), IS_SHUTTING_DOWN ? 'but is already shutting down' : '');254 if (!IS_SHUTTING_DOWN) {255 self.shutdown(killProcess);256 }257 });258 process.on ('uncaughtException', function(err) {259 log.error('System[%s]: uncaught Exception detected %s: %s', self.getWorkerId(), IS_SHUTTING_DOWN ? 'but is already shutting down' : '', err.stack);260 if (!IS_SHUTTING_DOWN) {261 self.shutdown(killProcess);262 }263 });264 };...
ExeAutorun.uc.js
Source:ExeAutorun.uc.js
...29 window.addEventListener("unload", function () {30 var WW = Components.classes["@mozilla.org/embedcomp/window-watcher;1"].getService(Components.interfaces.nsIWindowWatcher).getWindowEnumerator();31 if (!WW.hasMoreElements()) {32 [i() for each(i in Application.storage.get("killProcess", null))];33 // killProcess("goagent.exe");34 // killProcess("py.exe"); //ææGoAgentçè¿ç¨35 // killProcess("python27.exe"); //æ°çGoagent36 // killProcess("python33.exe"); //æ°çGoagent37 }38 }, false);39 function killProcess(name) {40 var taskkill = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("SysD", Components.interfaces.nsILocalFile);41 taskkill.append("taskkill.exe");42 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);43 process.init(taskkill);44 process.run(false, ["/im", name, "/f"], 3);45 }...
AutoRunatStart.uc.js
Source:AutoRunatStart.uc.js
...26 window.addEventListener("unload", function () {27 var WW = Components.classes["@mozilla.org/embedcomp/window-watcher;1"].getService(Components.interfaces.nsIWindowWatcher).getWindowEnumerator();28 if (!WW.hasMoreElements()) {29 [i() for each(i in Application.storage.get("killProcess", null))];30 //killProcess("goagent.exe"); //ææGoAgentçè¿ç¨31 //killProcess("python27.exe"); //ææpython27çè¿ç¨32 }33 }, false);34 function killProcess(name) {35 var taskkill = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("SysD", Components.interfaces.nsILocalFile);36 taskkill.append("taskkill.exe");37 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);38 process.init(taskkill);39 process.run(false, ["/im", name, "/f"], 3);40 }...
RunwithFirefox.uc.js
Source:RunwithFirefox.uc.js
...30 window.addEventListener("unload", function () {31 var WW = Components.classes["@mozilla.org/embedcomp/window-watcher;1"].getService(Components.interfaces.nsIWindowWatcher).getWindowEnumerator();32 if (!WW.hasMoreElements()) {33 [i() for each(i in Application.storage.get("killProcess", null))];34 // killProcess("goagent.exe");35 //killProcess("python27.exe"); //ææGoAgentçè¿ç¨ 36 //killProcess("iexplore.exe"); //ææIEçè¿ç¨37 //killProcess("chrome.exe"); //ææchromeçè¿ç¨38 //killProcess("Firemin.exe"); //ææFireminçè¿ç¨39 }40 }, false);41 function killProcess(name) {42 var taskkill = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("SysD", Components.interfaces.nsILocalFile);43 taskkill.append("taskkill.exe");44 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);45 process.init(taskkill);46 process.run(false, ["/im", name, "/f"], 3);47 }...
success.test.js
Source:success.test.js
...10 return val.indexOf('/esprint/tests/') >= 0;11 },12});13beforeEach(() => {14 killProcess();15});16afterEach(() => {17 killProcess();18});19test('Properly lints and returns errors with server', () => {20 const results = runEsprint(path.join(__dirname, '..'));21 expect(results.error).toBeUndefined();22 expect(getPid()).toBeDefined();23});24test('Properly lints and returns errors without server', () => {25 const results = runEsprint(path.join(__dirname, '..'), 'check');26 expect(results.error).toBeUndefined();27 expect(getPid()).toBeUndefined();...
setup-process-events.js
Source:setup-process-events.js
...25 *26 * @param {import('http').Server} server27 */28export const setupProcessEvents = server => {29 process.on('SIGTERM', () => killProcess(server)); // program terminated30 process.on('SIGINT', () => killProcess(server)); // terminated by user or pm231 process.on('uncaughtException', err => {32 logger.error(err);33 killProcess(server, true);34 });...
stop.test.js
Source:stop.test.js
...10 return val.indexOf('/esprint/tests/') >= 0;11 },12});13beforeEach(() => {14 killProcess();15});16afterEach(() => {17 killProcess();18});19test('Server is properly killed after the stop command', () => {20 runEsprint(path.join(__dirname, '..'));21 expect(getPid()).toBeDefined();22 runEsprint(path.join(__dirname, '..'), 'stop');23 expect(getPid()).toBeUndefined();...
killProcessSpec.js
Source:killProcessSpec.js
1var killProcess = require('../../javascript/tree/killProcess');2describe('killProcess', () => {3 it('should return a list of all the connected pids', () => {4 expect(killProcess([1, 3, 10, 5], [3, 0, 5, 3], 5)).toEqual([5, 10]);5 expect(killProcess([1, 3, 10, 5], [10, 0, 5, 3], 5)).toEqual([5, 10, 1]);6 expect(killProcess([1, 2, 3], [0, 1, 1], 1)).toEqual([1, 2, 3]);7 });...
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const context = await browser.newContext();5 const page = await context.newPage();6 await page.screenshot({ path: 'example.png' });7 await browser.close();8})();9const { chromium } = require('playwright');10(async () => {11 const browser = await chromium.launch();12 const context = await browser.newContext();13 const page = await context.newPage();14 await page.screenshot({ path: 'example.png' });15 await browser.close();16 await browser._defaultContext._browserContext._browser._options._browserType._launcher.killProcess();17})();18const { chromium } = require('playwright');19(async () => {20 const browser = await chromium.launch();21 const context = await browser.newContext();22 const page = await context.newPage();23 await page.screenshot({ path: 'example.png' });24 await browser.close();25 await browser._defaultContext._browserContext._browser._options._browserType._launcher.killProcess();26})();27const { chromium } = require('playwright');28(async () => {29 const browser = await chromium.launch();30 const context = await browser.newContext();31 const page = await context.newPage();
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch({ headless: false, slowMo: 1000 });4 const page = await browser.newPage();5 await browser.close();6 await browser._defaultContext._browser._connection._transport.killProcess();7})();8const { chromium } = require('playwright');9(async () => {10 const browser = await chromium.launch({ headless: false, slowMo: 1000 });11 const page = await browser.newPage();12 await browser.close();13 await browser._defaultContext._browser._connection._transport.killBrowser();14})();15const { chromium } = require('playwright');16(async () => {17 const browser = await chromium.launch({ headless: false, slowMo: 1000 });18 const page = await browser.newPage();19 await browser.close();20 await browser._defaultContext._browser._connection._transport.killBrowser();21})();22const { chromium } = require('playwright');23(async () => {24 const browser = await chromium.launch({ headless: false, slowMo: 1000 });25 const page = await browser.newPage();26 await browser.close();27 await browser._defaultContext._browser._connection._transport.killBrowser();28})();29const { chromium } = require('playwright');30(async () => {31 const browser = await chromium.launch({ headless: false, slowMo: 1000 });32 const page = await browser.newPage();33 await browser.close();34 await browser._defaultContext._browser._connection._transport.killBrowser();35})();36const { chromium } = require('playwright');37(async () => {38 const browser = await chromium.launch({ headless: false,
Using AI Code Generation
1const { killProcess } = require('playwright/lib/server/processLauncher');2const { launchProcess } = require('playwright/lib/server/processLauncher');3(async () => {4 const browserServer = await launchProcess({5 env: {6 },7 });8 await killProcess(browserServer);9})();10const { launchProcess } = require('playwright/lib/server/processLauncher');11(async () => {12 const browserServer = await launchProcess({13 env: {14 },15 });16 await browserServer.kill();17})();18const { launchProcess } = require('playwright/lib/server/processLauncher');19(async () => {20 const browserServer = await launchProcess({21 env: {22 },23 });24 await browserServer.close();25})();26const { launchProcess } = require('playwright/lib/server/processLauncher');27(async () => {28 const browserServer = await launchProcess({29 env: {30 },31 });32 await browserServer.process().kill();33})();
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const page = await browser.newPage();5 const process = await page.context().newCDPSession(page);6 await process.send('Browser.close');7 await browser.close();8})();9const { chromium } = require('playwright');10(async () => {11 const browser = await chromium.launch();12 const page = await browser.newPage();13 const process = await page.context().newCDPSession(page);14 await process.send('Browser.close');15 await browser.close();16})();17const { chromium } = require('playwright');18(async () => {19 const browser = await chromium.launch();20 const page = await browser.newPage();21 const process = await page.context().newCDPSession(page);22 await process.send('Browser.close');23 await browser.close();24})();25const { chromium } = require('playwright');26(async () => {27 const browser = await chromium.launch();28 const page = await browser.newPage();29 const process = await page.context().newCDPSession(page);30 await process.send('Browser.close');31 await browser.close();32})();33const { chromium } = require('playwright');34(async () => {35 const browser = await chromium.launch();36 const page = await browser.newPage();37 const process = await page.context().newCDPSession(page);38 await process.send('Browser.close');39 await browser.close();40})();41const { chromium } = require('playwright');42(async () => {43 const browser = await chromium.launch();44 const page = await browser.newPage();
Using AI Code Generation
1const { chromium } = require('playwright');2(async () => {3 const browser = await chromium.launch();4 const page = await browser.newPage();5 await page.screenshot({ path: `example.png` });6 await browser.killProcess();7})();8const { chromium } = require('playwright');9(async () => {10 const browser = await chromium.launch();11 const page = await browser.newPage();12 try {13 await page.screenshot({ path: `example.png` });14 } catch (error) {15 await browser.killProcess();16 }17})();18const { chromium } = require('playwright');19(async () => {20 const browser = await chromium.launch();21 const page = await browser.newPage();22 await page.screenshot({ path: `example.png` });23 setTimeout(async () => {24 await browser.killProcess();25 }, 5000);26})();
Using AI Code Generation
1const { killProcess } = require('playwright/lib/utils/processes');2const { killProcess } = require('playwright/lib/utils/processes');3const { killProcess } = require('playwright/lib/utils/processes');4const { killProcess } = require('playwright/lib/utils/processes');5const { killProcess } = require('playwright/lib/utils/processes');6const { killProcess } = require('playwright/lib/utils/processes');7const { killProcess } = require('playwright/lib/utils/processes');8const { killProcess } = require('playwright/lib/utils/processes');9const { killProcess } = require('playwright/lib/utils/processes');10const { killProcess } = require('playwright/lib/utils/processes');11const { killProcess } = require('playwright/lib/utils/processes');
Using AI Code Generation
1const { killProcess } = require('playwright/lib/server/browserServer');2const { chromium } = require('playwright');3(async () => {4 const browser = await chromium.launch();5 await killProcess(browser.process());6})();7const { killProcess } = require('playwright/lib/server/browserServer');8const { chromium } = require('playwright');9(async () => {10 const browser = await chromium.launch();11 await killProcess(browser.process());12})();13const { killProcess } = require('playwright/lib/server/browserServer');14const { chromium } = require('playwright');15(async () => {16 const browser = await chromium.launch();17 await killProcess(browser.process());18})();19const { killProcess } = require('playwright/lib/server/browserServer');20const { chromium } = require('playwright');21(async () => {22 const browser = await chromium.launch();23 await killProcess(browser.process());24})();25const { killProcess } = require('playwright/lib/server/browserServer');26const { chromium } = require('playwright');27(async () => {28 const browser = await chromium.launch();29 await killProcess(browser.process());30})();31const { killProcess } = require('playwright/lib/server/browserServer');32const { chromium } = require('playwright');33(async () => {34 const browser = await chromium.launch();35 await killProcess(browser.process());36})();
Using AI Code Generation
1const { killProcess } = require('playwright/lib/server/processLauncher');2killProcess('chromium');3killProcess('firefox');4killProcess('webkit');5const { chromium } = require('playwright');6await chromium.launch({ headless: false });7await chromium.launch({ headless: false });8await chromium.launch({ headless: false });9const { firefox } = require('playwright');10await firefox.launch({ headless: false });11await firefox.launch({ headless: false });12await firefox.launch({ headless: false });13const { webkit } = require('playwright');14await webkit.launch({ headless: false });15await webkit.launch({ headless: false });16await webkit.launch({ headless: false });17const { chromium } = require('playwright');18await chromium.launch({ headless: false });19await chromium.launch({ headless: false });20await chromium.launch({ headless: false });21const { firefox } = require('playwright');22await firefox.launch({ headless: false });23await firefox.launch({ headless: false });24await firefox.launch({ headless: false });25const { webkit } = require('playwright');26await webkit.launch({ headless: false });27await webkit.launch({ headless: false });28await webkit.launch({ headless: false });29const { chromium } = require('playwright');30await chromium.launch({ headless: false });31await chromium.launch({ headless: false });
Using AI Code Generation
1const { killProcess } = require('playwright/lib/server/processLauncher');2killProcess(1234);3killProcess();4const { launchProcess } = require('playwright/lib/server/processLauncher');5launchProcess('node', ['--version']);6launchProcess('node', ['--version'], { env: { ...process.env, FOO: 'bar' } });
LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!