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.

Copy code

    /* * 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  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.

Copy code

    [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

## Tap a label on a public device

Copy code

          [Test]
            public void TestPulicDevice()
            {
                //Lock a public device
                var deviceDesc = new DeviceDescription { OsType = "ANDROID", FleetType = "PUBLIC", Source = "HOSTED", Location ="SHANGHAI" };

                var device = MobileLab.LockDevice(deviceDesc);

                //Describe the label to tap
                var photosLabel = device.Describe<IApplication>(new ApplicationDescription
                {
                    Identifier = @"MC.Home",
                    IsPackaged = false
                })
                .Describe<ILabel>(new LabelDescription
                {
                    AccessibilityId = @"Photos",
                    ClassName = @"Label",
                    Text = @"Photos",
                    NativeClass = @"android.widget.TextView",
                    MobileCenterIndex = 4
                });
                photosLabel.Tap();
                device.Unlock();
            }

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.

Copy code

    /* * 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  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.

Copy code

    [Test]
    public void MobileLockAndunlockDeviceWithUsingStatementTest()
    {
        // Lock device using its  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.

Copy code

    [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

Copy code

    [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.

Copy code

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

        // Describe the fingerprint authentication dialog AUT
        var 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 pops up an authentication dialog
        var 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  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.

Copy code

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

        // Describe the authentication AUT
        var 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

https://admhelp.microfocus.com Functional Testing for Developers 8942
Tips to improve your search results... OpenText™ Functional Testing for Developers Help Center OpenText™ Functional Testing for Developers Help 25.2 All View Local Help Center View Online Help Center Version: