Skip to content

Commit

Permalink
[java] Handling a special case of a driver returning null as a result…
Browse files Browse the repository at this point in the history
… of findElement command. Fixes #5809
  • Loading branch information
barancev committed Sep 23, 2018
1 parent 4e6f011 commit 5fc802d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
Expand Up @@ -36,6 +36,7 @@
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.NoSuchFrameException;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.OutputType;
Expand Down Expand Up @@ -322,6 +323,9 @@ protected WebElement findElement(String by, String using) {
Response response = execute(DriverCommand.FIND_ELEMENT,
ImmutableMap.of("using", by, "value", using));
Object value = response.getValue();
if (value == null) { // see https://github.com/SeleniumHQ/selenium/issues/5809
throw new NoSuchElementException(String.format("Cannot locate an element using %s=%s", by, using));
}
WebElement element;
try {
element = (WebElement) value;
Expand Down
Expand Up @@ -22,6 +22,7 @@
import org.openqa.selenium.Beta;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.Rectangle;
Expand Down Expand Up @@ -185,6 +186,9 @@ protected WebElement findElement(String using, String value) {
ImmutableMap.of("id", id, "using", using, "value", value));

Object responseValue = response.getValue();
if (responseValue == null) { // see https://github.com/SeleniumHQ/selenium/issues/5809
throw new NoSuchElementException(String.format("Cannot locate an element using %s=%s", using, value));
}
WebElement element;
try {
element = (WebElement) responseValue;
Expand Down Expand Up @@ -346,6 +350,7 @@ public Dimension getSize() {
return new Dimension(width, height);
}

@SuppressWarnings({"unchecked"})
public Rectangle getRect() {
Response response = execute(DriverCommand.GET_ELEMENT_RECT, ImmutableMap.of("id", id));
Map<String, Object> rawRect = (Map<String, Object>) response.getValue();
Expand Down
1 change: 1 addition & 0 deletions java/client/test/org/openqa/selenium/remote/BUCK
Expand Up @@ -30,6 +30,7 @@ java_test(name = 'client-tests',
'RemoteClientTests.java',
'RemoteLogsTest.java',
'RemoteWebDriverInitializationTest.java',
'RemoteWebDriverUnitTest.java',
'W3CHandshakeResponseTest.java',
'W3CRemoteDriverTest.java',
'internal/ApacheHttpClientTest.java',
Expand Down
Expand Up @@ -30,6 +30,7 @@
ProtocolHandshakeTest.class,
RemoteLogsTest.class,
RemoteWebDriverInitializationTest.class,
RemoteWebDriverUnitTest.class,
W3CHandshakeResponseTest.class
})
public class RemoteClientTests {
Expand Down
@@ -0,0 +1,98 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.remote;

import static java.util.Collections.EMPTY_MAP;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;

import java.io.IOException;
import java.util.List;

public class RemoteWebDriverUnitTest {

@Test
public void returnsEmptyListIfRemoteEndReturnsNullFromFindElements() throws IOException {
CommandExecutor executor = prepareExecutorMock();

RemoteWebDriver driver = new RemoteWebDriver(executor, new ImmutableCapabilities());
List<WebElement> result = driver.findElements(By.id("id"));
assertThat(result).isNotNull().isEmpty();
}

@Test
public void returnsEmptyListIfRemoteEndReturnsNullFromFindChildren() throws IOException {
CommandExecutor executor = prepareExecutorMock();

RemoteWebDriver driver = new RemoteWebDriver(executor, new ImmutableCapabilities());
RemoteWebElement element = new RemoteWebElement();
element.setParent(driver);
element.setId("unique");

List<WebElement> result = element.findElements(By.id("id"));
assertThat(result).isNotNull().isEmpty();
}

@Test
public void throwsIfRemoteEndReturnsNullFromFindElement() throws IOException {
CommandExecutor executor = prepareExecutorMock();

RemoteWebDriver driver = new RemoteWebDriver(executor, new ImmutableCapabilities());
assertThatExceptionOfType(NoSuchElementException.class)
.isThrownBy(() -> driver.findElement(By.id("id")));
}

@Test
public void throwIfRemoteEndReturnsNullFromFindChild() throws IOException {
CommandExecutor executor = prepareExecutorMock();

RemoteWebDriver driver = new RemoteWebDriver(executor, new ImmutableCapabilities());
RemoteWebElement element = new RemoteWebElement();
element.setParent(driver);
element.setId("unique");

assertThatExceptionOfType(NoSuchElementException.class)
.isThrownBy(() -> element.findElement(By.id("id")));
}

private CommandExecutor prepareExecutorMock() throws IOException {
CommandExecutor executor = mock(CommandExecutor.class);
when(executor.execute(any())).thenAnswer(invocation -> {
if (invocation.<Command>getArgument(0).getName().equals(DriverCommand.NEW_SESSION)) {
Response newSessionResponse = new Response();
newSessionResponse.setValue(EMPTY_MAP);
return newSessionResponse;
} else {
Response nullResponse = new Response();
nullResponse.setValue(null);
return nullResponse;
}
});
return executor;
}

}

0 comments on commit 5fc802d

Please sign in to comment.