Skip to content

Commit d7e0e9d

Browse files
authored
Code Quality: Improved Axe assertion message (#17283)
1 parent f36a19d commit d7e0e9d

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

tests/Files.InteractionTests/Helper/AxeHelper.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
using Axe.Windows.Automation;
55
using Axe.Windows.Core.Enums;
6+
using System;
67
using System.Diagnostics;
78
using System.Linq;
9+
using System.Text;
810

911
namespace Files.InteractionTests.Helper
1012
{
@@ -25,11 +27,37 @@ internal static void InitializeAxe()
2527
public static void AssertNoAccessibilityErrors()
2628
{
2729
var testResult = AccessibilityScanner.Scan(null).WindowScanOutputs.SelectMany(output => output.Errors).Where(error => error.Rule.ID != RuleId.BoundingRectangleNotNull);
28-
if (testResult.Count() != 0)
30+
if (testResult.Any())
2931
{
30-
var mappedResult = testResult.Select(result => "Element " + result.Element.Properties["ControlType"] + " violated rule '" + result.Rule.Description + "'.");
31-
Assert.Fail("Failed with the following accessibility errors \r\n" + string.Join("\r\n", mappedResult));
32+
StringBuilder sb = new();
33+
sb.AppendLine();
34+
sb.AppendLine("============================================================");
35+
sb.AppendJoin(Environment.NewLine, testResult.Select(BuildAssertMessage));
36+
sb.AppendLine();
37+
sb.AppendLine("============================================================");
38+
39+
Assert.Fail(sb.ToString());
40+
}
41+
}
42+
43+
private static string BuildAssertMessage(ScanResult result)
44+
{
45+
// e.g., "Element Button(50000) violated rule 'The Name property of a focusable element must not be null.'."
46+
return $"Element {result.Element.Properties["ControlType"]} at ({ParseBoundingRectangle(result.Element.Properties["BoundingRectangle"])}) violated rule \"{result.Rule.Description}\".";
47+
}
48+
49+
private static string ParseBoundingRectangle(string boundingRectangle)
50+
{
51+
// e.g., "[l=1617,t=120,r=1663,b=152]" to "x=1617,y=120,w=46,h=32"
52+
var output = new ushort[4];
53+
var parts = boundingRectangle.Trim('[').Trim(']').Split(',');
54+
for (int index = 0; index < 4; index++)
55+
{
56+
if (ushort.TryParse(parts[index][2..], out var res))
57+
output[index] = res;
3258
}
59+
60+
return $"x={output[0]},y={output[1]},w={output[2] - output[0]},h={output[3] - output[1]}";
3361
}
3462
}
3563
}

0 commit comments

Comments
 (0)