Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Commit

Permalink
Merge pull request #313 from Cognifide/bugfix-webdriverclose-extension
Browse files Browse the repository at this point in the history
The actual fix for WebDriverCloseExtension issues #306
  • Loading branch information
mkrzyzanowski authored Jan 3, 2019
2 parents 6ba0550 + f6f527e commit 2039784
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
3 changes: 3 additions & 0 deletions bb-junit5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Djunit.jupiter.extensions.autodetection.enabled=false</argLine>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.openqa.selenium.WebDriver;

Expand All @@ -35,20 +36,22 @@
* <p>
* Loaded automatically by ServiceLoader.
*/
public class WebdriverCloseExtension implements AfterTestExecutionCallback, AfterAllCallback {
public class WebdriverCloseExtension
implements BeforeTestExecutionCallback, AfterTestExecutionCallback, AfterAllCallback {

private WebDriver webDriver;

@Override
public void beforeTestExecution(ExtensionContext context) {
webDriver = getWebDriver(context);
}

/**
* {@inheritDoc}
*/
@Override
public void afterTestExecution(ExtensionContext context) {
Injector injector = getInjector(context);
if (injector != null) {
if (webDriver == null) {
webDriver = injector.getInstance(Key.get(WebDriver.class));
}
if (webDriver != null) {
webDriver.quit();
}
}
Expand All @@ -63,7 +66,11 @@ public void afterAll(ExtensionContext context) {
}

//for mocking purposes
Injector getInjector(ExtensionContext context) {
return InjectorUtils.retrieveInjectorFromStore(context, NAMESPACE);
WebDriver getWebDriver(ExtensionContext context) {
Injector injector = InjectorUtils.retrieveInjectorFromStore(context, NAMESPACE);
if (injector != null) {
return injector.getInstance(Key.get(WebDriver.class));
}
throw new IllegalStateException("Could not obtain WebDriver instance");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
package com.cognifide.qa.bb.junit5.selenium;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -31,15 +31,9 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.openqa.selenium.WebDriver;

import com.google.inject.Injector;
import com.google.inject.Key;

@ExtendWith(MockitoExtension.class)
class WebdriverCloseExtensionTest {

@Mock
private Injector injector;

@Mock
private WebDriver webdriver;

Expand All @@ -48,8 +42,8 @@ class WebdriverCloseExtensionTest {

@BeforeEach
void setup() {
doReturn(injector).when(tested).getInjector(any());
when(injector.getInstance(eq(Key.get(WebDriver.class)))).thenReturn(webdriver);
doReturn(webdriver).when(tested).getWebDriver(any());
tested.beforeTestExecution(any());
}

@Test
Expand Down

0 comments on commit 2039784

Please sign in to comment.