Skip to content

Add WebDriverListener example with logging #2384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from

Conversation

testervippro
Copy link

@testervippro testervippro commented Jul 20, 2025

User description

Add WebDriverListener example with logging

  • Added example of WebDriverListener with logging before findElement and click.
  • Included logic to capture screenshots on error for better debugging.
  • Updated junit-platform.properties for Windows compatibility:
    • forkCount = 0
    • junit.jupiter.execution.parallel.config.fixed.parallelism = 1
    • junit.jupiter.execution.parallel.config.fixed.max-pool-size = 1
    • -Run test : mvn clean test -Dtest=WebDriverListenerTest

PR Type

Tests, Enhancement


Description

  • Add WebDriverListener example with comprehensive logging functionality

  • Fix Maven Surefire configuration for Windows compatibility

  • Include screenshot capture on error for debugging

  • Update documentation with working code example


Diagram Walkthrough

flowchart LR
  A["CustomWebDriverListener"] --> B["Logging Hooks"]
  A --> C["Screenshot Capture"]
  B --> D["findElement/click events"]
  C --> E["Error handling"]
  F["Maven Config"] --> G["Windows compatibility"]
  H["Test Examples"] --> A
  I["Documentation"] --> H
Loading

File Walkthrough

Relevant files
Configuration changes
pom.xml
Fix Maven Surefire Windows compatibility configuration     

examples/java/pom.xml

  • Fix Maven Surefire plugin configuration for Windows compatibility
  • Set forkCount to 0 to resolve fork startup errors
  • Update JUnit parallel execution settings with fixed values
  • Improve XML formatting and indentation
+18/-14 
Enhancement
CustomWebDriverListener.java
Add comprehensive WebDriverListener with logging and screenshots

examples/java/src/test/java/dev/selenium/listeners/CustomWebDriverListener.java

  • Implement comprehensive WebDriverListener with logging hooks
  • Add colored console output for better readability
  • Include screenshot capture functionality on errors
  • Provide detailed logging for findElement, click, and driver calls
+157/-0 
Tests
WebDriverListenerTest.java
Add WebDriverListener test examples                                           

examples/java/src/test/java/dev/selenium/listeners/WebDriverListenerTest.java

  • Create test class demonstrating WebDriverListener usage
  • Add test for normal operations with logging
  • Include test for error scenarios with screenshot capture
  • Use EventFiringDecorator to wrap ChromeDriver with listener
+57/-0   
Documentation
listeners.en.md
Update listeners documentation with code example                 

website_and_docs/content/documentation/webdriver/support_features/listeners.en.md

  • Update documentation to reference actual working code example
  • Replace badge-code placeholder with GitHub code block reference
  • Point to WebDriverListenerTest.java for live example
+4/-1     

… config for Windows

- Added example of `WebDriverListener` with logging before `findElement` and `click`.
- Included logic to capture screenshots on error for better debugging.
- Updated `junit-platform.properties` for Windows compatibility:
  - forkCount = 0
  - junit.jupiter.execution.parallel.config.fixed.parallelism = 1
  - junit.jupiter.execution.parallel.config.fixed.max-pool-size = 1
-Run test : mvn clean test -Dtest=WebDriverListenerTest
Copy link

netlify bot commented Jul 20, 2025

👷 Deploy request for selenium-dev pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 465c6aa

@CLAassistant
Copy link

CLAassistant commented Jul 20, 2025

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Redundant Logic

The beforeFindElement and beforeFindElements methods contain identical logic that calls findElements again, which could cause performance issues and infinite recursion. This debug counting should be removed or implemented differently.

    try {
        if (driver instanceof HasCapabilities) {
            List<WebElement> elements = driver.findElements(locator);
            logger.info("DEBUG: Number of elements matching '" + locator + "': " + elements.size());
        }
    } catch (Exception e) {
        logger.warning("Error while counting elements for locator " + locator + ": " + e.getMessage());
    }
}

@Override
public void afterFindElement(WebDriver driver, By locator, WebElement result) {
    logger.info("AFTER findElement -> Locator: " + locator + ", Result: " + getElementInfo(result));
}

