Skip to content
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
45 changes: 45 additions & 0 deletions bellatrix.addons.visual-regression-tracker/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>solutions.bellatrix</groupId>
<artifactId>bellatrix</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>bellatrix.addons.visual-regression-tracker</artifactId>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>solutions.bellatrix</groupId>
<artifactId>bellatrix.core</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>solutions.bellatrix</groupId>
<artifactId>bellatrix.plugins.screenshots</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.github.Visual-Regression-Tracker</groupId>
<artifactId>sdk-java</artifactId>
<version>5.2.1</version>
</dependency>
</dependencies>

<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2024 Automate The Planet Ltd.
* Author: Miriam Kyoseva
* 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 solutions.bellatrix.plugins.vrt;

import io.visual_regression_tracker.sdk_java.VisualRegressionTrackerConfig;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import plugins.screenshots.ScreenshotPlugin;
import solutions.bellatrix.core.configuration.ConfigurationService;
import solutions.bellatrix.core.utilities.SingletonFactory;

import java.util.Objects;

@UtilityClass
public class VisualRegressionTracker {
private static final VisualRegressionTrackerSettings settings = ConfigurationService.get(VisualRegressionTrackerSettings.class);
private static final VisualRegressionTrackerConfig config = new VisualRegressionTrackerConfig(
settings.getApiUrl(),
settings.getApiKey(),
settings.getProject(),
settings.getBranch(),
settings.getCiBuildId(),
settings.isEnableSoftAssert(),
settings.getHttpTimeout()
);
private static io.visual_regression_tracker.sdk_java.VisualRegressionTracker tracker;

static {
tracker = new io.visual_regression_tracker.sdk_java.VisualRegressionTracker(config);
}

@SneakyThrows
public static void takeSnapshot() {
var caller = Thread.currentThread().getStackTrace()[1]; // gets the caller method of screenshot()
var name = caller.getMethodName();

tracker.track(name, Objects.requireNonNull(SingletonFactory.getInstance(ScreenshotPlugin.class)).takeScreenshot(name));
}

@SneakyThrows
public static void takeSnapshot(String name) {
tracker.track(name, Objects.requireNonNull(SingletonFactory.getInstance(ScreenshotPlugin.class)).takeScreenshot(name));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2024 Automate The Planet Ltd.
* Author: Miriam Kyoseva
* 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 solutions.bellatrix.plugins.vrt;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter @Setter @NoArgsConstructor
public class VisualRegressionTrackerSettings {
private String apiUrl;
private String project;
private String apiKey;
private String branch;
private boolean enableSoftAssert;
private String ciBuildId;
private int httpTimeout;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import plugins.screenshots.ScreenshotPlugin;
import plugins.screenshots.ScreenshotPluginEventArgs;
import solutions.bellatrix.android.configuration.AndroidSettings;
import solutions.bellatrix.core.configuration.ConfigurationService;
import solutions.bellatrix.core.utilities.PathNormalizer;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.UUID;

Expand All @@ -33,10 +36,43 @@ public MobileScreenshotPlugin() {

@Override
@SneakyThrows
protected void takeScreenshot(String screenshotSaveDir, String filename) {
File screenshot = ((TakesScreenshot)DriverService.getWrappedAndroidDriver()).getScreenshotAs(OutputType.FILE);
var destFile = new File(Paths.get(screenshotSaveDir, filename) + ".png");
FileUtils.copyFile(screenshot, destFile);
public String takeScreenshot(String name) {
var screenshotSaveDir = getOutputFolder();
var filename = getUniqueFileName(name);

var screenshot = ((TakesScreenshot)DriverService.getWrappedAndroidDriver()).getScreenshotAs(OutputType.BASE64);

var path = Paths.get(screenshotSaveDir, filename) + ".png";

var file = new File(path);

try (FileWriter writer = new FileWriter(file)) {
writer.write(screenshot);
} catch (IOException e) {
e.printStackTrace();
}

SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, screenshot));
return screenshot;
}

@Override
@SneakyThrows
public String takeScreenshot(String screenshotSaveDir, String filename) {
var screenshot = ((TakesScreenshot)DriverService.getWrappedAndroidDriver()).getScreenshotAs(OutputType.BASE64);

var path = Paths.get(screenshotSaveDir, filename) + ".png";

var file = new File(path);

try (FileWriter writer = new FileWriter(file)) {
writer.write(screenshot);
} catch (IOException e) {
e.printStackTrace();
}

SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, screenshot));
return screenshot;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import plugins.screenshots.ScreenshotPlugin;
import plugins.screenshots.ScreenshotPluginEventArgs;
import solutions.bellatrix.core.configuration.ConfigurationService;
import solutions.bellatrix.desktop.configuration.DesktopSettings;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

Expand All @@ -32,10 +36,41 @@ public DesktopScreenshotPlugin() {

@Override
@SneakyThrows
protected void takeScreenshot(String screenshotSaveDir, String filename) {
File screenshot = ((TakesScreenshot)DriverService.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
var destFile = new File(Paths.get(screenshotSaveDir, filename) + ".png");
FileUtils.copyFile(screenshot, destFile);
public String takeScreenshot(String name) {
var screenshotSaveDir = getOutputFolder();
var filename = getUniqueFileName(name);

var screenshot = ((TakesScreenshot)DriverService.getWrappedDriver()).getScreenshotAs(OutputType.BASE64);
Path path = Paths.get(screenshotSaveDir, filename);

var file = new File(path + ".png");

try (FileWriter writer = new FileWriter(file)) {
writer.write(screenshot);
} catch (IOException e) {
e.printStackTrace();
}

SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, screenshot));
return screenshot;
}

@Override
@SneakyThrows
public String takeScreenshot(String screenshotSaveDir, String filename) {
var screenshot = ((TakesScreenshot)DriverService.getWrappedDriver()).getScreenshotAs(OutputType.BASE64);
Path path = Paths.get(screenshotSaveDir, filename);

var file = new File(path + ".png");

try (FileWriter writer = new FileWriter(file)) {
writer.write(screenshot);
} catch (IOException e) {
e.printStackTrace();
}

SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, screenshot));
return screenshot;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import plugins.screenshots.ScreenshotPlugin;
import plugins.screenshots.ScreenshotPluginEventArgs;
import solutions.bellatrix.core.configuration.ConfigurationService;
import solutions.bellatrix.core.utilities.PathNormalizer;
import solutions.bellatrix.ios.configuration.IOSSettings;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.UUID;

Expand All @@ -33,10 +36,39 @@ public MobileScreenshotPlugin() {

@Override
@SneakyThrows
protected void takeScreenshot(String screenshotSaveDir, String filename) {
File screenshot = ((TakesScreenshot)DriverService.getWrappedIOSDriver()).getScreenshotAs(OutputType.FILE);
var destFile = new File(Paths.get(screenshotSaveDir, filename) + ".png");
FileUtils.copyFile(screenshot, destFile);
public String takeScreenshot(String name) {
var screenshotSaveDir = getOutputFolder();
var filename = getUniqueFileName(name);

var screenshot = ((TakesScreenshot)DriverService.getWrappedIOSDriver()).getScreenshotAs(OutputType.BASE64);
var path = Paths.get(screenshotSaveDir, filename) + ".png";
var file = new File(path);

try (FileWriter writer = new FileWriter(file)) {
writer.write(screenshot);
} catch (IOException e) {
e.printStackTrace();
}

SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, screenshot));
return screenshot;
}

@Override
@SneakyThrows
public String takeScreenshot(String screenshotSaveDir, String filename) {
var screenshot = ((TakesScreenshot)DriverService.getWrappedIOSDriver()).getScreenshotAs(OutputType.BASE64);
var path = Paths.get(screenshotSaveDir, filename) + ".png";
var file = new File(path);

try (FileWriter writer = new FileWriter(file)) {
writer.write(screenshot);
} catch (IOException e) {
e.printStackTrace();
}

SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, screenshot));
return screenshot;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import com.microsoft.playwright.Page;
import com.microsoft.playwright.options.ScreenshotType;
import plugins.screenshots.ScreenshotPlugin;
import plugins.screenshots.ScreenshotPluginEventArgs;
import solutions.bellatrix.core.utilities.PathNormalizer;
import solutions.bellatrix.playwright.utilities.Settings;

import java.io.File;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.UUID;

public class WebScreenshotPlugin extends ScreenshotPlugin {
Expand All @@ -29,12 +31,36 @@ public WebScreenshotPlugin() {
}

@Override
protected void takeScreenshot(String screenshotSaveDir, String filename) {
public String takeScreenshot(String name) {
var screenshotSaveDir = getOutputFolder();
var filename = getUniqueFileName(name);

var path = Paths.get(screenshotSaveDir, filename);
var screenshot = PlaywrightService.wrappedBrowser().getCurrentPage()
.screenshot(new Page.ScreenshotOptions()
.setPath(path)
.setType(ScreenshotType.PNG)
);

var image = Base64.getEncoder().encodeToString(screenshot);

SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, image));
return image;
}

@Override
public String takeScreenshot(String screenshotSaveDir, String filename) {
var path = Paths.get(screenshotSaveDir, filename);
var screenshot = PlaywrightService.wrappedBrowser().getCurrentPage()
.screenshot(new Page.ScreenshotOptions()
.setPath(Paths.get(screenshotSaveDir, filename))
.setPath(path)
.setType(ScreenshotType.PNG)
);

var image = Base64.getEncoder().encodeToString(screenshot);

SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, image));
return image;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public ScreenshotPlugin(boolean isEnabled) {
this.isEnabled = isEnabled;
}

protected abstract void takeScreenshot(String screenshotSaveDir, String filename);
public abstract String takeScreenshot(String fileName);
public abstract String takeScreenshot(String screenshotSaveDir, String filename);
protected abstract String getOutputFolder();
protected abstract String getUniqueFileName(String testName);

Expand All @@ -41,7 +42,7 @@ public void postAfterTest(TestResult testResult, Method memberInfo, Throwable fa

var screenshotSaveDir = getOutputFolder();
var screenshotFileName = getUniqueFileName(memberInfo.getName());
takeScreenshot(screenshotSaveDir, screenshotFileName);
SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(Paths.get(screenshotSaveDir, screenshotFileName).toString(), screenshotFileName));
var image = takeScreenshot(screenshotSaveDir, screenshotFileName);
SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(Paths.get(screenshotSaveDir, screenshotFileName).toString(), screenshotFileName, image));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ScreenshotPluginEventArgs {
private final String screenshotPath;
private final String fileName;

public ScreenshotPluginEventArgs(String screenshotPath, String fileName) {
public ScreenshotPluginEventArgs(String screenshotPath, String fileName, String image) {
this.screenshotPath = screenshotPath;
this.fileName = fileName;
}
Expand Down
Loading