SAPGUI code samples

Launch an SAP GUI session and run transactions

The following example uses GuiSession and GuiSessionFactory methods to launch a GuiSession and execute transaction codes.

[Test]
public void TestManipulatingGuiSession()
{
	// Program flow: 
	//  1. Launch a GuiSession from GuiSessionFactory.
	//  2. Execute transaction codes in the GuiSession.
	//  3. Return to the initial session window.
	//  4. Close the session.

	// Launch a GuiSession instance.
	IGuiSession session = GuiSessionFactory.Launch("ECC 6.0", "800", "qa01", "55c1b303f6a77cb79fcbb321af78eed77befc906");

	// Execute a transaction code and verify it is executed successfully.
	session.Reset(@"sbwp");
	Assert.AreEqual(@"Business Workplace of QA01", session.ActiveWindow);

	// Execute another transaction code and verify it is executed successfully.
	// Unlike IOKCode.Set(code), Reset method can be performed at any point.
	session.Reset(@"dwdm");
	Assert.AreEqual(@"Enjoy Demo Center: Display", session.ActiveWindow);

	// Return to the initial session window.
	session.Reset();
	Assert.AreEqual(@"SAP Easy Access", session.ActiveWindow);

	// Exit the session.
	session.Close();
}		

Back to top

Run a transaction using IOKCode

The following example uses IOKCode and IWindow methods to execute a transaction code.

[Test]
public void TestOkCode()
{
	// Program flow: 
	//  1. Launch a GuiSession from GuiSessionFactory.
	//  2. Execute a transaction code in the IOKCode.
    
	// Launch a GuiSession instance.
	IGuiSession session = GuiSessionFactory.Launch("ECC 6.0", "800", "qa01", "55c1b303f6a77cb79fcbb321af78eed77befc906");

	// Describe the parent window and the OKCode objects.
	var window = session.Describe<IWindow>(new WindowDescription
	{
		Transaction = @"SESSION_MANAGER",
		Program = @"SAPLSMTR_NAVIGATION",
		Type = HP.LFT.SDK.SAP.GUI.ComponentType.MainWindow,
		Name = @"wnd[0]"
	});

	var okCode = window.Describe<IOKCode>(new OKCodeDescription
	{
		Type = HP.LFT.SDK.SAP.GUI.ComponentType.OkCodeField
	});

	// 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.
	Assert.AreEqual(@"Business Workplace of QA01", session.ActiveWindow);

	// Exit the session.
	session.Close();
}

Back to top

Set data for a table cell

The example below uses ITreeView, ITreeViewNode, ITable, ITableRow, and ITableCell methods to activate an item in the tree node, to navigate to a table object, and set data for the table cells.

[Test]
public void TestTreeViewAndTable()
{
	// Program flow: 
	//  1. Launch a GuiSession from GuiSessionFactory.
	//  2. Navigate to a ITreeView object of type ColumnTree.
	//  3. Activate an item in a TreeView Node.

	// Launch a GuiSession instance.
	IGuiSession session = GuiSessionFactory.Launch("ECC 6.0", "800", "qa01", "55c1b303f6a77cb79fcbb321af78eed77befc906");

	// Navigates to the session window where holds the tree view control.
	session.Reset(@"dwdm");

	// Describe the tree view control.
	var treeView = session.Describe<IWindow>(new WindowDescription
	{
		Transaction = @"DWDM",
		Program = @"SAPMSDM1",
		Type = HP.LFT.SDK.SAP.GUI.ComponentType.MainWindow,
		Name = @"wnd[0]"
	}).Describe<ITreeView>(new TreeViewDescription
	{
		Type = HP.LFT.SDK.SAP.GUI.ComponentType.CtrlTree,
		Name = @"shell"
	});

	// Build the path to the node.
	var nodePath = treeView.BuildNodePath(@"Workbench Demos", @"Interface Elements", @"Table Control");

	// Get the node object.
	var treeViewNode = treeView.GetNode(nodePath);

	// Navigate to the table object.
	treeViewNode.ActivateItem(@"Table Control");

	// Describe the table.
	var table = session.Describe<IWindow>(new WindowDescription
	{
		Transaction = @"DWDM",
		Program = @"RSDEMO_TABLE_CONTROL",
		Type = HP.LFT.SDK.SAP.GUI.ComponentType.MainWindow,
		Name = @"wnd[0]"
	}).Describe<ITable>(new TableDescription
	{
		Type = HP.LFT.SDK.SAP.GUI.ComponentType.TableControl,
		Name = @"RSDEMO_TABLE_CONTROLTABLE_CONTROL"
	});

	// Verify the value of a table cell.
	Assert.AreEqual(@"American Airlines", table.Rows[0].Cells[0].Value);

	// Set the value of the table cell.
	table.Rows[0].Cells[0].SetValue(@"Lauda Air");

	// Verify value set.
	Assert.AreEqual(@"Lauda Air", table.Rows[0].Cells[0].Value);

	// Exit the session.
	session.Close();
}

