Giter VIP home page Giter VIP logo

loggingseleniumwebdriverjava's Introduction

LoggingSeleniumWebDriver - Java version

Simple logging framework to wrap a Java Selenium WebDriver instance to make it output customizable and extendible logs. This library by default logs event by the WebDriver instance in a natural flow. You may also log your own messages to the log, for example for verification results.

If you are looking for similar functionality for C#/.NET, check this out:https://damberg.one/alster/work/loggingselenium.html

What it is

Selenium WebDriver is a useful tool to drive web browser activities for test purposes. However, Selenium WebDriver doesn't have any means of telling what it has done for logging purposes.

For example; the test:

        By searchField = By.tagName("textarea");
        WebDriver driver = new ChromeDriver();
        driver.get("https://google.com");
        driver.findElement(searchField).click();
        driver.findElement(searchField).sendKeys("Logging");
        driver.findElement(searchField).submit();
        driver.quit();

Would only output some text about starting a ChromeDriver instance.

If we would wrap this with LogginSeleniumWebDriver it produces the followin output:

2023-05-11 16:30:06 INFO:           Starting driver of type org.openqa.selenium.chrome.ChromeDriver.
2023-05-11 16:30:06 EXECUTION_STEP: Navigating to 'https://google.com'.
2023-05-11 16:30:06 EXECUTION_STEP: Clicking textarea element Sök.
2023-05-11 16:30:07 EXECUTION_STEP: Sending keys 'Logging' to textarea element Sök.
2023-05-11 16:30:07 EXECUTION_STEP: Submitting
2023-05-11 16:30:07 INFO:           Quitting web driver instance.

Depending on the log level this could get even more detailed (as can be seen below).

2023-05-11 16:38:19 INFO:           Starting driver of type org.openqa.selenium.chrome.ChromeDriver.
2023-05-11 16:38:19 EXECUTION_STEP: Navigating to 'https://google.com'.
2023-05-11 16:38:20 DEBUG:          Identified element for By statement 'By.tagName: textarea'.
2023-05-11 16:38:20 EXECUTION_STEP: Clicking textarea element Sök.
2023-05-11 16:38:20 DEBUG:          Identified element for By statement 'By.tagName: textarea'.
2023-05-11 16:38:20 EXECUTION_STEP: Sending keys 'Logging' to textarea element Sök.
2023-05-11 16:38:20 DEBUG:          Identified element for By statement 'By.tagName: textarea'.
2023-05-11 16:38:20 EXECUTION_STEP: Submitting
2023-05-11 16:38:20 INFO:           Quitting web driver instance.

Getting started

Including dependency

Maven dependency

Get it from maven central by including this to your dependencies section of your pom.xml file:

<dependency>
    <groupId>com.github.claremontqualitymanagement.seleniumextensions</groupId>
    <artifactId>LoggingSeleniumWebDriver</artifactId>
    <version>1.4.0</version>
</dependency>

Direct download

You can also download released artifacts direclty from this repository and include them in your classpath. The latest released binaries can be found here: https://github.com/kejsardamberg/LoggingSeleniumWebDriverJava/releases/tag/LoggingSeleniumWebDriver-1.2.4

Clone and compile

Of course you may also clone the code from this repository and compile it yourself as long as you adhere to the Apache 2.0 license terms.

Usage

Regular instantiation

    WebDriver driver = new LoggingSeleniumWebDriver(new ChromeDriver()); //Enables console logger by default

Builder pattern constructor

        WebDriver driver = new LoggingSeleniumWebDriver.Builder()
                .attachWebDriverInstance(new ChromeDriver())
                .addLogger(new ConsoleLogger())
                .setMinimumLogLevel(TestFlowLogLevel.DEBUG)
                .build();
        driver.get("https://mysaite.com");

Other features

Using this library is meant to be pretty straight forward. However, since there are a few features these could also be configured.

sendKeys() variant: sendSecretKeys(CharSequence... keysToSent)

Sometimes you don't want strings to be sent to the log, like password inputs. Hence a version of the regular Selenium WebElement sendKeys() method is included. It produces log output of sent characters like '****' instead of actual characters.

Set log level

There are several log levels, DEBUG, INFO, EXECUTION_STEP, and EXCEPTION (in this order). To set the minimum log level, use the setMinimumLogLevel() method.

        LoggingSeleniumWebDriver driver = new LoggingSeleniumWebDriver(new FirefoxDriver());
        driver.setMinimumLogLevel(TestFlowLogLevel.INFO);

Using the builder pattern you may also set the logging level at instansiation.

Custom log messages

Sometimes you want to write out your own messages to the log. This could be done easily by:

    driver.log("You are now in the code line that produces my own log message."); //Default log level is INFO
    driver.logDebug("This is a detailed debug information message.");
    driver.logInfo("This is important information regarding execution.");
    driver.logExecutionStep("This is information about a performed execution step.");
    driver.logException(new LoggingSeleniumWebDriverException("Oups!!!"));

Adding your own Loggers

If not used as a WebDriver, but kept as a LoggingSeleniumWebDriver you may add other loggers and custom loggers:

    LoggingSeleniumWebDriver driver = new LoggingSeleniumWebDriver(new ChromeDriver());
    driver.addLogger(new HtmlLogger());

Pausing and resuming logging

Logging may be paused or resumed anywhere by

    LoggingSeleniumWebDriver driver = new LoggingSeleniumWebDriver(new ChromeDriver());
    driver.get("https://mypage.com");
    driver.findElement(userNameField).sendKeys("SuperAdmin");
    driver.pauseLogging();
    driver.findElement(passwordField).sendKeys("TopSecret!");
    driver.resumeLogging();
    driver.findElement(loginButton).click();

Advanced usage

Original WebDriver instance

The original and non-logging WebDriver instance could be accessed and used at any time by using:

        driver.originalWebDriver.get("https://newsite.com");

Extending with your own Logger classes

THe Logger is an interface. Feel free to implement your own classes based on this interface.

Chance WebDriver instance at runtime

        LoggingSeleniumWebDriver driver = new LoggingSeleniumWebDriver(new FirefoxDriver());
        // Do stuff with this Firefox browser instance
        driver.detachWebDriverInstance();
        driver.attachWebDriverInstance(new ChromeDriver());
        //Continue exeuction in Chrome

Extend with custom log levels

Any new log level should implement the LogLevel interface. You might need to override existing log levels for log level filtering log level to work as expected.

loggingseleniumwebdriverjava's People

Contributors

kejsardamberg avatar

Watchers

 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.