Skip to content

Commit

Permalink
Lighthouse Reports Workflow Plugins update
Browse files Browse the repository at this point in the history
One can run a lighthouse analysis only with LighthouseAnalysisRunAttribute, so there is need to validate if in the config file the lighthouse is enabled or not.
Changes to these files are made due to the fact that if one forgets to set 'isEnabled' in LighthouseSettings to 'false', even if the tests are not lighthouse analysis, this plugin will attach the latest reports (if any) to them.
  • Loading branch information
MiriamKyoseva committed Mar 29, 2024
1 parent 0a7be4a commit 06ebf6d
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Bellatrix.Playwright.Services;
using Bellatrix.Playwright.Enums;
using Bellatrix.Playwright;
using System.Reflection;
using Microsoft.TeamFoundation.Common;

namespace Bellatrix.GoogleLighthouse.MSTest;

Expand All @@ -30,8 +33,9 @@ public class MSTestLighthouseReportsWorkflowPlugin : Plugin

protected override void PostTestCleanup(object sender, PluginEventArgs e)
{
var settings = ConfigurationService.GetSection<LighthouseSettings>();
if (settings.IsEnabled && WrappedBrowserCreateService.BrowserConfiguration.ExecutionType == ExecutionType.Regular)
// As lighthouse analysis run is possible only if there is LighthouseAnalysisRunAttribute,
// The only condition that needs to be met is if there is such attribute.
if (HasLighthouseAttribute(e) && WrappedBrowserCreateService.BrowserConfiguration.ExecutionType == ExecutionType.Regular)
{
lock (_lockObject)
{
Expand All @@ -56,4 +60,25 @@ protected override void PostTestCleanup(object sender, PluginEventArgs e)
}
}
}

