Code-Snippets

In the following there is shown how to perform certain tasks.
For a complete documentation please consult the API-documentation.

Start variants

Start variant 1: Start a WPF UserControl directly (with an initialize-action)
CustomControl control = CustomControl.Start<SimpleUserControl>((c) => c.Initialize("init"));

Start variant 2: Start a WPF Window directly (with passing some parameters for the ApplicationTestWindow-constructor)
WindowControl window = WindowControl.Start<ApplicationTestWindow>("foo", "bar");

Start variant 3: Start an entire application (and fetching the window)
this.application = Application.Start(@"WpfTestApplication.exe");
Window window = application.GetWindowById("WindowWpfTestApplication");

Search possibilities

Simple search with the type and the Automation-Id
Button button = window.Get<Button>("btnAdd");
// or
Button button = window.Get<Button>(SearchCondition.ByAutomationId("btnAdd"));

Simple search with the type and the name
Button button = window.GetByName<Button>("Add");
// or
Button button = window.Get<Button>(SearchCondition.ByName("Add"));

Combine search conditions
Button button = window.Get<Button>(SearchCondition.ByClassName("Button").And(SearchCondition.ByProcessId(22)));

Search condition with a UI Automation property (from UI Automation framework)
Button button = window.Get<Button>(SearchCondition.ByAutomationProperty(AutomationElement.IsPasswordProperty, true));

Search for a user-specific control which is not supported by Guia
UiElement myControl = window.Get("myUserControl");

Search all buttons
Button[] buttons = window.GetAll<Button>();

Search all buttons with a given name
Button[] buttons = window.GetAll<Button>(SearchCondition.ByName("Add"));

Search with a given scope
SearchCondition condition = SearchCondition.ByName("Add");
condition.Scope = TreeScope.Subtree;
Button button = window.Get<Button>(condition);

Some operations of different UI elements

Get the text of a Label
label.TextValue;

Get the tooltip of a Label
label.ToolTip;

Get the value of a TextBox / Set the value of a TextBox
txtText.Value;
txtText.Value = "foo";

Execute a Button action (like a click)
button.Invoke();

Select a Checkbox
checkBox.Check();

Select a Combobox Item
comboBox.GetItem("item 1").Select();
// or
comboBox["item 1"].Select();
// or
comboBox[2].Select();
// or
comboBox[2].IsSelected = true;

Execute a Menu action (like a click)
menuMain["miFile"]["miOpen"].Invoke();

Get the context-menu and execute a Menu-item
window.ClickRight();
window.Get<ContextMenu>("cm")["miCopy"].Invoke();

Select an element in a TreeView and expand it
treeView1["Item 1"].Expand();
// or
treeView1[2].Expand();
// or
treeView1.GetItem(2).Expand();

Add data in a DataGrid
dataGrid[2, 1].TextValue = "test";
// or
dataGrid.Rows[2].Cells[1].TextValue = "test";

Maximize a window
window.Maximize();

Last edited Jun 15, 2010 at 4:29 PM by Ghostinus, version 2

Comments

No comments yet.