Mobile code samples (in .NET)

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]
public void MobileButtonTest()
{
       // Lock device by its Mobile Center 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

Get device vitals

This example retrieves the CPU and free memory of a device.

[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]
public void WebViewTest()
{
       // Lock device by its Mobile Center 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("HP LeanFT");

       // 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 Mobile Center 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:

[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