@Override
public void beforeFindElements(WebDriver driver, By locator) {
    logger.info("BEFORE findElements -> Locator: " + locator);
    try {
        if (driver instanceof HasCapabilities) {
            List<WebElement> elements = driver.findElements(locator);
            logger.info("DEBUG: Number of elements matching '" + locator + "': " + elements.size());
        }
    } catch (Exception e) {
        logger.warning("Error while counting elements for locator " + locator + ": " + e.getMessage());
    }
Configuration Issue

The Maven Surefire configuration has malformed XML with incomplete configurationParameters tag and comments inside XML values, which could cause build failures.

<configurationParameters> junit.jupiter.execution.parallel.enabled = true
    junit.jupiter.execution.parallel.mode.default = concurrent
    junit.jupiter.execution.parallel.config.strategy = fixed <!-- Fix Failed
    to transform configuration parameter with key
    'junit.jupiter.execution.parallel.config.fixed.parallelism' and initial
    value ' -->
    junit.jupiter.execution.parallel.config.fixed.parallelism = 1
    junit.jupiter.execution.parallel.config.fixed.max-pool-size = 1 </configurationParameters>
Resource Leak

WebDriver instances are not properly managed with try-with-resources or @AfterEach cleanup, potentially causing resource leaks if tests fail before reaching quit() calls.

private WebDriver driver;

@Test
@Order(1)
public void testWebDriverListener() {
    WebDriverListener listener = new CustomWebDriverListener();
    driver = new EventFiringDecorator<>(listener).decorate(new ChromeDriver());

    driver.get("https://www.selenium.dev/");
    driver.manage().window().maximize();

    WebElement documentation = driver.findElement(By.cssSelector("a[href='/documentation']"));

    documentation.click();

    driver.quit();

}

@Test
@Order(2)
public void testWebDriverListenerOnError() {
    WebDriverListener listener = new CustomWebDriverListener();
    driver = new EventFiringDecorator<>(listener).decorate(new ChromeDriver());

    try {
        driver.get(null);
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    driver.quit();

}

Copy link
Contributor

qodo-merge-pro bot commented Jul 20, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Remove recursive findElements call
Suggestion Impact:The entire beforeFindElement() method was removed from the code, which eliminates the infinite recursion issue caused by calling driver.findElements() within the listener

code diff:

-    @Override
-    public void beforeFindElement(WebDriver driver, By locator) {
-        logger.info("BEFORE findElement -> Locator: " + locator);
-        try {
-            if (driver instanceof HasCapabilities) {
-                List<WebElement> elements = driver.findElements(locator);
-                logger.info("DEBUG: Number of elements matching '" + locator + "': " + elements.size());
-            }
-        } catch (Exception e) {
-            logger.warning("Error while counting elements for locator " + locator + ": " + e.getMessage());
-        }
-    }

Calling findElements() inside beforeFindElement() creates infinite recursion
since findElements() triggers the listener again. This will cause a stack
overflow error.

examples/java/src/test/java/dev/selenium/listeners/CustomWebDriverListener.java [90-101]

 @Override
 public void beforeFindElement(WebDriver driver, By locator) {
     logger.info("BEFORE findElement -> Locator: " + locator);
-    try {
-        if (driver instanceof HasCapabilities) {
-            List<WebElement> elements = driver.findElements(locator);
-            logger.info("DEBUG: Number of elements matching '" + locator + "': " + elements.size());
-        }
-    } catch (Exception e) {
-        logger.warning("Error while counting elements for locator " + locator + ": " + e.getMessage());
-    }
 }

[Suggestion processed]

Suggestion importance[1-10]: 10

__

Why: The suggestion correctly identifies a critical issue where calling driver.findElements() within the beforeFindElement() listener method will cause infinite recursion, leading to a StackOverflowError.

High
Fix malformed XML comment
Suggestion Impact:The commit removed the malformed XML comment that was embedded within the configuration parameter value, fixing the XML structure as suggested

code diff:

-                            junit.jupiter.execution.parallel.config.strategy = fixed <!-- Fix Failed
-                            to transform configuration parameter with key
-                            'junit.jupiter.execution.parallel.config.fixed.parallelism' and initial
-                            value ' -->
+                            junit.jupiter.execution.parallel.config.strategy = fixed 

The XML comment is malformed and breaks the configuration parameters. XML
comments cannot be placed inside element content and should be properly closed
with -->.

examples/java/pom.xml [64-71]

-<configurationParameters> junit.jupiter.execution.parallel.enabled = true
+<!-- Fix Failed to transform configuration parameter with key 'junit.jupiter.execution.parallel.config.fixed.parallelism' and initial value -->
+                    <configurationParameters>
+                        junit.jupiter.execution.parallel.enabled = true
                         junit.jupiter.execution.parallel.mode.default = concurrent
-                        junit.jupiter.execution.parallel.config.strategy = fixed <!-- Fix Failed
-                        to transform configuration parameter with key
-                        'junit.jupiter.execution.parallel.config.fixed.parallelism' and initial
-                        value ' -->
+                        junit.jupiter.execution.parallel.config.strategy = fixed
                         junit.jupiter.execution.parallel.config.fixed.parallelism = 1
-                        junit.jupiter.execution.parallel.config.fixed.max-pool-size = 1 </configurationParameters>
+                        junit.jupiter.execution.parallel.config.fixed.max-pool-size = 1
+                    </configurationParameters>

[Suggestion processed]

Suggestion importance[1-10]: 9

__

Why: The suggestion correctly identifies a malformed XML comment within the <configurationParameters> tag, which would likely break the surefire plugin's configuration parsing and cause a build failure.

High
  • Update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants