Skip to content

Commit a99960f

Browse files
authored
Merge pull request #40 from AutomateThePlanet/bellatrix-visual-regression-tracker-integration
bellatrix-visual-regression-tracker-integration
2 parents 5b23070 + 477e1c0 commit a99960f

File tree

11 files changed

+334
-20
lines changed

11 files changed

+334
-20
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>solutions.bellatrix</groupId>
8+
<artifactId>bellatrix</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>bellatrix.addons.visual-regression-tracker</artifactId>
13+
14+
<repositories>
15+
<repository>
16+
<id>jitpack.io</id>
17+
<url>https://jitpack.io</url>
18+
</repository>
19+
</repositories>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>solutions.bellatrix</groupId>
24+
<artifactId>bellatrix.core</artifactId>
25+
<version>1.0</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>solutions.bellatrix</groupId>
29+
<artifactId>bellatrix.plugins.screenshots</artifactId>
30+
<version>1.0</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>com.github.Visual-Regression-Tracker</groupId>
34+
<artifactId>sdk-java</artifactId>
35+
<version>5.2.1</version>
36+
</dependency>
37+
</dependencies>
38+
39+
<properties>
40+
<maven.compiler.source>19</maven.compiler.source>
41+
<maven.compiler.target>19</maven.compiler.target>
42+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
43+
</properties>
44+
45+
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2024 Automate The Planet Ltd.
3+
* Author: Miriam Kyoseva
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package solutions.bellatrix.plugins.vrt;
15+
16+
import io.visual_regression_tracker.sdk_java.VisualRegressionTrackerConfig;
17+
import lombok.SneakyThrows;
18+
import lombok.experimental.UtilityClass;
19+
import plugins.screenshots.ScreenshotPlugin;
20+
import solutions.bellatrix.core.configuration.ConfigurationService;
21+
import solutions.bellatrix.core.utilities.SingletonFactory;
22+
23+
import java.util.Objects;
24+
25+
@UtilityClass
26+
public class VisualRegressionTracker {
27+
private static final VisualRegressionTrackerSettings settings = ConfigurationService.get(VisualRegressionTrackerSettings.class);
28+
private static final VisualRegressionTrackerConfig config = new VisualRegressionTrackerConfig(
29+
settings.getApiUrl(),
30+
settings.getApiKey(),
31+
settings.getProject(),
32+
settings.getBranch(),
33+
settings.getCiBuildId(),
34+
settings.isEnableSoftAssert(),
35+
settings.getHttpTimeout()
36+
);
37+
private static io.visual_regression_tracker.sdk_java.VisualRegressionTracker tracker;
38+
39+
static {
40+
tracker = new io.visual_regression_tracker.sdk_java.VisualRegressionTracker(config);
41+
}
42+
43+
@SneakyThrows
44+
public static void takeSnapshot() {
45+
var caller = Thread.currentThread().getStackTrace()[1]; // gets the caller method of screenshot()
46+
var name = caller.getMethodName();
47+
48+
tracker.track(name, Objects.requireNonNull(SingletonFactory.getInstance(ScreenshotPlugin.class)).takeScreenshot(name));
49+
}
50+
51+
@SneakyThrows
52+
public static void takeSnapshot(String name) {
53+
tracker.track(name, Objects.requireNonNull(SingletonFactory.getInstance(ScreenshotPlugin.class)).takeScreenshot(name));
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2024 Automate The Planet Ltd.
3+
* Author: Miriam Kyoseva
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package solutions.bellatrix.plugins.vrt;
15+
16+
import lombok.Getter;
17+
import lombok.NoArgsConstructor;
18+
import lombok.Setter;
19+
20+
@Getter @Setter @NoArgsConstructor
21+
public class VisualRegressionTrackerSettings {
22+
private String apiUrl;
23+
private String project;
24+
private String apiKey;
25+
private String branch;
26+
private boolean enableSoftAssert;
27+
private String ciBuildId;
28+
private int httpTimeout;
29+
}

bellatrix.android/src/main/java/solutions/bellatrix/android/infrastructure/MobileScreenshotPlugin.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
import org.openqa.selenium.OutputType;
1919
import org.openqa.selenium.TakesScreenshot;
2020
import plugins.screenshots.ScreenshotPlugin;
21+
import plugins.screenshots.ScreenshotPluginEventArgs;
2122
import solutions.bellatrix.android.configuration.AndroidSettings;
2223
import solutions.bellatrix.core.configuration.ConfigurationService;
2324
import solutions.bellatrix.core.utilities.PathNormalizer;
2425

2526
import java.io.File;
27+
import java.io.FileWriter;
28+
import java.io.IOException;
2629
import java.nio.file.Paths;
2730
import java.util.UUID;
2831

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

3437
@Override
3538
@SneakyThrows
36-
protected void takeScreenshot(String screenshotSaveDir, String filename) {
37-
File screenshot = ((TakesScreenshot)DriverService.getWrappedAndroidDriver()).getScreenshotAs(OutputType.FILE);
38-
var destFile = new File(Paths.get(screenshotSaveDir, filename) + ".png");
39-
FileUtils.copyFile(screenshot, destFile);
39+
public String takeScreenshot(String name) {
40+
var screenshotSaveDir = getOutputFolder();
41+
var filename = getUniqueFileName(name);
42+
43+
var screenshot = ((TakesScreenshot)DriverService.getWrappedAndroidDriver()).getScreenshotAs(OutputType.BASE64);
44+
45+
var path = Paths.get(screenshotSaveDir, filename) + ".png";
46+
47+
var file = new File(path);
48+
49+
try (FileWriter writer = new FileWriter(file)) {
50+
writer.write(screenshot);
51+
} catch (IOException e) {
52+
e.printStackTrace();
53+
}
54+
55+
SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, screenshot));
56+
return screenshot;
57+
}
58+
59+
@Override
60+
@SneakyThrows
61+
public String takeScreenshot(String screenshotSaveDir, String filename) {
62+
var screenshot = ((TakesScreenshot)DriverService.getWrappedAndroidDriver()).getScreenshotAs(OutputType.BASE64);
63+
64+
var path = Paths.get(screenshotSaveDir, filename) + ".png";
65+
66+
var file = new File(path);
67+
68+
try (FileWriter writer = new FileWriter(file)) {
69+
writer.write(screenshot);
70+
} catch (IOException e) {
71+
e.printStackTrace();
72+
}
73+
74+
SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, screenshot));
75+
return screenshot;
4076
}
4177

