Skip to content

Commit d69bc71

Browse files
committed
[java] Adding unit tests for RemoteWebDriver that cover the case of finding elements using non-standard By implementations, in this case we should delegate search to the By object and don't call the command executor.
1 parent 87aa2b8 commit d69bc71

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,10 @@ protected WebElement findElement(String by, String using) {
355355
if (value == null) { // see https://github.com/SeleniumHQ/selenium/issues/5809
356356
throw new NoSuchElementException(String.format("Cannot locate an element using %s=%s", by, using));
357357
}
358-
WebElement element;
359-
try {
360-
element = (WebElement) value;
361-
} catch (ClassCastException ex) {
362-
throw new WebDriverException("Returned value cannot be converted to WebElement: " + value, ex);
358+
if (!(value instanceof WebElement)) {
359+
throw new WebDriverException("Returned value cannot be converted to WebElement: " + value);
363360
}
361+
WebElement element = (WebElement) value;
364362
setFoundBy(this, element, by, using);
365363
return element;
366364
}

java/client/test/org/openqa/selenium/remote/RemoteWebDriverUnitTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.openqa.selenium.Platform;
4848
import org.openqa.selenium.Point;
4949
import org.openqa.selenium.Rectangle;
50+
import org.openqa.selenium.SearchContext;
5051
import org.openqa.selenium.SessionNotCreatedException;
5152
import org.openqa.selenium.WebDriver;
5253
import org.openqa.selenium.WebElement;
@@ -293,6 +294,25 @@ public void canHandleFindElementW3CCommand() throws IOException {
293294
"using", "id", "value", "cheese")));
294295
}
295296

297+
@Test
298+
public void canHandleFindElementCommandWithNonStandardLocator() throws IOException {
299+
WebElement element1 = mock(WebElement.class);
300+
WebElement element2 = mock(WebElement.class);
301+
By locator = new By() {
302+
@Override
303+
public List<WebElement> findElements(SearchContext context) {
304+
return Arrays.asList(element1, element2);
305+
}
306+
};
307+
CommandExecutor executor = prepareExecutorMock(echoCapabilities);
308+
309+
RemoteWebDriver driver = new RemoteWebDriver(executor, new ImmutableCapabilities());
310+
WebElement found = driver.findElement(locator);
311+
312+
assertThat(found).isSameAs(element1);
313+
verifyCommands(executor, driver.getSessionId());
314+
}
315+
296316
@Test
297317
public void canHandleFindElementsOSSCommand() throws IOException {
298318
CommandExecutor executor = prepareExecutorMock(
@@ -329,6 +349,25 @@ public void canHandleFindElementsW3CCommand() throws IOException {
329349
"using", "id", "value", "cheese")));
330350
}
331351

352+
@Test
353+
public void canHandleFindElementsCommandWithNonStandardLocator() throws IOException {
354+
WebElement element1 = mock(WebElement.class);
355+
WebElement element2 = mock(WebElement.class);
356+
By locator = new By() {
357+
@Override
358+
public List<WebElement> findElements(SearchContext context) {
359+
return Arrays.asList(element1, element2);
360+
}
361+
};
362+
CommandExecutor executor = prepareExecutorMock(echoCapabilities);
363+
364+
RemoteWebDriver driver = new RemoteWebDriver(executor, new ImmutableCapabilities());
365+
List<WebElement> found = driver.findElements(locator);
366+
367+
assertThat(found).containsExactly(element1, element2);
368+
verifyCommands(executor, driver.getSessionId());
369+
}
370+
332371
@Test
333372
public void returnsEmptyListIfRemoteEndReturnsNullFromFindElements() {
334373
CommandExecutor executor = prepareExecutorMock(echoCapabilities, nullValueResponder);

0 commit comments

Comments
 (0)