Best JavaScript code snippet using devicefarmer-stf
service.js
Source:service.js
...312 // @TODO Not sure how to force the device to copy the current selection313 // yet.314 return plugin.getClipboard()315 }316 function runServiceCommand(type, cmd) {317 var resolver = Promise.defer()318 var id = Math.floor(Math.random() * 0xFFFFFF)319 service.writer.write(new apk.wire.Envelope(320 id321 , type322 , cmd.encodeNB()323 ).encodeNB())324 return messageResolver.await(id, resolver)325 }326 plugin.getDisplay = function(id) {327 return runServiceCommand(328 apk.wire.MessageType.GET_DISPLAY329 , new apk.wire.GetDisplayRequest(id)330 )331 .timeout(10000)332 .then(function(data) {333 var response = apk.wire.GetDisplayResponse.decode(data)334 if (response.success) {335 return {336 id: id337 , width: response.width338 , height: response.height339 , xdpi: response.xdpi340 , ydpi: response.ydpi341 , fps: response.fps342 , density: response.density343 , rotation: response.rotation344 , secure: response.secure345 , size: Math.sqrt(346 Math.pow(response.width / response.xdpi, 2) +347 Math.pow(response.height / response.ydpi, 2)348 )349 }350 }351 throw new Error('Unable to retrieve display information')352 })353 }354 plugin.wake = function() {355 return runAgentCommand(356 apk.wire.MessageType.DO_WAKE357 , new apk.wire.DoWakeRequest()358 )359 }360 plugin.rotate = function(rotation) {361 return runAgentCommand(362 apk.wire.MessageType.SET_ROTATION363 , new apk.wire.SetRotationRequest(rotation, options.lockRotation || false)364 )365 }366 plugin.freezeRotation = function(rotation) {367 return runAgentCommand(368 apk.wire.MessageType.SET_ROTATION369 , new apk.wire.SetRotationRequest(rotation, true)370 )371 }372 plugin.thawRotation = function() {373 return runAgentCommand(374 apk.wire.MessageType.SET_ROTATION375 , new apk.wire.SetRotationRequest(0, false)376 )377 }378 plugin.version = function() {379 return runServiceCommand(380 apk.wire.MessageType.GET_VERSION381 , new apk.wire.GetVersionRequest()382 )383 .timeout(10000)384 .then(function(data) {385 var response = apk.wire.GetVersionResponse.decode(data)386 if (response.success) {387 return response.version388 }389 throw new Error('Unable to retrieve version')390 })391 }392 plugin.unlock = function() {393 return runServiceCommand(394 apk.wire.MessageType.SET_KEYGUARD_STATE395 , new apk.wire.SetKeyguardStateRequest(false)396 )397 .timeout(10000)398 .then(function(data) {399 var response = apk.wire.SetKeyguardStateResponse.decode(data)400 if (!response.success) {401 throw new Error('Unable to unlock device')402 }403 })404 }405 plugin.lock = function() {406 return runServiceCommand(407 apk.wire.MessageType.SET_KEYGUARD_STATE408 , new apk.wire.SetKeyguardStateRequest(true)409 )410 .timeout(10000)411 .then(function(data) {412 var response = apk.wire.SetKeyguardStateResponse.decode(data)413 if (!response.success) {414 throw new Error('Unable to lock device')415 }416 })417 }418 plugin.acquireWakeLock = function() {419 return runServiceCommand(420 apk.wire.MessageType.SET_WAKE_LOCK421 , new apk.wire.SetWakeLockRequest(true)422 )423 .timeout(10000)424 .then(function(data) {425 var response = apk.wire.SetWakeLockResponse.decode(data)426 if (!response.success) {427 throw new Error('Unable to acquire WakeLock')428 }429 })430 }431 plugin.releaseWakeLock = function() {432 return runServiceCommand(433 apk.wire.MessageType.SET_WAKE_LOCK434 , new apk.wire.SetWakeLockRequest(false)435 )436 .timeout(10000)437 .then(function(data) {438 var response = apk.wire.SetWakeLockResponse.decode(data)439 if (!response.success) {440 throw new Error('Unable to release WakeLock')441 }442 })443 }444 plugin.identity = function() {445 return runServiceCommand(446 apk.wire.MessageType.DO_IDENTIFY447 , new apk.wire.DoIdentifyRequest(options.serial)448 )449 .timeout(10000)450 .then(function(data) {451 var response = apk.wire.DoIdentifyResponse.decode(data)452 if (!response.success) {453 throw new Error('Unable to identify device')454 }455 })456 }457 plugin.setClipboard = function(text) {458 return runServiceCommand(459 apk.wire.MessageType.SET_CLIPBOARD460 , new apk.wire.SetClipboardRequest(461 apk.wire.ClipboardType.TEXT462 , text463 )464 )465 .timeout(10000)466 .then(function(data) {467 var response = apk.wire.SetClipboardResponse.decode(data)468 if (!response.success) {469 throw new Error('Unable to set clipboard')470 }471 })472 }473 plugin.getClipboard = function() {474 return runServiceCommand(475 apk.wire.MessageType.GET_CLIPBOARD476 , new apk.wire.GetClipboardRequest(477 apk.wire.ClipboardType.TEXT478 )479 )480 .timeout(10000)481 .then(function(data) {482 var response = apk.wire.GetClipboardResponse.decode(data)483 if (response.success) {484 switch (response.type) {485 case apk.wire.ClipboardType.TEXT:486 return response.text487 }488 }489 throw new Error('Unable to get clipboard')490 })491 }492 plugin.getBrowsers = function() {493 return runServiceCommand(494 apk.wire.MessageType.GET_BROWSERS495 , new apk.wire.GetBrowsersRequest()496 )497 .timeout(15000)498 .then(function(data) {499 var response = apk.wire.GetBrowsersResponse.decode(data)500 if (response.success) {501 delete response.success502 return response503 }504 throw new Error('Unable to get browser list')505 })506 }507 plugin.getProperties = function(properties) {508 return runServiceCommand(509 apk.wire.MessageType.GET_PROPERTIES510 , new apk.wire.GetPropertiesRequest(properties)511 )512 .timeout(15000)513 .then(function(data) {514 var response = apk.wire.GetPropertiesResponse.decode(data)515 if (response.success) {516 var mapped = Object.create(null)517 response.properties.forEach(function(property) {518 mapped[property.name] = property.value519 })520 return mapped521 }522 throw new Error('Unable to get properties')523 })524 }525 plugin.getAccounts = function(data) {526 return runServiceCommand(527 apk.wire.MessageType.GET_ACCOUNTS528 , new apk.wire.GetAccountsRequest({type: data.type})529 )530 .timeout(15000)531 .then(function(data) {532 var response = apk.wire.GetAccountsResponse.decode(data)533 if (response.success) {534 return response.accounts535 }536 throw new Error('No accounts returned')537 })538 }539 plugin.removeAccount = function(data) {540 return runServiceCommand(541 apk.wire.MessageType.DO_REMOVE_ACCOUNT542 , new apk.wire.DoRemoveAccountRequest({543 type: data.type544 , account: data.account545 })546 )547 .timeout(15000)548 .then(function(data) {549 var response = apk.wire.DoRemoveAccountResponse.decode(data)550 if (response.success) {551 return true552 }553 throw new Error('Unable to remove account')554 })555 }556 plugin.addAccountMenu = function() {557 return runServiceCommand(558 apk.wire.MessageType.DO_ADD_ACCOUNT_MENU559 , new apk.wire.DoAddAccountMenuRequest()560 )561 .timeout(15000)562 .then(function(data) {563 var response = apk.wire.DoAddAccountMenuResponse.decode(data)564 if (response.success) {565 return true566 }567 throw new Error('Unable to show add account menu')568 })569 }570 plugin.setRingerMode = function(mode) {571 return runServiceCommand(572 apk.wire.MessageType.SET_RINGER_MODE573 , new apk.wire.SetRingerModeRequest(mode)574 )575 .timeout(10000)576 .then(function(data) {577 var response = apk.wire.SetRingerModeResponse.decode(data)578 if (!response.success) {579 throw new Error('Unable to set ringer mode')580 }581 })582 }583 plugin.getRingerMode = function() {584 return runServiceCommand(585 apk.wire.MessageType.GET_RINGER_MODE586 , new apk.wire.GetRingerModeRequest()587 )588 .timeout(10000)589 .then(function(data) {590 var response = apk.wire.GetRingerModeResponse.decode(data)591 // Reflection to decode enums to their string values, otherwise592 // we only get an integer593 var ringerMode = apk.builder.lookup(594 'jp.co.cyberagent.stf.proto.RingerMode')595 .children[response.mode].name596 if (response.success) {597 return ringerMode598 }599 throw new Error('Unable to get ringer mode')600 })601 }602 plugin.setWifiEnabled = function(enabled) {603 return runServiceCommand(604 apk.wire.MessageType.SET_WIFI_ENABLED605 , new apk.wire.SetWifiEnabledRequest(enabled)606 )607 .timeout(10000)608 .then(function(data) {609 var response = apk.wire.SetWifiEnabledResponse.decode(data)610 if (!response.success) {611 throw new Error('Unable to set Wifi')612 }613 })614 }615 plugin.getWifiStatus = function() {616 return runServiceCommand(617 apk.wire.MessageType.GET_WIFI_STATUS618 , new apk.wire.GetWifiStatusRequest()619 )620 .timeout(10000)621 .then(function(data) {622 var response = apk.wire.GetWifiStatusResponse.decode(data)623 if (response.success) {624 return response.status625 }626 throw new Error('Unable to get Wifi status')627 })628 }629 plugin.getSdStatus = function() {630 return runServiceCommand(631 apk.wire.MessageType.GET_SD_STATUS632 , new apk.wire.GetSdStatusRequest()633 )634 .timeout(10000)635 .then(function(data) {636 var response = apk.wire.GetSdStatusResponse.decode(data)637 if (response.success) {638 return response.mounted639 }640 throw new Error('Unable to get SD card status')641 })642 }643 plugin.pressKey = function(key) {644 keyEvent({event: apk.wire.KeyEvent.PRESS, keyCode: keyutil.namedKey(key)})645 return Promise.resolve(true)646 }647 plugin.setMasterMute = function(mode) {648 return runServiceCommand(649 apk.wire.MessageType.SET_MASTER_MUTE650 , new apk.wire.SetMasterMuteRequest(mode)651 )652 .timeout(10000)653 .then(function(data) {654 var response = apk.wire.SetMasterMuteResponse.decode(data)655 if (!response.success) {656 throw new Error('Unable to set master mute')657 }658 })659 }660 return openAgent()661 .then(openService)662 .then(function() {...
Using AI Code Generation
1var client = require('devicefarmer-stf-client');2service.runServiceCommand('emulator-5554', 'shell', 'input', 'keyevent', 'KEYCODE_MENU')3.then(function(res) {4 console.log(res);5})6.catch(function(err) {7 console.log(err);8});9{ statusCode: 200,10 { date: 'Thu, 11 Jun 2015 11:02:29 GMT',11 connection: 'close' } }12{ statusCode: 500,13 { date: 'Thu, 11 Jun 2015 11:05:00 GMT',14 connection: 'close' } }15{ statusCode: 404,16 { date: 'Thu, 11 Jun 2015 11:05:21 GMT',17 connection: 'close' } }18{ statusCode: 404,19 { date: 'Thu, 11 Jun 2015 11:05:21 GMT',20 connection: 'close' } }21{ statusCode: 500,22 { date: 'Thu, 11 Jun 2015 11:05:00 GMT',23 connection: 'close' } }24{ statusCode: 404,25 { date: 'Thu, 11 Jun 2015 11:05:21 GMT',
Using AI Code Generation
1var df = require('devicefarmer-stf');2var config = require('./config.json');3var stf = new df.Stf(config);4var device = new df.Device(stf);5var runServiceCommand = device.runServiceCommand;6var args = {
Using AI Code Generation
1var client = require('devicefarmer-stf-client');2var command = 'am instrument -w com.example.package.test/android.support.test.runner.AndroidJUnitRunner';3device.runServiceCommand('emulator-5554', command, function(err, res){4 if(err){5 console.log(err);6 }7 console.log(res);8});9var client = require('devicefarmer-stf-client');10var command = 'am instrument -w com.example.package.test/android.support.test.runner.AndroidJUnitRunner';11device.runServiceCommand('emulator-5554', command, function(err, res){12 if(err){13 console.log(err);14 }15 console.log(res);16});17var client = require('devicefarmer-stf-client');18var command = 'am instrument -w com.example.package.test/android.support.test.runner.AndroidJUnitRunner';19device.runServiceCommand('emulator-5554', command, function(err, res){20 if(err){21 console.log(err);22 }23 console.log(res);24});25var client = require('devicefarmer-stf-client');26var command = 'am instrument -w com.example.package.test/android.support.test.runner.AndroidJUnitRunner';27device.runServiceCommand('emulator-5554', command, function(err, res){28 if(err){29 console.log(err);30 }31 console.log(res);32});33var client = require('devicefarmer-stf-client');34var command = 'am instrument -w com.example.package.test/android.support.test.runner.AndroidJUnitRunner';35device.runServiceCommand('emulator-5554', command, function(err, res){36 if(err){37 console.log(err);38 }39 console.log(res);40});
Using AI Code Generation
1var stfClient = require('devicefarmer-stf-client');2var device = {3}4client.runServiceCommand(device, 'am', ['start', '-n', 'com.android.settings/.Settings'], function (err, result) {5 if (err) {6 console.log('An error occurred: ', err);7 } else {8 console.log('result: ', result);9 }10});
Using AI Code Generation
1var stfClient = require('devicefarmer-stf-client');2var device = new stfClient.Device(client, 'b0a7c7a1');3device.runServiceCommand('shell', 'input', 'keyevent', 'KEYCODE_HOME', function(err, stdout, stderr) {4 if (err) {5 console.log('Error running service command: ' + err);6 return;7 }8 console.log('stdout: ' + stdout);9 console.log('stderr: ' + stderr);10});11var stfClient = require('devicefarmer-stf-client');12var device = new stfClient.Device(client, 'b0a7c7a1');13device.runServiceCommand('shell', 'input', 'keyevent', 'KEYCODE_HOME', function(err, stdout, stderr) {14 if (err) {15 console.log('Error running service command: ' + err);16 return;17 }18 console.log('stdout: ' + stdout);19 console.log('stderr: ' + stderr);20});21var stfClient = require('devicefarmer-stf-client');22var device = new stfClient.Device(client, 'b0a7c7a1');23device.runServiceCommand('shell', 'input', 'keyevent', 'KEYCODE_HOME', function(err, stdout, stderr) {24 if (err) {25 console.log('Error running service command: ' + err);26 return;27 }28 console.log('stdout: ' + stdout);29 console.log('stderr: ' + stderr);30});31var stfClient = require('devicefarmer-stf-client');32var device = new stfClient.Device(client, 'b0a7c7a1');33device.runServiceCommand('shell',
Using AI Code Generation
1var deviceFarmer = require('devicefarmer-stf-service');2device.runServiceCommand('com.android.phone', 'com.android.phone.InCallScreen', 'hangupRingingCall', function (err, data) {3 console.log('runServiceCommand: ' + data);4});5var deviceFarmer = require('devicefarmer-stf-service');6device.runShellCommand('input keyevent 3', function (err, data) {7 console.log('runShellCommand: ' + data);8});9var deviceFarmer = require('devicefarmer-stf-service');10device.runAdbCommand('shell', 'input keyevent 3', function (err, data) {11 console.log('runAdbCommand: ' + data);12});13var deviceFarmer = require('devicefarmer-stf-service');14device.runMonkeyCommand('com.android.phone', 1000, function (err, data) {15 console.log('runMonkeyCommand: ' + data);16});17var deviceFarmer = require('devicefarmer-stf-service');18device.runMonkeyCommand('com.android.phone', 1000, function (err, data) {19 console.log('runMonkeyCommand: ' + data);20});21var deviceFarmer = require('devicefarmer-stf-service');22device.runMonkeyCommand('com.android.phone', 1000, function (err,
Using AI Code Generation
1var deviceService = require('devicefarmer-stf-device-service');2var device = deviceService.getDevice('deviceid');3device.runServiceCommand('command', 'deviceid');4var deviceService = require('devicefarmer-stf-device-service');5var device = deviceService.getDevice('deviceid');6device.runServiceCommand('command', 'deviceid', function(err, response) {7 if(err) {8 console.log(err);9 } else {10 console.log(response);11 }12});13The MIT License (MIT)14Copyright (c) 2016 DeviceFarmer
Using AI Code Generation
1var stfService = require('devicefarmer-stf-service');2var service = new stfService();3var device = service.getDevice("device_serial");4device.runServiceCommand("input keyevent 3");5var stfService = require('devicefarmer-stf-service');6var service = new stfService();7var device = service.getDevice("device_serial");8device.sendKeyEvent(3);9var stfService = require('devicefarmer-stf-service');10var service = new stfService();11var device = service.getDevice("device_serial");12device.sendTap(100, 200);13var stfService = require('devicefarmer-stf-service');14var service = new stfService();15var device = service.getDevice("device_serial");16device.sendSwipe(100, 200, 300, 400);17var stfService = require('devicefarmer-stf-service');18var service = new stfService();19var device = service.getDevice("device_serial");20device.sendText("test");21var stfService = require('devicefarmer-stf-service');22var service = new stfService();23var device = service.getDevice("device_serial");24device.sendRotation(90);25var stfService = require('devicefarmer-stf-service');26var service = new stfService();27var device = service.getDevice("device_serial");28device.sendFile("C:\\test.txt", "/sdcard/test.txt");
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!!