4278
@Override

bellatrix.desktop/src/main/java/solutions/bellatrix/desktop/infrastructure/DesktopScreenshotPlugin.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
import org.openqa.selenium.OutputType;
1919
import org.openqa.selenium.TakesScreenshot;
2020
import plugins.screenshots.ScreenshotPlugin;
21+
import plugins.screenshots.ScreenshotPluginEventArgs;
2122
import solutions.bellatrix.core.configuration.ConfigurationService;
2223
import solutions.bellatrix.desktop.configuration.DesktopSettings;
2324

2425
import java.io.File;
26+
import java.io.FileWriter;
27+
import java.io.IOException;
28+
import java.nio.file.Path;
2529
import java.nio.file.Paths;
2630
import java.util.UUID;
2731

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

3337
@Override
3438
@SneakyThrows
35-
protected void takeScreenshot(String screenshotSaveDir, String filename) {
36-
File screenshot = ((TakesScreenshot)DriverService.getWrappedDriver()).getScreenshotAs(OutputType.FILE);
37-
var destFile = new File(Paths.get(screenshotSaveDir, filename) + ".png");
38-
FileUtils.copyFile(screenshot, destFile);
39+
public String takeScreenshot(String name) {
40+
var screenshotSaveDir = getOutputFolder();
41+
var filename = getUniqueFileName(name);
42+
43+
var screenshot = ((TakesScreenshot)DriverService.getWrappedDriver()).getScreenshotAs(OutputType.BASE64);
44+
Path path = Paths.get(screenshotSaveDir, filename);
45+
46+
var file = new File(path + ".png");
47+
48+
try (FileWriter writer = new FileWriter(file)) {
49+
writer.write(screenshot);
50+
} catch (IOException e) {
51+
e.printStackTrace();
52+
}
53+
54+
SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, screenshot));
55+
return screenshot;
56+
}
57+
58+
@Override
59+
@SneakyThrows
60+
public String takeScreenshot(String screenshotSaveDir, String filename) {
61+
var screenshot = ((TakesScreenshot)DriverService.getWrappedDriver()).getScreenshotAs(OutputType.BASE64);
62+
Path path = Paths.get(screenshotSaveDir, filename);
63+
64+
var file = new File(path + ".png");
65+
66+
try (FileWriter writer = new FileWriter(file)) {
67+
writer.write(screenshot);
68+
} catch (IOException e) {
69+
e.printStackTrace();
70+
}
71+
72+
SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, screenshot));
73+
return screenshot;
3974
}
4075

4176
@Override

bellatrix.ios/src/main/java/solutions/bellatrix/ios/infrastructure/MobileScreenshotPlugin.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
import org.openqa.selenium.OutputType;
1919
import org.openqa.selenium.TakesScreenshot;
2020
import plugins.screenshots.ScreenshotPlugin;
21+
import plugins.screenshots.ScreenshotPluginEventArgs;
2122
import solutions.bellatrix.core.configuration.ConfigurationService;
2223
import solutions.bellatrix.core.utilities.PathNormalizer;
2324
import solutions.bellatrix.ios.configuration.IOSSettings;
2425

2526
import java.io.File;
27+
import java.io.FileWriter;
28+
import java.io.IOException;
2629
import java.nio.file.Paths;
2730
import java.util.UUID;
2831

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

