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

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.

Related

Unable to get the alert text using selenium webdriver

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;
}

selenium remote driver: cannot accept alert on window close- Window not found exception

I have a window popup , as soon as i click on the submit button the popup window closes and a javascript alert appears.
I have been getting : window not found exception in the logs.
If the window would not closed ,selenium is able to identify the alert but since the popup has closed I get the above exception.
I have tried using $driver->switch_to_window after clicking the submit button but that doesn't handle the alert.
Any thoughts is greatly appreciated.
Are you using the alert functionality with Selenium?
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
}
}

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();

Alert Handling + Java + webdriver [duplicate]

This question already has answers here:
Selenium WebDriver with Java: Can't accept alert
(3 answers)
Closed 9 years ago.
I am trying to use switch to alert and perform an action but i face error.
Now the real issue is when i put the below code in try,catch it works perfectly. i mean it handles the alert perfectly. But when i use the same without try, catch code it throws the below exception
Alert alert = driver.switchTo().alert();
String AlertText = alert.getText();
System.out.println(javascriptconfirm.getText());
alert.accept();
Please find the error below
No alert is present (WARNING: The server did not provide any stacktrace information)
The idea is when you deal with alerts you have to check whether alert is present first.
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;
}
here you can get details Also do not forget about debug step by step to get to know on what step alert appears/not appears.
Hope this helps you.

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.