SAP GUI code samples

Launch an SAP GUI session instance and run transactions

The example below uses GuiSession and GuiSessionFactory to launch an SAP GUI session instance and run transactions.

@Test
public void verify_GuiSessionAndGuiSessionFactory() throws Exception {	
 // Launch an SAP GUI session instance.
 GuiSession session = GuiSessionFactory.launch("ECC 6.0", "800", "qa01", "55c1b303f6a77cb79fcbb321af78eed77befc906");   
			
 try{            
	
  // Enter a transaction code and verify that it runs successfully.	
  session.reset("sbwp");
  assertEquals("Business Workplace of QA01", session.getActiveWindow());
			
  // Enter another transaction code and verify that it runs successfully.
  // Unlike OKCode.setValue(code), you can use the Reset method at any point.            	
  session.reset("dwdm");
  assertEquals("Enjoy Demo Center: Display", session.getActiveWindow());
			
  // Return to the initial session window and verify that the correct window opens.			
  session.reset();		
  assertEquals("SAP Easy Access", session.getActiveWindow());			
 }	
 // Add a step to the LeanFT HTML Report if validation fails.
 catch(AssertionError e){	
  Reporter.reportEvent("verify_GuiSessionAndGuiSessionFactory", "Failed during validation", Status.Failed, e);	
  throw e;	
 }			
 finally{	
  // Exit the session.			
  session.close();			
 }			
}		

Back to top

Run a transaction using OKCode

The example below uses OKCode and Window objects to run a transaction.

@Test
public void verify_OkCode() throws Exception {	
 // Launch an SAP GUI session instance.
 GuiSession session = GuiSessionFactory.launch("ECC 6.0", "800", "qa01", "55c1b303f6a77cb79fcbb321af78eed77befc906");        
			
 try{			
  // Describe the parent window and OKCode objects.
  Window window = session.describe(Window.class, new WindowDescription.Builder().transaction("SESSION_MANAGER").program("SAPLSMTR_NAVIGATION").type(ComponentType.MAIN_WINDOW).name("wnd[0]").build());
			
  OKCode okCode = window.describe(OKCode.class, new OKCodeDescription.Builder().type(ComponentType.OK_CODE_FIELD).build());
			
  // Enter the transaction code in the OKCode field.			
  okCode.setValue("sbwp");
			
  // Commit the transaction by clicking the Enter key.			
  window.sendKey(SapFunctionKey.ENTER);
			
  // Verify navigation to the correct session window.			
  assertEquals("Business Workplace of QA01", session.getActiveWindow());			
 }			
 // Add a step to the LeanFT HTML Report if validation fails.
 catch(AssertionError e){			
  Reporter.reportEvent("verify_OkCode", "Failed during validation", Status.Failed, e);			
  throw e;			
 }			
 finally{			
  // Exit the session.			
  session.close();			
 }			
}		

Back to top

Set data for table cells

The example below uses treeView, treeViewNode, table, tableRow, tableCell and their respective methods and properties to:

  • Activate an item in the tree node to navigate to a table object;
  • Then set data for the table cells.
@Testpublic void TestTreeViewAndTable() throws Exception {			
 // Launch the GuiSession instance.
 GuiSession session = GuiSessionFactory.launch("ECC 6.0", "800", "qa01", "55c1b303f6a77cb79fcbb321af78eed77befc906");
        			
 try{			
  // Navigates to the session window where the tree view control resides.			
  session.reset("dwdm");
            			
  // Describe the tree view object.
  TreeView treeView = session.describe(Window.class, new WindowDescription.Builder().transaction("DWDM").program("SAPMSDM1").type(ComponentType.MAIN_WINDOW).name("wnd[0]").build())			
   .describe(TreeView.class, new TreeViewDescription.Builder().type(ComponentType.CTRL_TREE).name("shell").build());
			
  // Build the path to the node.
  String nodePath = treeView.buildNodePath("Workbench Demos", "Interface Elements", "Table Control");

  // Get the node object.
  TreeViewNode treeViewNode = treeView.getNode(nodePath);

  // Activate the specific item in the node to navigate to the table  object.
  treeViewNode.activateItem("Table Control");

  // Describe the table without its parent window.
  Table table = session.describe(Table.class, new TableDescription.Builder().type(ComponentType.TABLE_CONTROL).name("RSDEMO_TABLE_CONTROLTABLE_CONTROL").build());
			
  // Verify the value of a table cell.			
  assertEquals("American Airlines", table.getRows().get(0).getCells().get(0).getValue());
			
  // Set the value of the table cell.			
  table.getRows().get(0).getCells().get(0).setValue("Lauda Air");
			
  // Verify the table cell value.			
  assertEquals("Lauda Air", table.getRows().get(0).getCells().get(0).getValue());			
 }			
 // Add a step to the LeanFT HTML Report if validation fails.        
 catch(AssertionError e){			
  Reporter.reportEvent("TestTreeViewAndTable", "Failed during validation", Status.Failed, e);		
  throw e;			
 }			
 finally{
  // Exit the session.			
  session.close();			
 }			
}	

Back to top

Set an edit box value

The example below uses editField to set a value for a SAP GUI edit box.

@Test
public void verify_EditField() throws Exception {				
 // Launch an SAP GUI session instance.
 GuiSession session = GuiSessionFactory.launch("ECC 6.0", "800", "qa01", "55c1b303f6a77cb79fcbb321af78eed77befc906");      
				
 try{				
  // Run a transaction to navigate to the edit box.			
  session.reset("bibs");
            				
  // Describe the edit box.
  EditField editField = session.describe(EditField.class, new EditFieldDescription.Builder().type(ComponentType.TEXT_FIELD).name("F1").build());
				
  // Set text for the edit box.				
  editField.setText("No #99");
				
  // Verify the text.				
  assertEquals("No #99", editField.getText());				
 }				
 // Add a step to the LeanFT HTML Report if validation fails.        
 catch(AssertionError e){				
  Reporter.reportEvent("TestEditField", "Failed during validation", Status.Failed, e);				
  throw e;				
 }				
 finally{				
  // Exit the session.				
  session.close();				
 }				
}

Back to top

Select an item in a combo box

The example below selects an item in a combo box.

@Test
public void verify_comboBox() throws Exception {
 // Launch an SAP GUI session instance.
 GuiSession session = GuiSessionFactory.launch("ECC 6.0", "800", "qa01", "55c1b303f6a77cb79fcbb321af78eed77befc906");
       				
 try{				
  // Run a transaction and select a menu bar item to navigate to the combo box.			
  session.reset("bibs");
				
  session.describe(Menubar.class, new MenubarDescription.Builder().type(ComponentType.MENUBAR).build()).select("Goto;Elements;Dropdown List");
				
  // Describe the combo box.
  ComboBox comboBox = session.describe(ComboBox.class, new ComboBoxDescription.Builder().type(ComponentType.COMBO_BOX).name("SCARR-CARRID").build());
				
  assertEquals(19, comboBox.getItems().size());
				
  // Make a selection.				
 comboBox.select(3);
				
  // Verify the selection.				
  assertEquals("Air France", comboBox.getSelectedItem().getText());				
  assertEquals("AF", comboBox.getSelectedItem().getKey());				
 }				
 // Add a step to the LeanFT HTML Report if validation fails.        
 catch(AssertionError e){				
  Reporter.reportEvent("verify_comboBox", "Failed during validation", Status.Failed, e);				
  throw e;				
 }				
 finally{				
  // Exit the session.				
  session.close();				
 }				
}	

Back to top

See also: