Skip to content

Commit 73ce767

Browse files
glibasshs96c
authored andcommitted
Handle null argument in sendKeys
Signed-off-by: Simon Stewart <simon.m.stewart@gmail.com>
1 parent f943605 commit 73ce767

File tree

9 files changed

+51
-0
lines changed

9 files changed

+51
-0
lines changed

java/client/src/org/openqa/selenium/WebElement.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public interface WebElement extends SearchContext, TakesScreenshot {
6262
* Use this method to simulate typing into an element, which may set its value.
6363
*
6464
* @param keysToSend character sequence to send to the element
65+
*
66+
* @throws IllegalArgumentException if keysToSend is null
6567
*/
6668
void sendKeys(CharSequence... keysToSend);
6769

java/client/src/org/openqa/selenium/interactions/Actions.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ public Actions keyUp(WebElement target, CharSequence key) {
184184
*
185185
* @param keys The keys.
186186
* @return A self reference.
187+
*
188+
* @throws IllegalArgumentException if keys is null
187189
*/
188190
public Actions sendKeys(CharSequence... keys) {
189191
if (isBuildingActions()) {
@@ -204,6 +206,8 @@ public Actions sendKeys(CharSequence... keys) {
204206
* @param target element to focus on.
205207
* @param keys The keys.
206208
* @return A self reference.
209+
*
210+
* @throws IllegalArgumentException if keys is null
207211
*/
208212
public Actions sendKeys(WebElement target, CharSequence... keys) {
209213
if (isBuildingActions()) {
@@ -223,6 +227,9 @@ private Keys asKeys(CharSequence key) {
223227
}
224228

225229
private Actions sendKeysInTicks(CharSequence... keys) {
230+
if (keys == null) {
231+
throw new IllegalArgumentException("Keys should be a not null CharSequence");
232+
}
226233
for (CharSequence key : keys) {
227234
key.codePoints().forEach(codePoint -> {
228235
tick(defaultKeyboard.createKeyDown(codePoint));

java/client/src/org/openqa/selenium/interactions/Keyboard.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public interface Keyboard {
3434
*
3535
* @param keysToSend one or more sequences of characters or key representations to type on the
3636
* keyboard
37+
* @throws IllegalArgumentException if keysToSend is null
3738
*/
3839
void sendKeys(CharSequence... keysToSend);
3940

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public RemoteKeyboard(ExecuteMethod executor) {
3232
}
3333

3434
public void sendKeys(CharSequence... keysToSend) {
35+
if(keysToSend==null) {
36+
throw new IllegalArgumentException("Keys to send should be a not null CharSequence");
37+
}
3538
executor.execute(DriverCommand.SEND_KEYS_TO_ACTIVE_ELEMENT,
3639
ImmutableMap.of("value", keysToSend));
3740
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,15 @@ public String getText() {
10431043
return (String) execute(DriverCommand.GET_ALERT_TEXT).getValue();
10441044
}
10451045

1046+
/**
1047+
* @param keysToSend character sequence to send to the alert
1048+
*
1049+
* @throws IllegalArgumentException if keysToSend is null
1050+
*/
10461051
public void sendKeys(String keysToSend) {
1052+
if(keysToSend==null) {
1053+
throw new IllegalArgumentException("Keys to send should be a not null CharSequence");
1054+
}
10471055
execute(DriverCommand.SET_ALERT_VALUE, ImmutableMap.of("text", keysToSend));
10481056
}
10491057

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public void submit() {
8787
}
8888

8989
public void sendKeys(CharSequence... keysToSend) {
90+
if(keysToSend==null) {
91+
throw new IllegalArgumentException("Keys to send should be a not null CharSequence");
92+
}
9093
File localFile = fileDetector.getLocalFile(keysToSend);
9194
if (localFile != null) {
9295
String remotePath = upload(localFile);

java/client/test/org/openqa/selenium/AlertsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ public void testShouldAllowUsersToAcceptAnAlertManually() {
111111
assertEquals("Testing Alerts", driver.getTitle());
112112
}
113113

114+
@Test(expected = IllegalArgumentException.class)
115+
public void testShouldThrowIllegalArgumentExceptionWhenKeysNull() {
116+
driver.get(alertPage("cheese"));
117+
118+
driver.findElement(By.id("alert")).click();
119+
Alert alert = wait.until(alertIsPresent());
120+
alert.sendKeys(null);
121+
}
122+
114123
@Test
115124
public void testShouldAllowUsersToAcceptAnAlertWithNoTextManually() {
116125
driver.get(alertPage(""));

java/client/test/org/openqa/selenium/TypingTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,13 @@ public void testShouldBeAbleToTypeOnANumberInputField() {
617617
assertThat(email.getAttribute("value"), equalTo("33"));
618618
}
619619

620+
@Test(expected = IllegalArgumentException.class)
621+
public void testShouldThrowIllegalArgumentException() {
622+
driver.get(pages.formPage);
623+
WebElement email = driver.findElement(By.id("age"));
624+
email.sendKeys(null);
625+
}
626+
620627
@Test
621628
@Ignore(SAFARI)
622629
public void canSafelyTypeOnElementThatIsRemovedFromTheDomOnKeyPress() {

java/client/test/org/openqa/selenium/interactions/ActionsTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ public void creatingAllKeyboardActions() {
7070
order.verifyNoMoreInteractions();
7171
}
7272

73+
74+
@Test(expected = IllegalArgumentException.class)
75+
public void throwsIllegalArgumentExceptionIfKeysNull() {
76+
new Actions(driver).sendKeys(null).perform();
77+
}
78+
79+
@Test(expected = IllegalArgumentException.class)
80+
public void throwsIllegalArgumentExceptionOverridenIfKeysNull() {
81+
new Actions(driver).sendKeys(dummyLocatableElement,null).perform();
82+
}
83+
7384
@Test
7485
public void providingAnElementToKeyboardActions() {
7586
new Actions(driver).keyDown(dummyLocatableElement, Keys.SHIFT).perform();

0 commit comments

Comments
 (0)