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 :-)
Related
I am working on an app, where i get a timeout after certain period of inactivity. I want to capture that and click on 'Continue' when that alert comes up so that the test is not interrupted.
Since this alert is not triggered by any action, it is challenging to predict when this occurs. Any suggestions on how this can be handled?
Performing a check like below after each and every step doesn't look like an ideal solution.
if (alert.exists()){
button.click;
}
You can try smth like this:
WebDriverWait wait = new WebDriverWait(driver, 30);
try {
wait.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
} catch (Exception e) {
System.out.println(" no alert visible after 30 sec.");
}
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?
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();
}
}
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
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();
}