Skip to content

Commit

Permalink
[java] Enhance error message for NoSuchElementException and return un…
Browse files Browse the repository at this point in the history
…modifiable set for pinned scripts (#14707)

Co-authored-by: Puja Jagani <[email protected]>
  • Loading branch information
iampopovich and pujagani authored Nov 5, 2024
1 parent cd7303c commit 4a0d05e
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 8 deletions.
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/By.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static By cssSelector(String cssSelector) {
public WebElement findElement(SearchContext context) {
List<WebElement> 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);
}
Expand Down
8 changes: 3 additions & 5 deletions java/src/org/openqa/selenium/JavascriptExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -177,10 +176,9 @@ default void unpin(ScriptKey key) {
* @return The {@link ScriptKey}s of all currently pinned scripts.
*/
default Set<ScriptKey> 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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ByChained(By... bys) {
public WebElement findElement(SearchContext context) {
List<WebElement> 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);
}

Expand Down

0 comments on commit 4a0d05e

Please sign in to comment.