System.WIndows.Forms.Ribbon controls in CodedUI

2019-05-29 09:56发布

问题:

I am developing C# winform application. I have Ribbon in my form which contains several tabs and several ribbonbuttons. I am using Coded UI for testing. The problem i am facing is i am unable to find the ribbonbuttons using coded ui, whenever i hover on any control of ribbon (say ribbonbutton), it detects only ribbon but not that control. Can any one suggest whether ribbon controls supports Coded UI for testing? If yes, then how can i do so?

回答1:

There is another ribbonbar inside the ribbonbar you found.

Here is my code: File tab Open button

var mainWindow = new WinWindow(app);
mainWindow.SearchProperties.Add(WinWindow.PropertyNames.ControlName, "frmMain");
var ribbonBar = new WinWindow(mainWindow);
ribbonBar.SearchProperties.Add(WinWindow.PropertyNames.ControlName, "radRibbonBar");

var ribbonBarInside = new WinMenuBar(ribbonBar);
ribbonBarInside.SearchProperties.Add(WinWindow.PropertyNames.Name, "radRibbonBar");   //Name not ControlName!!
Assert.IsTrue(ribbonBarInside.TryFind());

var tabs = new WinTabPage(ribbonBarInside);
var tab = tabs.FindMatchingControls().Where(t => (t as WinTabPage).AccessibleDescription.Trim() == "File").FirstOrDefault();
Assert.IsNotNull(tab);
Mouse.Click(tab);

var buttons = new WinButton(ribbonBarInside);
var button = buttons.FindMatchingControls().Where(t => (t as WinButton).AccessibleDescription.Trim() == "Open").FirstOrDefault();
Assert.IsNotNull(button);
//Mouse.Click(button);   <-- this throw an Exception, next 2 lines solve this.
Mouse.Location = new Point(button.Left + button.Width / 2, button.Top + button.Height / 2);
Mouse.Click();


回答2:

Assuming that Pal Bognar's answer is correct, I would recommend creating your own reusable type for this situation. Here is an example. It would be simple to make this more generic and have sub-classes with the actual buttons and menu tabs available in each instance.

public class WinRibbon : WinControl
{
    protected WinMenuBar RibbonBarInside {
        get
        {
            var ribbonBarInside = new WinMenuBar(this.RibbonBar);
            ribbonBarInside.SearchConfigurations.Add(WinControl.PropertyNames.Name, "radRibbonBar");
            return ribbonBarInside;
        }
    }

    public IEnumerable<WinTabPage> Tabs => new WinTabPage(this.RibbonBarInside).FindMatchingControls().OfType<WinTabPage>();

    public WinControl FileTab => this.Tabs.FirstOrDefault(t => t.AccessibleDescription.Trim() == "File");

    public IEnumerable<WinButton> Buttons => new WinButton(this.RibbonBarInside).FindMatchingControls().OfType<WinButton>();

    public WinButton OpenButton => this.Buttons.FirstOrDefault(t => t.AccessibleDescription.Trim() == "Open");

    public WinRibbon(UITestControl parent = null) : base(parent)
    {
        this.SearchProperties.Add(WinControl.PropertyNames.ControlName, "radRibbonBar");
    }

    public void ClickOpenButton()
    {
        var openButton = this.OpenButton; // to prevent creating a new one each time
        Mouse.Location = new Point(openButton.Left + openButton.Width / 2, openButton.Top + openButton.Height / 2);
        Mouse.Click();
    }
}