Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,7 @@ public boolean exists() {
"Cannot check existence of component [%s] with no underlying element"
.formatted(this.getClass().getSimpleName()));
}
boolean exists = true;
try {
this.underlying.isEnabled();
} catch (NoSuchElementException | TimeoutException | StaleElementReferenceException e) {
exists = false;
}
boolean exists = this.underlying.exists();

logger.debug("{} {}.", this.getClass().getSimpleName(), exists ? "exists" : "does not exist");
return exists;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,7 @@ public Point getLocation() {

@Override
public boolean exists() {
boolean exists = true;
try {
this.underlying.isEnabled();
} catch (NoSuchElementException | TimeoutException | StaleElementReferenceException e) {
exists = false;
}
boolean exists = this.underlying.exists();

logger.debug("{} {}.", this.getClass().getSimpleName(), exists ? "exists" : "does not exist");
return exists;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,11 +556,21 @@ public Duration getWaitPollingInterval() {
return this.pollingInterval;
}

/**
* Checks if the element exists in the DOM. This is done by calling isEnabled() and catching any
* exceptions that indicate the element isn't present.
*/
@Override
@SuppressWarnings(
"PMD.LambdaCanBeMethodReference") // Lambda required to defer null check for lazy-loading
public boolean exists() {
return runLazily(() -> this.underlying.isEnabled());
boolean exists = true;
try {
this.isEnabled();
} catch (NoSuchElementException | TimeoutException | StaleElementReferenceException e) {
exists = false;
}

logger.debug("{} {}.", this.getClass().getSimpleName(), exists ? "exists" : "does not exist");
return exists;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ private PageObjectContext getMockDriver() {
when(mockDriver.findElements(By.id("top-grand-child"))).thenReturn(List.of(grandChildMock));
when(mockDriver.findElement(By.id("%s"))).thenThrow(new NoSuchElementException(""));
when(mockDriver.findElements(By.id("%s"))).thenThrow(new NoSuchElementException(""));
when(mockDriver.findElement(By.id("no-such-element")))
.thenThrow(new NoSuchElementException(""));

final var jQueryMock = mock(WebElement.class);
when(jQueryMock.getAttribute("id")).thenReturn("fake-id");
Expand Down Expand Up @@ -464,4 +466,21 @@ public void testElementWithJavaScript() {
logger.info("STEP 3: Get an attribute from the element, finding it on the page.");
assertEquals(element.getAttribute("id"), "fake-id");
}

/**
* Validate that the exists() function does not throw a no such element exception when the element
* is not found, and instead returns false.
*/
@Test
public void testExistsNoSuchElement() {
logger.info("STEP 1: Create a Locator for a non-existent element.");
Locator locator = new Locator(LocatedBy.id("no-such-element"), this.context.getPlatform());

logger.info("STEP 2: Create a LazyWebElement with the Locator.");
LazyWebElement lazy = new LazyWebElement(locator, this.context);

logger.info(
"STEP 3: Call exists() on the LazyWebElement, which should return false without throwing an exception.");
assertFalse(lazy.exists());
}
}