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

Support lighthouse reports #310

Merged
merged 19 commits into from
Dec 3, 2024
Merged

Conversation

kqmpetenz
Copy link

added the possibility to create lighthouse reports

@kqmpetenz kqmpetenz linked an issue Nov 18, 2024 that may be closed by this pull request
7 tasks
Copy link
Contributor

@wurzelkuchen wurzelkuchen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work, now for the polishing ;)

config/neodymium.properties Show resolved Hide resolved
config/neodymium.properties Show resolved Hide resolved
validateAudits(jsonFile);

// add report html to allure
Allure.addAttachment(reportName, "text/html", FileUtils.openInputStream(new File("target/" + reportName + ".report.html")), "html");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if any assertion is thrown, we won't add the report as an attachment. we need to first add it as an attachment, and afterwards, validate it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

String assertAuditsString = Neodymium.configuration().lighthouseAssertAudits();
List<String> errorAudits = new ArrayList<>();

if (!assertAuditsString.isEmpty())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This value is null if not set, so calling isEmpty() on a null object will crash. Use StringUtils.isNotBlank(assertAuditString) for this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* The name of the Lighthouse report attachment in the Allure report
* @throws Exception
*/
public static void createLightHouseReport(WebDriver driver, String reportName) throws Exception
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make the interface as simple as possible. There is no need to have the webdriver given as a parameter. Just use Neodymium.getWebdriver() inside this method.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

else if (System.getProperty("os.name").toLowerCase().contains("linux") || System.getProperty("os.name").toLowerCase().contains("mac"))
{
Neodymium.configuration().setProperty("neodymium.lighthouse.binaryPath", "echo {\"categories\": {\"performance\": {\"score\": 0.5}, \"accessibility\": {\"score\": 0.5}, \"best-practices\": {\"score\": 0.5}, \"seo\": {\"score\": 0.5}}} > target/lighthouseUtilsReport.report.json");
builder = new ProcessBuilder("fabricatedHtml > target/lighthouseUtilsReport.report.html");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this needs to be changed to new ProcessBuilder("sh", "-c", "echo fabricatedHtml > target/lighthouseUtilsReport.report.html");

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
else if (System.getProperty("os.name").toLowerCase().contains("linux") || System.getProperty("os.name").toLowerCase().contains("mac"))
{
builder = new ProcessBuilder(Neodymium.configuration().lighthouseBinaryPath(), "--chrome-flags=\"--ignore-certificate-errors\"", URL, "--port=" + Neodymium.getRemoteDebuggingPort(), "--preset=desktop", "--output=json", "--output=html", "--output-path=target/" + reportName + ".json");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for linux please change this to call the shell by using new ProcessBuilder("sh", "-c", Neodymium.config....

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
else if (System.getProperty("os.name").toLowerCase().contains("linux") || System.getProperty("os.name").toLowerCase().contains("mac"))
{
new ProcessBuilder(Neodymium.configuration().lighthouseBinaryPath(), "--version").start();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for linux please change this to call the shell by using new ProcessBuilder("sh", "-c", Neodymium.config....

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Author

@kqmpetenz kqmpetenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only the documentation of the audits property and the link to the documentation in the neo config (when finished)

config/neodymium.properties Show resolved Hide resolved
config/neodymium.properties Show resolved Hide resolved
config/neodymium.properties Show resolved Hide resolved
* The name of the Lighthouse report attachment in the Allure report
* @throws Exception
*/
public static void createLightHouseReport(WebDriver driver, String reportName) throws Exception
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
else if (System.getProperty("os.name").toLowerCase().contains("linux") || System.getProperty("os.name").toLowerCase().contains("mac"))
{
builder = new ProcessBuilder(Neodymium.configuration().lighthouseBinaryPath(), "--chrome-flags=\"--ignore-certificate-errors\"", URL, "--port=" + Neodymium.getRemoteDebuggingPort(), "--preset=desktop", "--output=json", "--output=html", "--output-path=target/" + reportName + ".json");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

String assertAuditsString = Neodymium.configuration().lighthouseAssertAudits();
List<String> errorAudits = new ArrayList<>();

if (!assertAuditsString.isEmpty())
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
if (errorAudits.size() > 0)
{
throw new Exception("the following audits " + errorAudits + " contain errors that need to be fixed, please look into the corresponding html for further information");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

else if (System.getProperty("os.name").toLowerCase().contains("linux") || System.getProperty("os.name").toLowerCase().contains("mac"))
{
Neodymium.configuration().setProperty("neodymium.lighthouse.binaryPath", "echo {\"categories\": {\"performance\": {\"score\": 0.5}, \"accessibility\": {\"score\": 0.5}, \"best-practices\": {\"score\": 0.5}, \"seo\": {\"score\": 0.5}}} > target/lighthouseUtilsReport.report.json");
builder = new ProcessBuilder("fabricatedHtml > target/lighthouseUtilsReport.report.html");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

# The actual value for the seo score varies alot, so consider using a lower threshold to avoid a lot of false alerts
neodymium.lighthouse.assert.thresholdScore.seo = 0.5

# To be able to validate Lighthouse report audits, wo use internal json id's from the report itself
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

}
else if (System.getProperty("os.name").toLowerCase().contains("linux") || System.getProperty("os.name").toLowerCase().contains("mac"))
{
new ProcessBuilder("sh", "-c", Neodymium.configuration().lighthouseBinaryPath(), "--version").start();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several things here:

  1. We need to check for the error code as we did for windows
  2. the "sh" "c" parameter need to be removed to make it work (on my system) and the mac I tested on, recheck on another linux mashine is WIP.
  3. since this seems to be the main cause of pain and other users will have other setups, we should improve our error handling here. We should add the proccess output to the error message in cases something goes wrong.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we have for cases (these two and the two where we actually create the report) where we need to start a process with a given set of parameters, check for the error code and collect the logs, we should create a seperate method for this, and just pass all the parameters we would give to the ProcessBuilder

Inside this method we could also add the PATH variable of the process to the error message in a failure case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we have a seperate method We can do some double safety and try each process with "sh" "-c" and if it fails we can do it without. that way we have an additional safety net for strange setups and will make sure our unit test mokup works as intendet ;)

@wurzelkuchen wurzelkuchen merged commit ff72b1c into develop Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integrate Google Lighthouse into Neodyimum
2 participants