Skip to content

Commit

Permalink
Handle null argument in sendKeys
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Stewart <simon.m.stewart@gmail.com>
  • Loading branch information
glibas authored and shs96c committed Aug 8, 2017
1 parent f943605 commit 73ce767
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions java/client/src/org/openqa/selenium/WebElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public interface WebElement extends SearchContext, TakesScreenshot {
* Use this method to simulate typing into an element, which may set its value.
*
* @param keysToSend character sequence to send to the element
*
* @throws IllegalArgumentException if keysToSend is null
*/
void sendKeys(CharSequence... keysToSend);

Expand Down
7 changes: 7 additions & 0 deletions java/client/src/org/openqa/selenium/interactions/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ public Actions keyUp(WebElement target, CharSequence key) {
*
* @param keys The keys.
* @return A self reference.
*
* @throws IllegalArgumentException if keys is null
*/
public Actions sendKeys(CharSequence... keys) {
if (isBuildingActions()) {
Expand All @@ -204,6 +206,8 @@ public Actions sendKeys(CharSequence... keys) {
* @param target element to focus on.
* @param keys The keys.
* @return A self reference.
*
* @throws IllegalArgumentException if keys is null
*/
public Actions sendKeys(WebElement target, CharSequence... keys) {
if (isBuildingActions()) {
Expand All @@ -223,6 +227,9 @@ private Keys asKeys(CharSequence key) {
}

private Actions sendKeysInTicks(CharSequence... keys) {
if (keys == null) {
throw new IllegalArgumentException("Keys should be a not null CharSequence");
}
for (CharSequence key : keys) {
key.codePoints().forEach(codePoint -> {
tick(defaultKeyboard.createKeyDown(codePoint));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public interface Keyboard {
*
* @param keysToSend one or more sequences of characters or key representations to type on the
* keyboard
* @throws IllegalArgumentException if keysToSend is null
*/
void sendKeys(CharSequence... keysToSend);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public RemoteKeyboard(ExecuteMethod executor) {
}

public void sendKeys(CharSequence... keysToSend) {
if(keysToSend==null) {
throw new IllegalArgumentException("Keys to send should be a not null CharSequence");
}
executor.execute(DriverCommand.SEND_KEYS_TO_ACTIVE_ELEMENT,
ImmutableMap.of("value", keysToSend));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,15 @@ public String getText() {
return (String) execute(DriverCommand.GET_ALERT_TEXT).getValue();
}

/**
* @param keysToSend character sequence to send to the alert
*
* @throws IllegalArgumentException if keysToSend is null
*/
public void sendKeys(String keysToSend) {
if(keysToSend==null) {
throw new IllegalArgumentException("Keys to send should be a not null CharSequence");
}
execute(DriverCommand.SET_ALERT_VALUE, ImmutableMap.of("text", keysToSend));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public void submit() {
}

public void sendKeys(CharSequence... keysToSend) {
if(keysToSend==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
9 changes: 9 additions & 0 deletions java/client/test/org/openqa/selenium/AlertsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ public void testShouldAllowUsersToAcceptAnAlertManually() {
assertEquals("Testing Alerts", driver.getTitle());
}

@Test(expected = IllegalArgumentException.class)
public void testShouldThrowIllegalArgumentExceptionWhenKeysNull() {
driver.get(alertPage("cheese"));

driver.findElement(By.id("alert")).click();
Alert alert = wait.until(alertIsPresent());
alert.sendKeys(null);
}

@Test
public void testShouldAllowUsersToAcceptAnAlertWithNoTextManually() {
driver.get(alertPage(""));
Expand Down
7 changes: 7 additions & 0 deletions java/client/test/org/openqa/selenium/TypingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,13 @@ public void testShouldBeAbleToTypeOnANumberInputField() {
assertThat(email.getAttribute("value"), equalTo("33"));
}

@Test(expected = IllegalArgumentException.class)
public void testShouldThrowIllegalArgumentException() {
driver.get(pages.formPage);
WebElement email = driver.findElement(By.id("age"));
email.sendKeys(null);
}

@Test
@Ignore(SAFARI)
public void canSafelyTypeOnElementThatIsRemovedFromTheDomOnKeyPress() {
Expand Down
11 changes: 11 additions & 0 deletions java/client/test/org/openqa/selenium/interactions/ActionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ public void creatingAllKeyboardActions() {
order.verifyNoMoreInteractions();
}


@Test(expected = IllegalArgumentException.class)
public void throwsIllegalArgumentExceptionIfKeysNull() {
new Actions(driver).sendKeys(null).perform();
}

@Test(expected = IllegalArgumentException.class)
public void throwsIllegalArgumentExceptionOverridenIfKeysNull() {
new Actions(driver).sendKeys(dummyLocatableElement,null).perform();
}

@Test
public void providingAnElementToKeyboardActions() {
new Actions(driver).keyDown(dummyLocatableElement, Keys.SHIFT).perform();
Expand Down

0 comments on commit 73ce767

Please sign in to comment.