Mobile code samples

Tap a button on a mobile device

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

@Test
public void mobileButtonTest() throws Exception {
 // Lock device by its Mobile Center name.
 Device device = MobileLab.lockDeviceByName("MyDevice");
	
 // Describe the AUT.
 Application app = device.describe(Application.class, new ApplicationDescription.Builder()
  .identifier("com.sample.UICatalog").build());
			
 // Launch or restart the app.
 app.restart();
		
 // Describe the table in the app's front screen.
 // Select the 15th item in the table to open a screen with a button.	
 app.describe(Table.class, new TableDescription.Builder().className("Table").resourceId("list").build()).select(14); // Index is 0-based.
 
 // Describe a button in the application.
 Button button = app.describe(Button.class, new ButtonDescription.Builder()
  .className("Button")
  .resourceId("button1")
  .text("Tap Me").build());
	
 // Tap the button to change the text in an EditField to "You Tapped Me".	
 button.tap();

 // Describe the EditField.
 EditField editField = app.describe(EditField.class, new EditFieldDescription.Builder()
  .className("Input")
  .resourceId("editText1").build());
			
 // Verify the text.
 assertEquals("You Tapped Me", editField.getText());

 // Unlock the device at the end of the test.
 device.unlock(); 
}

Back to top

Perform a search in a WebView

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

@Test
public void webViewTest() throws Exception {		
 // Lock device by its Mobile Center name.
 Device device = MobileLab.lockDeviceByName("MyDevice");
			
 // Describe the AUT.
 Application app = device.describe(Application.class, new ApplicationDescription.Builder()
  .identifier("com.sample.UICatalog").build());
		
 // 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(Table.class, new TableDescription.Builder().className("Table").resourceId("list").build()).select(16); // Index is 0-based.

 // Describe the page in the WebView.
 com.hp.lft.sdk.web.Page webViewPage = app.describe(WebView.class, new WebViewDescription.Builder()
  .className("WebView")
  .resourceId("webview")
  .mobileCenterIndex(0).build())
  .describe(com.hp.lft.sdk.web.Page.class, new com.hp.lft.sdk.web.PageDescription());
	
 // Describe the Web EditField of the Google search.
 com.hp.lft.sdk.web.EditField searchEditField = webViewPage.describe(com.hp.lft.sdk.web.EditField.class,
  new com.hp.lft.sdk.web.EditFieldDescription.Builder().type("search").tagName("INPUT").name("q").build());

 // Enter some text in the edit field.	
 searchEditField.setValue("HP LeanFT");
			
 // Describe the search Web button.
 com.hp.lft.sdk.web.Button searchGoButton = webViewPage.describe(com.hp.lft.sdk.web.Button.class,
  new com.hp.lft.sdk.web.ButtonDescription.Builder().buttonType("submit").tagName("BUTTON").name("btnG").build());
			
 // Click the button to perform the search.
 searchGoButton.click();		
}		

Back to top

Iterate through connected devices

This example show how to iterate through all the mobile devices using the getDeviceList method

If a device named "MyDevice" is found, the sample locks it, and then unlocks it.

@Test
public void mobileDevicesListTest() throws Exception {
 // Iterate through all the mobile devices using the getDeviceList method
 for (DeviceInfo deviceInfo : MobileLab.getDeviceList()) {				
  System.out.printf("The device ID is: %s, and its name is: %s", deviceInfo.getId(), deviceInfo.getName());					
  if (deviceInfo.getName().equals("MyDevice")){					
   Device device = MobileLab.lockDeviceByInfo(deviceInfo);				
   // Do some stuff here
   // .
   // .
   // .
   // And finally release this device's lock					
   device.unlock();					
  }					
 }					
}			

Back to top

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

This example locks a device by choosing a device with the Android OS, 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.

@Test
public void mobileDeviceCapabilitiesTest() throws Exception { // Lock device by its Mobile Center name.
Device device = MobileLab.lockDevice(new DeviceDescription.Builder(). osType("ANDROID").osVersion(">4.4.0").build()); // Describe the AUT.
Application app = device.describe(Application.class, new ApplicationDescription.Builder() .identifier("com.sample.UICatalog").build()); // Launch or restart the app. app.restart(); // Describe the table in the app's front screen.
// Select the 15th item in the table to open a screen with a button. app.describe(Table.class, new TableDescription.Builder().className("Table").resourceId("list").build()).select(14); // Index is 0-based.

// Describe a button in the application.
Button button = app.describe(Button.class, new ButtonDescription.Builder() .className("Button") .resourceId("button1") .text("Tap Me").build()); // Tap the button to change the text in an EditField to "You Tapped Me". button.tap(); // Describe the EditField.
EditField editField = app.describe(EditField.class, new EditFieldDescription.Builder() .className("Input") .resourceId("editText1").build()); // Verify the text. assertEquals("You Tapped Me", editField.getText()); device.unlock(); // unlock the device at the end of the test }

Back to top

See also: