MCPcopy Index your code
hub / github.com/barancev/webdriver-factory

github.com/barancev/webdriver-factory @webdriver-factory-4.4

Chat with this repo
repository ↗ · DeepWiki ↗ · release webdriver-factory-4.4 ↗ · + Follow
142 symbols 408 edges 19 files 7 documented · 5% updated 6y ago★ 874 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Web Driver Factory

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.

4) 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.

Extension points exported contracts — how you extend this code

LocalDriverProvider (Interface)
(no doc) [5 implementers]
src/main/java/ru/stqa/selenium/factory/LocalDriverProvider.java
RemoteDriverProvider (Interface)
(no doc) [4 implementers]
src/main/java/ru/stqa/selenium/factory/RemoteDriverProvider.java
WebDriverPool (Interface)
An utility that helps to create, reuse and dismiss WebDriver instances. The WebDriver instances created by the pool and [1 …
src/main/java/ru/stqa/selenium/factory/WebDriverPool.java
DriverAlivenessChecker (Interface)
(no doc) [2 implementers]
src/main/java/ru/stqa/selenium/factory/DriverAlivenessChecker.java

Core symbols most depended-on inside this repo

getDriver
called by 51
src/main/java/ru/stqa/selenium/factory/WebDriverPool.java
isEmpty
called by 33
src/main/java/ru/stqa/selenium/factory/WebDriverPool.java
dismissDriver
called by 17
src/main/java/ru/stqa/selenium/factory/WebDriverPool.java
setLocalDriverProvider
called by 7
src/main/java/ru/stqa/selenium/factory/WebDriverPool.java
createDriver
called by 6
src/main/java/ru/stqa/selenium/factory/LocalDriverProvider.java
dismissAll
called by 5
src/main/java/ru/stqa/selenium/factory/WebDriverPool.java
createNewDriver
called by 4
src/main/java/ru/stqa/selenium/factory/ThreadLocalSingleWebDriverPool.java
createKey
called by 4
src/main/java/ru/stqa/selenium/factory/AbstractWebDriverPool.java

Shape

Method 120
Class 18
Interface 4

Languages

Java100%

Modules by API surface

src/test/java/ru/stqa/selenium/factory/ThreadLocalSingleWebDriverPoolTest.java19 symbols
src/test/java/ru/stqa/selenium/factory/FakeWebDriver.java16 symbols
src/test/java/ru/stqa/selenium/factory/FakeAlertiveWebDriver.java16 symbols
src/test/java/ru/stqa/selenium/factory/SingleWebDriverPoolTest.java13 symbols
src/test/java/ru/stqa/selenium/factory/AbstractWebDriverPoolTest.java12 symbols
src/test/java/ru/stqa/selenium/factory/LooseWebDriverPoolTest.java9 symbols
src/main/java/ru/stqa/selenium/factory/WebDriverPool.java8 symbols
src/main/java/ru/stqa/selenium/factory/ThreadLocalSingleWebDriverPool.java8 symbols
src/main/java/ru/stqa/selenium/factory/SingleWebDriverPool.java7 symbols
src/main/java/ru/stqa/selenium/factory/LooseWebDriverPool.java6 symbols
src/main/java/ru/stqa/selenium/factory/AbstractWebDriverPool.java6 symbols
src/test/java/ru/stqa/selenium/factory/DefaultLocalDriverProviderTest.java5 symbols

For agents

$ claude mcp add webdriver-factory \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact