we are trying to authenticate our website https://staging.rockettes.com. It asks for a user name and password, which we have to supply via our selenium java code. Can you help? Thanks, Rachit
-
1I think you need to ask specific questions. – alayor Jan 17 '17 at 17:31
-
1Please consider revising what you posted in this question. As it currently stands, its formatting and scope make it hard for us to help you; here is a [great resource](http://stackoverflow.com/help/mcve) to get you started on that – Daniel Corzo Jan 17 '17 at 18:11
-
Possible duplicate of [How to handle authentication popup with Selenium WebDriver using Java](http://stackoverflow.com/q/24304752/1072229) – Grisha Levit Jan 17 '17 at 18:51
-
I have actually tried with the ink above but not successful in passing values in the popup which we get on https://staging.rockettes.com. also it has to run on Saucelabs so I can't use Robots for Control actions e.g. ctrl+C / ctrl+V. – Jan 17 '17 at 19:15
6 Answers
You will need to construct a testURL before you call your driver.get command.
So assuming that the username=admin and pass=pass
String testURL = "https://" + "admin" + ":" + "pass" + "@" + "staging.rockettes.com/";
Now you can safely call your driver.get as following:
driver.get(testURL);
Best of luck!
Updated answer after op's comment:
Okay then, so in order to accept the alert you can use:
WebDriverWait waitTime = new WebDriverWait(driver, 5);
Boolean isAlertPresent = wait.until(ExpectedConditions.alertIsPresent());
if(isAlertPresent==true){
Alert alert = driver.switchTo().alert();
alertText = alert.getText();
alert.accept();
}
else{
System.out.println("No alert was present!")
}
- 1,831
- 21
- 22
-
Thank you I have done that but it's taking me to another screen with some message and asking me to click Yes/no. My query was inline to the fact that - Can I find the elements of popup and send login/pass by keys and then click on the button Yes/No etc. – Jan 17 '17 at 19:13
-
Okay Rachit, I've updated my answer, see if this helps and upvote if it does or comment here if it does not :) Best of luck in solving this! – Xwris Stoixeia Jan 18 '17 at 09:36
-
Actually it's also getting timed out. i've tried following so far: 1. Alert mechanism 2. passing Login/Pass in URL itself 3: Robots Class. Situation is little complex here - we have to open this URL in SauceConnect (Saucelabs.com) for running our test script. However SauceConnect forms a tunnel so it doesn't really entertain any of above 3 mechanisms. – Jan 22 '17 at 04:53
-
Send more info regarding this popup. Is it a browser alert or an actual browser popup (i.e. new window?). Please also send the html code if possible. – Xwris Stoixeia Jan 23 '17 at 10:06
I think that, the pop-up on hitting the ur URL is not web based alert rather it's a window alert, so you can handle it with the help of AutoIT or you can do this using Robot class as follows (though not recommended):
String userName = "ADMIN";
StringSelection stringSelection = new StringSelection(userName);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
String password = "PASS";
StringSelection stringSelection1 = new StringSelection(password);
Clipboard clipboard1 = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard1.setContents(stringSelection1, stringSelection1);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
- 3,349
- 5
- 23
- 46
-
sorry it's not working as we are using a tunnel and executing the script on a remote system so it's throwing an exemption as clipboard of that system might not be accessible. – Jan 22 '17 at 04:55
-
if your handling popups, use the following command before passing username and password
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
- 91
- 1
- 9
You can used any ways below:
1. By pass with URL:
String url= "https://" + "username" + ":" + "password" + "@" + "staging.rockettes.com";
driver.get(url);
2. Alert
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword("username", "password"));
3. AutoIT
Autoit script to handle the authentication window:
WinWaitActive("Windows Security")
Send("username")
Send("{TAB}")
Send("password")
Send("{ENTER}")
Save this file as “auth.au3" Right click on the file and chose “Compile Script (x86)” option and it will make “auth.exe” Now write the sample java code to use it:
driver.get("https://staging.rockettes.com/");
Runtime.getRuntime().exec("E:\\AutoIT\\auth.exe");
4. Sikuli
Screen screen = new Screen();
driver = new FirefoxDriver();
driver.get("https://staging.rockettes.com/");
screen.type("C:\\username.png"),"username");
screen.type("C:\\password.png","password");
screen.click("C:\\okButton.png");
- 373
- 1
- 2
- 14
Try this, below solution is specific to chrome and the same approach can be applied for different browsers as well
public String getBaseUrl() {
StringBuilder stagingURl = new StringBuilder();
try {
URL url = new URL(baseUrl);
stagingURl
.append(url.getProtocol())
.append("://")
.append(URLEncoder.encode(stagingUsername, "utf-8"))
.append(":")
.append(URLEncoder.encode(stagingPassword, "utf-8"))
.append("@")
.append("staging.rockettes.com");
return stagingURl.toString();
} catch (UnsupportedEncodingException | MalformedURLException e) {
return "Issue while encoding URL" + e.getMessage();
}
- 91
- 5
The given solutions wouldn't work using the selenium get() method to load such a URL prompting for authentication with a JavaScript Popup, I was also stuck here for a long time. It's because of Chrome driver will not allow such authentication techniques after the update 59 (probably). There are still backdoors via Selenium using the JavaScript engine in the browser to load such URLs.
driver.get("https://www.google.com");
JavascriptExecutor jse = (JavascriptExecutor) driver;
URL = "https://username:password@www.example.com";
jse.executeScript("window.open('"+URL+"')");
- 171
- 2
- 4