/// <summary>
/// Checks if the test is a lighthouse analysis or not.
/// </summary>
private bool HasLighthouseAttribute(PluginEventArgs e)
{
// Does it have any attribute of type BrowserAttribute?
bool testHasAnyAttribute = !e.TestMethodMemberInfo.GetCustomAttributes().Where(x => x is BrowserAttribute).IsNullOrEmpty();


if (testHasAnyAttribute)
{
// Checks if this attribute is for lighthouse analysis
return e.TestMethodMemberInfo.GetCustomAttribute<LighthouseAnalysisRunAttribute>() != null;
}
else
{
// Otherwise, checks if the class has this attribute
return e.TestClassType.GetCustomAttribute<LighthouseAnalysisRunAttribute>() != null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
using NUnit.Framework;
using Bellatrix.Playwright.Services;
using Bellatrix.Playwright.Enums;
using Bellatrix.Playwright;
using System.Reflection;
using Microsoft.TeamFoundation.Common;

namespace Bellatrix.GoogleLighthouse.NUnit;

Expand All @@ -32,8 +35,9 @@ public NUnitLighthouseReportsWorkflowPlugin()

protected override void PostTestCleanup(object sender, PluginEventArgs e)
{
var settings = ConfigurationService.GetSection<LighthouseSettings>();
if (settings != null && settings.IsEnabled && WrappedBrowserCreateService.BrowserConfiguration.ExecutionType == ExecutionType.Regular)
// As lighthouse analysis run is possible only if there is LighthouseAnalysisRunAttribute,
// The only condition that needs to be met is if there is such attribute.
if (HasLighthouseAttribute(e) && WrappedBrowserCreateService.BrowserConfiguration.ExecutionType == ExecutionType.Regular)
{
lock (_lockObject)
{
Expand All @@ -58,4 +62,25 @@ protected override void PostTestCleanup(object sender, PluginEventArgs e)
}
}
}

/// <summary>
/// Checks if the test is a lighthouse analysis or not.
/// </summary>
private bool HasLighthouseAttribute(PluginEventArgs e)
{
// Does it have any attribute of type BrowserAttribute?
bool testHasAnyAttribute = !e.TestMethodMemberInfo.GetCustomAttributes().Where(x => x is BrowserAttribute).IsNullOrEmpty();


if (testHasAnyAttribute)
{
// Checks if this attribute is for lighthouse analysis
return e.TestMethodMemberInfo.GetCustomAttribute<LighthouseAnalysisRunAttribute>() != null;
}
else
{
// Otherwise, checks if the class has this attribute
return e.TestClassType.GetCustomAttribute<LighthouseAnalysisRunAttribute>() != null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
// <site>https://bellatrix.solutions/</site>
using System.IO;
using System.Linq;
using System.Reflection;
using Bellatrix.Plugins;
using Bellatrix.Plugins.Screenshots;
using Bellatrix.Plugins.Screenshots.Plugins;
using Bellatrix.Utilities;
using Bellatrix.Web;
using Microsoft.TeamFoundation.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Bellatrix.GoogleLighthouse.MSTest;
Expand All @@ -29,8 +31,9 @@ public class MSTestLighthouseReportsWorkflowPlugin : Plugin

protected override void PostTestCleanup(object sender, PluginEventArgs e)
{
var settings = ConfigurationService.GetSection<LighthouseSettings>();
if (settings.IsEnabled && WrappedWebDriverCreateService.BrowserConfiguration.ExecutionType == Web.Enums.ExecutionType.Regular)
// As lighthouse analysis run is possible only if there is LighthouseAnalysisRunAttribute,
// The only condition that needs to be met is if there is such attribute.
if (HasLighthouseAttribute(e) && WrappedWebDriverCreateService.BrowserConfiguration.ExecutionType == Web.Enums.ExecutionType.Regular)
{
lock (_lockObject)
{
Expand All @@ -56,4 +59,25 @@ protected override void PostTestCleanup(object sender, PluginEventArgs e)
}
}
}

/// <summary>
/// Checks if the test is a lighthouse analysis or not.
/// </summary>
private bool HasLighthouseAttribute(PluginEventArgs e)
{
// Does it have any attribute of type BrowserAttribute?
bool testHasAnyAttribute = !e.TestMethodMemberInfo.GetCustomAttributes().Where(x => x is BrowserAttribute).IsNullOrEmpty();


if (testHasAnyAttribute)
{
// Checks if this attribute is for lighthouse analysis
return e.TestMethodMemberInfo.GetCustomAttribute<LighthouseAnalysisRunAttribute>() != null;
}
else
{
// Otherwise, checks if the class has this attribute
return e.TestClassType.GetCustomAttribute<LighthouseAnalysisRunAttribute>() != null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
// <site>https://bellatrix.solutions/</site>
using System.IO;
using System.Linq;
using System.Reflection;
using Bellatrix.Plugins;
using Bellatrix.Plugins.Screenshots;
using Bellatrix.Plugins.Screenshots.Plugins;
using Bellatrix.Utilities;
using Bellatrix.Web;
using Microsoft.TeamFoundation.Common;
using NUnit.Framework;

namespace Bellatrix.GoogleLighthouse.NUnit;
Expand All @@ -34,8 +36,9 @@ public NUnitLighthouseReportsWorkflowPlugin()

protected override void PostTestCleanup(object sender, PluginEventArgs e)
{
var settings = ConfigurationService.GetSection<LighthouseSettings>();
if (settings != null && settings.IsEnabled && WrappedWebDriverCreateService.BrowserConfiguration.ExecutionType == Web.Enums.ExecutionType.Regular)
// As lighthouse analysis run is possible only if there is LighthouseAnalysisRunAttribute,
// The only condition that needs to be met is if there is such attribute.
if (HasLighthouseAttribute(e) && WrappedWebDriverCreateService.BrowserConfiguration.ExecutionType == Web.Enums.ExecutionType.Regular)
{
lock (_lockObject)
{
Expand All @@ -60,4 +63,25 @@ protected override void PostTestCleanup(object sender, PluginEventArgs e)
}
}
}

/// <summary>
/// Checks if the test is a lighthouse analysis or not.
/// </summary>
private bool HasLighthouseAttribute(PluginEventArgs e)
{
// Does it have any attribute of type BrowserAttribute?
bool testHasAnyAttribute = !e.TestMethodMemberInfo.GetCustomAttributes().Where(x => x is BrowserAttribute).IsNullOrEmpty();


if (testHasAnyAttribute)
{
// Checks if this attribute is for lighthouse analysis
return e.TestMethodMemberInfo.GetCustomAttribute<LighthouseAnalysisRunAttribute>() != null;
}
else
{
// Otherwise, checks if the class has this attribute
return e.TestClassType.GetCustomAttribute<LighthouseAnalysisRunAttribute>() != null;
}
}
}

0 comments on commit 06ebf6d

Please sign in to comment.