Unable to get the alert text using selenium webdriver - selenium

I have the below code to verify the alert text and then dismiss. Verifying the alert text is failing.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alertText = alert.getText();
alertText is returned as null. So the next step to assert the text is failing.
Next step:
driver.switchTo().alert().accept();
Accepts the alert without any issue.
Another weird scenario is when I debug in eclipse its working fine. But when I am running assertion throws error.
Another differecnce I found while running and debugging.
During running the alert come with this message don't let this page create more messages
Looks very weird.

Showing message as "don't let this page create more messages" is browser feature there to prevent sites from showing hundreds of alerts.
As while debugging your code it is working, it should work even while running too. If not working, definitely its sync issue. I hope adding some hardwait definitely help.
Other approach to get alert text from alert popup:
public String getJsAlertText()
{
Object txt = ((JavascriptExecutor)driver).executeScript("return
window.alert.myAlertText;");
return (String)txt;
}

Related

Unable to validate the visibility of the notification message using explicit wait

I want to validate whether a notification message appears on the screen upon successful save.
The Problem here is the notification message disappears in few seconds(less than 5 sec).
I tried the following code to validate whether notification message appears or not:
new WebDriverWait(driver, 15).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h2[contains(text(),'Notification Message')]")));
}
catch(Exception e) {
System.out.println("The Exception is ::::::::::::: "+e.getMessage());
return false;
}
return true;
Additionally I also tried the following
new WebDriverWait(driver, 15).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//h2[contains(text(),'Notification Message')]")));
In both the cases Exception is thrown though the notification message appears.
Please recommend if any other way to validate the notification message.

Handle javascript alert in iframe using Selenium 2

I trying to practice handling Javascript alert. I want to accept alert ,which comes out of iframe in w3school website.
WebDriver driver = new ChromeDriver();
driver.get("http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert");
driver.switchTo().frame("iframeResult");
driver.findElement(By.xpath("/html/body/button")).click();
WebElement editable = driver.switchTo().activeElement();
editable.sendKeys("enter");
//handle pop alert
String mainPage = driver.getWindowHandle();
Alert alt = driver.switchTo().alert();
alt.accept();
driver.switchTo().window(mainPage);
I am just getting alert box and then below error. I am not able to accept it.
Exception in thread "main" org.openqa.selenium.UnhandledAlertException: unexpected alert open: {Alert text : I am an alert box!}
You have all the right code. The error comes when you try to do something other than handle the alert once the alert is up. You have some extraneous code that isn't needed to handle the alert. I've removed it in the code below. You can add things back but you will need to add them before or after launching the alert and handling it.
WebDriver driver = new ChromeDriver();
driver.get("http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert");
driver.switchTo().frame("iframeResult");
driver.findElement(By.xpath("/html/body/button")).click(); // this launches the alert
driver.switchTo().alert().accept(); // this one line accepts the alert
driver.switchTo().defaultContent(); // you will probably need to switch back out of the IFRAME at some point to access the main page again so I added this line
One thing that I would strongly suggest for you is to read some guides on debugging in the IDE of your choice. If you learned how to debug your code, you could walk through it and more likely see where the issue was and fix it.

selenium : how to handle error messages that get displayed when we dont give credentails

I am new to selenium, and when testing a web application I want to check when credentials are wrong the message in alert which gets displayed and the error message that is displayed on the page after accepting the alert are same or not please can anybody help me how can I do this
I have handled alert using
web.swichto.alert().accept
but I want to compare the message that alert displays and the error message that is displayed in that page like
Error: Invalid User ID/Password Or Network Is Down!
and message in alert box like
Invalid username/password
thanks in advance
you can store text present in the alert into a variable as follows,
String msg_in_alert=driver.switchTo().alert().getText();
then you can use assert to compare,
Assert.assertEquals(driver.findElement(By.cssSelector("selector_of_error_element")).getText().compareTo(msg_in_alert), true);
"true" specifies that the text should match..
If text does not match,it will result in step failure...
WebElement element = _driver.findElement(By.xpath("//span[#class='ng-binding error-class ng-hide']"));
element.getText();
if(element.equals("Recall and Volume data does not exist for calculation. Please upload the data.")){
System.out.println("true");
}else{
System.out.println("false");
}
WebElement mandmessActionName = driver.findElement(By.xpath("//*[#id='container']/div/form/div[1]/div/fieldset/div[1]/div/span[1]"));
String mandmessActionName1 = mandmessActionName.getText();
if (mandmessActionName1.equals("Action Name is required."))
{
System.out.println("true");
}
else
{
System.out.println("false");
}

How to switch back to main window after handling alert in Selenium Web Driver

I have to write a selenium automation test script , where test has to create a template that fills up Generalize data in form and enter specific details manually(trying to wait here till manual entry is finished) and then click on SAVE button. However if some of mandatory field is left the system shows JavaScript's validation alert. I am handling this alert by using
Alert alert = driver.switchTo().alert();
alert.accept();
After this I want to get back to main page and wait for several minutes to write the description for missing fields and now click on SAVE button.
How can I achive this in Selenium web driver?
when you deal with alerts firstly you have to check whether alert is present. I would use this approach:
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;
for more in click here, Also do not forget about debug step by step to get to know on what step alert appears/not appears. Hope this helps you.
You can go to the alert window from parent window using switchTo() method:
<html>
Alert alrt = driver.switchTo().alert(); // Switching the control to Alert window
alrt.accept(); // to accept Yes to delete the task
//alrt.dismiss();
// to dismiss the action - once you click on YES, of course you don't have option to dismiss
</html>
Now, answering to your question -
once you select any option from the Alert window (say - YES/NO), the control automatically comes back to the parent or main window. And you know what, the same follows in Hidden divs too.The switching back to parent window comes into picture when it is a question of HTML Pop ups.
Hope this helps.
After Accepting Alert box. we want to Switch to main window by geting the window handle details.
//Stores the handle details
String windowHandle=driver.getWindowHandles();
//Alert portion
Alert alert = driver.switchTo().alert();
alert.accept();
//After switching back to main window..
driver.navigate.windows(windowHandle);
//It takes to the main window....
You can give a try on the following code:
driver.navigate.back();

How to check alert is present or not continuously?

I'm testing Gmail unread mail counts, everything is working fine but due to slow internet I'm getting "Some feature cannot be load alert" how to handle this?
What my problem is: I need to check for the alert continuously because it can appear at any time, if it present then only i can able to dismiss it. Even if we use separate thread for to check alert is present like this
public void checkAlert()
{
try
{
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
}
catch (Exception e)
{
//exception handling
}
}
also it won't work i think any idea!!!!!!!!!
I think GMail shown message in custom popup (actually it's div which looks like popup). Alert class can be used only with native java script popups.