webdriver-factory

An utility to manage WebDriver instances

License

License

GroupId

GroupId

ru.stqa.selenium
ArtifactId

ArtifactId

webdriver-factory
Last Version

Last Version

4.4
Release Date

Release Date

Type

Type

jar
Description

Description

webdriver-factory
An utility to manage WebDriver instances
Project URL

Project URL

https://github.com/barancev/webdriver-factory
Source Code Management

Source Code Management

https://github.com/barancev/webdriver-factory

Download webdriver-factory

How to add to project

<!-- https://jarcasting.com/artifacts/ru.stqa.selenium/webdriver-factory/ -->
<dependency>
    <groupId>ru.stqa.selenium</groupId>
    <artifactId>webdriver-factory</artifactId>
    <version>4.4</version>
</dependency>
// https://jarcasting.com/artifacts/ru.stqa.selenium/webdriver-factory/
implementation 'ru.stqa.selenium:webdriver-factory:4.4'
// https://jarcasting.com/artifacts/ru.stqa.selenium/webdriver-factory/
implementation ("ru.stqa.selenium:webdriver-factory:4.4")
'ru.stqa.selenium:webdriver-factory:jar:4.4'
<dependency org="ru.stqa.selenium" name="webdriver-factory" rev="4.4">
  <artifact name="webdriver-factory" type="jar" />
</dependency>
@Grapes(
@Grab(group='ru.stqa.selenium', module='webdriver-factory', version='4.4')
)
libraryDependencies += "ru.stqa.selenium" % "webdriver-factory" % "4.4"
[ru.stqa.selenium/webdriver-factory "4.4"]

Dependencies

compile (1)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-engine jar 5.5.2

provided (1)

Group / Artifact Type Version
org.seleniumhq.selenium : selenium-java jar 3.141.59

test (2)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-api jar 5.5.2
org.seleniumhq.selenium : htmlunit-driver jar 2.36.0

Project Modules

There are no modules declared in this project.

Web Driver Factory

Shippable Run Status CircleCI Run Status

This library provides an utility to manage WebDriver instances. It helps to create, reuse and dismiss WebDriver instances.

To use this library in a maven project you have to add these dependencies:

<dependency>
    <groupId>ru.stqa.selenium</groupId>
    <artifactId>webdriver-factory</artifactId>
    <version>4.4</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>

The library implements Object Pool design pattern, but for a historical reason it is called "factory".

The instances created by the pool and stored in the pool are called "managed instances".

The library provides three ways to manage instances:

  • SingleWebDriverPool allows a single managed instance of WebDriver to exist at any given moment,
  • ThreadLocalSingleWebDriverPool allows a single managed instance of WebDriver to exist for each thread,
  • LooseWebDriverPool does not impose any restrictions, it creates a new managed instance on each request.

You can use as many separate pools as you like, but there is also WebDriverPool.DEFAULT that is an instance of ThreadLocalSingleWebDriverPool.

1) The simplest use case

// create a new managed instance
WebDriver driver = WebDriverPool.DEFAULT.getDriver(new FirefoxOptions());
// do something with the driver
driver.get("http://seleniumhq.org/");
// destroy the instance (calls driver.quit() implicitly)
WebDriverPool.DEFAULT.dismissDriver(driver);

2) If one requests a new driver with the same capabilities the existing instance should be reused by SingleWebDriverPool and ThreadLocalSingleWebDriverPool:

// create a new managed instance
WebDriver driver = WebDriverPool.DEFAULT.getDriver(new FirefoxOptions());
// do something with the driver
driver.get("http://seleniumhq.org/");

// obtain the same instance from the pool of the managed instances
driver = WebDriverPool.DEFAULT.getDriver(new FirefoxOptions());
// do something with the driver
driver.get("http://selenium2.ru/");

// destroy the driver
WebDriverPool.DEFAULT.dismissDriver(driver);

Additionally, the pool checks availability of the browser (by default it checks that driver.getWindowHandles().size() > 0) before returning the instance to the client. If the browser is not available the pool dismisses the "broken" driver and creates a new WebDriver instance as a replacement.

3) If one requests a new driver with different capabilities a new WebDriver instance should be created

What happens to the previous instances depends on the pool implementation:

  • SingleWebDriverPool destroys and dismisses the previous managed instance of the driver,
  • ThreadLocalSingleWebDriverPool destroys and dismisses the previous managed instance of the driver associated with the current thread; managed instances created in other threads are kept untouched,
  • LooseWebDriverPool does nothing to all the running instances.
  1. One can destroy all managed WebDriver instances in a pool at once:
@Test
public void testSomething() {
  WebDriver driver = WebDriverPool.DEFAULT.getDriver(new FirefoxOptions());
  // do something with the driver
  driver.get("http://seleniumhq.org/");
}

@Test
public void testSomethingElse() {
  WebDriver driver = WebDriverPool.DEFAULT.getDriver(new ChromeOptions());
  // do something with the driver
  driver.get("http://seleniumhq.org/");
}

@AfterSuite
public void stopAllDrivers() {
  WebDriverPool.DEFAULT.dismissAll();
}

(Ability to destroy all managed instances at once is probably the only usable feature of LooseWebDriverPool)

There are several samples that show how to use WebDriverFactory with test frameworks JUnit and TestNG.

Versions

Version
4.4
4.3
4.2
4.1
4.0
3.0
2.1
2.0
1.1.52
1.1.51
1.1.49
1.1.47
1.1.46
1.1.45
1.1.43
1.1.42
1.1.41
1.0