From 4a0d05e50ea1750482211e04ece8062436eb5c6b Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 5 Nov 2024 16:17:57 +0700 Subject: [PATCH] [java] Enhance error message for NoSuchElementException and return unmodifiable set for pinned scripts (#14707) Co-authored-by: Puja Jagani --- java/src/org/openqa/selenium/By.java | 2 +- java/src/org/openqa/selenium/JavascriptExecutor.java | 8 +++----- .../org/openqa/selenium/support/pagefactory/ByAll.java | 2 +- .../openqa/selenium/support/pagefactory/ByChained.java | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/java/src/org/openqa/selenium/By.java b/java/src/org/openqa/selenium/By.java index 7a03e34bf5b0b..5ed367d994a09 100644 --- a/java/src/org/openqa/selenium/By.java +++ b/java/src/org/openqa/selenium/By.java @@ -121,7 +121,7 @@ public static By cssSelector(String cssSelector) { public WebElement findElement(SearchContext context) { List allElements = findElements(context); if (allElements == null || allElements.isEmpty()) { - throw new NoSuchElementException("Cannot locate an element using " + toString()); + throw new NoSuchElementException("Cannot locate an element using " + this); } return allElements.get(0); } diff --git a/java/src/org/openqa/selenium/JavascriptExecutor.java b/java/src/org/openqa/selenium/JavascriptExecutor.java index a744eb8d8df91..056935aaa4a8b 100644 --- a/java/src/org/openqa/selenium/JavascriptExecutor.java +++ b/java/src/org/openqa/selenium/JavascriptExecutor.java @@ -17,7 +17,6 @@ package org.openqa.selenium; -import java.util.Collections; import java.util.Set; import java.util.stream.Collectors; import org.jspecify.annotations.NullMarked; @@ -177,10 +176,9 @@ default void unpin(ScriptKey key) { * @return The {@link ScriptKey}s of all currently pinned scripts. */ default Set getPinnedScripts() { - return Collections.unmodifiableSet( - UnpinnedScriptKey.getPinnedScripts(this).stream() - .map(key -> (ScriptKey) key) - .collect(Collectors.toSet())); + return UnpinnedScriptKey.getPinnedScripts(this).stream() + .map(key -> (ScriptKey) key) + .collect(Collectors.toUnmodifiableSet()); } /** diff --git a/java/src/org/openqa/selenium/support/pagefactory/ByAll.java b/java/src/org/openqa/selenium/support/pagefactory/ByAll.java index 0a9e0988bc0fc..8ee9e0bee71d5 100644 --- a/java/src/org/openqa/selenium/support/pagefactory/ByAll.java +++ b/java/src/org/openqa/selenium/support/pagefactory/ByAll.java @@ -54,7 +54,7 @@ public WebElement findElement(SearchContext context) { return elements.get(0); } } - throw new NoSuchElementException("Cannot locate an element using " + toString()); + throw new NoSuchElementException("Cannot locate an element using " + this); } @Override diff --git a/java/src/org/openqa/selenium/support/pagefactory/ByChained.java b/java/src/org/openqa/selenium/support/pagefactory/ByChained.java index 5d631cc254c24..2beb51681c1b5 100644 --- a/java/src/org/openqa/selenium/support/pagefactory/ByChained.java +++ b/java/src/org/openqa/selenium/support/pagefactory/ByChained.java @@ -52,7 +52,7 @@ public ByChained(By... bys) { public WebElement findElement(SearchContext context) { List elements = findElements(context); if (elements.isEmpty()) - throw new NoSuchElementException("Cannot locate an element using " + toString()); + throw new NoSuchElementException("Cannot locate an element using " + this); return elements.get(0); }