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

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

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

How to handle a popup in Selenium

I'm automating an application where on clicking a button a popup opens up , I am a bit confused about handling this popup.I am attaching the screenshot below where i have to handle the " Daily Trip details" popup. Can it be handled by driver.getwindowhandles() code or autoit?Thanks
Image of the popup:
You can handle using getWindowHadles(). After switching to new window you can perform actions which are required for your test case.
Following code may help you out for switching to modal
String currentWindowHandle=driver.getWindowHandle();
for(String windowHandle : driver.getWindowHandles()){
if(!windowHandle.equals(currentWindowHandle)){
driver.switchTo().window(windowHandle);
//perform actions which are required
}
}

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.

How to handle pop ups without title in selenium

Please find attached pic, i need to select continue shopping from the pop upenter image description here
Actually, in your case it's not a regular Popup of browser. That's just a combination of Div instances.
You can try sth like this:
driver.findElement(By.id("fancy_notification_content")).findElement(By.className("continue_shopping")).click();
popups handler is IAlert.
IAlert alert = driver.SwitchTo().Alert();
if (alert != null)
{
alert.Accept();
}
but i didn't know if this is the case.

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.