Accessibility Testing in Playwright C#

In the previous article, we discussed accessibility testing in Selenium C#. In this article, I would like to share the implementation of accessibility testing in Playwright using the C# programming language.

The following are the prerequisites:
Step-by-step approach:

Step 1: Create a .cs file AccessibilityHandler.cs to keep the logic to track violations. Implement the following method:

/// <summary>
/// Method to track the violations using AxeResult support
/// </summary>
/// Author: Sanoj Swaminathan
/// Since: 31-03-2024
/// <param name="page"></param>
/// <param name="pageName"></param>
public async Task TrackViolations(IPage page,string pageName)
{
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add(pageName);
worksheet.Cell(1, 1).Value = "ID";
worksheet.Cell(1, 2).Value = "Description";
worksheet.Cell(1, 3).Value = "Impact";
worksheet.Cell(1, 4).Value = "Help";
worksheet.Cell(1, 5).Value = "Help URL";
worksheet.Cell(1, 6).Value = "Tags";

AxeResult axeResults = await page.RunAxe();
int row = 2;
foreach (var violation in axeResults.Violations)
{
worksheet.Cell(row, 1).Value = violation.Id;
worksheet.Cell(row, 2).Value = violation.Description;
worksheet.Cell(row, 3).Value = violation.Impact;
worksheet.Cell(row, 4).Value = violation.Help;
worksheet.Cell(row, 5).Value = violation.HelpUrl;
string tagsString = string.Join(", ", violation.Tags);
worksheet.Cell(row, 6).Value = tagsString;
row++;
}
StringBuilder TimeAndDate = new StringBuilder(DateTime.Now.ToString());
TimeAndDate.Replace("/", "_");
TimeAndDate.Replace(":", "_");
string filePath = projectDirectory + "/Test_Execution_Reports/Accessibility_Testing/Summary/"+ pageName + "_" + TimeAndDate + ".xlsx";
workbook.SaveAs(filePath);
Console.WriteLine($"Accessibility report is generated at: {filePath}");
}
}
  • TrackViolations this is a public method that can be used in your test classes to track violations. In this method, we used the RunAxe() method of the AxeResult class to identify the violations, after that create an Excel file with a timestamp, and write the violation details (violation id, description, impact, help, help URL, and WCAG tags) into the file.

Step 2: Create a .cs file, SampleAccessibilityTest.cs and create a test case let’s say TC001_Verify_Accessibility_Audit. In the test case, we need to create a page instance and use the page instance to load the browser there you need to perform the Accessibility Testing and after that, call the method TrackViolations. The actual implementation is below:

[Test(Description ="Verify any violation in the website"), Order(1)]
public async Task TC001_Verify_Accessibility_Audit()
{
IPlaywright playwright = await Playwright.CreateAsync();
IBrowser _browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
{
Headless = false,
});
IPage _page = await _browser.NewPageAsync();
await _page.GotoAsync("https://journeyofquality.com/");
await new AccessibilityHandler().TrackViolations(page, "HomePage");
}

Step 3: Execute the above test case and see the reports in the format of Excel. The Excel report will be generated in Test_Execution_Reports/Accessibility_Testing/Summary/ of the project structure. Based on the above TrackViolations method, a sheet called HomePage will be created in the Excel report that tracks all the violations. If you are using TrackViolations method for different pages of your web application, it will create new sheets to track the violations of your different pages.

   Accessibility Testing is one of the important types of testing that adds value to your business and delivers user-friendly applications. I hope you enjoyed reading about the implementation of accessibility testing in Playwright C#. Please try to utilize this opportunity in your test automation world. 

make it perfect!

One thought on “Accessibility Testing in Playwright C#

Add yours

Leave a comment

Create a website or blog at WordPress.com

Up ↑