Liren Yeo
Liren Yeo

Reputation: 3451

C# TestStack.White - Unable to pinpoint to the button I want

I want to open a windows application, and press the "Save Screen" button from the toolbar:

Save Screen Button

Using Spy++ from Visual Studio, I am able to get the following properties for this toolbar:

General Tab Class Tab

With above information, I tried this:

Application app = Application.Attach(7492); //The process was manually opened
Window window = app.GetWindow("Scope Link - Virtual Instrument", InitializeOption.Nocache);
SearchCriteria sc = SearchCriteria.ByClassName("ToolbarWindows32"); //AutomationID is 59392
var saveBtn = window.Get(sc);
saveBtn.Click();

With the code above, the program was able to locate the toolbar, but it would simply click at the middle of the toolbar, which is 9th icon from the left (the "Print Preview" button Print Preview).

I also tried SearchCriteria.ByText("Save Screen") but that gave me error.

So how can make the mouseclick to be at the button I want? Is there a mouseclick coordinate offset that I can adjust?

Upvotes: 1

Views: 3333

Answers (1)

unickq
unickq

Reputation: 3295

The problem is that all buttons on your toolbar have same ClassName - ToolbarWindows32.

Try to find all buttons on your toolbar and get yours by index.

toolbar.GetMultiple(SeacrhCriteria.ByControlType(ControlType.Button))[INDEX].Click();

Generally, you can use native UI Automation:

1) Find the toolbar:

var toolbar = window.Get(SearchCriteria.ByClassName("ToolbarWindows32"));

2) Get its Automation element for toolbar:

AutomationElement toolbarAe = toolbar.AutomationElement;

3) Get all children for this AutomationElement.

var listAeChildren = toolbarAe.findAll(Treescope.Children, new PropertyCondition(AutomationElement.ControltypeProperty, ControlType.Button));

4) Find your button from the list and get in Point

var clickablePoint = new Point();
foreach(AutomationElement button : listAeChildren)
{
    if(button.Current.Name.Equals("Save Screen"))
    {
        clickablePoint = button.GetClickablePoint();
    }
}

5) Click on point:

Mouse.Instance.Click(clickablePoint);

Check the code cause I wrote it from memory without IDE :)

Upvotes: 4

Related Questions