Skip to content

Commit

Permalink
Optimise screenshot of accessibility test (from png to webp format)
Browse files Browse the repository at this point in the history
  • Loading branch information
huaxing-yuan committed Sep 10, 2024
1 parent f96aef3 commit 4faf047
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/AxaFrance.AxeExtended.HtmlReport/OverallReportBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ public OverallReportBuilder Build()
/// </summary>
/// <param name="fileName">the filename of export report, default value "index.html"</param>
/// <returns>the complete path of report</returns>
/// <exception cref="NotImplementedException"></exception>
public string Export(string fileName = null)
{
if (!hasBuilt) Build();
string path = Options.OutputFolder ?? Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

//clean the folder
if (Directory.Exists(path))

// if the outpur format is Zip. we need to create a sub-folder and zip the content of that folder.
if(Options.OutputFormat == OutputFormat.Zip)
{
Directory.Delete(path, true);
path = Path.Combine(path, Guid.NewGuid().ToString());
}
Directory.CreateDirectory(path);
int sequence = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/AxaFrance.AxeExtended.HtmlReport/PageReportBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private string GenerateRuleSection(AxeResultEnhancedItem[] items, string path)
if (node.Screenshot != null)
{
Guid id = Guid.NewGuid();
filename = id.ToString() + ".png";
filename = id.ToString() + ".webp";
File.WriteAllBytes(Path.Combine(path, filename), node.Screenshot);
}

Expand Down
34 changes: 28 additions & 6 deletions src/AxaFrance.AxeExtended.Selenium/AxeSeleniumHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Xml.Linq;

Expand Down Expand Up @@ -112,11 +113,11 @@ private static byte[] ScreenShot(WebDriver driver, AxeResultNode node, PageRepor
{
element = driver.FindElement(By.XPath(xPath));
}
}catch(Exception ex)
}
catch (Exception ex)
{
//sometimes the cssSelector provided by axe can not be used by selenium.
//in this case can't make screenshot on the element.

Console.WriteLine("[A11y] Unable to get element from cssSelector or xPath");
}
}
Expand All @@ -125,7 +126,7 @@ private static byte[] ScreenShot(WebDriver driver, AxeResultNode node, PageRepor
{
try
{
var screenshot = options.UseAdvancedScreenshot ? AdvancedScreenshot(driver, we, options) : we.GetScreenshot().AsByteArray;
var screenshot = options.UseAdvancedScreenshot ? AdvancedScreenshot(driver, we, options) : ToWebpByteArray(we.GetScreenshot());
driver.SwitchTo().DefaultContent(); //goes back to default context (leaving iframes)
return screenshot;
}
Expand All @@ -140,16 +141,37 @@ private static byte[] ScreenShot(WebDriver driver, AxeResultNode node, PageRepor
{
driver.SwitchTo().DefaultContent();
Console.WriteLine("[A11y] The element can not be converted to type WebElement for screenshot.");
return Array.Empty<byte>();
return Array.Empty<byte>();
}


}

/// <summary>
/// Convert selenium screenshot in png format to webp format.
/// </summary>
/// <param name="screenshot">Selenium screenshot object.</param>
/// <returns>Byte array of an image in webp format</returns>
private static byte[] ToWebpByteArray(Screenshot screenshot)
{
//covert screenshot in png format to webp
using (SKBitmap bitmap = SKBitmap.Decode(screenshot.AsByteArray))
{
using (SKImage img = SKImage.FromBitmap(bitmap))
{
using (SKData data = img.Encode(SKEncodedImageFormat.Webp, 80))
{
return data.ToArray();
}
}
}

}

private static byte[] AdvancedScreenshot(WebDriver driver, WebElement element, PageReportOptions options)
{
BringToView(element, driver);
var imageViewPort = driver.GetScreenshot();
var imageViewPort = driver.GetScreenshot();
var locatable = (ILocatable)element;

var location = locatable.Coordinates.LocationInViewport; //location and size are for 100% dpi
Expand Down Expand Up @@ -189,7 +211,7 @@ private static byte[] MarkOnImage(Screenshot imageViewPort, Point location, Size
Style = SKPaintStyle.Stroke,
StrokeWidth = options.HighlightThickness
});
using (var data = bitmap.Encode(SKEncodedImageFormat.Png, 100))
using (var data = bitmap.Encode(SKEncodedImageFormat.Webp, 80))
{
return data.ToArray();
}
Expand Down
2 changes: 2 additions & 0 deletions src/WebEngine.Test/UnitTests/Accessibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public void AuditWithRGAAAndExportHtml()
.WithRgaaExtension()
.WithSelenium(driver).Build().Export();
Debug.WriteLine($"Report generated in {sw.Elapsed.TotalSeconds} seconds");
Debug.WriteLine($"Report {filename}");
Assert.IsTrue(File.Exists(filename));
}

Expand Down Expand Up @@ -167,6 +168,7 @@ public void UserJourneyTest()

string report = builder.Build().Export();
Assert.IsTrue(File.Exists(report));
Console.WriteLine(report);
}
}

Expand Down

0 comments on commit 4faf047

Please sign in to comment.