Accept Windows Security Alert with Selenium - java

Environment:
I have to test a web-application with Selenium. Before accessing the startpage, a windows security alert is thrown. Cause the alert/verification-popup is on os level, it can't be handled by the selenium alert-api.
Workaround:
To access the page, I wrote a script, which fills username and password and afterward clicks enter (script code from: https://automationtestingsimplified.wordpress.com/2011/08/11/how-to-handle-window-based-pop-up-using-selenium-and-autoit/).
Problem to solve:
Selenium throws "UnhandledAlertException: Modal dialog present", although the modal dialog is already accepted.
I tried:
Accessing Modal dialog via alert-api. Problem here is, I can not fill
in username. Even if I use Robot.
"driver.switchTo().alert().accept();" also doesn't work.
Let the driver wait till alert isn't present with
"wait.until(ExpectedConditions.not(ExpectedConditions.alertIsPresent()));"
To accept the dialog, event if it already has been accepted.
Possible solution:
Ignore dialog or set it as not present in the driver. But I can't find a solution for that.

Im Running into the same problem. until I find a better solution i'm using Robot to send the keys to the alert to accept it
private void acceptAlert() {
try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_ENTER);
} catch (AWTException e) {
e.printStackTrace();
}
}

Related

How do I access an alert on a new window when the window hasn't loaded anything yet?

I'm running a Selenium script and at one point, we have a new window open and before anything in the window loads, an alert pops up. We switch to the new window and attempt to confirm the alert. This is the same kind of alert we see in other places, but when we try to confirm, or even just try to get the alert object itself, the script hangs, trying to forward getAlertText for the next 10 minutes.
Finally it fails:
[1568822219.289][SEVERE]: Timed out receiving message from renderer: 600.000
...which is no surprise. What I don't understand is why we can't get the alert, which looks the same as every other alert I've ever handled through Chrome and Selenium, and why it doesn't return a NoSuchAlertException instead of very-slowly failing. I cannot inspect the alert through the developer tools and it doesn't register as a window:
We're using driver.switchTo().alert(). I've also used the ExpectedConditions.alertIsPresent() condition. I've even tried accessing the alert before switching to the new window, which I had no real hopes of working, but when you have nothing logical left you try the illogical stuff.
Has anyone else had this issue? What did you do to overcome it? Thanks!
This alert cannot be handled using selenium to handle it you can use sikuli
same I have handled using sikuli
just import sikuli package and write code
String dir = System.getProperty("user.dir");
String allow = dir+"/extra/allow.png";
Screen s = new Screen();
Pattern allowimg = new Pattern(allow);
System.out.println("Trying to click on Allow popup...");
Thread.sleep(2000);
s.click(allowimg);

Login at web prompt - Selenium and Java

I am not able to map the elements of the prompt because it is not possible to inspect them to get the ID or name of the login and password fields, I'm trying to code the code below, give a tab in the elements and even without success.
How do I automate login on this interface?
driver.get("url");
Alert alert = driver.switchTo().alert();
Thread.sleep(2000);
Robot robot = new Robot();
robot.keyPress(KeyboardEvent.DOM_VK_TAB);
This seems to be a basic authentication popup. You can resolve it by passing the credentials in the URL itself as below:
driver.get("http://<username>:<password>#www.example.com");

Selenium 2 WebDriver UnhandledAlertException Java

Now before I start getting scolded, I DID read through most of the existing questions about this and applied different solutions (which mostly repeat the same thing), but it still does not work for me.
I have a maven project with all necessary dependencies, and the website in testing is done specifically for IE and requires me to have a specific certificate in order to access it. I have the certificate for it, and when I go onto the website, before it loads the page it asks me to confirm that I have the certificate and I need to confirm on the pop-up window, THEN the login page fully loads.
I have done to typical:
WebDriverWait wait = new WebDriverWait(driver, 3);
try {
// Handle alert box
driver.navigate().to("https://ke.m-pesa.com/ke/");
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
}
catch(Exception e) {
//whatever
}
Can you tell me where I am going wrong? So far I have used only Selenium RC up till now so this webdriver stuff is still kind of new to me. Please tell me if you need any more info I need to provide.
Why do I still get the UnhandledAlertException?? and why can't I access the login page until I manually press the OK button?
Did you try using Robot? Something like :
Alert alert = driver.switchTo().alert();
Robot a = new Robot();
a.keyPress(KeyEvent.VK_ENTER);
Why robot and not Actions
From this answer :
There is a huge difference in terms of how do these tools work.
Selenium uses the WebDriver API and sends commands to a browser to
perform actions (through the "JSON wire protocol").
Java AWT Robot uses native system events to control the mouse and
keyboard.
If you are doing browser automation, ideally, you don't ever use
things like Robot since usually the functionality provided by selenium
is more than enough. Though, there are cases when there is a browser
or native OS popup opened, for example, to upload/download a file -
this is something that can be also solved with Robot -

Selenium Web-Driver Firefox Profile - Disable popup and alert windows

