diff --git a/bellatrix.android/src/main/java/solutions/bellatrix/android/infrastructure/junit/AndroidTest.java b/bellatrix.android/src/main/java/solutions/bellatrix/android/infrastructure/junit/AndroidTest.java index 92e78359..bff09573 100644 --- a/bellatrix.android/src/main/java/solutions/bellatrix/android/infrastructure/junit/AndroidTest.java +++ b/bellatrix.android/src/main/java/solutions/bellatrix/android/infrastructure/junit/AndroidTest.java @@ -14,9 +14,9 @@ package solutions.bellatrix.android.infrastructure.junit; import org.junit.jupiter.api.extension.ExtendWith; +import plugins.screenshots.ScreenshotPlugin; import solutions.bellatrix.android.components.listeners.BddLogging; import solutions.bellatrix.android.infrastructure.AppLifecyclePlugin; -import solutions.bellatrix.android.infrastructure.DownloadDemoAppsPlugin; import solutions.bellatrix.android.infrastructure.MobileScreenshotPlugin; import solutions.bellatrix.android.infrastructure.MobileVideoPlugin; import solutions.bellatrix.android.services.App; @@ -33,7 +33,7 @@ public App app() { @Override protected void configure() { addPlugin(AppLifecyclePlugin.class); - addPlugin(MobileScreenshotPlugin.class); + addPluginAs(ScreenshotPlugin.class, MobileScreenshotPlugin.class); addPlugin(MobileVideoPlugin.class); //addPlugin(DownloadDemoAppsPlugin.class); addListener(BddLogging.class); diff --git a/bellatrix.android/src/main/java/solutions/bellatrix/android/infrastructure/testng/AndroidTest.java b/bellatrix.android/src/main/java/solutions/bellatrix/android/infrastructure/testng/AndroidTest.java index 1f49e1db..d2188da8 100644 --- a/bellatrix.android/src/main/java/solutions/bellatrix/android/infrastructure/testng/AndroidTest.java +++ b/bellatrix.android/src/main/java/solutions/bellatrix/android/infrastructure/testng/AndroidTest.java @@ -13,6 +13,7 @@ package solutions.bellatrix.android.infrastructure.testng; +import plugins.screenshots.ScreenshotPlugin; import solutions.bellatrix.android.components.listeners.BddLogging; import solutions.bellatrix.android.infrastructure.AppLifecyclePlugin; import solutions.bellatrix.android.infrastructure.DownloadDemoAppsPlugin; @@ -30,7 +31,7 @@ public App app() { @Override protected void configure() { addPlugin(AppLifecyclePlugin.class); - addPlugin(MobileScreenshotPlugin.class); + addPluginAs(ScreenshotPlugin.class, MobileScreenshotPlugin.class); addPlugin(MobileVideoPlugin.class); addPlugin(DownloadDemoAppsPlugin.class); addListener(BddLogging.class); diff --git a/bellatrix.core/src/main/java/solutions/bellatrix/core/plugins/UsesPlugins.java b/bellatrix.core/src/main/java/solutions/bellatrix/core/plugins/UsesPlugins.java index a1d46a24..1a19de78 100644 --- a/bellatrix.core/src/main/java/solutions/bellatrix/core/plugins/UsesPlugins.java +++ b/bellatrix.core/src/main/java/solutions/bellatrix/core/plugins/UsesPlugins.java @@ -9,6 +9,12 @@ public void addPlugin(Class plugin, Object... args) { PluginExecutionEngine.addPlugin(SingletonFactory.getInstance(plugin, args)); } + public void addPluginAs(Class key, Class plugin, Object... args) { + var instance = SingletonFactory.getInstance(plugin, args); + SingletonFactory.register(key, instance); + PluginExecutionEngine.addPlugin(instance); + } + public void addListener(Class listener, Object... args) { Objects.requireNonNull(SingletonFactory.getInstance(listener, args)).addListener(); } diff --git a/bellatrix.desktop/src/main/java/solutions/bellatrix/desktop/infrastructure/junit/DesktopTest.java b/bellatrix.desktop/src/main/java/solutions/bellatrix/desktop/infrastructure/junit/DesktopTest.java index 62e4579d..40f0566d 100644 --- a/bellatrix.desktop/src/main/java/solutions/bellatrix/desktop/infrastructure/junit/DesktopTest.java +++ b/bellatrix.desktop/src/main/java/solutions/bellatrix/desktop/infrastructure/junit/DesktopTest.java @@ -13,6 +13,7 @@ package solutions.bellatrix.desktop.infrastructure.junit; +import plugins.screenshots.ScreenshotPlugin; import solutions.bellatrix.core.plugins.junit.BaseTest; import solutions.bellatrix.desktop.components.listeners.BddLogging; import solutions.bellatrix.desktop.infrastructure.AppLifecyclePlugin; @@ -30,7 +31,7 @@ public App app() { @Override protected void configure() { addPlugin(AppLifecyclePlugin.class); - addPlugin(DesktopScreenshotPlugin.class); + addPluginAs(ScreenshotPlugin.class, DesktopScreenshotPlugin.class); addPlugin(DesktopVideoPlugin.class); addPlugin(DownloadDemoAppsPlugin.class); addListener(BddLogging.class); diff --git a/bellatrix.desktop/src/main/java/solutions/bellatrix/desktop/infrastructure/testng/DesktopTest.java b/bellatrix.desktop/src/main/java/solutions/bellatrix/desktop/infrastructure/testng/DesktopTest.java index 3f271fb0..9822fd80 100644 --- a/bellatrix.desktop/src/main/java/solutions/bellatrix/desktop/infrastructure/testng/DesktopTest.java +++ b/bellatrix.desktop/src/main/java/solutions/bellatrix/desktop/infrastructure/testng/DesktopTest.java @@ -13,6 +13,7 @@ package solutions.bellatrix.desktop.infrastructure.testng; +import plugins.screenshots.ScreenshotPlugin; import solutions.bellatrix.core.plugins.testng.BaseTest; import solutions.bellatrix.desktop.components.listeners.BddLogging; import solutions.bellatrix.desktop.infrastructure.AppLifecyclePlugin; @@ -30,7 +31,7 @@ public App app() { @Override protected void configure() { addPlugin(AppLifecyclePlugin.class); - addPlugin(DesktopScreenshotPlugin.class); + addPluginAs(ScreenshotPlugin.class, DesktopScreenshotPlugin.class); addPlugin(DesktopVideoPlugin.class); addPlugin(DownloadDemoAppsPlugin.class); addListener(BddLogging.class); diff --git a/bellatrix.ios/src/main/java/solutions/bellatrix/ios/infrastructure/junit/IOSTest.java b/bellatrix.ios/src/main/java/solutions/bellatrix/ios/infrastructure/junit/IOSTest.java index 49d979da..dc72f4c3 100644 --- a/bellatrix.ios/src/main/java/solutions/bellatrix/ios/infrastructure/junit/IOSTest.java +++ b/bellatrix.ios/src/main/java/solutions/bellatrix/ios/infrastructure/junit/IOSTest.java @@ -13,6 +13,7 @@ package solutions.bellatrix.ios.infrastructure.junit; +import plugins.screenshots.ScreenshotPlugin; import solutions.bellatrix.core.plugins.junit.BaseTest; import solutions.bellatrix.ios.components.listeners.BddLogging; import solutions.bellatrix.ios.infrastructure.AppLifecyclePlugin; @@ -29,7 +30,7 @@ public App app() { @Override protected void configure() { addPlugin(AppLifecyclePlugin.class); - addPlugin(MobileScreenshotPlugin.class); + addPluginAs(ScreenshotPlugin.class, MobileScreenshotPlugin.class); addPlugin(MobileVideoPlugin.class); addPlugin(DownloadDemoAppsPlugin.class); addListener(BddLogging.class); diff --git a/bellatrix.ios/src/main/java/solutions/bellatrix/ios/infrastructure/testng/IOSTest.java b/bellatrix.ios/src/main/java/solutions/bellatrix/ios/infrastructure/testng/IOSTest.java index b96590df..96ea2e75 100644 --- a/bellatrix.ios/src/main/java/solutions/bellatrix/ios/infrastructure/testng/IOSTest.java +++ b/bellatrix.ios/src/main/java/solutions/bellatrix/ios/infrastructure/testng/IOSTest.java @@ -13,6 +13,7 @@ package solutions.bellatrix.ios.infrastructure.testng; +import plugins.screenshots.ScreenshotPlugin; import solutions.bellatrix.core.plugins.testng.BaseTest; import solutions.bellatrix.ios.components.listeners.BddLogging; import solutions.bellatrix.ios.infrastructure.AppLifecyclePlugin; @@ -29,7 +30,7 @@ public App app() { @Override protected void configure() { addPlugin(AppLifecyclePlugin.class); - addPlugin(MobileScreenshotPlugin.class); + addPluginAs(ScreenshotPlugin.class, MobileScreenshotPlugin.class); addPlugin(MobileVideoPlugin.class); addPlugin(DownloadDemoAppsPlugin.class); addListener(BddLogging.class); diff --git a/bellatrix.playwright/pom.xml b/bellatrix.playwright/pom.xml index 1cceb608..ef684b0d 100644 --- a/bellatrix.playwright/pom.xml +++ b/bellatrix.playwright/pom.xml @@ -21,7 +21,7 @@ com.microsoft.playwright playwright - 1.44.0 + 1.49.0 org.junit.jupiter diff --git a/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/infrastructure/junit/WebTest.java b/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/infrastructure/junit/WebTest.java index 486d5396..78bae81e 100644 --- a/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/infrastructure/junit/WebTest.java +++ b/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/infrastructure/junit/WebTest.java @@ -14,6 +14,7 @@ package solutions.bellatrix.playwright.infrastructure.junit; import org.junit.jupiter.api.extension.ExtendWith; +import plugins.screenshots.ScreenshotPlugin; import solutions.bellatrix.core.plugins.junit.BaseTest; import solutions.bellatrix.core.plugins.junit.TestResultWatcher; import solutions.bellatrix.core.utilities.SingletonFactory; @@ -35,7 +36,7 @@ public App app() { @Override protected void configure() { addPlugin(BrowserLifecyclePlugin.class); - addPlugin(WebScreenshotPlugin.class); + addPluginAs(ScreenshotPlugin.class, WebScreenshotPlugin.class); addPlugin(WebVideoPlugin.class); addPlugin(LogLifecyclePlugin.class); addListener(BddConsoleLogging.class); diff --git a/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/infrastructure/testng/WebTest.java b/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/infrastructure/testng/WebTest.java index d4571fb1..e9f49a2e 100644 --- a/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/infrastructure/testng/WebTest.java +++ b/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/infrastructure/testng/WebTest.java @@ -13,6 +13,7 @@ package solutions.bellatrix.playwright.infrastructure.testng; +import plugins.screenshots.ScreenshotPlugin; import solutions.bellatrix.core.plugins.testng.BaseTest; import solutions.bellatrix.core.utilities.SingletonFactory; import solutions.bellatrix.playwright.components.listeners.BddConsoleLogging; @@ -31,7 +32,7 @@ public App app() { @Override protected void configure() { addPlugin(BrowserLifecyclePlugin.class); - addPlugin(WebScreenshotPlugin.class); + addPluginAs(ScreenshotPlugin.class, WebScreenshotPlugin.class); addPlugin(WebVideoPlugin.class); addPlugin(LogLifecyclePlugin.class); addListener(BddConsoleLogging.class); diff --git a/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/pages/WebPage.java b/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/pages/WebPage.java index ecc8c8e0..963ec93b 100644 --- a/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/pages/WebPage.java +++ b/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/pages/WebPage.java @@ -14,7 +14,6 @@ package solutions.bellatrix.playwright.pages; import solutions.bellatrix.core.infrastructure.PageObjectModel; -import solutions.bellatrix.core.utilities.InstanceFactory; import solutions.bellatrix.core.utilities.SingletonFactory; import solutions.bellatrix.playwright.services.App; import solutions.bellatrix.playwright.services.BrowserService; diff --git a/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/pages/WebSection.java b/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/pages/WebSection.java index 3f6f301f..b958696f 100644 --- a/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/pages/WebSection.java +++ b/bellatrix.playwright/src/main/java/solutions/bellatrix/playwright/pages/WebSection.java @@ -14,10 +14,10 @@ package solutions.bellatrix.playwright.pages; import solutions.bellatrix.core.infrastructure.PageObjectModel; -import solutions.bellatrix.core.utilities.InstanceFactory; import solutions.bellatrix.core.utilities.SingletonFactory; import solutions.bellatrix.playwright.services.BrowserService; import solutions.bellatrix.playwright.services.ComponentCreateService; + public abstract class WebSection> implements PageObjectModel { public BrowserService browser() { return SingletonFactory.getInstance(BrowserService.class); diff --git a/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/ViewportSize.java b/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/ViewportSize.java new file mode 100644 index 00000000..0e0f4f84 --- /dev/null +++ b/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/ViewportSize.java @@ -0,0 +1,51 @@ +/* + * 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; + +@Getter +public enum ViewportSize { + + MOBILE_S(300, 873), + MOBILE_M(320, 873), + MOBILE_L(393, 873), + TABLET(768, 873), + DESKTOP_M(1280, 873), + DESKTOP(1440, 873), + DESKTOP_TALL(1440, 1500), + IPHONE_X(375, 667), + NEXUS_7_TABLET(600, 960), + IPAD(768, 1024), + IPHONE_SE(375, 667), + IPHONE_13_PRO(375, 667), + IPHONE_12_PRO(390, 844); + + private final int width; + private final int height; + + ViewportSize(int width, int height) { + this.width = width; + this.height = height; + } + + public String getName(){ + return this.name(); + } + + @Override + public String toString() { + return String.format("%sx%s", getWidth(), getHeight()); + } +} diff --git a/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegression.java b/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegression.java new file mode 100644 index 00000000..2d5932e6 --- /dev/null +++ b/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegression.java @@ -0,0 +1,26 @@ +/* + * 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 java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.TYPE, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface VisualRegression { + String projectName() default ""; + ViewportSize viewportSize() default ViewportSize.DESKTOP; +} diff --git a/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionAssertions.java b/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionAssertions.java new file mode 100644 index 00000000..e6d21c5c --- /dev/null +++ b/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionAssertions.java @@ -0,0 +1,54 @@ +/* + * 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.TestRunResult; +import io.visual_regression_tracker.sdk_java.TestRunStatus; +import lombok.experimental.UtilityClass; +import org.junit.jupiter.api.Assertions; +import solutions.bellatrix.core.configuration.ConfigurationService; +import solutions.bellatrix.core.infrastructure.PageObjectModel; + +@UtilityClass +public class VisualRegressionAssertions { + private static float defaultDiffTolerance() { + return ConfigurationService.get(VisualRegressionSettings.class).getDefaultDiffTolerance(); + } + + public static void assertSameAsBaseline(String name, String... testCasesName) { + TestRunResult result = VisualRegressionService.track(name + String.join(" ", testCasesName), defaultDiffTolerance()); + Assertions.assertSame(result.getTestRunResponse().getStatus(), TestRunStatus.OK, + String.format("Visual comparison has detected a difference at: %s", result.getTestRunResponse().getUrl())); + } + + public static void assertSameAsBaseline(String name, String screenshot, String... testCasesName) { + TestRunResult result = VisualRegressionService.track(name + String.join(" ", testCasesName), defaultDiffTolerance(), screenshot); + Assertions.assertSame(result.getTestRunResponse().getStatus(), TestRunStatus.OK, + String.format("Visual comparison has detected a difference at: %s", result.getTestRunResponse().getUrl())); + } + + public static void assertSameAsBaseline(PageObjectModel pageObjectModel, String... testCasesName) { + String name = pageObjectModel.getClass().getSimpleName(); + assertSameAsBaseline(name, testCasesName); + } + + public static void assertSameAsBaseline(PageObjectModel pageObjectModel) { + assertSameAsBaseline(pageObjectModel, ""); + } + + public static void assertSameAsBaseline(TestRunResult result) { + Assertions.assertSame(result.getTestRunResponse().getStatus(), TestRunStatus.OK, + String.format("Visual comparison has detected a difference at: %s", result.getTestRunResponse().getUrl())); + } +} diff --git a/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionPlugin.java b/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionPlugin.java new file mode 100644 index 00000000..84463a58 --- /dev/null +++ b/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionPlugin.java @@ -0,0 +1,47 @@ +/* + * 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 solutions.bellatrix.core.configuration.ConfigurationService; +import solutions.bellatrix.core.plugins.Plugin; +import solutions.bellatrix.core.plugins.TestResult; + +import java.lang.reflect.Method; + +public class VisualRegressionPlugin extends Plugin { + @Override + public void postAfterTest(TestResult testResult, Method memberInfo, Throwable failedTestException) { + VisualRegressionService.stop(); + } + + @Override + public void preBeforeTest(TestResult testResult, Method memberInfo) { + VisualRegression attribute = getVisualRegressionConfiguration(memberInfo); + + if (attribute != null) { + VisualRegressionService.initService(attribute, ConfigurationService.get(VisualRegressionSettings.class)); + VisualRegressionService.start(); + } + } + + private VisualRegression getVisualRegressionConfiguration(Method memberInfo) { + var classAttribute = (VisualRegression)memberInfo.getDeclaringClass().getDeclaredAnnotation(VisualRegression.class); + var methodAttribute = (VisualRegression)memberInfo.getDeclaredAnnotation(VisualRegression.class); + if (methodAttribute != null) { + return methodAttribute; + } + + return classAttribute; + } +} diff --git a/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionService.java b/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionService.java new file mode 100644 index 00000000..ad660a35 --- /dev/null +++ b/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionService.java @@ -0,0 +1,113 @@ +/* + * 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.TestRunOptions; +import io.visual_regression_tracker.sdk_java.TestRunResult; +import io.visual_regression_tracker.sdk_java.VisualRegressionTracker; +import io.visual_regression_tracker.sdk_java.VisualRegressionTrackerConfig; +import plugins.screenshots.ScreenshotPlugin; +import solutions.bellatrix.core.configuration.ConfigurationService; +import solutions.bellatrix.core.utilities.SingletonFactory; + +import java.io.IOException; + +public class VisualRegressionService { + private static final float DEFAULT_DIFF_TOLERANCE = ConfigurationService.get(VisualRegressionSettings.class).getDefaultDiffTolerance(); + + private static final ThreadLocal VISUAL_REGRESSION_TRACKER_THREAD_LOCAL; + private static final ThreadLocal VISUAL_REGRESSION_ATTRIBUTE; + + static { + try { + VISUAL_REGRESSION_TRACKER_THREAD_LOCAL = ThreadLocal.withInitial(() -> null); + VISUAL_REGRESSION_ATTRIBUTE = ThreadLocal.withInitial(() -> null); + } catch (Exception e) { + throw new RuntimeException("Failed to initialize VisualRegressionService", e); + } + } + + public static VisualRegressionTracker tracker() { + return VISUAL_REGRESSION_TRACKER_THREAD_LOCAL.get(); + } + + public static void initService(VisualRegression attribute, VisualRegressionSettings settings) { + VISUAL_REGRESSION_ATTRIBUTE.set(attribute); + + var config = new VisualRegressionTrackerConfig( + settings.getApiUrl(), + settings.getApiKey(), + !attribute.projectName().isBlank() ? attribute.projectName() : settings.getProject(), + settings.getBranch(), + settings.getCiBuildId(), + settings.isEnableSoftAssert(), + settings.getHttpTimeout() + ); + + VISUAL_REGRESSION_TRACKER_THREAD_LOCAL.set(new VisualRegressionTracker(config)); + } + + public static TestRunResult track(String name, Float diffTolerance, String screenshot) { + try { + return tracker().track(name, screenshot, buildRunOptions(diffTolerance)); + } catch (IOException | InterruptedException e) { + throw new RuntimeException(e); + } + } + + public static TestRunResult track(String name, Float diffTolerance) { + return track(name, diffTolerance, takeSnapshot(name)); + } + + public static TestRunResult track(String name) { + return track(name, DEFAULT_DIFF_TOLERANCE); + } + + public static String takeSnapshot(String name) { + var screenshotPlugin = SingletonFactory.getInstance(ScreenshotPlugin.class); + if (screenshotPlugin == null) { + throw new IllegalArgumentException("It seems that the screenshot plugin isn't registered by the 'ScreenshotPlugin.class' key inside SingletonFactory's map or isn't registered at all!"); + } + + return screenshotPlugin.takeScreenshot(name); + } + + public static TestRunOptions buildRunOptions(Float diffTolerance) { + var options = TestRunOptions.builder() + .device(System.getProperty("COMPUTERNAME")) + .os(System.getProperty("os.name")) + .viewport(String.format("%dx%d", VISUAL_REGRESSION_ATTRIBUTE.get().viewportSize().getWidth(), VISUAL_REGRESSION_ATTRIBUTE.get().viewportSize().getHeight())) + .customTags(String.format("%s", VISUAL_REGRESSION_ATTRIBUTE.get().viewportSize().name())) + .diffTollerancePercent(diffTolerance) + .build(); + + return options; + } + + public static void start() { + try { + VISUAL_REGRESSION_TRACKER_THREAD_LOCAL.get().start(); + } catch (IOException | InterruptedException e) { + throw new RuntimeException(e); + } + } + + public static void stop() { + try { + VISUAL_REGRESSION_TRACKER_THREAD_LOCAL.get().stop(); + } catch (IOException | InterruptedException e) { + throw new RuntimeException(e); + } + } +} diff --git a/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionTrackerSettings.java b/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionSettings.java similarity index 89% rename from bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionTrackerSettings.java rename to bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionSettings.java index 7369f03a..07205757 100644 --- a/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionTrackerSettings.java +++ b/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionSettings.java @@ -13,17 +13,19 @@ package solutions.bellatrix.plugins.vrt; +import lombok.Data; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; -@Getter @Setter @NoArgsConstructor -public class VisualRegressionTrackerSettings { +@Data +public class VisualRegressionSettings { private String apiUrl; - private String project; private String apiKey; + private String project; private String branch; private boolean enableSoftAssert; private String ciBuildId; private int httpTimeout; + private float defaultDiffTolerance; } diff --git a/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionTracker.java b/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionTracker.java deleted file mode 100644 index b4a9142f..00000000 --- a/bellatrix.plugins.visual-regression-tracker/src/main/java/solutions/bellatrix/plugins/vrt/VisualRegressionTracker.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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)); - } -} diff --git a/bellatrix.web/src/main/java/solutions/bellatrix/web/infrastructure/junit/WebTest.java b/bellatrix.web/src/main/java/solutions/bellatrix/web/infrastructure/junit/WebTest.java index efbaa2b8..0d0e44ec 100644 --- a/bellatrix.web/src/main/java/solutions/bellatrix/web/infrastructure/junit/WebTest.java +++ b/bellatrix.web/src/main/java/solutions/bellatrix/web/infrastructure/junit/WebTest.java @@ -14,6 +14,7 @@ package solutions.bellatrix.web.infrastructure.junit; import org.junit.jupiter.api.extension.ExtendWith; +import plugins.screenshots.ScreenshotPlugin; import solutions.bellatrix.core.plugins.junit.BaseTest; import solutions.bellatrix.core.plugins.junit.TestResultWatcher; import solutions.bellatrix.core.utilities.SingletonFactory; @@ -36,7 +37,7 @@ public App app() { @Override protected void configure() { addPlugin(BrowserLifecyclePlugin.class); - addPlugin(WebScreenshotPlugin.class); + addPluginAs(ScreenshotPlugin.class, WebScreenshotPlugin.class); addPlugin(WebVideoPlugin.class); addPlugin(LogLifecyclePlugin.class); addListener(BddConsoleLogging.class); diff --git a/bellatrix.web/src/main/java/solutions/bellatrix/web/infrastructure/testng/WebTest.java b/bellatrix.web/src/main/java/solutions/bellatrix/web/infrastructure/testng/WebTest.java index 4655c95b..a67a81f0 100644 --- a/bellatrix.web/src/main/java/solutions/bellatrix/web/infrastructure/testng/WebTest.java +++ b/bellatrix.web/src/main/java/solutions/bellatrix/web/infrastructure/testng/WebTest.java @@ -13,6 +13,7 @@ package solutions.bellatrix.web.infrastructure.testng; +import plugins.screenshots.ScreenshotPlugin; import solutions.bellatrix.core.plugins.testng.BaseTest; import solutions.bellatrix.core.utilities.SingletonFactory; import solutions.bellatrix.web.components.listeners.BddConsoleLogging; @@ -32,7 +33,7 @@ public App app() { @Override protected void configure() { addPlugin(BrowserLifecyclePlugin.class); - addPlugin(WebScreenshotPlugin.class); + addPluginAs(ScreenshotPlugin.class, WebScreenshotPlugin.class); addPlugin(WebVideoPlugin.class); addPlugin(LogLifecyclePlugin.class); addListener(BddConsoleLogging.class);