Best JavaScript code snippet using root
GenyInstanceLauncher.test.js
Source:GenyInstanceLauncher.test.js
1// @ts-nocheck2describe('Genymotion-Cloud instance launcher', () => {3 const recipeName = 'mock-recipe-name';4 const anInstance = () => {5 const instance = new GenyInstance();6 instance.uuid = 'mock-instance-uuid';7 instance.name = 'mock-instance-name';8 instance.recipeName = recipeName;9 instance.toString = () => 'mock-instance-toString()';10 return instance;11 };12 const anOfflineInstance = () => {13 const instance = anInstance();14 instance.isAdbConnected.mockReturnValue(false);15 instance.isOnline.mockReturnValue(false);16 instance.adbName = '0.0.0.0';17 return instance;18 };19 const anOnlineInstance = () => {20 const instance = anOfflineInstance();21 instance.isOnline.mockReturnValue(true);22 return instance;23 };24 const aDisconnectedInstance = anOnlineInstance;25 const aFullyConnectedInstance = () => {26 const instance = anOnlineInstance();27 instance.isAdbConnected.mockReturnValue(true);28 instance.adbName = 'localhost:1234';29 return instance;30 };31 const givenInstanceQueryResult = (instance) => instanceLookupService.getInstance.mockResolvedValue(instance);32 const givenAnInstanceDeletionError = () => instanceLifecycleService.deleteInstance.mockRejectedValue(new Error());33 const givenInstanceConnectResult = (instance) => instanceLifecycleService.adbConnectInstance.mockResolvedValue(instance);34 const givenInstanceConnectError = () => instanceLifecycleService.adbConnectInstance.mockRejectedValue(new Error());35 const expectDeviceBootEvent = (instance, coldBoot) =>36 expect(eventEmitter.emit).toHaveBeenCalledWith('bootDevice', {37 coldBoot,38 deviceId: instance.adbName,39 type: recipeName,40 });41 const expectNoDeviceBootEvent = () => expect(eventEmitter.emit).not.toHaveBeenCalled();42 let retry;43 let eventEmitter;44 let deviceCleanupRegistry;45 let instanceLookupService;46 let instanceLifecycleService;47 let GenyInstance;48 let uut;49 beforeEach(() => {50 jest.mock('../../../../../utils/retry');51 retry = require('../../../../../utils/retry');52 retry.mockImplementation((options, func) => func());53 const AsyncEmitter = jest.genMockFromModule('../../../../../utils/AsyncEmitter');54 eventEmitter = new AsyncEmitter();55 const InstanceLifecycleService = jest.genMockFromModule('../../../../common/drivers/android/genycloud/services/GenyInstanceLifecycleService');56 instanceLifecycleService = new InstanceLifecycleService();57 const InstanceLookupService = jest.genMockFromModule('../../../../common/drivers/android/genycloud/services/GenyInstanceLookupService');58 instanceLookupService = new InstanceLookupService();59 const DeviceRegistry = jest.genMockFromModule('../../../../DeviceRegistry');60 deviceCleanupRegistry = new DeviceRegistry();61 GenyInstance = jest.genMockFromModule('../../../../common/drivers/android/genycloud/services/dto/GenyInstance');62 const GenyInstanceLauncher = require('./GenyInstanceLauncher');63 uut = new GenyInstanceLauncher({64 instanceLifecycleService,65 instanceLookupService,66 deviceCleanupRegistry,67 eventEmitter68 });69 });70 describe('Launch', () => {71 it('should register a *fresh* instance in cleanup registry', async () => {72 const instance = anOnlineInstance();73 givenInstanceQueryResult(instance);74 givenInstanceConnectResult(instance);75 await uut.launch(instance, true);76 expect(deviceCleanupRegistry.allocateDevice).toHaveBeenCalledWith(instance.uuid, { name: instance.name });77 });78 it('should not register instance in cleanup registry if not a fresh instance', async () => {79 const instance = anOnlineInstance();80 givenInstanceQueryResult(instance);81 givenInstanceConnectResult(instance);82 await uut.launch(instance, false);83 expect(deviceCleanupRegistry.allocateDevice).not.toHaveBeenCalled();84 });85 it('should register instance in cleanup registry by default', async () => {86 const instance = anOnlineInstance();87 givenInstanceQueryResult(instance);88 givenInstanceConnectResult(instance);89 await uut.launch(instance);90 expect(deviceCleanupRegistry.allocateDevice).toHaveBeenCalledWith(instance.uuid, { name: instance.name });91 });92 it('should wait for the cloud instance to become online', async () => {93 const instance = anOfflineInstance();94 const instanceOnline = anOnlineInstance();95 givenInstanceQueryResult(instanceOnline);96 givenInstanceConnectResult(instanceOnline);97 retry.mockImplementationOnce(async (options, func) => {98 const instance = await func();99 expect(instanceLookupService.getInstance).toHaveBeenCalledWith(instance.uuid);100 return instance;101 });102 const result = await uut.launch(instance);103 expect(result).toEqual(instanceOnline);104 expect(retry).toHaveBeenCalled();105 });106 it('should not wait for cloud instance to become online if already online', async () => {107 const instance = anOnlineInstance();108 givenInstanceQueryResult(instance);109 givenInstanceConnectResult(instance);110 const result = await uut.launch(instance);111 expect(instanceLookupService.getInstance).not.toHaveBeenCalled();112 expect(retry).not.toHaveBeenCalled();113 expect(result).toEqual(instance);114 });115 it('should fail if instance never becomes online', async () => {116 const instanceOffline = anOfflineInstance();117 givenInstanceQueryResult(instanceOffline);118 givenInstanceConnectResult(instanceOffline);119 await expect(uut.launch(instanceOffline))120 .rejects121 .toThrowError(`Timeout waiting for instance ${instanceOffline.uuid} to be ready`);122 });123 it('should wait for the cloud instance to become online, with decent retry arguments', async () => {124 const expectedRetryArgs = {125 initialSleep: 45000,126 backoff: 'none',127 interval: 5000,128 retries: 25,129 };130 const instance = anOfflineInstance();131 const instanceOnline = anOnlineInstance();132 givenInstanceQueryResult(instanceOnline);133 givenInstanceConnectResult(instanceOnline);134 await uut.launch(instance);135 expect(retry).toHaveBeenCalledWith(expect.objectContaining(expectedRetryArgs), expect.any(Function));136 });137 it('should adb-connect to instance if disconnected', async () => {138 const disconnectedInstance = aDisconnectedInstance();139 const connectedInstance = aFullyConnectedInstance();140 givenInstanceQueryResult(disconnectedInstance);141 givenInstanceConnectResult(connectedInstance);142 const result = await uut.launch(disconnectedInstance);143 expect(instanceLifecycleService.adbConnectInstance).toHaveBeenCalledWith(disconnectedInstance.uuid);144 expect(result).toEqual(connectedInstance);145 });146 it('should not connect a connected instance', async () => {147 const connectedInstance = aFullyConnectedInstance();148 givenInstanceQueryResult(connectedInstance);149 givenInstanceConnectResult(connectedInstance);150 await uut.launch(connectedInstance);151 expect(instanceLifecycleService.adbConnectInstance).not.toHaveBeenCalled();152 });153 it('should emit boot event for a reused instance', async () => {154 const isNew = true;155 const instance = aFullyConnectedInstance();156 givenInstanceQueryResult(instance);157 givenInstanceConnectResult(instance);158 await uut.launch(instance, isNew);159 expectDeviceBootEvent(instance, true);160 });161 it('should emit boot event for a newly allocated instance', async () => {162 const isNew = false;163 const instance = aFullyConnectedInstance();164 givenInstanceQueryResult(instance);165 givenInstanceConnectResult(instance);166 await uut.launch(instance, isNew);167 expectDeviceBootEvent(instance, false);168 });169 it('should not emit boot event if adb-connect fails (implicit call-order check)', async () => {170 const instance = aDisconnectedInstance();171 givenInstanceQueryResult(instance);172 givenInstanceConnectError();173 try {174 await uut.launch(instance, false);175 } catch (e) {}176 expectNoDeviceBootEvent();177 });178 });179 describe('Shutdown', () => {180 it('should delete the associated instance', async () => {181 const instance = anInstance();182 await uut.shutdown(instance);183 expect(instanceLifecycleService.deleteInstance).toHaveBeenCalledWith(instance.uuid);184 });185 it('should fail if deletion fails', async () => {186 givenAnInstanceDeletionError();187 const instance = anInstance();188 await expect(uut.shutdown(instance)).rejects.toThrowError();189 });190 it('should remove the instance from the cleanup registry', async () => {191 const instance = anInstance();192 await uut.shutdown(instance);193 expect(deviceCleanupRegistry.disposeDevice).toHaveBeenCalledWith(instance.uuid);194 });195 it('should emit associated events', async () => {196 const instance = anInstance();197 await uut.shutdown(instance);198 expect(eventEmitter.emit).toHaveBeenCalledWith('beforeShutdownDevice', { deviceId: instance.uuid });199 expect(eventEmitter.emit).toHaveBeenCalledWith('shutdownDevice', { deviceId: instance.uuid });200 });201 it('should not emit shutdownDevice prematurely', async () => {202 givenAnInstanceDeletionError();203 const instance = anInstance();204 await expect(uut.shutdown(instance)).rejects.toThrowError();205 expect(eventEmitter.emit).toHaveBeenCalledTimes(1);206 expect(eventEmitter.emit).not.toHaveBeenCalledWith('shutdownDevice', expect.any(Object));207 });208 });...
GenyInstanceLifecycleService.test.js
Source:GenyInstanceLifecycleService.test.js
...44 const givenAdbConnectResult = (instance) => exec.adbConnect.mockResolvedValue({ instance });45 it('should exec adb-connect', async () => {46 const instance = anInstance();47 givenAdbConnectResult(instance);48 await uut.adbConnectInstance(instance.uuid);49 expect(exec.adbConnect).toHaveBeenCalledWith(instance.uuid);50 });51 it('should return the updated instance', async () => {52 const instance = anInstance();53 givenAdbConnectResult(instance);54 const result = await uut.adbConnectInstance(instance.uuid);55 expect(result.uuid).toEqual(instance.uuid);56 expect(result.constructor.name).toContain('Instance');57 });58 });59 describe('device instance deletion', () => {60 const givenResult = (instance) => exec.stopInstance.mockResolvedValue({ instance });61 it('should exec instance deletion', async () => {62 const instance = anInstance();63 givenResult(instance);64 await uut.deleteInstance(instance.uuid);65 expect(exec.stopInstance).toHaveBeenCalledWith(instance.uuid);66 });67 it('should return result', async () => {68 const instance = anInstance();...
GenyInstanceLifecycleService.js
Source:GenyInstanceLifecycleService.js
...7 async createInstance(recipeUUID) {8 const result = await this.genyCloudExec.startInstance(recipeUUID, this.instanceNaming.generateName());9 return new Instance(result.instance);10 }11 async adbConnectInstance(instanceUUID) {12 const result = (await this.genyCloudExec.adbConnect(instanceUUID));13 return new Instance(result.instance);14 }15 async deleteInstance(instanceUUID) {16 const result = await this.genyCloudExec.stopInstance(instanceUUID);17 return new Instance(result.instance);18 }19}...
Using AI Code Generation
1var root = require('./root.js');2var adbConnectInstance = root.adbConnectInstance;3var adb = adbConnectInstance();4adb.shell('ls -l', function(err, out) {5 console.log(out);6});
Using AI Code Generation
1var root = require('./root');2var adbConnectInstance = root.adbConnectInstance;3var adb = adbConnectInstance();4adb.shell('input keyevent 3', function(err, out){5 if(err){6 console.log(err);7 }8 else{9 console.log(out);10 }11});12### adbConnectInstance()13### adbConnectInstance(serial)14### adbConnectInstance(serial, port)15MIT © [Saurabh Gupta](
Using AI Code Generation
1var root = require('./root.js');2var adbConnectInstance = root.adbConnectInstance;3var adbConnect = adbConnectInstance();4adbConnect.connect(function(err, device){5 if (err) {6 throw err;7 }8 console.log('device', device);9});
Using AI Code Generation
1var adb = require('./root.js');2var adbConnectInstance = adb.adbConnectInstance;3var adbConnect = adb.adbConnect;4var device = adbConnectInstance();5device.connect(function(){6 device.install('path/to/your/app.apk', function(){7 device.run('com.your.app', function(){8 device.screenshot('path/to/your/screenshot.png', function(){9 device.disconnect(function(){10 console.log('Done!');11 });12 });13 });14 });15});16var adb = require('./root.js');17var adbConnect = adb.adbConnect;18var device = adbConnect();19device.connect(function(){20 device.install('path/to/your/app.apk', function(){21 device.run('com.your.app', function(){22 device.screenshot('path/to/your/screenshot.png', function(){23 device.disconnect(function(){24 console.log('Done!');25 });26 });27 });28 });29});30### adbConnectInstance([options])31### adbConnect([options])32### adbConnect#connect(callback)33### adbConnect#disconnect(callback)34### adbConnect#install(apk, callback)
Using AI Code Generation
1import { adbConnectInstance } from '@testim/root-cause-core/lib/adb';2const { page } = adbConnect;3import { adbConnectInstance } from '@testim/root-cause-core/lib/adb';4const { page } = adbConnect;5import { adbConnect } from '@testim/root-cause-core/lib/adb';6import { adbConnectWithBrowser } from '@testim/root-cause-core/lib/adb';
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!!