SAP GUI code samples (Java SDK)

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");
		Verify.areEqual("Business Workplace of QA01", session.getActiveWindow(), "Verify Transaction Code", "Verify that the transaction was executed successfully.");

		// 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");
		Verify.areEqual("Enjoy Demo Center: Display", session.getActiveWindow(), "Verify Second Transaction", "Verify a second transaction was executed successfully.");

		// Return to the initial session window and verify that the correct window opens.
		session.reset();
		Verify.areEqual("SAP Easy Access", session.getActiveWindow(), "Verify Return to Initial Session Windows", "Verify return to initial session windows by checking the session's active window.");
	}
	// Add a step to the UFT Developer HTML Report if an error happens.
	catch(Error e){
		Reporter.reportEvent("verify_GuiSessionAndGuiSessionFactory", "Failed during test", 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.
		Verify.areEqual("Business Workplace of QA01", session.getActiveWindow(), "Verify Correct Navigation", "Verify navigation to the correct session window.");
	}
	// Add a step to the UFT Developer HTML Report if there is an error in the test.
	catch(Error e){
		Reporter.reportEvent("verify_OkCode", "Failed during test", 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.
@Test
public 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.
		Verify.areEqual("American Airlines", table.getRows().get(0).getCells().get(0).getValue(), "Verify Cell Value", "Verify that the cell value has correct value.");

		// Set the value of the table cell.
		table.getRows().get(0).getCells().get(0).setValue("Lauda Air");

		// Verify the table cell value.
		Verify.areEqual("Lauda Air", table.getRows().get(0).getCells().get(0).getValue(), "Verify Value Was Set", "Verify that the expected value was set.");
	}
	// Add a step to the UFT Developer HTML Report if there is an error in the test.
	catch(Error e){
		Reporter.reportEvent("TestTreeViewAndTable", "Failed during test", 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.
		Verify.areEqual("No #99", editField.getText(), "Verify Text", "Verify that the edit field contains the expected value.");
	}
	// Add a step to the UFT Developer HTML Report if there is an error in the test.
	catch(Error e){
		Reporter.reportEvent("TestEditField", "Failed during test", 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());

		Verify.areEqual(19, comboBox.getItems().size(), "Verify ComboBox Number of Items", "Verify that the combo box contains 19 items.");

		// Make a selection.
		comboBox.select(3);

		// Verify the selection.
		Verify.areEqual("Air France", comboBox.getSelectedItem().getText(), "Verify Selected Item in ComboBox Using Selected Item's Text", "Verify that the correct combobox item was selected by checking the selected item's text.");
		Verify.areEqual("AF", comboBox.getSelectedItem().getKey(), "Verify Selected Item in ComboBox Using Selected Item's Key", "Verify that the correct combobox item was selected by checking the selected item's key.");
	}
	// Add a step to the UFT Developer HTML Report if there is an error in the test.
	catch(AssertionError e){
		Reporter.reportEvent("verify_comboBox", "Failed during test", Status.Failed, e);
		throw e;
	}
	finally{
		// Exit the session.
		session.close();
	}
}

Back to top

See also: