Mobile code samples (JavaScript SDK)

Tap a button on a mobile device

This example shows how to tap a button on an AUT and verify that the required string is displayed in an edit field.

it("Should successfully tap a button on a mobile device application",function(done){
	Mobile.Lab.lockDeviceByName("Nexus 7").then(function (device){
		var app = device.$(Mobile.Application({
		identifier: "com.sample.UICatalog"
		}));

		app.restart().then(function(){
			var table = app.$(Mobile.Table({
				className:"Table",
				resourceId:"list"
			}));
			table.select(14);

			var button = app.$(Mobile.Button({
				className:"Button",
				resourceId:"button1",
				text:"Tap Me"
			}));
			button.tap();

			var editField = app.$(Mobile.EditField({
				className:"Input",
				resourceId:"editText1"
			}));
			expect(editField.text().toEqual("You tapped me"));
		});

		LFT.whenDone(done);
	});
});

Back to top

Perform a search in a WebView

This example shows the use of a WebView using Mobile and Web technologies by accessing the Google search page in a WebView and performing a search.

it("Should perform a search on Google using UFT Developer's Mobile WebView",function(done){
	Mobile.Lab.lockDeviceByName("Nexus 7").then(function (device){
		var app = device.$(Mobile.Application({
			identifier: "com.sample.UICatalog"
		}));

		app.restart().then(function(){
			var table = app.$(Mobile.Table({
				className:"Table",
				resourceId:"list"
			}));
			table.select(16);

			// Describe the WebView.
var webView = app.$(Mobile.WebView({ className:"WebView", resourceId:"webview", mobileCenterIndex:0 })); // Describe the page in the WebView.
var webViewPage = webView.$(Web.Page({ })); // Describe the Web EditField of the Google search.
var searchEditField = webViewPage.$(Web.Edit({ type:"search", tagName:"Input", name:"q" })); // Enter some text in the edit field searchEditField.setValue("Some Text"); // Describe the Search Web button.
var searchGoButton = webViewPage.$(Web.Edit({ buttonType:"submit", tagName:"BUTTON", name:"btnG" })); // Perform the search. searchGoButton.click(); }); LFT.whenDone(done); }); });

Back to top

Lock device based on device properties

This example shows how to lock a device based on particular device properties: Android OS and version later than 4.4.0.

It then taps a button on an AUT and verifies that the required string is displayed in an edit field.

It then unlocks the device at the end of the session.

it("Should successfully select a device and lock it using device properties",function(done){
	Mobile.Lab.lockDevice(Mobile.Device({
			osType: "ANDROID",
			osVersion: ">4.4.0"
		})).then(function (device){
		var app = device.$(Mobile.Application({
			identifier: "com.sample.UICatalog"
		}));

		app.restart().then(function(){
			var table = app.$(Mobile.Table({
				className:"Table",
				resourceId:"list"
			}));
			table.select(14);

			var button = app.$(Mobile.Button({
				className:"Button",
				resourceId:"button1",
				text:"Tap Me"
			}));
			button.tap();

			var editField = app.$(Mobile.EditField({
				className:"Input",
				resourceId:"editText1"
			}));
			expect(editField.text().toEqual("You tapped me"));
		});

		LFT.whenDone(done);
	});
});

Back to top

List devices in the Digital Lab (UFT Mobile) lab

This example shows how to list of all the devices in the Digital Lab (UFT Mobile) lab.

it("Should print the name of all the devices in the digital lab",function(done){
	Mobile.Lab.getDeviceList().then(function (deviceList){
		console.log("Number of devices:" + deviceList.length)
		deviceList.forEach(function(device){
			console.log(device.name);
		});
	});

	LFT.whenDone(done);
});

Back to top

Collect accumulated device vitals data

This example retrieves the CPU and free memory data of a device, accumulated while it was locked.

it("Should successfully collect device vitals info and print it",function(done){
	Mobile.Lab.lockDeviceWithOptions(Mobile.Device({name: "MyDevice"}),
			 {collectVitals: {cpu: true, memory: true}}).then(function (device){
		device.getVitals().then(function(vitalsString)){
			};

		LFT.whenDone(done);
	});
});		

Back to top

Simulate fingerprint authentication

This example shows how to simulate fingerprint authentication.

it("Should simulate fingerprint authentication in an app",function(done){
	Mobile.Lab.lockDeviceByName("Nexus 7").then(function (device){
		var app = device.$(Mobile.Application({
			identifier: "com.ll.fingerprintdialog",
				isPackaged: true
		}));

		app.restart().then(function(){
			var purchase = app.$(Mobile.Button({
			      className:"Button",
						mobileCenterIndex: 0,
						nativeClass: "android.widget.Button",
			      resourceId:"com.ll.fingerprintdialog:id/purchase_button",
			      text:"Purchase"
			}));
			purchase.tap();

			// Simulate a successful authentication
			app.simulateAuthSucceed();
				
			// The above authentication simulation can also simulate a failure of the authentication. Each of the following ones can be used:
			//
			// app.simulateAuthFail(Mobile.SimulateAuthFailReason.lockout);
			// app.simulateAuthFail(Mobile.SimulateAuthFailReason.notRecognized);
			// app.simulateAuthFail(Mobile.SimulateAuthFailReason.notRegistered);
			// app.simulateAuthFail(Mobile.SimulateAuthFailReason.fingerprintIncomplete);
			// app.simulateAuthFail(Mobile.SimulateAuthFailReason.sensorDirty);
			// app.simulateAuthCancel(Mobile.SimulateAuthCancelOrigin.system);
			// app.simulateAuthCancel(Mobile.SimulateAuthCancelOrigin.user);
		});

		LFT.whenDone(done);
	});
});

Back to top

Simulate barcode or QR code authentication

This example shows how to simulate barcode or QR code authentication.

it("should lock a device, launch app, start QR barcode simulation.",function(done){
	Mobile.Lab.lockDeviceByName("Nexus 7").then(function (device){
		var app = device.$(Mobile.Application({
			identifier: "com.company.QRBarcodeSample",
				isPackaged: true
		}));

		var mediaId = "QRBarcodeImage1";
			
		device.uploadMedia({Base64Data:"PUT HERE THE BASE64 STRING OF THE QR BARCODE IMAGE", MimeType:"image/jpeg"}, mediaId).then(function(){
			app.launch().then(function(){
			app.simulateBarcodeScan(mediaId).then(function (){
			// Do some code here after the bar code simulation
						// ...                        
			});
		   });
	    });

	    LFT.whenDone(done);   
     });
});		

Back to top

See also: