简体   繁体   中英

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:

保存屏幕按钮

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

常规选项卡类标签

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 打印预览 ).

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?

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM