SAPUI5 code samples

Select an item from a list

This example locates and selects an item in a list box using a property-based description.

@Test
public void verify_listBoxSelectListItemByIndex() throws Exception {			
 // Launch the Internet Explorer browser.
 Browser browser = BrowserFactory.launch(BrowserType.INTERNET_EXPLORER);
			
 // Use try-catch to add a warning to the run report if the assert validation fails.          
 try{			
  // Navigate to the AUT site.			
  browser.navigate("https://sapui5.netweaver.ondemand.com/sdk/#test-resources/sap/ui/commons/ListBox.html");
			
  // Identify the list box.
  ListBox listBox = browser.describe(ListBox.class, new ListBoxDescription.Builder()			
  .tagName("DIV").id("lb3").build());
            			
  // Select the first item in the list by index.			
  listBox.select(1);
         			
  // Enclosing the Assert method in a try-catch statement ensures the application does not throw a runtime error if the Assert returns false.
  // Check that only one item in the list box is selected.			
  assertEquals(1, listBox.getSelectedItems().size());			
 }			
 catch(AssertionError e){			
  // Adds a step to the results report on failure.
  Reporter.reportEvent("verify_listBoxSelectListItemByIndex","Failed during validation",Status.Failed, e);			
  throw e;			
 }			
 finally{			
  browser.close();			
 }			
}		

Back to top

Delete an item from a list

This example deletes an item from a list box using a property-based description.

@Test
 // Launch the Internet Explorer browser
 Browser browser = BrowserFactory.launch(BrowserType.INTERNET_EXPLORER);
			
 try{			
  // Navigate to the web site.			
  browser.navigate("https://sapui5.hana.ondemand.com/sdk/test-resources/sap/m/List.html");
			
  // Select the list box.                			
  browser.describe(ListBox.class, new ListBoxDescription.Builder()			
  .tagName("DIV").name("List Overview").build()).select("Standard List Thumb");            
			
  // Click the Delete button. The delete button adds a button next to each item in the list box.
  // This enables you to select single items in the list box to delete.      			
  browser.describe(Button.class, new ButtonDescription.Builder()			
  .tagName("BUTTON").buttonType("submit").name("Delete").build()).click();          
			
  // Identify the list box.
  ListBox listBox = browser.describe(ListBox.class, new ListBoxDescription.Builder()			
  .tagName("DIV").name("Travel [List-Mode: Delete]").build());
            			
  // Delete an item from the list box.
   listBox.getMobile().delete("Travel Settings");
 			
 }catch(AssertionError e){			
  Reporter.reportEvent("verify_listBoxFunctionalityForSubType","Failed during validation",Status.Failed, e);			
  throw e;			
 }			
 finally{			
  browser.close();			
 }			
}		

Back to top

See also: