Share this page

# Mobile code samples (.NET SDK)

In this topic:

## Tap a button on a mobile device

This example taps a button on an application and verifies that the required string is displayed in an edit field.

    /* * This example taps a button on an application and verifies that the required string is displayed in an edit field. */
    [Test]
    publicvoid MobileButtonTest()
    {
           // Lock device by its UFT Mobile name.var device = MobileLab.LockDeviceByName("MyDevice");

           // Describe the AUT.var app = device.Describe<IApplication>(new ApplicationDescription
           {
    		Identifier = "com.sample.UICatalog"
           });

           // Launch or restart the app.
           app.Restart();

           // Describe the Table on the front screen. // Select the 15th item in the Table to open a screen that has a button.
           app.Describe<ITable>(new TableDescription
           {
    		ClassName = "Table",
    		ResourceId = "list"
           }).Select(14); // Index is 0-based.// Describe a button in the application.var button = app.Describe<IButton>(new ButtonDescription
           {
    		ClassName = "Button",
    		ResourceId = "button1",
    		Text = "Tap Me"
           });

           // Tap the button to change the text in an EditField to "You Tapped Me".
           button.Tap();

           // Describe the EditField.var editField = app.Describe<IEditField>(new EditFieldDescription
           {
    		ClassName = "Input",
    		ResourceId = "editText1"
           });

           // Verify the text.
           Verify.AreEqual("You Tapped Me", editField.Text, "MobileButtonTest - Verify Buttons's Text", "Verify the button contains the text 'You Tapped Me'.");
           // Unlock the device at the end of the test.
           device.Unlock();
    }

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.

    [Test]
    public void TestDeviceVitalsExample()
    {
           // lock a device and ask to collect CPU and FreeMemory
           var deviceDesc = new DeviceDescription { Name = "MyDevice" };
           var deviceSessionOpts = new DeviceSessionOptions
           {
    		CollectVitals = new DeviceVitalsCollectOptions
    		{
    			CollectCPU = true,
    			CollectFreeMemory = true,
    			CollectMemory = false
    		}
           };
           var device = HP.LFT.SDK.Mobile.MobileLab.LockDevice(deviceDesc, deviceSessionOpts);

           // ... do something
           // get the collected vitals
           var vitals = device.GetVitals();

           // process vitals
           var vitalLines = vitals.Split("\n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
           foreach (var vitalLine in vitalLines)
           {
    		var vitalLineParts = vitalLine.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
    		Console.WriteLine(String.Format("at {0}: {1} = {2}", vitalLineParts[2], vitalLineParts[0], vitalLineParts[1]));
           }
    }

Back to top

## Perform a search with WebView

This example taps a button on an application and verifies that the required string is displayed in an edit field.

    /* * This test demonstrates the use of a WebView object using Mobile and Web technologies. * This scenario accesses a WebView of the Google search page and then performs a search */
    [Test]
    publicvoid WebViewTest()
    {
           // Lock device by its UFT Mobile name.var device = MobileLab.LockDeviceByName("MyDevice");

           // Describe the app.var app = device.Describe<IApplication>(new ApplicationDescription
           {
    		Identifier = "com.sample.UICatalog"
           });

           // Launch or restart the app.
           app.Restart();

           // Describe the Table on the front screen. // Select the 17th item in the table to open a screen with a WebView of google.com.
           app.Describe<ITable>(new TableDescription
           {
    		ClassName = "Table",
    		ResourceId = "list"
           }).Select(16); // Index is 0-based.// Describe the page in the WebView.var webViewPage = device.Describe<IApplication>(new ApplicationDescription
           {
    		Identifier = @"com.sample.UICatalog",
    		Name = @"UICatalog"
           }).Describe<IWebView>(new WebViewDescription
           {
    		ClassName = @"WebView",
    		ResourceId = @"webview",
    		MobileCenterIndex = 0
    	}).Describe<Web.IPage>(new Web.PageDescription());

           // Describe the Web EditField of the Google search.var searchEditField = webViewPage.Describe<Web.IEditField>(new Web.EditFieldDescription
           {
    		Type = @"search",
    		TagName = @"INPUT",
    		Name = @"q"
           });

           // Enter text in the edit field.
           searchEditField.SetValue("Some Text");

           // Describe the search Web button.var searchGoButton = webViewPage.Describe<Web.IButton>(new Web.ButtonDescription
           {
    		ButtonType = @"submit",
    		TagName = @"BUTTON",
    		Name = @"btnG"
           });

           // Perform the search.
           searchGoButton.Click();
    }

