IHttpActionResult CaptureData(Guid id, DataContextMode dm, TreeViewMode tvm, bool force) { try { if (CaptureAction.SetTestModeDataContext(id, dm, tvm, force)) { if (dm == DataContextMode.Test) { var dc = GetDataAction.GetElementDataContext(id); dc.PublishScanResults(); } return(Ok()); } else { return(StatusCode(HttpStatusCode.NotModified)); } } catch (Exception ex) { return(BadRequest(ex.ToString())); } }
/// <summary> /// Highlights the given rectangle on a clone of the given data context's /// inner bitmap and returns it /// /// If the given rectangle is null (might happen if bounding rectangle doesn't exist), /// then the original bitmap is returned /// </summary> /// <param name="ecId">Element context id</param> /// <param name="rect"></param> /// <returns></returns> private static Bitmap GetScreenShotForIssueDescription(Guid ecId, Rectangle?rect) { var dc = GetDataAction.GetElementDataContext(ecId); if (dc.Screenshot != null) { Bitmap newImg = new Bitmap(dc.Screenshot); if (rect.HasValue) { Rectangle valueRect = rect.Value; using (var graphics = Graphics.FromImage(newImg)) using (Pen pen = new Pen(Color.Red, 5)) { // Use element var el = GetDataAction.GetA11yElementInDataContext(ecId, dc.ScreenshotElementId); var outerRect = el.BoundingRectangle; valueRect.X -= outerRect.X; valueRect.Y -= outerRect.Y; graphics.DrawRectangle(pen, valueRect); } } return(newImg); } else { return(null); } }
/// <summary> /// Code to extract all A11yElement objects with failed results. Based on /// the code to display the failed results in Accessibility Insights for Windows. /// </summary> /// <param name="sa">The SelectAction that defines the context</param> private static List <(RuleResult, A11yElement)> ExtractFailedResults(SelectAction sa) { Guid ecId = sa.SelectedElementContextId.Value; ElementDataContext dataContext = GetDataAction.GetElementDataContext(ecId); List <(RuleResult, A11yElement)> list = new List <(RuleResult, A11yElement)>(); foreach (var element in dataContext.Elements.Values) { if (element.ScanResults?.Items == null || element.ScanResults.Items.Count == 0) { continue; } foreach (var item in element.ScanResults.Items) { var failures = from ruleResult in item.Items where ruleResult.Status == ScanStatus.Fail || ruleResult.Status == ScanStatus.ScanNotSupported select(ruleResult, element); list.AddRange(failures); } } return(list); }
public ResultsT Scan <ResultsT>(A11yElement element, ScanActionCallback <ResultsT> scanCallback) { if (element == null) { throw new ArgumentNullException(nameof(element)); } if (scanCallback == null) { throw new ArgumentNullException(nameof(scanCallback)); } using (var dataManager = DataManager.GetDefaultInstance()) using (var sa = SelectAction.GetDefaultInstance()) { sa.SetCandidateElement(element); if (!sa.Select()) { throw new AxeWindowsAutomationException(DisplayStrings.ErrorUnableToSetDataContext); } using (ElementContext ec2 = sa.POIElementContext) { Stopwatch stopwatch = Stopwatch.StartNew(); GetDataAction.GetProcessAndUIFrameworkOfElementContext(ec2.Id); if (!CaptureAction.SetTestModeDataContext(ec2.Id, DataContextMode.Test, TreeViewMode.Control)) { throw new AxeWindowsAutomationException(DisplayStrings.ErrorUnableToSetDataContext); } long scanDurationInMilliseconds = stopwatch.ElapsedMilliseconds; // send telemetry of scan results. var dc = GetDataAction.GetElementDataContext(ec2.Id); dc.PublishScanResults(scanDurationInMilliseconds); if (dc.ElementCounter.UpperBoundExceeded) { throw new AxeWindowsAutomationException(string.Format(CultureInfo.InvariantCulture, DisplayStrings.ErrorTooManyElementsToSetDataContext, dc.ElementCounter.UpperBound)); } return(scanCallback(ec2.Element, ec2.Id)); } // using }// using }
public IHttpActionResult DataContext(Guid id) { try { // check whether Id exist first. if (GetDataAction.ExistElementContext(id)) { return(Ok(GetDataAction.GetElementDataContext(id))); } else { return(NotFound()); } } catch (Exception ex) { return(BadRequest(ex.ToString())); } }
/// <summary> /// set element /// </summary> /// <param name="ecId"></param> public async Task SetElement(Guid ecId) { if (GetDataAction.ExistElementContext(ecId)) { try { HollowHighlightDriver.GetDefaultInstance().HighlighterMode = HighlighterMode.Highlighter; HollowHighlightDriver.GetDefaultInstance().SetElement(ecId, 0); this.ctrlContrast.ActivateProgressRing(); ElementContext ec = null; string warning = string.Empty; string toolTipText = string.Empty; await Task.Run(() => { var updated = CaptureAction.SetTestModeDataContext(ecId, this.DataContextMode, Configuration.TreeViewMode); ec = GetDataAction.GetElementContext(ecId); // send telemetry of scan results. var dc = GetDataAction.GetElementDataContext(ecId); if (dc.ElementCounter.UpperBoundExceeded) { warning = string.Format(CultureInfo.InvariantCulture, Properties.Resources.SetElementCultureInfoFormatMessage, dc.ElementCounter.UpperBound); } }).ConfigureAwait(false); Application.Current.Dispatcher.Invoke(() => { if (ec == null || ec.Element == null) { toolTipText = "No Eelement Selected!"; } else { if (CCAControlTypesFilter.GetDefaultInstance().Contains(ec.Element.ControlTypeId)) { Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => { this.ctrlContrast.SetElement(ec); })).Wait(); toolTipText = string.Format(CultureInfo.InvariantCulture, "Ratio: {0}\nConfidence: {1}", this.ctrlContrast.getRatio(), this.ctrlContrast.getConfidence()); } else { toolTipText = "Unknown Element Type!"; } } MainWin.CurrentView = CCAView.Automatic; HollowHighlightDriver.GetDefaultInstance().HighlighterMode = HighlighterMode.HighlighterTooltip; HollowHighlightDriver.GetDefaultInstance().SetText(toolTipText); // enable element selector MainWin.EnableElementSelector(); }); this.ctrlContrast.DeactivateProgressRing(); } catch (Exception ex) { ex.ReportException(); Application.Current.Dispatcher.Invoke(() => { MainWin.CurrentView = CCAView.Automatic; HollowHighlightDriver.GetDefaultInstance().HighlighterMode = HighlighterMode.HighlighterTooltip; HollowHighlightDriver.GetDefaultInstance().SetText("Unable to detect colors!"); // enable element selector MainWin.EnableElementSelector(); this.ctrlContrast.DeactivateProgressRing(); }); } } }
/// <summary> /// Update Element Info UI(Properties/Patterns/Tests) /// </summary> /// <param name="ecId"></param> public async Task SetElement(Guid ecId) { this.ctrlProgressRing.Activate(); EnableMenuButtons(this.DataContextMode != DataContextMode.Load); bool selectionFailure = false; try { this.ctrlHierarchy.IsEnabled = false; ElementContext ec = null; await Task.Run(() => { CaptureAction.SetTestModeDataContext(ecId, this.DataContextMode, Configuration.TreeViewMode); ec = GetDataAction.GetElementContext(ecId); // send telemetry of scan results. var dc = GetDataAction.GetElementDataContext(ecId); dc.PublishScanResults(); }).ConfigureAwait(false); Application.Current.Dispatcher.Invoke(() => { if (ec != null && ec.DataContext != null) { this.ElementContext = ec; this.ctrlTabs.Clear(); if (!SelectAction.GetDefaultInstance().IsPaused) { this.ctrlHierarchy.CleanUpTreeView(); } this.ctrlHierarchy.SetElement(ec); this.ctrlTabs.SetElement(ec.Element, false, ec.Id); if (ec.DataContext.Screenshot == null && ec.DataContext.Mode != DataContextMode.Load) { Application.Current.MainWindow.WindowStyle = WindowStyle.ToolWindow; Application.Current.MainWindow.Visibility = Visibility.Hidden; HollowHighlightDriver.GetDefaultInstance().IsEnabled = false; this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => { ScreenShotAction.CaptureScreenShot(ecId); Application.Current.MainWindow.Visibility = Visibility.Visible; Application.Current.MainWindow.WindowStyle = WindowStyle.SingleBorderWindow; })).Wait(); } var imageOverlay = ImageOverlayDriver.GetDefaultInstance(); imageOverlay.SetImageElement(ecId); //disable button on highlighter. imageOverlay.SetHighlighterButtonClickHandler(null); if (Configuration.IsHighlighterOn) { imageOverlay.Show(); } } // if it is enforced to select a specific element(like fastpass click case) if (ec.DataContext.FocusedElementUniqueId.HasValue) { this.ctrlHierarchy.SelectElement(ec.DataContext.FocusedElementUniqueId.Value); } if (!ec.DataContext.Elements.TryGetValue(ec.DataContext.FocusedElementUniqueId ?? 0, out A11yElement selectedElement)) { selectedElement = ec.DataContext.Elements[0]; } AutomationProperties.SetName(this, string.Format(CultureInfo.InvariantCulture, Properties.Resources.SetElementInspectTestDetail, selectedElement.Glimpse)); FireAsyncContentLoadedEvent(AsyncContentLoadedState.Completed); // Set focus on hierarchy tree Dispatcher.InvokeAsync(() => this.ctrlHierarchy.SetFocusOnHierarchyTree(), DispatcherPriority.Input).Wait(); ec.DataContext.FocusedElementUniqueId = null; }); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception e) { e.ReportException(); Application.Current.Dispatcher.Invoke(() => { Application.Current.MainWindow.Visibility = Visibility.Visible; EnableMenuButtons(false); this.ElementContext = null; selectionFailure = true; }); } #pragma warning restore CA1031 // Do not catch general exception types finally { Application.Current.Dispatcher.Invoke(() => { this.ctrlProgressRing.Deactivate(); this.ctrlHierarchy.IsEnabled = true; // if focus has gone to the window, we set focus to the hierarchy control. We do this because disabling the hierarchy control // will throw keyboard focus to mainwindow, where it is not very useful. if (Keyboard.FocusedElement is Window) { this.ctrlHierarchy.Focus(); } if (selectionFailure) { MainWin.HandleFailedSelectionReset(); } else { // Set the right view state : if (MainWin.CurrentView is TestView iv && iv == TestView.ElementHowToFix) { MainWin.SetCurrentViewAndUpdateUI(TestView.ElementHowToFix); }
/// <summary> /// Sets element context and updates UI /// </summary> /// <param name="ecId"></param> public async Task SetElement(Guid ecId) { EnableMenuButtons(false); // first disable them to avoid any conflict. bool selectionFailure = false; if (GetDataAction.ExistElementContext(ecId)) { EnableMenuButtons(this.DataContextMode != DataContextMode.Load); this.ctrlAutomatedChecks.Visibility = Visibility.Visible; this.tbSelectElement.Visibility = Visibility.Collapsed; this.tabControl.IsEnabled = true; try { AutomationProperties.SetName(this, "Test"); this.tabControl.IsEnabled = false; this.ctrlProgressRing.Activate(); ElementContext ec = null; string warning = string.Empty; await Task.Run(() => { var updated = CaptureAction.SetTestModeDataContext(ecId, this.DataContextMode, Configuration.TreeViewMode); ec = GetDataAction.GetElementContext(ecId); // send telemetry of scan results. var dc = GetDataAction.GetElementDataContext(ecId); if (dc.ElementCounter.UpperBoundExceeded) { warning = string.Format(CultureInfo.InvariantCulture, Properties.Resources.SetElementCultureInfoFormatMessage, dc.ElementCounter.UpperBound); IsSaveEnabled = false; } dc.PublishScanResults(); }).ConfigureAwait(false); Application.Current.Dispatcher.Invoke(() => { if (ec.Element == null || ec.Element.Properties == null || ec.Element.Properties.Count == 0) { this.ctrlAutomatedChecks.ClearUI(); selectionFailure = true; } else { if (ec.DataContext.Screenshot == null && ec.DataContext.Mode != DataContextMode.Load) { if (ec.Element.BoundingRectangle.IsEmpty == true) { MessageDialog.Show(Properties.Resources.noScreenShotAvailableMessage); } else { Application.Current.MainWindow.WindowStyle = WindowStyle.ToolWindow; Application.Current.MainWindow.Visibility = Visibility.Hidden; HollowHighlightDriver.GetDefaultInstance().IsEnabled = false; this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => { ScreenShotAction.CaptureScreenShot(ecId); Application.Current.MainWindow.WindowStyle = WindowStyle.SingleBorderWindow; // This needs to happen before the call to ctrlAutomatedChecks.SetElement. Otherwise, // ctrlAutomatedChecks's checkboxes become out of sync with the highlighter Application.Current.MainWindow.Visibility = Visibility.Visible; })).Wait(); } } this.tabControl.IsEnabled = true; this.ctrlAutomatedChecks.SetElement(ec); this.ElementContext = ec; if (ec.DataContext.Mode == DataContextMode.Test) { tbiTabStop.Visibility = Visibility.Visible; this.ctrlTabStop.SetElement(ec); } else { tbiTabStop.Visibility = Visibility.Collapsed; } var count = ec.DataContext?.GetRuleResultsViewModelList()?.Count ?? 0; AutomationProperties.SetName(this, Invariant($"{Properties.Resources.detectedFailuresMessagePart1} {this.ElementContext.Element.Glimpse}. {count} {Properties.Resources.detectedFailuresMessagePart2}")); if (!string.IsNullOrEmpty(warning)) { MessageDialog.Show(warning); } ///Set focus on automated checks tab. FireAsyncContentLoadedEvent(AsyncContentLoadedState.Completed); } }); } catch (Exception) { Application.Current.Dispatcher.Invoke(() => { Application.Current.MainWindow.Visibility = Visibility.Visible; this.Clear(); selectionFailure = true; EnableMenuButtons(false); this.ElementContext = null; }); } finally { Application.Current.Dispatcher.Invoke(() => { this.ctrlProgressRing.Deactivate(); }); } } Application.Current.Dispatcher.Invoke(() => { // Set the right view state : if (selectionFailure) { MainWin.HandleFailedSelectionReset(); } else { MainWin.SetCurrentViewAndUpdateUI(TestView.AutomatedTestResults); } // Improves the reliability of the rendering but does not solve across the board issues seen with all WPF apps. this.Dispatcher.BeginInvoke(DispatcherPriority.Send, new Action(() => { Application.Current.MainWindow.InvalidateVisual(); Application.Current.MainWindow.Visibility = Visibility.Visible; this.tbiAutomatedChecks.Focus(); })).Wait(); }); }
/// <summary> /// set element /// </summary> /// <param name="ecId"></param> public async Task SetElement(Guid ecId) { if (GetDataAction.ExistElementContext(ecId)) { try { HollowHighlightDriver.GetDefaultInstance().HighlighterMode = HighlighterMode.Highlighter; HollowHighlightDriver.GetDefaultInstance().SetElement(ecId, 0); this.ctrlContrast.ActivateProgressRing(); ElementContext ec = null; string warning = string.Empty; string toolTipText = string.Empty; await Task.Run(() => { var updated = CaptureAction.SetTestModeDataContext(ecId, this.DataContextMode, Configuration.TreeViewMode); ec = GetDataAction.GetElementContext(ecId); // send telemetry of scan results. var dc = GetDataAction.GetElementDataContext(ecId); if (dc.ElementCounter.UpperBoundExceeded) { warning = string.Format(CultureInfo.InvariantCulture, Properties.Resources.SetElementCultureInfoFormatMessage, dc.ElementCounter.UpperBound); } }).ConfigureAwait(false); Application.Current.Dispatcher.Invoke(() => { if (ec == null || ec.Element == null) { toolTipText = Properties.Resources.ColorContrast_NoElementSelected; } else { if (ControlType.GetInstance().Values.Contains(ec.Element.ControlTypeId)) { Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(() => { this.ctrlContrast.SetElement(ec); })).Wait(); toolTipText = string.Format(CultureInfo.InvariantCulture, Properties.Resources.ColorContrast_RatioAndConfidenceFormat, this.ctrlContrast.Ratio, this.ctrlContrast.Confidence); } else { toolTipText = Properties.Resources.ColorContrast_UnknownElementType; } } MainWin.CurrentView = CCAView.Automatic; HollowHighlightDriver.GetDefaultInstance().HighlighterMode = HighlighterMode.HighlighterTooltip; HollowHighlightDriver.GetDefaultInstance().SetText(toolTipText); // enable element selector MainWin.EnableElementSelector(); }); this.ctrlContrast.DeactivateProgressRing(); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { ex.ReportException(); Application.Current.Dispatcher.Invoke(() => { MainWin.CurrentView = CCAView.Automatic; HollowHighlightDriver.GetDefaultInstance().HighlighterMode = HighlighterMode.HighlighterTooltip; HollowHighlightDriver.GetDefaultInstance().SetText(Properties.Resources.ColorContrast_UnableToDetectColors); // enable element selector MainWin.EnableElementSelector(); this.ctrlContrast.ClearUI(); this.ctrlContrast.DeactivateProgressRing(); }); } #pragma warning restore CA1031 // Do not catch general exception types } }