DotnetLighthouse 1.0.5
dotnet add package DotnetLighthouse --version 1.0.5
NuGet\Install-Package DotnetLighthouse -Version 1.0.5
<PackageReference Include="DotnetLighthouse" Version="1.0.5" />
paket add DotnetLighthouse --version 1.0.5
#r "nuget: DotnetLighthouse, 1.0.5"
// Install DotnetLighthouse as a Cake Addin #addin nuget:?package=DotnetLighthouse&version=1.0.5 // Install DotnetLighthouse as a Cake Tool #tool nuget:?package=DotnetLighthouse&version=1.0.5
DotnetLighthouse
DotnetLighthouse is a .NET library to run Lighthouse audits and process the results.
Prerequisites
- Make sure you have Google Chrome installed
- Make sure you have latest version of Node install (recommend to use LTS version) https://nodejs.org/en/download/prebuilt-installer
- Install Lighthouse cli globally, by running following command by opening up your terminal/command prompt
npm install -g lighthouse
- You already have your dotnet test project using .net8.0 or higher or use following to create a xunit project
dotnet new xunit -o DotnetLighthouse.Tests
Installation
Now navigate to your project directory (e.g. DotnetLighthouse.Tests) You can install the DotnetLighthouse package by running following command in terminal:
dotnet add package DotnetLighthouse
Sample Code
- Create a new test file "LighthouseAudit.cs"
- Add following code into the class
using System;
using Xunit;
using DotnetLighthouse;
public class LighthouseAudit
{
[Fact]
public void ValidateJsonAudit()
{
// Define the URL to be audited
var url = "https://www.google.com";
// Create an instance of LighthouseHelper with the specified URL
var lighthouseHelper = new LighthouseHelper(url);
// Run the Lighthouse audit for the specified URL
lighthouseHelper.RunLighthouseAudit();
// Retrieve the performance score from the Lighthouse audit results
var performanceScore = lighthouseHelper.PerformanceScore;
// Retrieve the accessibility score from the Lighthouse audit results
var accessibilityScore = lighthouseHelper.AccessibilityScore;
// Retrieve the best practices score from the Lighthouse audit results
var bestPracticesScore = lighthouseHelper.BestPracticesScore;
// Retrieve the SEO score from the Lighthouse audit results
var seoScore = lighthouseHelper.SeoScore;
// Retrieve the PWA score from the Lighthouse audit results
var pwaScore = lighthouseHelper.PwaScore;
// Assert that the performance score is greater than 90
Assert.True(performanceScore > 90);
}
}
More Configurations
- By Default DotnetLighthouse generate Lghthouse Audit report with name "lighthouse-report.json" in bin folder. In case you want a different name you can update by overriding property "OutputPath"
- You also have option to provide the custom config JS file as well, use following line of code to do the same
var lighthouseHelper = new LighthouseHelper(url);
// Use this line to enable custom config
lighthouseHelper.ConfigPath = "<YoucustomconfigconfigFile>.js";
// Run the Lighthouse audit for the specified URL
lighthouseHelper.RunLighthouseAudit();
- You can also pass additional allowed CLI aruguments separated by spaces and to set this use code as given below (Refer https://github.com/GoogleChrome/lighthouse?tab=readme-ov-file#cli-options for more details):
var url = "https://www.google.com";
var lighthouseHelper = new LighthouseHelper(url);
lighthouseHelper.Preset = string.Empty;
lighthouseHelper.AdditionalArguments = "--form-factor=desktop --screenEmulation.disabled";
// Run the Lighthouse audit for the specified URL
lighthouseHelper.RunLighthouseAudit();
Dependencies
This project has the following dependencies:
- HtmlAgilityPack
- Newtonsoft.Json
License
This project is licensed under the MIT License. See the LICENSE file for more details.
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
Known Issue
Only JSON reporting is supporting for Score Assertions and WIP for Supporting Score Extraction from HTML file.
Authors
Raj Uppadhyay
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- HtmlAgilityPack (>= 1.11.67)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.