SAPGUI code samples (.NET SDK)

Launch an SAP GUI session and run transactions

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

// The example below uses GuiSession and GuiSessionFactory
// 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");
       Verify.AreEqual(@"Business Workplace of QA01", session.ActiveWindow, "Verify Transaction Code", "Verify that the transaction was executed successfully.");

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

       // Return to the initial session window.
       session.Reset();
       Verify.AreEqual(@"SAP Easy Access", session.ActiveWindow, "Verify Return to Initial Session Windows", "Verify return to initial session windows by checking the session's active window.");

       // 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.

// The example below uses IOKCode, IWindow 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.
       Verify.AreEqual(@"Business Workplace of QA01", session.ActiveWindow, "Verify Correct Navigation", "Verify navigation to the correct session window.");

       // 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.

// The example below uses ITreeView, ITreeViewNode, ITable, ITableRow, and ITableCell 
// 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.
       Verify.AreEqual(@"American Airlines", table.Rows[0].Cells[0].Value, "Verify Cell Value", "Verify that the cell value has coorect value.");   
       // Set the value of the table cell.
       table.Rows[0].Cells[0].SetValue(@"Lauda Air");

       // Verify value set.
       Verify.AreEqual(@"Lauda Air", table.Rows[0].Cells[0].Value, "Verify Value Was Set", "Verify that the expected value was set.");

       // 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.

// The example below 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.
       Verify.AreEqual(@"No #99", editField.Text, "Verify Text", "Verify that the edit field contains the expected value.");

       // 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.

 // The example below 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"
       });

       Verify.AreEqual(19, comboBox.Items.Count, "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.SelectedItem.Text, "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.SelectedItem.Key, "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.");

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

Back to top