2017-07-28 59 views
1

我正在開發C#winform應用程序。我的表單中有Ribbon,其中包含幾個選項卡和幾個ribbonbuttons。我正在使用Coded UI進行測試。我面臨的問題是我無法找到使用編碼的UI的ribbonbuttons,每當我懸停在任何控制功能區(比如ribbonbutton)上時,它只檢測到Ribbon而不檢測控制。任何人都可以建議功能區控件是否支持用於測試的編碼UI?如果是,那我該怎麼辦?System.WIndows.Forms.Ribbon控件CodedUI

+0

如果您發現以下任一解決方案有幫助,請標記爲已接受。如果仍然存在問題,請更新您的問題,試用您已經嘗試過的以及哪些功能無法正常工作。 – MPavlak

回答

0

在找到的色帶欄中還有另一個色帶欄。

這裏是我的代碼:File選項卡打開按鈕

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(); 
0

假設帕爾Bognar的答案是正確的,我會建議創建自己的可重複使用的類型,這種局面。這是一個例子。使這個更通用並且在每個實例中都有可用的實際按鈕和菜單選項卡的子類將會很簡單。

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(); 
    } 
}