Selenium catch popup on close browser - java

I'm trying to test that when I close my window a popup shows with a warning message.
I've tried both driver.close() and driver.quit() after making sure I'm on the proper window but this just terminates the process since my popup doesn't show.
I could test it by using the awt Robot and sending alt+f4 but this doesn't seem too reliable.
What would be the proper way to do this?

Instead of using driver.quit() to close the browser, closing it using the Actions object may work for you. This is another way to close the browser using the keyboard shortcuts.
Actions act = new Actions(driver);
act.sendKeys(Keys.chord(Keys.CONTROL+"w")).perform();
Or, if there are multiple tabs opened in driver window:
act.sendKeys(Keys.chord(Keys.CONTROL,Keys.SHIFT+"w")).perform();

You need to switch into the alert if the alert is displayed accept or dismiss it and then call driver.quit();
if(driver.switchTo().alert() != null)//switches only if alert is displayed
{
Alert alert = driver.switchTo().alert();
alert.dismiss(); // alert.accept();
}
driver.quit();
Hope this helps you...kindly get back if it is not working for you

Related

Selenium Click with actions vs click without actions

I was testing a scraper and I noticed that when using:
WebElement tab = driver.findElement(By.cssSelector("div[id*='tabOne']"));
tab.click();
I receive this message constantly:
1536868112230 Marionette DEBUG [6442450945] Canceled page load listener because no navigation has been detected
However when I use this instead:
Actions builder = new Actions(driver);
WebElement tab = driver.findElement(By.cssSelector("div[id*='tabOne']"));
builder.click(tab).perform();
I don't receive a message every time I sift through the tabs or click on new links.
Am I receiving this message because I am not emulating the user behavior thus I get thrown this message because it was waiting for a user click on the tab? And since I am scraping and not testing, is it better to just use the former instead of the latter since I don't need to interact with the GUI anyways?

Notification(Alert) comes randomly throughout application in selenium webdriver

I am using selenium webdriver with Java language. I am facing an issue that notification alert appears randomly through out application. Basically these alert have some information as it is functionality .
As script performs its steps but suddenly these alert appears on the screen and my script would fail due to alert message.
Please give your suggestions, How can we handle this type of alert which appears randomly on any window?
Below are two points come in my mind to handle this scenario:
I will check Alert appears or not at every step (after click or
other action) but it increases my execution time.
Is there any way, we keep monitoring that Alert appears or not. If
Alert appears just close the Alert if not then keep execute the
steps of scripts.
Please suggest any workaround to handle such Alert so that our scripts do not fail.
This is the same scenario that we automate mobile application using Appium tool and suddenly any advertisement come on the screen.
It would be good if any one provide java code to handle this type of scenario.
Thanks in advance!!
If you want that unnecessary alert not appear during your script execution you can override you alert function before executing your script by using JavaScriptExecutor as below :-
JavascriptExecutor executor = (JavascriptExecutor)driver
executor.executeScript("window.alert = function () { return true}");
You can execute this script once every time when your page is loaded. this script will override your alert functionality and the alert will never occur.
I suggest you this script run when if your test is not depend on alert because after execute this script the alert will not be appear on the page.
other than you can handle alert as below :-
WebDriverWait wait = new WebDriverWait(driver, 100);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = webDriver.switchTo().alert();
Now if you want to accept alert, you can use :-
alert.accept();
and for cancel, you can use :-
alert.dismiss();
Note :- if alert is not present by using WebDriverWait it will throw TimeoutException.. so you need to handle it.
Edited..
For Appium automation to solve this problem, you can use a desired capability specifically designed to handle these alerts.
You can either always accept or always dismiss the alerts with these desired capabilities :-
autoAcceptAlerts = true
...
capabilities.SetCapability("autoAcceptAlerts", true);
or
autoDismissAlerts = true
...
capabilities.SetCapability("autoDismissAlerts", true);
Furthermore, some of the older versions of Appium haven’t worked with this solution, so you might want to try a small workaround with this :-
driver.SwitchTo().Alert().Accept();
For more info follow this
Hope it will help you..:)
You can call this method wherever you are getting the alert.This method will accept the alert.
public void checkAlert(){
if(ExpectedConditions.alertIsPresent() != null){
driver.switchTo().alert().accept();
}
}

