Skip to content

[java] fix "or" condition#17135

Merged
asolntsev merged 1 commit into
SeleniumHQ:trunkfrom
asolntsev:fix/17091-or-condition
Feb 24, 2026
Merged

[java] fix "or" condition#17135
asolntsev merged 1 commit into
SeleniumHQ:trunkfrom
asolntsev:fix/17091-or-condition

Conversation

@asolntsev
Copy link
Copy Markdown
Contributor

@asolntsev asolntsev commented Feb 24, 2026

We need to re-try in case of NoSuchElementException, not only StaleElementReferenceException.

@ammmze FYI

🔗 Related Issues

Fixes #17091

💥 What does this PR do?

  1. Fixes ExpectedConditions.or: now it does re-try when some of conditions throws NoSuchElementException.
  2. Improves the error message generated by or method (adds actual value or exception to every condition in or).

🔧 Implementation Notes

NoSuchElementException may be thrown by a page object because its @findby fields are lazy-initialized.

🔄 Types of changes

  • Bug fix (backwards compatible)

@asolntsev asolntsev changed the title fix "or" condition [java] fix "or" condition Feb 24, 2026
@qodo-code-review
Copy link
Copy Markdown
Contributor

PR Type

Bug fix


Description

  • Fixes ExpectedConditions.or() to retry on NoSuchElementException

  • Improves error messages with actual values and exceptions

  • Extends exception handling in multiple expected conditions

  • Captures and displays results for better debugging


File Walkthrough

Relevant files
Bug fix
ExpectedConditions.java
Enhanced or condition and exception handling                         

java/src/org/openqa/selenium/support/ui/ExpectedConditions.java

  • Modified or() method to catch RuntimeException instead of only
    StaleElementReferenceException, enabling retry on
    NoSuchElementException
  • Added results array to track condition outcomes for improved error
    messaging
  • Enhanced toString() method to display actual values or exceptions for
    each condition
  • Updated exception handling in textToBePresentInElement(),
    textToBePresentInElementValue(), elementToBeStale(), and refreshed()
    methods to catch both StaleElementReferenceException and
    NoSuchElementException
  • Changed error field type from StaleElementReferenceException to
    WebDriverException for broader exception handling
+28/-9   
Tests
ExpectedConditionsTest.java
Update or condition tests for NoSuchElementException         

java/test/org/openqa/selenium/support/ui/ExpectedConditionsTest.java

  • Updated test case whenOneThrows() to throw NoSuchElementException
    instead of StaleElementReferenceException
  • Updated test case whenAllThrow() to throw NoSuchElementException for
    first condition
  • Updated expected error message assertion to match new error message
    format with exception details
+4/-4     

@selenium-ci selenium-ci added C-java Java Bindings B-support Issue or PR related to support classes labels Feb 24, 2026
@asolntsev asolntsev self-assigned this Feb 24, 2026
@asolntsev asolntsev added this to the 4.42.0 milestone Feb 24, 2026
@selenium-ci
Copy link
Copy Markdown
Member

Thank you, @asolntsev for this code suggestion.

The support packages contain example code that many users find helpful, but they do not necessarily represent
the best practices for using Selenium, and the Selenium team is not currently merging changes to them.

After reviewing the change, unless it is a critical fix or a feature that is needed for Selenium
to work, we will likely close the PR.

We actively encourage people to add the wrapper and helper code that makes sense for them to their own frameworks.
If you have any questions, please contact us

@qodo-code-review
Copy link
Copy Markdown
Contributor

qodo-code-review Bot commented Feb 24, 2026

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
Sensitive data exposure

Description: The updated or(...) condition now records and prints per-condition "actual" results and
exception details (including exception messages) via toString()/resultAsString(...), which
can expose sensitive application data (e.g., element text/attribute values or error
messages containing PII/secrets) into logs/test reports when a wait times out.
ExpectedConditions.java [1680-1721]

Referred Code
@Nullable final Object[] results = new Object[conditions.length];

@Override
public Boolean apply(WebDriver driver) {
  for (int i = 0; i != conditions.length; ++i) {
    ExpectedCondition<?> condition = conditions[i];
    results[i] = null;
    try {
      Object result = condition.apply(driver);
      if (Boolean.TRUE.equals(result) || result != null && !(result instanceof Boolean)) {
        return true;
      }
      results[i] = result;
    } catch (RuntimeException e) {
      results[i] = e;
    }
  }
  return false;
}

@Override


 ... (clipped 21 lines)
Ticket Compliance
🎫 No ticket provided
  • Create ticket/issue
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

🔴
Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status:
Overbroad exception catch: or(...) now catches and suppresses all RuntimeExceptions, potentially hiding real
programming errors and making failures harder to detect beyond a later timeout.

Referred Code
} catch (RuntimeException e) {
  results[i] = e;
}

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status:
Exception details exposed: The updated or(...) diagnostic message embeds exception short descriptions (type/message),
which may be surfaced to end users via TimeoutException and could leak internal details
depending on upstream usage.

Referred Code
private Object resultAsString(@Nullable Object result) {
  return result == Boolean.FALSE
      ? ""
      : result instanceof WebDriverException
          ? " (caused by: " + shortDescription((WebDriverException) result) + ")"
          : " (actual: " + result + ")";
}

Learn more about managing compliance generic rules or creating your own custom rules

  • Update
Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@asolntsev asolntsev added I-defect Something is not working as intended I-regression Something was working but we "fixed" it labels Feb 24, 2026
@qodo-code-review
Copy link
Copy Markdown
Contributor

qodo-code-review Bot commented Feb 24, 2026

PR Code Suggestions ✨

No code suggestions found for the PR.

We need to re-try in case of `NoSuchElementException`, not only `StaleElementReferenceException`.

`NoSuchElementException` may be thrown by a page object because its @findby fields are lazy-initialized.

Fixes SeleniumHQ#17091
@asolntsev asolntsev force-pushed the fix/17091-or-condition branch from a6c954b to 58bbb1d Compare February 24, 2026 13:45
@ammmze
Copy link
Copy Markdown
Contributor

ammmze commented Feb 24, 2026

Thank you!

@asolntsev asolntsev merged commit 2ff06ec into SeleniumHQ:trunk Feb 24, 2026
44 checks passed
@asolntsev asolntsev deleted the fix/17091-or-condition branch February 24, 2026 19:25
AutomatedTester pushed a commit that referenced this pull request Mar 11, 2026
fix "or" condition

We need to re-try in case of `NoSuchElementException`, not only `StaleElementReferenceException`.

`NoSuchElementException` may be thrown by a page object because its @findby fields are lazy-initialized.

Fixes #17091
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

B-support Issue or PR related to support classes C-java Java Bindings I-defect Something is not working as intended I-regression Something was working but we "fixed" it Review effort 2/5

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[🐛 Bug]: [java] ExpectedConditions.or exits early on exceptions

3 participants