diff --git a/java/client/src/org/openqa/selenium/Alert.java b/java/client/src/org/openqa/selenium/Alert.java index 655e694fad135..e74b8f2017fa9 100644 --- a/java/client/src/org/openqa/selenium/Alert.java +++ b/java/client/src/org/openqa/selenium/Alert.java @@ -28,6 +28,9 @@ public interface Alert { void sendKeys(String keysToSend); + @Beta + void setCredentials(Credentials credentials); + /** * Authenticate an HTTP Basic Auth dialog. * diff --git a/java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java b/java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java index 5ac7f970e7cf9..38df47be85d46 100644 --- a/java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java +++ b/java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java @@ -934,6 +934,13 @@ public void sendKeys(String keysToSend) { execute(DriverCommand.SET_ALERT_VALUE, ImmutableMap.of("text", keysToSend)); } + @Beta + public void setCredentials(Credentials credentials) { + execute(DriverCommand.SET_ALERT_CREDENTIALS, ImmutableMap + .of("username", credentials.getUserPrincipal().getName(), "password", + credentials.getPassword())); + } + /** * Authenticate an HTTP Basic Auth dialog. * Implicitly 'clicks ok' @@ -944,9 +951,7 @@ public void sendKeys(String keysToSend) { */ @Beta public void authenticateUsing(Credentials credentials) { - execute(DriverCommand.SET_ALERT_CREDENTIALS, ImmutableMap - .of("username", credentials.getUserPrincipal().getName(), "password", - credentials.getPassword())); + this.setCredentials(credentials); this.accept(); } } diff --git a/java/server/src/org/openqa/selenium/remote/server/JsonHttpCommandHandler.java b/java/server/src/org/openqa/selenium/remote/server/JsonHttpCommandHandler.java index f8cd94cec318b..0b238b26ffd5c 100644 --- a/java/server/src/org/openqa/selenium/remote/server/JsonHttpCommandHandler.java +++ b/java/server/src/org/openqa/selenium/remote/server/JsonHttpCommandHandler.java @@ -88,6 +88,7 @@ import org.openqa.selenium.remote.server.handler.Rotate; import org.openqa.selenium.remote.server.handler.SendKeys; import org.openqa.selenium.remote.server.handler.SetAlertText; +import org.openqa.selenium.remote.server.handler.SetAlertCredentials; import org.openqa.selenium.remote.server.handler.SetScriptTimeout; import org.openqa.selenium.remote.server.handler.SetWindowPosition; import org.openqa.selenium.remote.server.handler.SetWindowSize; @@ -204,6 +205,7 @@ private void setUpMappings() { addNewMapping(ACCEPT_ALERT, AcceptAlert.class); addNewMapping(GET_ALERT_TEXT, GetAlertText.class); addNewMapping(SET_ALERT_VALUE, SetAlertText.class); + addNewMapping(SET_ALERT_CREDENTIALS, SetAlertCredentials.class); addNewMapping(GET, ChangeUrl.class); addNewMapping(GET_CURRENT_URL, GetCurrentUrl.class); diff --git a/java/server/src/org/openqa/selenium/remote/server/handler/SetAlertCredentials.java b/java/server/src/org/openqa/selenium/remote/server/handler/SetAlertCredentials.java new file mode 100644 index 0000000000000..5a56ae616c276 --- /dev/null +++ b/java/server/src/org/openqa/selenium/remote/server/handler/SetAlertCredentials.java @@ -0,0 +1,52 @@ +// 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.server.handler; + +import org.apache.http.auth.Credentials; +import org.apache.http.auth.UsernamePasswordCredentials; + +import org.openqa.selenium.remote.server.JsonParametersAware; +import org.openqa.selenium.remote.server.Session; + +import java.util.Map; + +public class SetAlertCredentials extends WebDriverHandler implements JsonParametersAware { + private String username; + private String password; + + public SetAlertCredentials(Session session) { + super(session); + } + + public void setJsonParameters(Map allParameters) throws Exception { + username = (String) allParameters.get("username"); + password = (String) allParameters.get("password"); + } + + @Override + public Void call() throws Exception { + UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); + getDriver().switchTo().alert().setCredentials(credentials); + return null; + } + + @Override + public String toString() { + return "[set alert credentials]"; + } +}