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

AUT-6940 helper class migration into OSS repo #36

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,7 @@ Temporary Items
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json


# =============================== Allure reporting ===============================
allure-results/
45 changes: 44 additions & 1 deletion auto-sdk-java-helpers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,53 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
</dependency>
<dependency>
<groupId>us.codecraft</groupId>
<artifactId>xsoup</artifactId>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
</dependency>
<dependency>
<groupId>io.qameta.allure</groupId>
<artifactId>allure-testng</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>io.github.yaml-path</groupId>
<artifactId>yaml-path</artifactId>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-sheets</artifactId>
</dependency>
<dependency>
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,11 @@ public <T extends AnalyticsEntry> List<NetworkEntry> getRequestsWithMultipleFilt
return filtered;
}

/**
* Returns a new instance of {@link AnalyticsInterceptor} associated with this object.
*
* @return A new {@link AnalyticsInterceptor} instance.
*/
public AnalyticsInterceptor getInterceptor() {
return new AnalyticsInterceptor(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.applause.auto.context.IPageObjectExtension;
import com.applause.auto.data.enums.DriverType;
import java.util.Locale;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.remote.RemoteWebDriver;
Expand All @@ -31,10 +30,18 @@
* driver type, which browser, which version.
*/
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
@AllArgsConstructor
public class EnvironmentHelper implements IPageObjectExtension {
private static final Logger logger = LogManager.getLogger(EnvironmentHelper.class);
private IPageObjectContext pageObjectContext;
private final IPageObjectContext pageObjectContext;

/**
* Constructor for EnvironmentHelper.
*
* @param pageObjectContext The {@link IPageObjectContext} to use.
*/
public EnvironmentHelper(final IPageObjectContext pageObjectContext) {
this.pageObjectContext = pageObjectContext;
}

/**
* determine the cast class type of the created driver object and set the DriverType into the
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
*
* Copyright © 2025 Applause App Quality, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.applause.auto.helpers.allure;

import io.qameta.allure.Allure;
import java.io.ByteArrayInputStream;
import lombok.NonNull;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

/** Provides utility methods for attaching information to Allure reports. */
public final class AllureDriverUtils {

private static final Logger LOGGER = LogManager.getLogger(AllureDriverUtils.class);
private static final String SCREENSHOT_ATTACHMENT = "Screenshot attachment";
private static final String IMAGE_PNG = "image/png";
private static final String CURRENT_URL = "Current URL";
private static final String TEXT_PLAIN = "text/plain";
private static final String CURRENT_PAGE_SOURCE = "Current page source";
private static final String LOG_EXTENSION = ".log";

private AllureDriverUtils() {
// Utility class - no public constructor
}

/**
* Attaches a screenshot to the Allure report.
*
* @param driver The WebDriver instance to capture the screenshot from.
*/
public static void attachScreenshot(@NonNull final WebDriver driver) {
LOGGER.info("Taking screenshot on test failure");
try {
var screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
Allure.addAttachment(
SCREENSHOT_ATTACHMENT, IMAGE_PNG, new ByteArrayInputStream(screenshot), "png");
} catch (Exception e) {
LOGGER.error("Error taking screenshot: {}", e.getMessage());
}
}

/**
* Attaches the current URL to the Allure report.
*
* @param driver The WebDriver instance to get the current URL from.
*/
public static void attachCurrentURL(@NonNull final WebDriver driver) {
LOGGER.info("Attaching current URL");
try {
Allure.addAttachment(CURRENT_URL, TEXT_PLAIN, driver.getCurrentUrl(), LOG_EXTENSION);
} catch (Exception e) {
LOGGER.error("Error taking current URL: {}", e.getMessage());
}
}

/**
* Attaches the current page source to the Allure report.
*
* @param driver The WebDriver instance to get the page source from.
*/
public static void attachCurrentPageSourceOnFailure(@NonNull final WebDriver driver) {
LOGGER.info("Attaching page source");
try {
Allure.addAttachment(CURRENT_PAGE_SOURCE, TEXT_PLAIN, driver.getPageSource(), LOG_EXTENSION);
} catch (Exception e) {
LOGGER.error("Error taking current page source: {}", e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
*
* Copyright © 2025 Applause App Quality, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.applause.auto.helpers.allure;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import lombok.NonNull;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/** Utility class for Allure reporting. */
public final class AllureUtils {

private static final Logger logger = LogManager.getLogger(AllureUtils.class);
private static final String CATEGORIES_JSON = "categories.json";

private AllureUtils() {}

/**
* Copies the Allure defects categorization JSON file to the Allure report directory.
*
* @param allureReportPath The path to the Allure report directory.
* @param allureDefectsCategorisationFilePath The path to the Allure defects categorization JSON
* file.
* @throws IOException If an I/O error occurs.
*/
public static void addAllureDefectsCategoriesConfiguration(
@NonNull final String allureReportPath,
@NonNull final String allureDefectsCategorisationFilePath)
throws IOException {

logger.info("Copying defects configs to {} file for Allure", CATEGORIES_JSON);

final Path categoriesAllureFile = Path.of(allureReportPath, CATEGORIES_JSON);

if (!Files.exists(categoriesAllureFile)) {
Files.createFile(categoriesAllureFile);
}

Files.copy(Path.of(allureDefectsCategorisationFilePath), categoriesAllureFile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
*
* Copyright © 2025 Applause App Quality, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.applause.auto.helpers.allure.appenders;

import io.qameta.allure.Allure;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
import org.apache.logging.log4j.core.*;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Node;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.layout.PatternLayout;

/** Appender for logging to Allure steps. */
@Plugin(
name = "AllureLogsToStepAppender",
category = Node.CATEGORY,
elementType = Appender.ELEMENT_TYPE)
public final class AllureLogsToStepAppender extends AbstractAppender {

private static final String COMMA = ",";
private static final Set<String> FILTERED_PACKAGES_TO_APPEND_SET = new ConcurrentSkipListSet<>();

private AllureLogsToStepAppender(
final String name,
final Filter filter,
final Layout<? extends Serializable> layout,
final boolean ignoreExceptions) {
super(name, filter, layout, ignoreExceptions, null);
}

/**
* Creates an AllureLogsToStepAppender.
*
* @param name The name of the appender.
* @param layout The layout to use for the appender.
* @param filter The filter to use for the appender.
* @param filteredPackagesToAppend A comma-separated list of packages to filter.
* @return A new AllureLogsToStepAppender instance, or null if the name is null.
*/
@PluginFactory
public static AllureLogsToStepAppender createAppender(
@PluginAttribute("name") final String name,
@PluginElement("Layout") final Layout<? extends Serializable> layout,
@PluginElement("Filter") final Filter filter,
@PluginAttribute("filteredPackagesToAppend") final String filteredPackagesToAppend) {

if (name == null) {
return null;
}

final var usedLayout = Objects.requireNonNullElse(layout, PatternLayout.createDefaultLayout());

if (filteredPackagesToAppend != null) {
FILTERED_PACKAGES_TO_APPEND_SET.addAll(Arrays.asList(filteredPackagesToAppend.split(COMMA)));
}

return new AllureLogsToStepAppender(name, filter, usedLayout, true);
}

/**
* Appends a log event to Allure step.
*
* @param event The log event to append.
*/
@Override
public void append(final LogEvent event) {
if (isSourcePackageValidForAppender(event)) {
Allure.step(event.getMessage().getFormattedMessage());
}
}

private boolean isSourcePackageValidForAppender(final LogEvent event) {
if (FILTERED_PACKAGES_TO_APPEND_SET.isEmpty()) {
return true;
}

final var sourceClassName = event.getSource().getClassName();

return FILTERED_PACKAGES_TO_APPEND_SET.stream().anyMatch(sourceClassName::contains);
}
}
Loading