Best JavaScript code snippet using devicefarmer-stf
launcher.spec.js
Source:launcher.spec.js
1'use strict'2const di = require('di')3const events = require('../../lib/events')4const launcher = require('../../lib/launcher')5const createMockTimer = require('./mocks/timer')6// promise mock7const stubPromise = (obj, method, stubAction) => {8 const promise = new Promise((resolve) => {9 obj[method].resolve = resolve10 })11 sinon.stub(obj, method).callsFake(() => {12 if (stubAction) stubAction()13 return promise14 })15}16function FakeBrowser (id, name, baseBrowserDecorator) {17 this.id = id18 this.name = name19 this.DEFAULT_CMD = {20 linux: '/script',21 darwin: '/script',22 win32: 'script.exe'23 }24 this.ENV_CMD = 'SCRIPT_BIN'25 baseBrowserDecorator(this)26 FakeBrowser._instances.push(this)27 sinon.stub(this, 'start').callsFake(() => {28 this.state = this.STATE_BEING_CAPTURED29 this._done()30 })31 stubPromise(this, 'forceKill')32 sinon.stub(this, 'restart')33}34function ScriptBrowser (id, name, baseBrowserDecorator) {35 this.id = id36 this.name = name37 this.DEFAULT_CMD = {38 linux: '/script',39 darwin: '/script',40 win32: 'script.exe'41 }42 this.ENV_CMD = 'SCRIPT_BIN'43 baseBrowserDecorator(this)44 ScriptBrowser._instances.push(this)45 sinon.stub(this, 'start').callsFake(() => {46 this.state = this.STATE_BEING_CAPTURED47 this._done()48 })49 stubPromise(this, 'forceKill')50 sinon.stub(this, 'restart')51}52describe('launcher', () => {53 // mock out id generator54 let lastGeneratedId = null55 launcher.Launcher.generateId = () => ++lastGeneratedId56 beforeEach(() => {57 lastGeneratedId = 058 FakeBrowser._instances = []59 ScriptBrowser._instances = []60 })61 describe('Launcher', () => {62 let emitter63 let server64 let config65 let l66 beforeEach(() => {67 emitter = new events.EventEmitter()68 server = { loadErrors: [] }69 config = {70 captureTimeout: 0,71 protocol: 'http:',72 hostname: 'localhost',73 port: 1234,74 urlRoot: '/root/'75 }76 const injector = new di.Injector([{77 'launcher:Fake': ['type', FakeBrowser],78 'launcher:Script': ['type', ScriptBrowser],79 server: ['value', server],80 emitter: ['value', emitter],81 config: ['value', config],82 timer: ['factory', createMockTimer]83 }])84 l = new launcher.Launcher(server, emitter, injector)85 })86 describe('launch', () => {87 it('should inject and start all browsers', (done) => {88 l.launch(['Fake'], 1)89 const browser = FakeBrowser._instances.pop()90 l.jobs.on('end', () => {91 expect(browser.start).to.have.been.calledWith('http://localhost:1234/root/')92 expect(browser.id).to.equal(lastGeneratedId)93 expect(browser.name).to.equal('Fake')94 done()95 })96 })97 describe('with upstream proxy settings', () => {98 beforeEach(() => {99 emitter = new events.EventEmitter()100 server = { loadErrors: [] }101 config = {102 captureTimeout: 0,103 protocol: 'http:',104 hostname: 'localhost',105 port: 1234,106 urlRoot: '/root/',107 upstreamProxy: {108 path: '/__proxy__/',109 hostname: 'proxy',110 port: '5678',111 protocol: 'https:'112 }113 }114 const injector = new di.Injector([{115 'launcher:Fake': ['type', FakeBrowser],116 'launcher:Script': ['type', ScriptBrowser],117 server: ['value', server],118 emitter: ['value', emitter],119 config: ['value', config],120 timer: ['factory', createMockTimer]121 }])122 l = new launcher.Launcher(server, emitter, injector)123 })124 it('should inject and start all browsers', (done) => {125 l.launch(['Fake'], 1)126 const browser = FakeBrowser._instances.pop()127 l.jobs.on('end', () => {128 expect(browser.start).to.have.been.calledWith('https://proxy:5678/__proxy__/root/')129 expect(browser.id).to.equal(lastGeneratedId)130 expect(browser.name).to.equal('Fake')131 done()132 })133 })134 })135 it('should not start when server has load errors', (done) => {136 server.loadErrors = ['error']137 l.launch(['Fake'], 1)138 l.jobs.on('end', () => {139 expect(FakeBrowser._instances).to.be.empty140 done()141 })142 })143 it('should allow launching a script', (done) => {144 l.launch(['/usr/local/bin/special-browser'], 1)145 const script = ScriptBrowser._instances.pop()146 l.jobs.on('end', () => {147 expect(script.start).to.have.been.calledWith('http://localhost:1234/root/')148 expect(script.name).to.equal('/usr/local/bin/special-browser')149 done()150 })151 })152 it('should use the non default host', (done) => {153 config.hostname = 'whatever'154 l.launch(['Fake'], 1)155 const browser = FakeBrowser._instances.pop()156 l.jobs.on('end', () => {157 expect(browser.start).to.have.been.calledWith('http://whatever:1234/root/')158 done()159 })160 })161 })162 describe('restart', () => {163 it('should restart the browser', () => {164 l.launch(['Fake'], 1)165 const browser = FakeBrowser._instances.pop()166 expect(l.restart(lastGeneratedId)).to.equal(true)167 expect(browser.restart).to.have.been.called168 })169 it('should return false if the browser was not launched by launcher (manual)', () => {170 l.launch([], 1)171 expect(l.restart('manual-id')).to.equal(false)172 })173 })174 describe('kill', () => {175 it('should kill browser with given id', (done) => {176 l.launch(['Fake'], 1)177 const browser = FakeBrowser._instances.pop()178 l.kill(browser.id, done)179 expect(browser.forceKill).to.have.been.called180 browser.forceKill.resolve()181 })182 it('should return false if browser does not exist, but still resolve the callback', (done) => {183 l.launch(['Fake'], 1)184 const browser = FakeBrowser._instances.pop()185 expect(l.kill('weird-id', done)).to.equal(false)186 expect(browser.forceKill).not.to.have.been.called187 })188 it('should not require a callback', (done) => {189 l.launch(['Fake'], 1)190 FakeBrowser._instances.pop()191 l.kill('weird-id')192 process.nextTick(done)193 })194 })195 describe('killAll', () => {196 it('should kill all running processe', () => {197 l.launch(['Fake', 'Fake'], 1)198 l.killAll()199 let browser = FakeBrowser._instances.pop()200 expect(browser.forceKill).to.have.been.called201 browser = FakeBrowser._instances.pop()202 expect(browser.forceKill).to.have.been.called203 })204 it('should call callback when all processes killed', (done) => {205 const exitSpy = sinon.spy()206 l.launch(['Fake', 'Fake'], 1)207 l.killAll(exitSpy)208 expect(exitSpy).not.to.have.been.called209 // finish the first browser210 let browser = FakeBrowser._instances.pop()211 browser.forceKill.resolve()212 setImmediate(() => {213 expect(exitSpy).not.to.have.been.called214 // finish the second browser215 browser = FakeBrowser._instances.pop()216 browser.forceKill.resolve()217 setImmediate(() => {218 expect(exitSpy).to.have.been.called219 done()220 })221 })222 })223 it('should call callback even if no browsers lanunched', (done) => {224 l.killAll(done)225 })226 })227 describe('areAllCaptured', () => {228 it('should return true if only if all browsers captured', () => {229 l._browsers = [{230 isCaptured: () => true231 }, {232 isCaptured: () => false233 }]234 expect(l.areAllCaptured()).to.be.equal(false)235 l._browsers = [{236 isCaptured: () => true237 }, {238 isCaptured: () => true239 }]240 expect(l.areAllCaptured()).to.be.equal(true)241 })242 })243 describe('markCaptured', () => {244 it('should not fail if an un-launched browser attaches', () => {245 expect(() => l.markCaptured('not-a-thing')).to.not.throw()246 })247 })248 describe('onExit', () => {249 it('should kill all browsers', (done) => {250 l.launch(['Fake', 'Fake'], 1)251 emitter.emitAsync('exit').then(done)252 let browser = FakeBrowser._instances.pop()253 browser.forceKill.resolve()254 browser = FakeBrowser._instances.pop()255 browser.forceKill.resolve()256 })257 })258 })...
_node-manage.script.js
Source:_node-manage.script.js
1// Manage purchased servers23var forceKill = false; // Default to NOT kill running subject script4if (args.length > 0) {5 forceKill = parseBoolean(args[0]); // Or accept second argument and forcefully kill any script6 tprint("INFO: Force killall set to: " + forceKill);7}89var execScript = "_farm-exp.script"; // Default to exp farming script10if (args.length > 1) {11 execScript = args[1]; // Or accept second argument as target script name12 tprint("INFO: Subject script set to: " + execScript);13}1415var servers = getPurchasedServers();1617while (servers.length > 0) {18 var target = servers.pop();1920 // If any scripts are running on the target already21 if (getServerRam(target)[1] > 0) {22 // If it is the right script, skip this target23 if (!forceKill && isRunning(execScript, target)) {24 tprint("Subject script already running, skipping target: " + target);25 continue;26 } else {27 // Otherwise kill all scripts28 tprint("Killing all scripts on: " + target);29 killall(target);30 }31 }3233 // Enslave the target34 if (execScript === "_farm-exp.script") {35 exec("_enslave.script", "home", 1, target);36 } else {37 // If non-default subject script, pass it along38 exec("_enslave.script", "home", 1, target, execScript);39 }404142 // Wait until enslave script is finished43 while (isRunning("_enslave.script", "home", target)) {44 sleep(1000);45 }4647 // Verify results48 if (isRunning(execScript, target)) {49 tprint("SUCCESS: Node managed: " + target);50 } else {51 tprint("ERROR: Management failed: " + target);52 }5354}5556function parseBoolean(str) {57 if (str == 'true') return true;58 else return false;
...
Using AI Code Generation
1var forceKill = require('devicefarmer-stf').forceKill;2forceKill('emulator-5554', 'com.android.chrome');3var forceStop = require('devicefarmer-stf').forceStop;4forceStop('emulator-5554', 'com.android.chrome');5var getBattery = require('devicefarmer-stf').getBattery;6getBattery('emulator-5554', function(err, battery) {7 if (err) {8 console.log(err);9 } else {10 console.log(battery);11 }12});13var getBattery = require('devicefarmer-stf').getBattery;14getBattery('emulator-5554', function(err, battery) {15 if (err) {16 console.log(err);17 } else {18 console.log(battery);19 }20});21var getDisplay = require('devicefarmer-stf').getDisplay;22getDisplay('emulator-5554', function(err, display) {23 if (err) {24 console.log(err);25 } else {26 console.log(display);27 }28});29var getDisplay = require('devicefarmer-stf').getDisplay;30getDisplay('emulator-5554', function(err, display) {31 if (err) {32 console.log(err);33 } else {34 console.log(display);35 }36});37var getDisplay = require('devicefarmer-stf').getDisplay;38getDisplay('emulator-5554', function(err, display) {39 if (err) {40 console.log(err);41 } else {42 console.log(display);43 }44});45var getDisplay = require('devicefarmer-stf').getDisplay;46getDisplay('emulator-5554', function(err, display) {47 if (err) {48 console.log(err);
Using AI Code Generation
1var deviceFarmer = require('devicefarmer-stf-client');2var deviceFarmerClient = new deviceFarmer.DeviceFarmerClient();3deviceFarmerClient.forceKill("deviceid");4var deviceFarmer = require('devicefarmer-stf-client');5var deviceFarmerClient = new deviceFarmer.DeviceFarmerClient();6deviceFarmerClient.forceStop("deviceid");7var deviceFarmer = require('devicefarmer-stf-client');8var deviceFarmerClient = new deviceFarmer.DeviceFarmerClient();9deviceFarmerClient.getDeviceStatus("deviceid");10var deviceFarmer = require('devicefarmer-stf-client');11var deviceFarmerClient = new deviceFarmer.DeviceFarmerClient();12deviceFarmerClient.getDeviceStatus("deviceid");13var deviceFarmer = require('devicefarmer-stf-client');14var deviceFarmerClient = new deviceFarmer.DeviceFarmerClient();15deviceFarmerClient.getDevices();16var deviceFarmer = require('devicefarmer-stf-client');17var deviceFarmerClient = new deviceFarmer.DeviceFarmerClient();18deviceFarmerClient.getDevices();19var deviceFarmer = require('devicefarmer-stf-client');20var deviceFarmerClient = new deviceFarmer.DeviceFarmerClient();21deviceFarmerClient.getDevices();22var deviceFarmer = require('devicefarmer-stf-client');23var deviceFarmerClient = new deviceFarmer.DeviceFarmerClient();24deviceFarmerClient.getDevices();25var deviceFarmer = require('devicefarmer-stf-client');
Using AI Code Generation
1const deviceFarmer = require('devicefarmer-stf');2const deviceFarmer = new DeviceFarmer();3deviceFarmer.forceKill('deviceID');4const deviceFarmer = require('devicefarmer-stf');5const deviceFarmer = new DeviceFarmer();6deviceFarmer.forceStop('deviceID');7const deviceFarmer = require('devicefarmer-stf');8const deviceFarmer = new DeviceFarmer();9deviceFarmer.reboot('deviceID');10const deviceFarmer = require('devicefarmer-stf');11const deviceFarmer = new DeviceFarmer();12deviceFarmer.install('deviceID', 'path/to/app.apk');13const deviceFarmer = require('devicefarmer-stf');14const deviceFarmer = new DeviceFarmer();15deviceFarmer.uninstall('deviceID', 'packageName');16const deviceFarmer = require('devicefarmer-stf');17const deviceFarmer = new DeviceFarmer();18deviceFarmer.clearData('deviceID', 'packageName');19const deviceFarmer = require('devicefarmer-stf');20const deviceFarmer = new DeviceFarmer();21deviceFarmer.clearCache('deviceID', 'packageName');22const deviceFarmer = require('devicefarmer-stf');23const deviceFarmer = new DeviceFarmer();24deviceFarmer.startApp('deviceID', 'packageName');25const deviceFarmer = require('devicefarmer-stf');26const deviceFarmer = new DeviceFarmer();27deviceFarmer.stopApp('deviceID', 'packageName');28const deviceFarmer = require('devicefarmer-stf');29const deviceFarmer = new DeviceFarmer();30deviceFarmer.getBatteryLevel('deviceID');
Using AI Code Generation
1var forceKill = require('devicefarmer-stf-provider').forceKill;2forceKill('deviceid', 'appname', function(err, result) {3 console.log(result);4});5var forceStop = require('devicefarmer-stf-provider').forceStop;6forceStop('deviceid', 'appname', function(err, result) {7 console.log(result);8});9var getBatteryLevel = require('devicefarmer-stf-provider').getBatteryLevel;10getBatteryLevel('deviceid', function(err, result) {11 console.log(result);12});13var getBatteryState = require('devicefarmer-stf-provider').getBatteryState;14getBatteryState('deviceid', function(err, result) {15 console.log(result);16});17var getDeviceDetails = require('devicefarmer-stf-provider').getDeviceDetails;18getDeviceDetails('deviceid', function(err, result) {19 console.log(result);20});21var getDeviceList = require('devicefarmer-stf-provider').getDeviceList;22getDeviceList(function(err, result) {23 console.log(result);24});25var getDeviceLocation = require('devicefarmer-stf-provider').getDeviceLocation;26getDeviceLocation('deviceid', function(err, result) {27 console.log(result);28});29var getDeviceLogs = require('devicefarmer-stf-provider').getDeviceLogs;30getDeviceLogs('deviceid', function(err, result) {31 console.log(result);32});33var getDeviceModel = require('devicefarmer-stf-provider').getDeviceModel;
Using AI Code Generation
1var forceKill = require('./index').forceKill;2var device = {3 "provider": {4 }5};6forceKill(device, function (err) {7 if (err) {8 console.log(err);9 } else {10 console.log("Force kill successful");11 }12});
Using AI Code Generation
1var devicefarmer = require('devicefarmer-stf');2var serial = 'HT4B2SK00000';3device.forceKill(serial, function(error, result) {4 if (error) {5 console.log(error);6 } else {7 console.log(result);8 }9});10var devicefarmer = require('devicefarmer-stf');11var serial = 'HT4B2SK00000';12var type = 'logcat';13var logcatArgs = ['*'];14device.getLog(serial, type, logcatArgs, function(error, result) {15 if (error) {16 console.log(error);17 } else {18 console.log(result);19 }20});21var devicefarmer = require('devicefarmer-stf');22var serial = 'HT4B2SK00000';23var logcatArgs = ['*'];24device.getLogcat(serial, logcatArgs, function(error, result) {25 if (error) {26 console.log(error);27 } else {28 console.log(result);29 }30});31var devicefarmer = require('devicefarmer-stf');32var serial = 'HT4B2SK00000';33device.getScreenshot(serial, function(error, result) {34 if (error) {35 console.log(error);36 } else {37 console.log(result);38 }39});40var devicefarmer = require('devicefarmer-stf');41var serial = 'HT4B2SK00000';42var logcatArgs = ['*'];43device.getLogcat(serial, logcatArgs, function(error, result) {44 if (error) {45 console.log(error);46 } else {47 console.log(result);
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!!