I am having a problem with certain websites that cause my browser to prompt an alert when I try to switch to a different URL, or even close the browser. Some examples:
http://grooveshark.com/
http://www.dollardays.com/
In order to workaround the alert with Selenium, I need to switch to that alert, and then sometimes accept it and sometimes reject it (depending on the contents of the alert).
I wish to avoid solving this problem that way because:
I need to guess whether I should accept the alert or reject the alert.
Switching to the alert sometimes throws an exception, even though the alert is present.
What preferences do I need to set in the Firefox-Profile, in order to prevent the browser from issuing such alerts (or any other alerts for that matter)?
Answers in Java or Python will be highly appreciated.
Thanks
To my knowledge you can only disable that behaviour globally.
There is a preference called dom.disable_beforeunload. You should change its value to true. With Selenium, you can create a new customized Firefox profile:
FirefoxProfile customProfile = new FirefoxProfile();
customProfile.setPreference("dom.disable_beforeunload", true);
FirefoxDriver driver = new FirefoxDriver(customProfile);
As far as I know it's not possible to disable native browser events like alerts, so you'll just have to handle them better.
1) You should be able to use alert.getText() to make an informed decision on whether to accept or dismiss an alert.
:
try {
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
if ( alert.getText().contains("Are you sure you want to leave this page?")) {
alert.accept();
}
else if ( alert.getText().contains("Some other text which means you need to dismiss")) {
alert.dismiss();
}
else {
//something else
}
}
catch (Exception e) {
}
2) Use a WebDriverWait to avoid race conditions. See above
I don't think Firefox profile would feature disabling such specific elements, but you can hard-code some lines of static logic that would remain consistent across the test case/project.
Like Click on the main page automatically closes the pop-up frame/alert msg on grooveshark.com
#Test
public void testUntitled() throws Exception {
driver.get(baseUrl + "/#!/genre/Rap/1748"); //complete URL becomes http://grooveshark.com/#!/genre/Rap/1748
driver.findElement(By.linkText("moreā€¦")).click(); // clicks a hyper-link which opens up that frame/pop-up
driver.findElement(By.id("lightbox-outer")).click(); // clicks outside the opened-up frame, or simply clicks on the main page in background
}
lightbox-outer is the main page.
You can't disable the popups (alert), just do alert.accept() means clicking the ok button of alert modal or alert.dismiss() means clicking the cancel or close button.
The worst in this case is that you need to wait a certain time if you are not very sure that alert is going to be present or not.
If the alert is present as a result of event sucess( just like clicing the button, webpage asks for your confirmation ) you dont need to do wait wait.until(ExpectedConditions.alertIsPresent());
you can save the time by going to next step which is switching the drver to alert. i.e
Alert alert = driver.switchTo().alert();
if ( alert.getText().contains("Are you sure you want to leave this page?")) {
alert.accept();
}
else if ( alert.getText().contains("Some other text which means you need to dismiss")) {
alert.dismiss();
}
else {
//something else
}
}
just to be sure you can use a small waiting time, but yes it depends uponthe network speed to load the web page for the case that alert is present when webpage is loaded.

Selenium WebDriver with Java: Can't accept alert

When recording in selenium IDE I can click the "OK" button in a popup, and expected to be able to click it using
driver.findElement(By.linkText("OK")).click();
but this was not the case.
Similarly this doesn't work.
driver.switchTo().alert().accept();
Selenium throws a NoAlertPresent exception. If what's popping up is not an alert, then what is it? And how do I click yes!
in such case I'd prefer to check(verify) the alert presence on the page and then if is present - accept it.
It be somthing like:
public boolean isAlertPresent() {
boolean presentFlag = false;
try {
// Check the presence of alert
Alert alert = driver.switchTo().alert();
// Alert present; set the flag
presentFlag = true;
// if present consume the alert
alert.accept();
} catch (NoAlertPresentException ex) {
// Alert not present
ex.printStackTrace();
}
return presentFlag;
}
here you can get details
Also do not forget about debug step by step.
Hope this helps you.
It could be anything. You should be telling us that.
If it is a Java Script alert then, this should work
driver.switchTo().alert().accept();
At the very least you could try sending enter/return key stroke, if the "OK" button is autoselected/highlighted by the web app.
import org.openqa.selenium.Keys
WebElement.sendKeys(Keys.RETURN);
Update
It could also be because your alert is not present at the time you are trying to click/accept it.
For a quick check put in a sleep of 4-5 seconds and then try driver.switchTo().alert().accept();. Once it is ascertained, then put in a wait for alert present in a try and catch loop (any exception handling).
if you are using latest version of webdriver, infact anything above 2.20 then
driver.switchTo().alert().accept();
should work provided the alert is a javascript alert similar to the one we get when we click
alert demo OR confirm pop-up demo
Updated
here this code will help you accept the alert
driver = new FirefoxDriver();
String baseUrl = "http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl);
driver.switchTo().frame(0);
driver.findElement(By.cssSelector("input[type=\"button\"]")).click();
driver.switchTo().alert().accept();

Categories

Resources