Back to top

Set an edit box value

The following example uses IEditField to set a value for a SAP GUI edit box.

[Test]
public void TestEditField()
{
	// Program flow: 
	//  1. Launch a GuiSession from GuiSessionFactory.
	//  2. Navigate to the edit box.
	//  3. Set a value for the editbox.

	// Launch a GuiSession instance.
	IGuiSession session = GuiSessionFactory.Launch("ECC 6.0", "800", "qa01", "55c1b303f6a77cb79fcbb321af78eed77befc906");

	// Execute a transaction code to navigate to the editbox.
	session.Reset(@"bibs");

	// Describe the editbox.
	var editField = session.Describe<IWindow>(new WindowDescription
	{
		Transaction = @"BIBS",
		Program = @"SAPLEXAMPLE_ENTRY_SCREEN",
		Type = HP.LFT.SDK.SAP.GUI.ComponentType.MainWindow,
		Name = @"wnd[0]"
	}).Describe<IEditField>(new EditFieldDescription
	{
		Type = HP.LFT.SDK.SAP.GUI.ComponentType.TextField,
		Name = @"F1"
	});

	// Set text for the editbox.
	editField.SetText(@"No #99");

	// Verify the text.
	Assert.AreEqual(@"No #99", editField.Text);

	// Exit the session.
	session.Close();
}

Back to top

Select an item in a combo box

The following example uses IComboBox to make a selection on a combo box control

[Test]public void TestComboBox()
{
	// Program flow: 
	//  1. Launch a GuiSession from GuiSessionFactory.
	//  2. Navigate to the combo box control.
	//  3. Make a selection on the combo box.

	// Launch a GuiSession instance.
	IGuiSession session = GuiSessionFactory.Launch("ECC 6.0", "800", "qa01", "55c1b303f6a77cb79fcbb321af78eed77befc906");

	// Navigates to the control box.
	session.Reset(@"bibs");

	var window = session.Describe<IWindow>(new WindowDescription
	{
		Transaction = @"BIBS",
		Program = @"SAPLEXAMPLE_ENTRY_SCREEN",
		Type = HP.LFT.SDK.SAP.GUI.ComponentType.MainWindow,
		Name = @"wnd[0]"
	});
	window.Describe<IMenubar>(new MenubarDescription
	{
		Type = HP.LFT.SDK.SAP.GUI.ComponentType.Menubar
	}).Select(@"Goto;Elements;Dropdown List");

	// Describe the combo box.
	var comboBox = window.Describe<IComboBox>(new ComboBoxDescription
	{
		Type = HP.LFT.SDK.SAP.GUI.ComponentType.ComboBox,
		Name = @"SCARR-CARRID"
	});

	Assert.AreEqual(19, comboBox.Items.Count);

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

	// Verify the selection.
	Assert.AreEqual(@"Air France", comboBox.SelectedItem.Text);
	Assert.AreEqual(@"AF", comboBox.SelectedItem.Key);

	// Exit the session.
	session.Close();
}

Back to top