Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indent & Format the page source for html reports #287

Open
3 tasks
lemonlion opened this issue Jun 16, 2023 · 2 comments
Open
3 tasks

Indent & Format the page source for html reports #287

lemonlion opened this issue Jun 16, 2023 · 2 comments
Labels

Comments

@lemonlion
Copy link

lemonlion commented Jun 16, 2023

Description

Currently the Html Reports put all the content on a single line. This is fine when viewing the reports in a browser. However, these reports are serving as specifications documents, and because of this, we are placing them in source control so that the specifications document changes can be tracked. However, due to the fact that everything is on one line, it makes clear Github diffs impossible. What I'd like to see is some way of easily having the HTML reports format their source code body so that specific changes will appear more clearly in diffs.

Progress

  • Feature is implemented,
  • Ensured backward-compatibility,
  • Ensured good debugging experience
@Suremaker
Copy link
Collaborator

The HtmlReports as we see today are designed to report on the test execution. It means each report element (feature/scenario/step) has an associated runtime id, status, execution time, error details, and attachments and in the case of the parameterized steps, they may contain runtime generated data (Guids, dates, fake data).

All of these make the Html report not suitable for being a source of audit logs, especially source-controlled diffs.

It seems like you are looking for the ability to capture/generate the test specs (which are more like the scenario prescriptions, not the execution result) and which at this point in time are not available in LightBDD.

What I would suggest here is to create your own implementation of IReportFormatter and produce own spec file (I'd suggest then using json, xml or just text instead of html) that would be stripped of the runtime data and friendly to perform diffs. PlainTextReportFormatter could potentially be used as a starting point.

The Report Generation Configuration wiki page describes how to wire in a custom formatter.

I will leave this issue open to have further conversation, but I won't have plans to implement this requirement on the Html report.

@lemonlion
Copy link
Author

lemonlion commented Jul 3, 2023

Ok, fair enough. I think HTML source controlled specifications documents are a really good future feature though that people would be interested in, so I'd urge you to consider it. I do want the end document to be in HTML (even if it's via an XML interim thing) because ultimately no one will read an XML document, but HTML documents can be reader friendly.

In the meantime I've done my own implementation which takes a copy of the HTML generated reports, cuts out the instance level information, renames it and gives it a new heading and sends it in to somewhere source controlled. For my current tests it succeeds in removing all instance information, and also formatting the html so it will be clearer in source control.

The downside is it's reliant on the current implementation of the HTML report not changing, so I'll have to maintain it if you change that. Also if I end up with different types of tests that have more instance level information, then I'll have to adjust as I go to cut that out, but I'm confident that wont be too difficult.

I may look at using the IReportFormatter in future, at the moment I'm just doing a post test run step, but this works for me as a starting point. I'll post the code here in case it's helpful to anyone else wants to have their reports in source control and this gives them some inspiration.

internal class ConfiguredLightBddScopeAttribute : LightBddScopeAttribute
{
    protected override void OnConfigure(LightBddConfiguration configuration)
    {
        configuration.ExecutionExtensionsConfiguration()
                .RegisterGlobalTearDown("create specifications", CreateSpecificationDocument);
    }

    private void CreateSpecificationDocument() => SpecificationsCreator.CreateFromReport("Component");
}
using AngleSharp.Html;
using AngleSharp.Html.Dom;
using AngleSharp.Html.Parser;

namespace Foo;

public static class SpecificationsCreator
{
    public const string DefaultFolderPath = @"../../../../../docs/"; // Using forward slashes so that it works in both Windows & Unix
    
    public static void CreateFromReport(string specificationsType,
        string? specificationsFolderPath = DefaultFolderPath,
        string projectName = "Foo Provider")
    {
        var html = File.ReadAllText(@"Reports/FeaturesReport.html");

        html = html.Replace("Feature details", $"{projectName} - {specificationsType} Specifications")
                   .Replace("applyToCheckbox('toggleScenarios', 'ts');",
                                 "applyToCheckbox('toggleScenarios', 'ts', false);");

        var document = new HtmlParser().ParseDocument(html);

        document.RemoveAll(".execution-summary")
                .RemoveAll(".features-summary")
                .RemoveAll(".duration")
                .RemoveAll(".optionsPanel .options:last-of-type");

        using var writer = new StringWriter();
        document.ToHtml(writer, new PrettyMarkupFormatter { Indentation = "\t", NewLine = "\n" });
        var indentedText = writer.ToString();
        File.WriteAllText(@$"{specificationsFolderPath}{specificationsType}Specifications.html", indentedText);
    }

    public static IHtmlDocument RemoveAll(this IHtmlDocument document, string selectors)
    {
        document.QuerySelectorAll(selectors).ToList().ForEach(x => x.Remove());
        return document;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants