Skip to content

Commit 87f28e8

Browse files
committed
Adding ability to set alert credentials via remote
This change is twofold. First, it adds a method to the Alert interface in the Java language bindings to enable the ability to set credentials without implicitly clicking OK on the authentication alert. The first change enables the second, which is to expose the 'set alert credentials' wire protocol end point to users of the Java remote server.
1 parent 82debdc commit 87f28e8

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

Diff for: java/client/src/org/openqa/selenium/Alert.java

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public interface Alert {
2828

2929
void sendKeys(String keysToSend);
3030

31+
@Beta
32+
void setCredentials(Credentials credentials);
33+
3134
/**
3235
* Authenticate an HTTP Basic Auth dialog.
3336
*

Diff for: java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,13 @@ public void sendKeys(String keysToSend) {
934934
execute(DriverCommand.SET_ALERT_VALUE, ImmutableMap.of("text", keysToSend));
935935
}
936936

937+
@Beta
938+
public void setCredentials(Credentials credentials) {
939+
execute(DriverCommand.SET_ALERT_CREDENTIALS, ImmutableMap
940+
.of("username", credentials.getUserPrincipal().getName(), "password",
941+
credentials.getPassword()));
942+
}
943+
937944
/**
938945
* Authenticate an HTTP Basic Auth dialog.
939946
* Implicitly 'clicks ok'
@@ -944,9 +951,7 @@ public void sendKeys(String keysToSend) {
944951
*/
945952
@Beta
946953
public void authenticateUsing(Credentials credentials) {
947-
execute(DriverCommand.SET_ALERT_CREDENTIALS, ImmutableMap
948-
.of("username", credentials.getUserPrincipal().getName(), "password",
949-
credentials.getPassword()));
954+
this.setCredentials(credentials);
950955
this.accept();
951956
}
952957
}

Diff for: java/server/src/org/openqa/selenium/remote/server/JsonHttpCommandHandler.java

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
import org.openqa.selenium.remote.server.handler.Rotate;
8989
import org.openqa.selenium.remote.server.handler.SendKeys;
9090
import org.openqa.selenium.remote.server.handler.SetAlertText;
91+
import org.openqa.selenium.remote.server.handler.SetAlertCredentials;
9192
import org.openqa.selenium.remote.server.handler.SetScriptTimeout;
9293
import org.openqa.selenium.remote.server.handler.SetWindowPosition;
9394
import org.openqa.selenium.remote.server.handler.SetWindowSize;
@@ -204,6 +205,7 @@ private void setUpMappings() {
204205
addNewMapping(ACCEPT_ALERT, AcceptAlert.class);
205206
addNewMapping(GET_ALERT_TEXT, GetAlertText.class);
206207
addNewMapping(SET_ALERT_VALUE, SetAlertText.class);
208+
addNewMapping(SET_ALERT_CREDENTIALS, SetAlertCredentials.class);
207209

208210
addNewMapping(GET, ChangeUrl.class);
209211
addNewMapping(GET_CURRENT_URL, GetCurrentUrl.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.remote.server.handler;
19+
20+
import org.apache.http.auth.Credentials;
21+
import org.apache.http.auth.UsernamePasswordCredentials;
22+
23+
import org.openqa.selenium.remote.server.JsonParametersAware;
24+
import org.openqa.selenium.remote.server.Session;
25+
26+
import java.util.Map;
27+
28+
public class SetAlertCredentials extends WebDriverHandler<Void> implements JsonParametersAware {
29+
private String username;
30+
private String password;
31+
32+
public SetAlertCredentials(Session session) {
33+
super(session);
34+
}
35+
36+
public void setJsonParameters(Map<String, Object> allParameters) throws Exception {
37+
username = (String) allParameters.get("username");
38+
password = (String) allParameters.get("password");
39+
}
40+
41+
@Override
42+
public Void call() throws Exception {
43+
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
44+
getDriver().switchTo().alert().setCredentials(credentials);
45+
return null;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return "[set alert credentials]";
51+
}
52+
}

0 commit comments

Comments
 (0)