3437
@Override
3538
@SneakyThrows
36-
protected void takeScreenshot(String screenshotSaveDir, String filename) {
37-
File screenshot = ((TakesScreenshot)DriverService.getWrappedIOSDriver()).getScreenshotAs(OutputType.FILE);
38-
var destFile = new File(Paths.get(screenshotSaveDir, filename) + ".png");
39-
FileUtils.copyFile(screenshot, destFile);
39+
public String takeScreenshot(String name) {
40+
var screenshotSaveDir = getOutputFolder();
41+
var filename = getUniqueFileName(name);
42+
43+
var screenshot = ((TakesScreenshot)DriverService.getWrappedIOSDriver()).getScreenshotAs(OutputType.BASE64);
44+
var path = Paths.get(screenshotSaveDir, filename) + ".png";
45+
var file = new File(path);
46+
47+
try (FileWriter writer = new FileWriter(file)) {
48+
writer.write(screenshot);
49+
} catch (IOException e) {
50+
e.printStackTrace();
51+
}
52+
53+
SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, screenshot));
54+
return screenshot;
55+
}
56+
57+
@Override
58+
@SneakyThrows
59+
public String takeScreenshot(String screenshotSaveDir, String filename) {
60+
var screenshot = ((TakesScreenshot)DriverService.getWrappedIOSDriver()).getScreenshotAs(OutputType.BASE64);
61+
var path = Paths.get(screenshotSaveDir, filename) + ".png";
62+
var file = new File(path);
63+
64+
try (FileWriter writer = new FileWriter(file)) {
65+
writer.write(screenshot);
66+
} catch (IOException e) {
67+
e.printStackTrace();
68+
}
69+
70+
SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, screenshot));
71+
return screenshot;
4072
}
4173

4274
@Override

bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/infrastructure/WebScreenshotPlugin.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
import com.microsoft.playwright.Page;
1717
import com.microsoft.playwright.options.ScreenshotType;
1818
import plugins.screenshots.ScreenshotPlugin;
19+
import plugins.screenshots.ScreenshotPluginEventArgs;
1920
import solutions.bellatrix.core.utilities.PathNormalizer;
2021
import solutions.bellatrix.playwright.utilities.Settings;
2122

2223
import java.io.File;
2324
import java.nio.file.Paths;
25+
import java.util.Base64;
2426
import java.util.UUID;
2527

2628
public class WebScreenshotPlugin extends ScreenshotPlugin {
@@ -29,12 +31,36 @@ public WebScreenshotPlugin() {
2931
}
3032

3133
@Override
32-
protected void takeScreenshot(String screenshotSaveDir, String filename) {
34+
public String takeScreenshot(String name) {
35+
var screenshotSaveDir = getOutputFolder();
36+
var filename = getUniqueFileName(name);
37+
38+
var path = Paths.get(screenshotSaveDir, filename);
39+
var screenshot = PlaywrightService.wrappedBrowser().getCurrentPage()
40+
.screenshot(new Page.ScreenshotOptions()
41+
.setPath(path)
42+
.setType(ScreenshotType.PNG)
43+
);
44+
45+
var image = Base64.getEncoder().encodeToString(screenshot);
46+
47+
SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, image));
48+
return image;
49+
}
50+
51+
@Override
52+
public String takeScreenshot(String screenshotSaveDir, String filename) {
53+
var path = Paths.get(screenshotSaveDir, filename);
3354
var screenshot = PlaywrightService.wrappedBrowser().getCurrentPage()
3455
.screenshot(new Page.ScreenshotOptions()
35-
.setPath(Paths.get(screenshotSaveDir, filename))
56+
.setPath(path)
3657
.setType(ScreenshotType.PNG)
3758
);
59+
60+
var image = Base64.getEncoder().encodeToString(screenshot);
61+
62+
SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(path.toString(), filename, image));
63+
return image;
3864
}
3965

4066
@Override

bellatrix.plugins.screenshots/src/main/java/plugins/screenshots/ScreenshotPlugin.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public ScreenshotPlugin(boolean isEnabled) {
2929
this.isEnabled = isEnabled;
3030
}
3131

32-
protected abstract void takeScreenshot(String screenshotSaveDir, String filename);
32+
public abstract String takeScreenshot(String fileName);
33+
public abstract String takeScreenshot(String screenshotSaveDir, String filename);
3334
protected abstract String getOutputFolder();
3435
protected abstract String getUniqueFileName(String testName);
3536

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

4243
var screenshotSaveDir = getOutputFolder();
4344
var screenshotFileName = getUniqueFileName(memberInfo.getName());
44-
takeScreenshot(screenshotSaveDir, screenshotFileName);
45-
SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(Paths.get(screenshotSaveDir, screenshotFileName).toString(), screenshotFileName));
45+
var image = takeScreenshot(screenshotSaveDir, screenshotFileName);
46+
SCREENSHOT_GENERATED.broadcast(new ScreenshotPluginEventArgs(Paths.get(screenshotSaveDir, screenshotFileName).toString(), screenshotFileName, image));
4647
}
4748
}

bellatrix.plugins.screenshots/src/main/java/plugins/screenshots/ScreenshotPluginEventArgs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class ScreenshotPluginEventArgs {
2020
private final String screenshotPath;
2121
private final String fileName;
2222

23-
public ScreenshotPluginEventArgs(String screenshotPath, String fileName) {
23+
public ScreenshotPluginEventArgs(String screenshotPath, String fileName, String image) {
2424
this.screenshotPath = screenshotPath;
2525
this.fileName = fileName;
2626
}

0 commit comments

Comments
 (0)