Best JavaScript code snippet using root
RuntimeDevice.test.js
Source:RuntimeDevice.test.js
...107 const device = aValidUnpreparedDevice(overrides);108 await device._prepare();109 return device;110 }111 async function aValidDeviceWithLaunchArgs(launchArgs) {112 return await aValidDevice({113 appsConfig: {114 default: {115 launchArgs,116 },117 },118 });119 }120 it('should return the name from the driver', async () => {121 driverMock.driver.getDeviceName.mockReturnValue('mock-device-name-from-driver');122 const device = await aValidDevice();123 expect(device.name).toEqual('mock-device-name-from-driver');124 });125 it('should return the type from the configuration', async () => {126 const device = await aValidDevice();127 expect(device.type).toEqual('ios.simulator');128 });129 it('should return the device ID, as provided by acquireFreeDevice', async () => {130 const device = await aValidUnpreparedDevice();131 await device._prepare();132 driverMock.driver.getExternalId.mockReturnValue('mockExternalId');133 expect(device.id).toEqual('mockExternalId');134 driverMock.expectExternalIdCalled();135 });136 describe('selectApp()', () => {137 let device;138 describe('when there is a single app', () => {139 beforeEach(async () => {140 device = await aValidUnpreparedDevice();141 jest.spyOn(device, 'selectApp');142 await device._prepare();143 });144 it(`should select the default app upon prepare()`, async () => {145 expect(device.selectApp).toHaveBeenCalledWith('default');146 });147 it(`should function as usual when the app is selected`, async () => {148 await device.launchApp();149 expect(driverMock.driver.launchApp).toHaveBeenCalled();150 });151 it(`should throw on call without args`, async () => {152 await expect(device.selectApp()).rejects.toThrowError(errorComposer.cantSelectEmptyApp());153 });154 it(`should throw on app interactions with no selected app`, async () => {155 await device.selectApp(null);156 await expect(device.launchApp()).rejects.toThrowError(errorComposer.appNotSelected());157 });158 it(`should throw on attempt to select a non-existent app`, async () => {159 await expect(device.selectApp('nonExistent')).rejects.toThrowError();160 });161 });162 describe('when there are multiple apps', () => {163 beforeEach(async () => {164 device = await aValidUnpreparedDevice({165 appsConfig: {166 withBinaryPath: {167 binaryPath: 'path/to/app',168 },169 withBundleId: {170 binaryPath: 'path/to/app2',171 bundleId: 'com.app2'172 },173 },174 });175 jest.spyOn(device, 'selectApp');176 driverMock.driver.getBundleIdFromBinary.mockReturnValue('com.app1');177 await device._prepare();178 });179 it(`should not select the app at all`, async () => {180 expect(device.selectApp).not.toHaveBeenCalled();181 });182 it(`upon select, it should infer bundleId if it is missing`, async () => {183 await device.selectApp('withBinaryPath');184 expect(driverMock.driver.getBundleIdFromBinary).toHaveBeenCalledWith('path/to/app');185 });186 it(`upon select, it should terminate the previous app`, async () => {187 jest.spyOn(device, 'terminateApp');188 await device.selectApp('withBinaryPath');189 expect(device.terminateApp).not.toHaveBeenCalled(); // because no app was running before190 await device.selectApp('withBundleId');191 expect(device.terminateApp).toHaveBeenCalled(); // because there is a running app192 });193 it(`upon select, it should not infer bundleId if it is specified`, async () => {194 await device.selectApp('withBundleId');195 expect(driverMock.driver.getBundleIdFromBinary).not.toHaveBeenCalled();196 });197 it(`upon re-selecting the same app, it should not infer bundleId twice`, async () => {198 await device.selectApp('withBinaryPath');199 await device.selectApp('withBundleId');200 await device.selectApp('withBinaryPath');201 expect(driverMock.driver.getBundleIdFromBinary).toHaveBeenCalledTimes(1);202 });203 });204 describe('when there are no apps', () => {205 beforeEach(async () => {206 device = await aValidUnpreparedDevice({207 appsConfig: null208 });209 jest.spyOn(device, 'selectApp');210 await device._prepare();211 });212 it(`should not select the app at all`, async () => {213 expect(device.selectApp).not.toHaveBeenCalled();214 });215 it(`should be able to execute actions with an explicit bundleId`, async () => {216 const bundleId = 'com.example.app';217 jest.spyOn(device, 'terminateApp');218 await device.uninstallApp(bundleId);219 expect(driverMock.driver.uninstallApp).toHaveBeenCalledWith(bundleId);220 await device.installApp('/tmp/app', '/tmp/app-test');221 expect(driverMock.driver.installApp).toHaveBeenCalledWith('/tmp/app', '/tmp/app-test');222 await device.launchApp({}, bundleId);223 expect(driverMock.driver.launchApp).toHaveBeenCalledWith(bundleId, expect.anything(), undefined);224 await device.terminateApp(bundleId);225 expect(driverMock.driver.terminate).toHaveBeenCalledWith(bundleId);226 await device.uninstallApp(bundleId);227 expect(driverMock.driver.uninstallApp).toHaveBeenCalledWith(bundleId);228 });229 });230 });231 describe('re/launchApp()', () => {232 const expectedDriverArgs = {233 'detoxServer': 'ws://localhost:8099',234 'detoxSessionId': 'test',235 };236 it(`with no args should launch app with defaults`, async () => {237 const expectedArgs = expectedDriverArgs;238 const device = await aValidDevice();239 await device.launchApp();240 driverMock.expectLaunchCalledWithArgs(bundleId, expectedArgs);241 });242 it(`given behaviorConfig.launchApp == 'manual' should wait for the app launch`, async () => {243 const expectedArgs = expectedDriverArgs;244 const device = await aValidDevice({245 behaviorConfig: { launchApp: 'manual' }246 });247 await device.launchApp();248 expect(driverMock.driver.launchApp).not.toHaveBeenCalled();249 driverMock.expectWaitForLaunchCalled(bundleId, expectedArgs);250 });251 it(`args should launch app and emit appReady`, async () => {252 driverMock.driver.launchApp = async () => 42;253 const device = await aValidDevice();254 await device.launchApp();255 expect(emitter.emit).toHaveBeenCalledWith('appReady', {256 deviceId: device.id,257 bundleId: device._bundleId,258 pid: 42,259 });260 });261 it(`(relaunch) with no args should use defaults`, async () => {262 const expectedArgs = expectedDriverArgs;263 const device = await aValidDevice();264 await device.relaunchApp();265 driverMock.expectLaunchCalledWithArgs(bundleId, expectedArgs);266 });267 it(`(relaunch) with no args should terminate the app before launch - backwards compat`, async () => {268 const device = await aValidDevice();269 await device.relaunchApp();270 driverMock.expectTerminateCalled();271 });272 it(`(relaunch) with newInstance=false should not terminate the app before launch`, async () => {273 const device = await aValidDevice();274 await device.relaunchApp({ newInstance: false });275 driverMock.expectTerminateNotCalled();276 });277 it(`(relaunch) with newInstance=true should terminate the app before launch`, async () => {278 const device = await aValidDevice();279 await device.relaunchApp({ newInstance: true });280 driverMock.expectTerminateCalled();281 });282 it(`(relaunch) with delete=true`, async () => {283 const expectedArgs = expectedDriverArgs;284 const device = await aValidDevice();285 await device.relaunchApp({ delete: true });286 driverMock.expectReinstallCalled();287 driverMock.expectLaunchCalledWithArgs(bundleId, expectedArgs);288 });289 it(`(relaunch) with delete=false when reuse is enabled should not uninstall and install`, async () => {290 const expectedArgs = expectedDriverArgs;291 const device = await aValidDevice();292 argparse.getArgValue.mockReturnValue(true);293 await device.relaunchApp();294 driverMock.expectReinstallNotCalled();295 driverMock.expectLaunchCalledWithArgs(bundleId, expectedArgs);296 });297 it(`(relaunch) with url should send the url as a param in launchParams`, async () => {298 const expectedArgs = { ...expectedDriverArgs, 'detoxURLOverride': 'scheme://some.url' };299 const device = await aValidDevice();300 await device.relaunchApp({ url: `scheme://some.url` });301 driverMock.expectLaunchCalledWithArgs(bundleId, expectedArgs);302 });303 it(`(relaunch) with url should send the url as a param in launchParams`, async () => {304 const expectedArgs = {305 ...expectedDriverArgs,306 'detoxURLOverride': 'scheme://some.url',307 'detoxSourceAppOverride': 'sourceAppBundleId',308 };309 const device = await aValidDevice();310 await device.relaunchApp({ url: `scheme://some.url`, sourceApp: 'sourceAppBundleId' });311 driverMock.expectLaunchCalledWithArgs(bundleId, expectedArgs);312 });313 it(`(relaunch) with userNofitication should send the userNotification as a param in launchParams`, async () => {314 const expectedArgs = {315 ...expectedDriverArgs,316 'detoxUserNotificationDataURL': 'url',317 };318 const device = await aValidDevice();319 device.deviceDriver.createPayloadFile = jest.fn(() => 'url');320 await device.relaunchApp({ userNotification: 'json' });321 driverMock.expectLaunchCalledWithArgs(bundleId, expectedArgs);322 });323 it(`(relaunch) with url and userNofitication should throw`, async () => {324 const device = await aValidDevice();325 try {326 await device.relaunchApp({ url: 'scheme://some.url', userNotification: 'notif' });327 fail('should fail');328 } catch (ex) {329 expect(ex).toBeDefined();330 }331 });332 it(`(relaunch) with permissions should send trigger setpermissions before app starts`, async () => {333 const device = await aValidDevice();334 await device.relaunchApp({ permissions: { calendar: 'YES' } });335 expect(driverMock.driver.setPermissions).toHaveBeenCalledWith(bundleId, { calendar: 'YES' });336 });337 it('with languageAndLocale should launch app with a specific language/locale', async () => {338 const expectedArgs = expectedDriverArgs;339 const device = await aValidDevice();340 const languageAndLocale = {341 language: 'es-MX',342 locale: 'es-MX'343 };344 await device.launchApp({ languageAndLocale });345 driverMock.expectLaunchCalledWithArgs(bundleId, expectedArgs, languageAndLocale);346 });347 it(`with disableTouchIndicators should send a boolean switch as a param in launchParams`, async () => {348 const expectedArgs = { ...expectedDriverArgs, 'detoxDisableTouchIndicators': true };349 const device = await aValidDevice();350 await device.launchApp({ disableTouchIndicators: true });351 driverMock.expectLaunchCalledWithArgs(bundleId, expectedArgs);352 });353 it(`with newInstance=false should check if process is in background and reopen it`, async () => {354 const processId = 1;355 const device = await aValidDevice();356 device.deviceDriver.launchApp.mockReturnValue(processId);357 await device._prepare();358 await device.launchApp({ newInstance: true });359 await device.launchApp({ newInstance: false });360 expect(driverMock.driver.deliverPayload).not.toHaveBeenCalled();361 });362 it(`with a url should check if process is in background and use openURL() instead of launch args`, async () => {363 const processId = 1;364 const device = await aValidDevice();365 device.deviceDriver.launchApp.mockReturnValue(processId);366 await device._prepare();367 await device.launchApp({ newInstance: true });368 await device.launchApp({ url: 'url://me' });369 expect(driverMock.driver.deliverPayload).toHaveBeenCalledTimes(1);370 });371 it(`with a url should check if process is in background and if not use launch args`, async () => {372 const launchParams = { url: 'url://me' };373 const processId = 1;374 const newProcessId = 2;375 const device = await aValidDevice();376 device.deviceDriver.launchApp.mockReturnValueOnce(processId).mockReturnValueOnce(newProcessId);377 await device._prepare();378 await device.launchApp(launchParams);379 expect(driverMock.driver.deliverPayload).not.toHaveBeenCalled();380 });381 it(`with a url should check if process is in background and use openURL() instead of launch args`, async () => {382 const launchParams = { url: 'url://me' };383 const processId = 1;384 const device = await aValidDevice();385 device.deviceDriver.launchApp.mockReturnValue(processId);386 await device._prepare();387 await device.launchApp({ newInstance: true });388 await device.launchApp(launchParams);389 expect(driverMock.driver.deliverPayload).toHaveBeenCalledWith({ delayPayload: true, url: 'url://me' });390 });391 it('with userActivity should check if process is in background and if it is use deliverPayload', async () => {392 const launchParams = { userActivity: 'userActivity' };393 const processId = 1;394 const device = await aValidDevice();395 device.deviceDriver.launchApp.mockReturnValueOnce(processId).mockReturnValueOnce(processId);396 device.deviceDriver.createPayloadFile = () => 'url';397 await device._prepare();398 await device.launchApp({ newInstance: true });399 await device.launchApp(launchParams);400 expect(driverMock.driver.deliverPayload).toHaveBeenCalledWith({ delayPayload: true, detoxUserActivityDataURL: 'url' });401 });402 it('with userNotification should check if process is in background and if it is use deliverPayload', async () => {403 const launchParams = { userNotification: 'notification' };404 const processId = 1;405 const device = await aValidDevice();406 device.deviceDriver.launchApp.mockReturnValueOnce(processId).mockReturnValueOnce(processId);407 device.deviceDriver.createPayloadFile = () => 'url';408 await device._prepare();409 await device.launchApp({ newInstance: true });410 await device.launchApp(launchParams);411 expect(driverMock.driver.deliverPayload).toHaveBeenCalledTimes(1);412 });413 it(`with userNotification should check if process is in background and if not use launch args`, async () => {414 const launchParams = { userNotification: 'notification' };415 const processId = 1;416 const newProcessId = 2;417 const device = await aValidDevice();418 device.deviceDriver.launchApp.mockReturnValueOnce(processId).mockReturnValueOnce(newProcessId);419 await device._prepare();420 await device.launchApp(launchParams);421 expect(driverMock.driver.deliverPayload).not.toHaveBeenCalled();422 });423 it(`with userNotification and url should fail`, async () => {424 const launchParams = { userNotification: 'notification', url: 'url://me' };425 const processId = 1;426 driverMock.driver.launchApp.mockReturnValueOnce(processId).mockReturnValueOnce(processId);427 const device = await aValidDevice();428 await device._prepare();429 try {430 await device.launchApp(launchParams);431 fail('should throw');432 } catch (ex) {433 expect(ex).toBeDefined();434 }435 expect(device.deviceDriver.deliverPayload).not.toHaveBeenCalled();436 });437 it('should keep user params unmodified', async () => {438 const params = {439 url: 'some.url',440 launchArgs: {441 some: 'userArg',442 }443 };444 const paramsClone = _.cloneDeep(params);445 const device = await aValidDevice();446 await device.launchApp(params);447 expect(params).toStrictEqual(paramsClone);448 });449 describe('launch arguments', () => {450 const baseArgs = {451 detoxServer: 'ws://localhost:8099',452 detoxSessionId: 'test',453 };454 const someLaunchArgs = () => ({455 argX: 'valX',456 argY: { value: 'Y' },457 });458 it('should pass preconfigured launch-args to device via driver', async () => {459 const launchArgs = someLaunchArgs();460 const device = await aValidDeviceWithLaunchArgs(launchArgs);461 await device.launchApp();462 driverMock.expectLaunchCalledContainingArgs(launchArgs);463 });464 it('should pass on-site launch-args to device via driver', async () => {465 const launchArgs = someLaunchArgs();466 const expectedArgs = {467 ...baseArgs,468 ...launchArgs,469 };470 const device = await aValidDevice();471 await device.launchApp({ launchArgs });472 driverMock.expectLaunchCalledWithArgs(bundleId, expectedArgs);473 });474 it('should allow for launch-args modification', async () => {475 const launchArgs = someLaunchArgs();476 const argsModifier = {477 argY: null,478 argZ: 'valZ',479 };480 const expectedArgs = {481 argX: 'valX',482 argZ: 'valZ',483 };484 const device = await aValidDeviceWithLaunchArgs(launchArgs);485 device.appLaunchArgs.modify(argsModifier);486 await device.launchApp();487 driverMock.expectLaunchCalledContainingArgs(expectedArgs);488 });489 it('should override launch-args with on-site launch-args', async () => {490 const launchArgs = {491 aLaunchArg: 'aValue?',492 };493 const device = await aValidDeviceWithLaunchArgs();494 device.appLaunchArgs.modify(launchArgs);495 await device.launchApp({496 launchArgs: {497 aLaunchArg: 'aValue!',498 },499 });500 driverMock.expectLaunchCalledContainingArgs({ aLaunchArg: 'aValue!' });501 });502 it('should allow for resetting all args', async () => {503 const launchArgs = someLaunchArgs();504 const expectedArgs = { ...baseArgs };505 const device = await aValidDeviceWithLaunchArgs(launchArgs);506 device.appLaunchArgs.modify({ argZ: 'valZ' });507 device.appLaunchArgs.reset();508 await device.launchApp();509 driverMock.expectLaunchCalledWithArgs(bundleId, expectedArgs);510 });511 });512 });513 describe('installApp()', () => {514 it(`with a custom app path should use custom app path`, async () => {515 const device = await aValidDevice();516 await device.installApp('newAppPath');517 expect(driverMock.driver.installApp).toHaveBeenCalledWith('newAppPath', device._deviceConfig.testBinaryPath);518 });519 it(`with no args should use the default path given in configuration`, async () => {...
Using AI Code Generation
1var target = UIATarget.localTarget();2var app = target.frontMostApp();3var window = app.mainWindow();4target.logElementTree();5target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT);6target.delay(5);7target.captureScreenWithName("Test");8target.logElementTree();9target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_LANDSCAPELEFT);10target.delay(5);11target.captureScreenWithName("Test");12target.logElementTree();13target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT);14target.delay(5);15target.captureScreenWithName("Test");16target.logElementTree();17target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_LANDSCAPELEFT);18target.delay(5);19target.captureScreenWithName("Test");20target.logElementTree();21target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT);22target.delay(5);23target.captureScreenWithName("Test");24target.logElementTree();25target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_LANDSCAPELEFT);26target.delay(5);27target.captureScreenWithName("Test");28target.logElementTree();29target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT);30target.delay(5);31target.captureScreenWithName("Test");32target.logElementTree();33target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_LANDSCAPELEFT);34target.delay(5);35target.captureScreenWithName("Test");36target.logElementTree();37target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT);38target.delay(5);39target.captureScreenWithName("Test");40target.logElementTree();41target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_LANDSCAPELEFT);42target.delay(5);43target.captureScreenWithName("Test");44target.logElementTree();45target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT);46target.delay(5);47target.captureScreenWithName("Test");48target.logElementTree();49target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_LANDSCAPELEFT);50target.delay(5);51target.captureScreenWithName("Test");52target.logElementTree();53target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT);54target.delay(5);55target.captureScreenWithName("Test");56target.logElementTree();57target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_LANDSCAPELEFT);58target.delay(5);59target.captureScreenWithName("Test");60target.logElementTree();61target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT);62target.delay(5);63target.captureScreenWithName("Test");
Using AI Code Generation
1var root = require("root");2var root = require("root");3var root = require("root");4var root = require("root");5var root = require("root");6var root = require("root");
Using AI Code Generation
1var root = require('root.js');2device.launchApp();3var root = require('root.js');4device.launchApp();5var root = require('root.js');6device.launchApp();7var root = require('root.js');8device.launchApp();9var root = require('root.js');10device.launchApp();11var root = require('root.js');12device.launchApp();13var root = require('root.js');14device.launchApp();15var root = require('root.js');16device.launchApp();17var root = require('root.js');18device.launchApp();19var root = require('root.js');20device.launchApp();
Using AI Code Generation
1var root = require('root');2var target = root.aValidDeviceWithLaunchArgs({3 "launch-args": {4 }5});6target.setDeviceOrientation("landscape");7target.delay(1);8target.captureScreenWithName("test");9var root = require('root');10var target = root.aValidDeviceWithLaunchArgs({11 "launch-args": {12 }13});14target.setDeviceOrientation("landscape");15target.delay(1);16target.captureScreenWithName("test");17I am using the following code to set the orientation of the device to landscape and take the screenshot of the screen. var root = require('root'); var target = root.aValidDeviceWithLaunchArgs({ "launch-args": { "test":"test" } }); target.setDeviceOrientation("landscape"); target.delay(1); target.captureScreenWithName("test");18var root = require('root');19var target = root.aValidDeviceWithLaunchArgs({20 "launch-args": {21 }22});23target.setDeviceOrientation("
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!!