Git Product home page Git Product logo

webdriver-factory's Introduction

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.

webdriver-factory's People

Contributors

barancev avatar emaks avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.