Skip to content

Commit

Permalink
Adding ability to set alert credentials via remote
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jimevans committed Jul 15, 2015
1 parent 82debdc commit 87f28e8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
3 changes: 3 additions & 0 deletions java/client/src/org/openqa/selenium/Alert.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public interface Alert {

void sendKeys(String keysToSend);

@Beta
void setCredentials(Credentials credentials);

/**
* Authenticate an HTTP Basic Auth dialog.
*
Expand Down
11 changes: 8 additions & 3 deletions java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Void> implements JsonParametersAware {
private String username;
private String password;

public SetAlertCredentials(Session session) {
super(session);
}

public void setJsonParameters(Map<String, Object> 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]";
}
}

0 comments on commit 87f28e8

Please sign in to comment.