Mobile code samples

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.

[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.
	Assert.AreEqual("You Tapped Me", editField.Text);
    
	// Unlock the device at the end of the test.
	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.

[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.
	Assert.AreEqual("You Tapped Me", editField.Text);
    
	// Unlock the device at the end of the test.
	device.Unlock();
}		

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 some stuff here
			// .
			// .
			// ,
			// And finally release this device's lock
			device.Unlock();
		}
	}
}

Back to top