How to Close the selenium webdriver if the url is not opening

There are many answers(driver.close()) there for this questions but none of them are helped me.
I try to open a website using selenium web driver for every minute.If the website was not opened within a minute code need to close the driver, but it is not closing.Because,the web page title is displaying as connecting.Please help me to work on this.
There is not a direct way to accomplish this unless you kill the browser and quit the driver instance. The easiest way probably is to add some hardcoded delay and killed the session with JavaScript
driver.get("something");
Thread.sleep(1000);
//Alternative to javaScript is to use Actions Class and send ESC key to stop execution
//Actions actions = new Actions(driver);
//actions.sendKeys(Keys.ESCAPE);
((JavascriptExecutor) driver).executeScript("window.stop;");
driver.quit();
Edit
If you want to add another way to bypass the killing of the session if in case the website loads properly try this
driver.get("");
try {
(new WebDriverWait(driver, 1)).until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id("something we know should exist if page load");
} catch (Exception ex) {
//Alternative to javaScript is to use Actions Class and send ESC key to stop execution
//Actions actions = new Actions(driver);
//actions.sendKeys(Keys.ESCAPE);
((JavascriptExecutor) driver).executeScript("window.stop;");
driver.quit();
}

Java Selenium: How is it possible to get a NoAlertPresentException from inside a UnhandledAlertException handler?

I am using Java selenium in order to control a browser (open up webpages, delete cookies, etc). Whenever I have an alert, I dismiss it as follows:
try
{
webDriver.someFunc();
}
catch (UnhandledAlertException error)
{
webDriver.switchTo().alert().dismiss();
}
Then, in some cases I get a NoAlertPresentException thrown when trying to dismiss the alert.
I do not understand, how is it possible that I get a NoAlertPresentException thrown from inside a UnhandledAlertException handler?????
Thanks
I too am getting the same error,but am unable to figure out why?
I tried this:
try
{
webDriver.someFunc();
webDriver.switchTo().alert().dismiss();
}
catch (UnhandledAlertException error)
{
//Ignore
}
This should work.
Problem with your code probably is:
suppose you click on some button which opens an alert and you have put that in try block.As soon as unhandledalertexception is triggered(when alert is opened) it automatically dismisses your alert so that the next statement becomes invalid so the statement you have defined in catch block becomes invalid and it shows No alert found exception as the alert has already been dismissed.
But am still trying to figure out why unhandledalert exception is triggered at the first place.It is triggered all of a sudden for some alerts and doesnot show for all alerts.
I hope this clarifies.
Firstly, i must say, that the concept of unhandled alert makes no sense to me, because alerts are triggered by actions. If you don't understand how the alert is triggered, you should discuss this with your developers and handle it the right way. That being said, take a look into DesiredCapabilities -> CapapilityType -> UNEXPECTED_ALERT_BEHAVIOUR

MessageBox with timeout in Selenium

After writing some automated tests with selenium, I want a message box
telling the testers which test is launched.
It is important, that the test must not run when the messagebox is shown.
They should be paused when the messagebox is shown.
I did this by JavaScript
Object result = ((JavascriptExecutor)TestRunner.driver).executeScript("alert('" + text + "');");
Now I want this messagebox to be shown some seconds. I tried:
TestRunner.driver.manage().wait(10);
And
Selenium selenium = new WebDriverBackedSelenium(TestRunner.driver, TestRunner.driver.getCurrentUrl());
selenium.start();
selenium.getConfirmation();
And
WebDriverWait waitForOkay = new WebDriverWait(TestRunner.driver, 10);
waitForOkay.wait(10);
But the alert always immediately disapears, just like there is some
alert.accept();
Is there any way to have a messagebox that is either clicked "Ok" or
some timeout (e.g. 10 seconds) to not block automated tests?
Any input (like other ways to achieve that) very welcome!
A wait condition is unnecessary in this situation, since by Default the JavaScript alert box.... blocks the execution till you click on "Ok" button.
I tried with sample code (C# snippet) and it was working.
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.google.com");
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
driver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(4));
Console.WriteLine("Entering the Sync Call");
js.ExecuteScript("alert('"+ Error+"');");
Console.WriteLine("Exiting the Sync Call");
I hope this helps...All the Best :-)

Categories