Back to top

## Lock and automatically unlock a device at the end of a session

This example shows how to lock a device using C# using statement. This automatically unlocks the device at the end of its usage.

    [Test]
    public void MobileLockAndunlockDeviceWithUsingStatementTest()
    {
    	// Lock device using its UFT Mobile name.
    	using (var device = MobileLab.LockDeviceByName("MyDevice"))
    	{
    		// Describe the AUT.
    		var app = device.Describe<IApplication>(new ApplicationDescription
    		{
    			Identifier = "com.sample.UICatalog"
    		});

    		// Launch or restart the app.
    		app.Restart();

    		// Perform actions on the app.
    		// .
    		// .
    		// .
    	} // The device is automatically unlocked when the device variable goes out of scope.
    }

Back to top

## List all available devices

This example returns a list of all available devices.

For each device, the device ID and name is printed to the Debug Output stream.

If a device named MyDevice is found, this example also locks it so actions can be performed on it. This example then unlocks the device.

    [Test]
    public void MobileDevicesListTest(){
    	var devicesList = MobileLab.GetDeviceList();
    	foreach (IDeviceInfo deviceInfo in devicesList)
    	{
    		Debug.WriteLine("The device ID is: {0}, and its name is: {1}", deviceInfo.Id, deviceInfo.Name);
    		if (string.Equals(deviceInfo.Name,"MyDevice"))
    		{
    			var device = MobileLab.LockDeviceByInfo(deviceInfo);
    			// Do something here
    			// .
    			// .
    			// ,
    			// And finally release this device's lock
    			device.Unlock();
    		}
    	}
    }

Back to top

## Camera simulation

This section includes two examples:

- Image upload
- Video upload

    [Test]
    public void MobileCameraSimulationExampleTestWithImageUpload()
    {
    	// First - lock the device you want to use
    	IDevice device = MobileLab.LockDeviceById("MyDeviceId");

    	// Next, upload a media file to the device
    	Image image = Image.FromFile(@"c:\Temp\abc.bmp");
    	string mediaId = "abc.bmp";
    	device.UploadMedia(image, mediaId);

    	// Launch the app to use

           var app = device.Describe<IApplication>(new ApplicationDescription
    	{
    		Identifier = "com.sample.UICatalog",
    		Version = "2.0",
    		IsPackaged = true
    	});
    	app.Launch();

    	// Start the camera simulation mode
    	app.StartCameraSimulationMode(mediaId);

    	// Now if the application under test shows an image from the camera, it should show the image we uploaded.      // .

           // . Add steps to the test

           // .

           // Stop the camera simulation mode
    	app.StopCameraSimulationMode();

    	app.Kill();
    }

Back to top

## Simulate fingerprint authentication

