public void InvokeButton(AddinRibbonButton button) { if ((button == AddinRibbonButton.Create && !_app.IsVersion2010OrAbove()) || //2007 create button does not support invoke pattern !WindowsUtility.WindowsVerSupportsModalInvokeWithoutHang()) { //Special-cases which lead to blocking modal dialog when using InvokePattern - must instead use mouse click UIAUtility.FindElementByNameFilteredByControlTypeAndMouseClick(_addinRibbonCtl, _buttonNameLookup[button], ControlType.Button, ControlType.Custom, AddinTestUtility.RibbonMouseMoveToClickDelayAllowForTooltip, TreeScope.Descendants); } else { var el = UIAUtility.FindElementByControlTypeAndNameWithTimeout(_addinRibbonCtl, ControlType.Button, ControlType.Custom, _buttonNameLookup[button], AddinTestUtility.FindRibbonButtonsTimeout, TreeScope.Descendants); UIAUtility.WaitForElementEnabledWithTimeout(el, AddinTestUtility.RibbonButtonsBecomeActivatedTimeout); UIAUtility.PressButton(el); if (button == AddinRibbonButton.Logout) { //Special case force ribbon refresh when logout button invoked ForceRibbonRefresh(); } } }
/// <summary> /// Forces ribbon refresh by clicking about then closing the modal dialog /// </summary> /// <remarks> /// Should be used sparingly, only when necessary to force refresh under automation /// </remarks> public void ForceRibbonRefresh() { if (WindowsUtility.WindowsVerSupportsModalInvokeWithoutHang()) { //Ensure event updating of ribbon in excel by invoking a dialog then closing it InvokeButton(AddinRibbonButton.About); var aboutDlg = AboutDriver.FindFromParent(_excel); aboutDlg.SelectOKVerifySuccess(); } else { const int retries = 2; for (int i = 0; i < retries; i++) { try { //Ensure event updating of ribbon in excel by invoking a dialog then closing it InvokeButton(AddinRibbonButton.About); var aboutDlg = AboutDriver.FindFromParent(_excel); aboutDlg.SelectOKVerifySuccess(); break; } catch (Exception) { //Go to retries } } } }