From fdc4661ff18d3e5cd8be53b439528c256a82e11b Mon Sep 17 00:00:00 2001 From: Michal Krzyzanowski Date: Fri, 10 Aug 2018 17:45:00 +0200 Subject: [PATCH] #174 Added JUnit 5 extension enabling Eyes --- bb-eyes/pom.xml | 31 +++++++++- .../qa/bb/eyes/guice/EyesModule.java | 1 - .../qa/bb/eyes/{ => guice}/EyesProvider.java | 2 +- .../cognifide/qa/bb/eyes/junit5/WithEyes.java | 33 ++++++++++ .../qa/bb/eyes/junit5/WithEyesExtension.java | 60 +++++++++++++++++++ 5 files changed, 124 insertions(+), 3 deletions(-) rename bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/{ => guice}/EyesProvider.java (97%) create mode 100644 bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/junit5/WithEyes.java create mode 100644 bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/junit5/WithEyesExtension.java diff --git a/bb-eyes/pom.xml b/bb-eyes/pom.xml index 18d337e9..3119baa1 100644 --- a/bb-eyes/pom.xml +++ b/bb-eyes/pom.xml @@ -1,4 +1,23 @@ + @@ -7,7 +26,7 @@ bobcat com.cognifide.qa.bb - 1.4.1-SNAPSHOT + 1.5.1-SNAPSHOT bb-eyes @@ -27,6 +46,10 @@ junit junit + + org.junit.jupiter + junit-jupiter-api + com.applitools eyes-selenium-java3 @@ -38,11 +61,17 @@ + com.cognifide.qa.bb bb-core ${project.version} + + com.cognifide.qa.bb + bb-junit5 + ${project.version} + diff --git a/bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/guice/EyesModule.java b/bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/guice/EyesModule.java index 60940469..22194cc1 100644 --- a/bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/guice/EyesModule.java +++ b/bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/guice/EyesModule.java @@ -20,7 +20,6 @@ package com.cognifide.qa.bb.eyes.guice; import com.applitools.eyes.selenium.Eyes; -import com.cognifide.qa.bb.eyes.EyesProvider; import com.google.inject.AbstractModule; public class EyesModule extends AbstractModule { diff --git a/bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/EyesProvider.java b/bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/guice/EyesProvider.java similarity index 97% rename from bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/EyesProvider.java rename to bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/guice/EyesProvider.java index f6d80685..17cbf691 100644 --- a/bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/EyesProvider.java +++ b/bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/guice/EyesProvider.java @@ -17,7 +17,7 @@ * limitations under the License. * #L% */ -package com.cognifide.qa.bb.eyes; +package com.cognifide.qa.bb.eyes.guice; import com.applitools.eyes.selenium.Eyes; import com.cognifide.qa.bb.guice.ThreadScoped; diff --git a/bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/junit5/WithEyes.java b/bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/junit5/WithEyes.java new file mode 100644 index 00000000..33cd5560 --- /dev/null +++ b/bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/junit5/WithEyes.java @@ -0,0 +1,33 @@ +/*- + * #%L + * Bobcat + * %% + * Copyright (C) 2018 Cognifide Ltd. + * %% + * 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. + * #L% + */ +package com.cognifide.qa.bb.eyes.junit5; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.extension.ExtendWith; + +@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@ExtendWith(WithEyesExtension.class) +public @interface WithEyes { +} diff --git a/bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/junit5/WithEyesExtension.java b/bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/junit5/WithEyesExtension.java new file mode 100644 index 00000000..512e10f7 --- /dev/null +++ b/bb-eyes/src/main/java/com/cognifide/qa/bb/eyes/junit5/WithEyesExtension.java @@ -0,0 +1,60 @@ +/*- + * #%L + * Bobcat + * %% + * Copyright (C) 2018 Cognifide Ltd. + * %% + * 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. + * #L% + */ +package com.cognifide.qa.bb.eyes.junit5; + +import static com.cognifide.qa.bb.junit5.JUnit5Constants.NAMESPACE; + +import java.util.Properties; + +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.events.EventFiringWebDriver; + +import com.applitools.eyes.selenium.Eyes; +import com.cognifide.qa.bb.junit5.guice.InjectorUtils; +import com.google.inject.Injector; +import com.google.inject.Key; + +public class WithEyesExtension implements BeforeEachCallback, AfterEachCallback { + + @Override + public void beforeEach(ExtensionContext context) throws Exception { + Injector injector = InjectorUtils.retrieveInjectorFromStore(context, NAMESPACE); + Properties properties = injector.getInstance(Key.get(Properties.class)); + WebDriver wrappedDriver = ((EventFiringWebDriver) injector.getInstance(WebDriver.class)).getWrappedDriver(); + getEyes(context).open(wrappedDriver, String.valueOf(properties.get("eyes.appName")), context.getDisplayName()); + } + + @Override + public void afterEach(ExtensionContext context) throws Exception { + Eyes eyes = getEyes(context); + try { + eyes.close(); + } finally { + eyes.abortIfNotClosed(); + } + } + + private Eyes getEyes(ExtensionContext context) { + return InjectorUtils.retrieveInjectorFromStore(context, NAMESPACE).getInstance(Eyes.class); + } +}