private static string GetCountContent(int violationCount, int incompleteCount, int passCount, int inapplicableCount, ReportTypes requestedResults) { StringBuilder countString = new StringBuilder(); if (requestedResults.HasFlag(ReportTypes.Violations)) { countString.AppendLine($" Violation: {violationCount}<br>"); } if (requestedResults.HasFlag(ReportTypes.Incomplete)) { countString.AppendLine($" Incomplete: {incompleteCount}<br>"); } if (requestedResults.HasFlag(ReportTypes.Passes)) { countString.AppendLine($" Pass: {passCount}<br>"); } if (requestedResults.HasFlag(ReportTypes.Inapplicable)) { countString.AppendLine($" Inapplicable: {inapplicableCount}"); } return(countString.ToString()); }
public static void CreateAxeHtmlReport(this ISearchContext context, AxeResult results, string destination, ReportTypes requestedResults) { // Get the unwrapped element if we are using a wrapped element context = context is IWrapsElement ? (context as IWrapsElement).WrappedElement : context; var selectors = new HashSet <string>(); var violationCount = GetCount(results.Violations, ref selectors); var incompleteCount = GetCount(results.Incomplete, ref selectors); var passCount = GetCount(results.Passes, ref selectors); var inapplicableCount = GetCount(results.Inapplicable, ref selectors); var doc = new HtmlDocument(); doc.CreateComment("<!DOCTYPE html>\r\n"); var htmlStructure = HtmlNode.CreateNode("<html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><title>Accessibility Check</title><style></style></head><body><content></content><script></script></body></html>"); doc.DocumentNode.AppendChild(htmlStructure); doc.DocumentNode.SelectSingleNode("//style").InnerHtml = GetCss(context); var contentArea = doc.DocumentNode.SelectSingleNode("//content"); var reportTitle = doc.CreateElement("h1"); reportTitle.InnerHtml = "Accessibility Check"; contentArea.AppendChild(reportTitle); var metaFlex = doc.CreateElement("div"); metaFlex.SetAttributeValue("id", "metadata"); contentArea.AppendChild(metaFlex); var contextGroup = doc.CreateElement("div"); contextGroup.SetAttributeValue("id", "context"); metaFlex.AppendChild(contextGroup); var contextHeader = doc.CreateElement("h3"); contextHeader.InnerHtml = "Context:"; contextGroup.AppendChild(contextHeader); var contextContent = doc.CreateElement("div"); contextContent.SetAttributeValue("class", "emOne"); contextContent.SetAttributeValue("id", "reportContext"); contextContent.InnerHtml = GetContextContent(results); contextGroup.AppendChild(contextContent); var imgGroup = doc.CreateElement("div"); imgGroup.SetAttributeValue("id", "image"); metaFlex.AppendChild(imgGroup); var imageHeader = doc.CreateElement("h3"); imageHeader.InnerHtml = "Image:"; imgGroup.AppendChild(imageHeader); var imageContent = doc.CreateElement("img"); imageContent.SetAttributeValue("class", "thumbnail"); imageContent.SetAttributeValue("id", "screenshotThumbnail"); imageContent.SetAttributeValue("alt", "A Screenshot of the page"); imageContent.SetAttributeValue("width", "33%"); imageContent.SetAttributeValue("height", "auto"); imgGroup.AppendChild(imageContent); var countsGroup = doc.CreateElement("div"); countsGroup.SetAttributeValue("id", "counts"); metaFlex.AppendChild(countsGroup); var countsHeader = doc.CreateElement("h3"); countsHeader.InnerHtml = "Counts:"; countsGroup.AppendChild(countsHeader); var countsContent = doc.CreateElement("div"); countsContent.SetAttributeValue("class", "emOne"); var countsString = GetCountContent(violationCount, incompleteCount, passCount, inapplicableCount, requestedResults); countsContent.InnerHtml = countsString.ToString(); countsGroup.AppendChild(countsContent); var resultsFlex = doc.CreateElement("div"); resultsFlex.SetAttributeValue("id", "results"); contentArea.AppendChild(resultsFlex); if (!string.IsNullOrEmpty(results.Error)) { var errorHeader = doc.CreateElement("h2"); errorHeader.InnerHtml = "SCAN ERRORS:"; contentArea.AppendChild(errorHeader); var errorContent = doc.CreateElement("div"); errorContent.SetAttributeValue("id", "ErrorMessage"); errorContent.InnerHtml = HttpUtility.HtmlEncode(results.Error); contentArea.AppendChild(errorContent); } if (violationCount > 0 && requestedResults.HasFlag(ReportTypes.Violations)) { GetReadableAxeResults(results.Violations, ResultType.Violations.ToString(), doc, resultsFlex); } if (incompleteCount > 0 && requestedResults.HasFlag(ReportTypes.Incomplete)) { GetReadableAxeResults(results.Incomplete, ResultType.Incomplete.ToString(), doc, resultsFlex); } if (passCount > 0 && requestedResults.HasFlag(ReportTypes.Passes)) { GetReadableAxeResults(results.Passes, ResultType.Passes.ToString(), doc, resultsFlex); } if (inapplicableCount > 0 && requestedResults.HasFlag(ReportTypes.Inapplicable)) { GetReadableAxeResults(results.Inapplicable, ResultType.Inapplicable.ToString(), doc, resultsFlex); } var modal = doc.CreateElement("div"); modal.SetAttributeValue("id", "modal"); contentArea.AppendChild(modal); var modalClose = doc.CreateElement("div"); modalClose.InnerHtml = "X"; modalClose.SetAttributeValue("id", "modalclose"); modal.AppendChild(modalClose); var modalImage = doc.CreateElement("img"); modalImage.SetAttributeValue("id", "modalimage"); modal.AppendChild(modalImage); doc.DocumentNode.SelectSingleNode("//script").InnerHtml = js; doc.Save(destination, Encoding.UTF8); }