Skip to content

Not possible to use own WebDriver bean with own lifecycle #7454

@nkonev

Description

@nkonev

From #6641 (comment)
@philwebb
I writed my own WebDriver bean with my own lifecycle: I want to start and stop "heavy" WebDriver once, at JUnit tests startup and stop after all tests done.
All works Ok in 1.4.0, but in Spring Boot 1.4.1 appear WebDriverTestExecutionListener which break my wanted behaviour.
I tried to exclude it with
spring.autoconfigure.exclude: org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener
but it not excluded.

Expected behaviour: using same instance of webdriver.
Actual behaviour: my SeleniumFactory opens new Chrome/mozilla/... window per test and not close previous.

@Configuration
public class IntegrationTestConfiguration {

    @Value(TestConstants.IMPLICITLY_WAIT_TIMEOUT)
    private int implicitlyWaitTimeout;

    @Value(TestConstants.BROWSER)
    private Browser browser;

    @Bean(initMethod = "start", destroyMethod = "stop")
    public SeleniumFactory seleniumComponent() throws Exception {
        return new SeleniumFactory(implicitlyWaitTimeout, browser);
    }
}
public class SeleniumFactory implements FactoryBean<WebDriver> {

    public static final String PROPERTY_BROWSER_PHANTOM_LOG = "phantomjs.log";

    public static final String FIREFOX_DRIVER_VERSION = "0.10.0";
    public static final String CHROME_DRIVER_VERSION = "2.23";
    public static final String PHANTOM_JS_DRIVER_VERSION = "2.1.1";

    private WebDriver driver;

    private static final Logger logger = LoggerFactory.getLogger(SeleniumFactory.class);

    private final int implicitlyWaitTimeout;

    private final Browser browser;

    public SeleniumFactory(int implicitlyWaitTimeout, Browser browser) throws Exception {
        this.implicitlyWaitTimeout = implicitlyWaitTimeout;
        this.browser = browser;

    }

    @Override
    public WebDriver getObject() throws Exception {
        return driver;
    }

    @Override
    public Class<WebDriver> getObjectType() {
        return WebDriver.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public void start() {
        // http://www.slf4j.org/legacy.html#jul-to-slf4j
        // http://www.slf4j.org/api/org/slf4j/bridge/SLF4JBridgeHandler.html
        // Optionally remove existing handlers attached to j.u.l root logger
        SLF4JBridgeHandler.removeHandlersForRootLogger();  // (since SLF4J 1.6.5)
        // add SLF4JBridgeHandler to j.u.l's root logger, should be done once during
        // the initialization phase of your application
        SLF4JBridgeHandler.install();

        switch(browser) {
            case PHANTOM:
            {
                PhantomJsDriverManager.getInstance().setup(PHANTOM_JS_DRIVER_VERSION); // download executables if need and set System.properties

                String phantomLogProp = System.getProperty(PROPERTY_BROWSER_PHANTOM_LOG);
                File phantomLog = null;
                if (phantomLogProp != null){
                    phantomLog = new File(phantomLogProp);
                }
                String phantomjspath = System.getProperty(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY); // setted by PhantomJsDriverManager.getInstance().setup();
                PhantomJSDriverService phantomJSDriverService = new PhantomJSDriverService.Builder()
                        .usingPhantomJSExecutable(new File(phantomjspath))
                        .withLogFile(phantomLog)
                        .usingAnyFreePort()
                        .build();
                driver = new PhantomJSDriver(phantomJSDriverService, DesiredCapabilities.phantomjs());
            }
            break;
            case FIREFOX:
            {
                // firefox
                MarionetteDriverManager.getInstance().setup(FIREFOX_DRIVER_VERSION); // download executables if need and set System.properties
                //Step 1- Driver Instantiation: Instantiate driver object as FirefoxDriver
                driver = new FirefoxDriver();
            }
            break;
            case CHROME:
            {
                ChromeDriverManager.getInstance().setup(CHROME_DRIVER_VERSION); // download executables if need and set System.properties
                driver = new ChromeDriver();
            }
            break;
        }

        driver.manage().window().setSize(new Dimension(1600, 900));
        driver.manage().timeouts().implicitlyWait(implicitlyWaitTimeout, TimeUnit.SECONDS); // wait for #findElement()
    }

    public void stop() {
        if (driver != null) {
            //Step 4- Close Driver
            driver.close();

            //Step 5- Quit Driver
            driver.quit();
        }

    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions