Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions tests/Files.InteractionTests/Helper/AxeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

using Axe.Windows.Automation;
using Axe.Windows.Core.Enums;
using System;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Files.InteractionTests.Helper
{
Expand All @@ -25,11 +27,37 @@ internal static void InitializeAxe()
public static void AssertNoAccessibilityErrors()
{
var testResult = AccessibilityScanner.Scan(null).WindowScanOutputs.SelectMany(output => output.Errors).Where(error => error.Rule.ID != RuleId.BoundingRectangleNotNull);
if (testResult.Count() != 0)
if (testResult.Any())
{
var mappedResult = testResult.Select(result => "Element " + result.Element.Properties["ControlType"] + " violated rule '" + result.Rule.Description + "'.");
Assert.Fail("Failed with the following accessibility errors \r\n" + string.Join("\r\n", mappedResult));
StringBuilder sb = new();
sb.AppendLine();
sb.AppendLine("============================================================");
sb.AppendJoin(Environment.NewLine, testResult.Select(BuildAssertMessage));
sb.AppendLine();
sb.AppendLine("============================================================");

Assert.Fail(sb.ToString());
}
}

private static string BuildAssertMessage(ScanResult result)
{
// e.g., "Element Button(50000) violated rule 'The Name property of a focusable element must not be null.'."
return $"Element {result.Element.Properties["ControlType"]} at ({ParseBoundingRectangle(result.Element.Properties["BoundingRectangle"])}) violated rule \"{result.Rule.Description}\".";
}

private static string ParseBoundingRectangle(string boundingRectangle)
{
// e.g., "[l=1617,t=120,r=1663,b=152]" to "x=1617,y=120,w=46,h=32"
var output = new ushort[4];
var parts = boundingRectangle.Trim('[').Trim(']').Split(',');
for (int index = 0; index < 4; index++)
{
if (ushort.TryParse(parts[index][2..], out var res))
output[index] = res;
}

return $"x={output[0]},y={output[1]},w={output[2] - output[0]},h={output[3] - output[1]}";
}
}
}
Loading