Skip to content

Commit

Permalink
[java] Fixing sendKeys to throw if it sees a null somewhere
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Aug 9, 2018
1 parent d9f0937 commit 7c63cea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class RemoteWebElement implements WebElement, FindsByLinkText, FindsById, FindsByName,
FindsByTagName, FindsByClassName, FindsByCssSelector,
Expand Down Expand Up @@ -88,9 +89,14 @@ public void submit() {
}

public void sendKeys(CharSequence... keysToSend) {
if (keysToSend == null) {
if (keysToSend == null || keysToSend.length == 0) {
throw new IllegalArgumentException("Keys to send should be a not null CharSequence");
}
for (CharSequence cs : keysToSend) {
if (cs == null) {
throw new IllegalArgumentException("Keys to send should be a not null CharSequence");
}
}
File localFile = fileDetector.getLocalFile(keysToSend);
if (localFile != null) {
String remotePath = upload(localFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,39 @@ public void testBasicKeyboardInputOnActiveElement() {
}

@Test
public void testThrowsIllegalArgumentExceptionWithNullKeys() {
public void testThrowsIllegalArgumentExceptionWithNoParameters() {
driver.get(pages.javascriptPage);
Throwable t = catchThrowable(
() -> driver.findElement(By.id("keyReporter")).sendKeys());
assertThat(t, instanceOf(IllegalArgumentException.class));
}

@Test
public void testThrowsIllegalArgumentExceptionWithNullParameter() {
driver.get(pages.javascriptPage);
Throwable t = catchThrowable(
() -> driver.findElement(By.id("keyReporter")).sendKeys(null));
assertThat(t, instanceOf(IllegalArgumentException.class));
}

@Test
public void testThrowsIllegalArgumentExceptionWithNullInParameters() {
driver.get(pages.javascriptPage);
Throwable t = catchThrowable(
() -> driver.findElement(By.id("keyReporter")).sendKeys("x", null, "y"));
assertThat(t, instanceOf(IllegalArgumentException.class));
}

@Test
public void testThrowsIllegalArgumentExceptionWithCharSequenceThatContainsNull() {
driver.get(pages.javascriptPage);
Throwable t = catchThrowable(
() -> driver.findElement(By.id("keyReporter")).sendKeys(new CharSequence[] {"x", null, "y"}));
assertThat(t, instanceOf(IllegalArgumentException.class));
}

@Test
public void testThrowsIllegalArgumentExceptionWithCharSequenceThatContainsNullOnly() {
driver.get(pages.javascriptPage);
Throwable t = catchThrowable(
() -> driver.findElement(By.id("keyReporter")).sendKeys(new CharSequence[] {null}));
Expand Down

0 comments on commit 7c63cea

Please sign in to comment.