This example shows how to simulate fingerprint authentication.

    [Test]
    publicvoid MobileSimulateAuthenticationExampleTest()
    {
    	// Lock device by its UFT Mobile name.var device = MobileLab.LockDeviceByName("MyDevice");

    	// Describe the fingerprint authentication dialog AUTvar fingerprintDialog = device.Describe<IApplication>(new ApplicationDescription
    	{
    		Identifier = @"com.ll.fingerprintdialog",
    		IsPackaged = true
    	});
    	// Launch or restart the app.
    	fingerprintDialog.Launch();

    	// This sample AUT has a purchase button which will pop up an authentication dialogvar purchase = fingerprintDialog.Describe<IButton>(new ButtonDescription
    	{
    		ClassName = @"Button",
    		MobileCenterIndex = 0,
    		NativeClass = @"android.widget.Button",
    		ResourceId = @"com.ll.fingerprintdialog:id/purchase_button",
    		Text = @"Purchase"
    	});
    	purchase.Tap();

    	// Simulate a successful authentication
    	fingerprintDialog.SimulateAuthentication.Succeed();

    	// The above authentication simulation can also simulate a failure of the authentication. Each of the following ones can be used://// Each of the next lines shows a simulation of authentication failure, with different reasons. For additional information please refer to the UFT Developer documentation// fingerprintDialog.SimulateAuthentication.Fail(HP.LFT.SDK.Mobile.SimulateAuthFailReason.Lockout);// fingerprintDialog.SimulateAuthentication.Fail(HP.LFT.SDK.Mobile.SimulateAuthFailReason.NotRecognized);// fingerprintDialog.SimulateAuthentication.Fail(HP.LFT.SDK.Mobile.SimulateAuthFailReason.NotRegistered);// fingerprintDialog.SimulateAuthentication.Fail(HP.LFT.SDK.Mobile.SimulateAuthFailReason.FingerprintIncomplete);// fingerprintDialog.SimulateAuthentication.Fail(HP.LFT.SDK.Mobile.SimulateAuthFailReason.SensorDirty);// fingerprintDialog.SimulateAuthentication.Cancel(HP.LFT.SDK.Mobile.SimulateAuthCancelOrigin.User);// fingerprintDialog.SimulateAuthentication.Cancel(HP.LFT.SDK.Mobile.SimulateAuthCancelOrigin.System);
    }

Back to top

## Simulate barcode or QR code authentication

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

    [Test]
    publicvoid MobileSimulateQRBarcodeScanExampleTest()
    {
    	// Lock device by its UFT Mobile name.var device = MobileLab.LockDeviceByName("MyDevice");

    	// Describe the authentication AUTvar qrBarcodeApp = device.Describe<IApplication>(new ApplicationDescription
    	{
    		Identifier = @"com.company.QRBarcodeSample",
    		IsPackaged = true
    	});

    	// Upload the barcode or QR code imagevar mediaId = "QRBarcodeImage1";
    	Image barcodeImage = new Bitmap("QRBarcodeImage.jpg");
    	device.UploadMedia(barcodeImage, mediaId);

    	// Launch or restart the app.
    	qrBarcodeApp.Launch();

    	// Simulate the barcode scan
    	qrBarcodeApp.SimulateBarcodeScan(mediaId);
    }

Back to top

## Service Virtualization integration

This example uses a Service Virtualization scenario to simulate a date change in a weather application.

    // Lock a device that is configured on UFT Mobile to work with Service Virtualization.

    DeviceDescription deviceDesc = new DeviceDescription {Id = "<device that was configured properly>"};
    DeviceSessionOptions sessionOptions = new DeviceSessionOptions {EnableServiceVirtualization = true};
    IDevice device = MobileLab.LockDevice(deviceDesc, sessionOptions);

    // Upload the Service Virtualization service and scenario.
    var serviceFile = File.ReadAllText(@"<path>\sv service file\sv-lab-weather.json");
    var serviceId = device.SvUploadService(serviceFile);
    Assert.IsNotNull(serviceId);
    var scenarioId = device.SvUploadScenario(serviceId, @"<path>\sv scenario files");
    Assert.IsNotNull(scenarioId);
    IApplication app = device.Describe<IApplication>(new ApplicationDescription
    {
    	Identifier = @"com.casticalabs.forecastie",
    	IsPackaged = true
    });
    app.Install();
    //
    // at this point if you click the refresh icon in the AUT you should get an error
    //
    device.SvStartScenario(scenarioId);
    //
    // at this point if you click the refresh icon in the AUT you should get some info from SV (update date should be updated)
    //
    device.SvStopScenario(scenarioId);
    //
    // at this point if you click the refresh icon in the AUT you should get an error again
    //

Back to top

Tips to improve your search results... UFT Developer .NET SDK Reference Version 15.0-15.0.2 UFT Developer SDK Reference All View Local Help Center View Online Help Center back Go back in browser history forward Go forward in browser history Remove search highlights next topic Go to next topic previous topic Go to previous topic Print the current topic Version: