Skip to content

Commit

Permalink
reduce amount of Neo version logs, add Neo version to Allure #139
Browse files Browse the repository at this point in the history
  • Loading branch information
oomelianchuk committed Jun 21, 2024
1 parent 39dd73a commit 416a965
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
16 changes: 14 additions & 2 deletions src/main/java/com/xceptance/neodymium/junit4/NeodymiumRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
import org.slf4j.LoggerFactory;

import com.codeborne.selenide.logevents.SelenideLogger;
import com.google.common.collect.ImmutableMap;
import com.xceptance.neodymium.common.WorkInProgress;
import com.xceptance.neodymium.common.browser.Browser;
import com.xceptance.neodymium.junit4.order.DefaultStatementRunOrder;
import com.xceptance.neodymium.util.AllureAddons;
import com.xceptance.neodymium.util.Neodymium;

import io.qameta.allure.selenide.AllureSelenide;
Expand Down Expand Up @@ -79,12 +81,22 @@ public class NeodymiumRunner extends BlockJUnit4ClassRunner

public static final String LISTENER_NAME = "allure-selenide-java";

private static boolean neoVersionLogged = false;

public NeodymiumRunner(Class<?> clazz) throws InitializationError
{
super(clazz);
SelenideLogger.addListener(LISTENER_NAME, new AllureSelenide());
LOGGER.info("This test uses Neodymium Library (version: " + Neodymium.getNeodymiumVersion()
+ "), MIT License, more details on https://github.com/Xceptance/neodymium-library");

if (!neoVersionLogged && Neodymium.configuration().logNeoVersion())
{
LOGGER.info("This test uses Neodymium Library (version: " + Neodymium.getNeodymiumVersion()
+ "), MIT License, more details on https://github.com/Xceptance/neodymium-library");
neoVersionLogged = true;
AllureAddons.addEnvironmentInformation(ImmutableMap.<String, String> builder()
.put("Testing Framework", "Neodymium " + Neodymium.getNeodymiumVersion())
.build());
}
}

public enum DescriptionMode
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/xceptance/neodymium/junit5/NeodymiumRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.slf4j.LoggerFactory;

import com.codeborne.selenide.logevents.SelenideLogger;
import com.google.common.collect.ImmutableMap;
import com.xceptance.neodymium.util.AllureAddons;
import com.xceptance.neodymium.util.Neodymium;

import io.qameta.allure.selenide.AllureSelenide;
Expand All @@ -23,11 +25,20 @@ public class NeodymiumRunner implements TestTemplateInvocationContextProvider

private NeodymiumData neoData;

private static boolean neoVersionLogged = false;

public NeodymiumRunner()
{
SelenideLogger.addListener(LISTENER_NAME, new AllureSelenide());
LOGGER.info("This test uses Neodymium Library (version: " + Neodymium.getNeodymiumVersion()
+ "), MIT License, more details on https://github.com/Xceptance/neodymium-library");
if (!neoVersionLogged && Neodymium.configuration().logNeoVersion())
{
LOGGER.info("This test uses Neodymium Library (version: " + Neodymium.getNeodymiumVersion()
+ "), MIT License, more details on https://github.com/Xceptance/neodymium-library");
neoVersionLogged = true;
AllureAddons.addEnvironmentInformation(ImmutableMap.<String, String> builder()
.put("Testing Framework", "Neodymium " + Neodymium.getNeodymiumVersion())
.build());
}
}

public enum DescriptionMode
Expand All @@ -47,7 +58,7 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex
{
// clear the context before next child run
Neodymium.clearThreadContext();

Class<?> testClass = context.getRequiredTestClass();
Method templateMethod = context.getRequiredTestMethod();
if (neoData == null)
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/xceptance/neodymium/util/AllureAddons.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
package com.xceptance.neodymium.util;

import java.io.File;
import java.util.UUID;
import java.util.function.Supplier;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.google.common.collect.ImmutableMap;

import io.qameta.allure.Attachment;
import io.qameta.allure.Step;
Expand All @@ -16,6 +32,8 @@
*/
public class AllureAddons
{
private static final Logger LOGGER = LoggerFactory.getLogger(AllureAddons.class);

/**
* Define a step without return value. This can be used to transport data (information) from test into the report.
*
Expand Down Expand Up @@ -92,4 +110,42 @@ public static byte[] attachPNG(final String filename)
{
return ((TakesScreenshot) Neodymium.getDriver()).getScreenshotAs(OutputType.BYTES);
}

public static void addEnvironmentInformation(ImmutableMap<String, String> environmentValuesSet)
{
try
{
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element environment = doc.createElement("environment");
doc.appendChild(environment);
environmentValuesSet.forEach((k, v) -> {
Element parameter = doc.createElement("parameter");
Element key = doc.createElement("key");
Element value = doc.createElement("value");
key.appendChild(doc.createTextNode(k));
value.appendChild(doc.createTextNode(v));
parameter.appendChild(key);
parameter.appendChild(value);
environment.appendChild(parameter);
});

// Write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
File allureResultsDir = new File(System.getProperty("allure.results.directory", System.getProperty("user.dir"))
+ "/target/allure-results");
if (!allureResultsDir.exists())
allureResultsDir.mkdirs();
StreamResult result = new StreamResult(new File(System.getProperty("user.dir")
+ "/target/allure-results/environment.xml"));
transformer.transform(source, result);
}
catch (ParserConfigurationException | TransformerException e)
{
LOGGER.warn("Failed to add information about environment to Allure report");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import org.aeonbits.owner.Config.LoadPolicy;
import org.aeonbits.owner.Config.LoadType;
import org.aeonbits.owner.Config.Sources;
import org.aeonbits.owner.Mutable;

import com.xceptance.neodymium.junit4.NeodymiumRunner.DescriptionMode;

import org.aeonbits.owner.Mutable;

@LoadPolicy(LoadType.MERGE)
@Sources(
{
Expand Down Expand Up @@ -264,4 +263,8 @@ public interface NeodymiumConfiguration extends Mutable
@Key("neodymium.workInProgress")
@DefaultValue("false")
public boolean workInProgress();

@Key("neodymium.logNeoVersion")
@DefaultValue("true")
public boolean logNeoVersion();
}

0 comments on commit 416a965

Please sign in to comment.