bonigarcia / wdm-agent-example

Example of maven project using Selenium WebDriver and WebDriverManager as Agent

Home Page:https://bonigarcia.dev/webdrivermanager/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Maven Central Build Status badge-jdk License badge Backers on Open Collective Sponsors on Open Collective Support badge Twitter Follow

WebDriverManager Agent Example

This repository contains a Maven repository with examples to automate the Selenium WebDriver binaries management using WebDriverManager as Agent. This repository is open source, released under the terms of Apache 2.0 License.

Usage

In order to use WebDriverManager Agent from tests in a Maven project, this example first includes the WebDriverManager dependency in the pom.xml using the test scope:

    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>${wdm.version}</version>
        <scope>test</scope>
    </dependency>

Then, the Java agent (-javaagent) is configured using the two Maven plugins: maven-dependency-plugin (which allows to manipulate Maven dependencies) and maven-surefire-plugin (which executes the unit tests in the Maven lifecycle), as follows:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>${maven-dependency-plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>properties</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <argLine>-javaagent:${io.github.bonigarcia:webdrivermanager:jar}</argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>

When the Maven tests are executed (mvn test), WebDriverManager Agent will check the objects being created in the JVM. Just before Selenium WebDriver objects are instantiated in the tests (org.openqa.selenium.chrome.ChromeDriver, org.openqa.selenium.firefox.FirefoxDriver, org.openqa.selenium.opera.OperaDriver, org.openqa.selenium.edge.EdgeDriver, or org.openqa.selenium.ie.InternetExplorerDriver), the proper WebDriverManager setup call is executed to manage the required driver (chromedriver, geckodriver, msedgedriver, etc).

The tests contained in this project are the following (notice there is no setup for chromedriver nor geckodriver).

class ChromeTest {

    WebDriver driver;

    @BeforeEach
    void setupTest() {
        driver = new ChromeDriver();
    }

    @AfterEach
    void teardown() {
        driver.quit();
    }

    @Test
    void test() {
        // Your test code here
    }

}
class FirefoxTest {

    WebDriver driver;

    @BeforeEach
    void setupTest() {
        driver = new FirefoxDriver();
    }

    @AfterEach
    void teardown() {
        driver.quit();
    }

    @Test
    void test() {
        // Your test code here
    }

}

When the test are executed through the maven-surefire-plugin (i.e. running mvn test), the WebDriverManager Agent resolves the proper driver, and the tests are executed correctly:

$ mvn test
...
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running io.github.bonigarcia.wdm.agent.test.ChromeTest
2020-05-12 15:38:49 [main] DEBUG io.github.bonigarcia.wdm.WdmAgent.transform(86) -- WebDriverManager Agent is going to resolve the driver for Chrome
2020-05-12 15:38:49 [main] DEBUG i.g.b.wdm.cache.ResolutionCache.checkKeyInResolutionCache(183) -- Resolution chrome=81 in cache (valid until 15:23:29 13/05/2020 CEST)
2020-05-12 15:38:49 [main] INFO  i.g.bonigarcia.wdm.WebDriverManager.resolveDriverVersion(571) -- Using chromedriver 81.0.4044.138 (since Chrome 81 is installed in your machine)
2020-05-12 15:38:49 [main] DEBUG i.g.bonigarcia.wdm.WebDriverManager.manage(520) -- Driver chromedriver 81.0.4044.138 found in cache
2020-05-12 15:38:49 [main] INFO  i.g.bonigarcia.wdm.WebDriverManager.exportDriver(615) -- Exporting webdriver.chrome.driver as /home/boni/.m2/repository/webdriver/chromedriver/linux64/81.0.4044.138/chromedriver
Starting ChromeDriver 81.0.4044.138 (8c6c7ba89cc9453625af54f11fd83179e23450fa-refs/branch-heads/4044@{#999}) on port 31286
2020-05-12 15:38:50 [main] DEBUG i.g.b.wdm.agent.test.ChromeTest.test(54) -- The title of https://bonigarcia.github.io/selenium-jupiter/ is Selenium-Jupiter: JUnit 5 extension for Selenium
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.814 s - in io.github.bonigarcia.wdm.agent.test.ChromeTest
[INFO] Running io.github.bonigarcia.wdm.agent.test.FirefoxTest
2020-05-12 15:46:04 [main] DEBUG io.github.bonigarcia.wdm.WdmAgent.transform(86) -- WebDriverManager Agent is going to resolve the driver for Firefox
2020-05-12 15:46:04 [main] DEBUG i.g.b.wdm.cache.ResolutionCache.checkKeyInResolutionCache(183) -- Resolution firefox=76 in cache (valid until 15:45:41 13/05/2020 CEST)
2020-05-12 15:46:04 [main] INFO  i.g.bonigarcia.wdm.WebDriverManager.resolveDriverVersion(571) -- Using geckodriver 0.26.0 (since Firefox 76 is installed in your machine)
2020-05-12 15:46:04 [main] DEBUG i.g.bonigarcia.wdm.WebDriverManager.manage(520) -- Driver geckodriver 0.26.0 found in cache
2020-05-12 15:46:04 [main] INFO  i.g.bonigarcia.wdm.WebDriverManager.exportDriver(615) -- Exporting webdriver.gecko.driver as /home/boni/.m2/repository/webdriver/geckodriver/linux64/0.26.0/geckodriver
2020-05-12 15:46:07 [main] DEBUG i.g.b.wdm.agent.test.FirefoxTest.test(54) -- The title of https://bonigarcia.github.io/selenium-jupiter/ is Selenium-Jupiter: JUnit 5 extension for Selenium
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.163 s - in io.github.bonigarcia.wdm.agent.test.FirefoxTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  10.017 s
[INFO] Finished at: 2020-05-12T15:46:10+02:00
[INFO] ------------------------------------------------------------------------

Help

If you have questions on how to use WebDriverManager properly with a special configuration or suchlike, please consider asking a question on Stack Overflow and tag it with webdrivermanager-java.

Support

WebDriverManager is part of OpenCollective, an online funding platform for open and transparent communities. You can support the project by contributing as a backer (i.e., a personal donation or recurring contribution) or as a sponsor (i.e., a recurring contribution by a company).

Backers

Sponsors

About

WebDriverManager (Copyright © 2015-2022) is a project created and maintained by Boni Garcia licensed under Apache 2.0 License.

About

Example of maven project using Selenium WebDriver and WebDriverManager as Agent

https://bonigarcia.dev/webdrivermanager/


Languages

Language:Java 100.0%