Browser crashes during automated test runs - selenium

For reliability testing, we are testing our web application through selenium automated tests runs on infinite loop on the Firefox /IE browser. Can some one have an idea on how to track when browser crashes/ hung up with out exiting/stopping the test run?

i guess it could be done this way,
if the browser crashes : as there will not be any window opened, you will get an error
generated in javascript for further commands which you send and that will be sent to the
client code.
example :
try to close the window which is already closed or not existed and you will get an exception / error and catch this in the code. (java code example) :
try {
selenium.close(); //if it is a main window
} catch(SeleniumException se) {
if(!se.getMessage().equals("Current window or frame is closed!"))
//do what ever you want here
}
as i said this is to give you some idea about how to go a head and may not be exactly what
you are looking for.
but i have a logic done in this way to close the entire application (multi window)
window by window and it works fine

Related

Trying to automate a web page and pause in debugger error [duplicate]

Everytime I try to access this website and open google-chrome-devtools I am unable to inspect any of the elements through the Inspector as the UI is having an overlay along with a message Paused in debugger.
The upvoted and accepted answer of this discussion says to check the Source tab, check under the Event Listener Breakpoints panel if you've set any breakpoints under 'Mouse'. I have cross checked that none of the Sources -> EventListenerBreakpoint are set.
The upvoted and accepted answer of this discussion says to check if the little octagonal stop/pause sign (at lower left of Chrome "Sources") is colored (blue or purple). I am not sure why do I need to do that additionally for selected websites.
Snapshot:
The upvoted and accepted answer of this discussion speaks about the Manual Steps.
All the solutions seem to point towards the manual process. But this issue seems to me the root cause behind Selenium being unable to getPageSource().
Code trials:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("http://rd.huangpuqu.sh.cn/website/html/shprd/shprd_tpxw/List/list_0.htm");
Output: Chrome opens but doesn't navigates to the url.
So my questions are:
In which case can Paused in debugger error occurs?
Is it an error from the frontend development?
How can I bypass this error during the Automated Tests through Selenium?
In which cases can the Paused in debugger error occur?
Anytime you are accessing this page with the dev tools open. The debugger; line will pause javascript execution, but browsers will ignore it if the dev tools are closed.
Is it an error from the frontend development?
In this case, no--they're deliberately trying to keep you out. The purpose of this function is to pause execution and then redirect your browser to a different page if it takes longer than 100ms to resume. I would speculate that this is designed to interfere with automated crawlers like selenium, because a regular user wouldn't be affected and a human developer can just hack around it.
How can I bypass this error during the Automated Tests through Selenium?
My first recommendation would be to try running Selenium headlessly, if that's an option. If not, use the hotkey to resume execution (F8). You can use whatever method you like for generating a keypress; with the java.awt package it will look something like this:
Robot robot = null;
try
{
robot = new Robot();
}
catch(Exception e)
{
//handle failure
}
robot.keyPress(KeyEvent.VK_F8);
Remember that you have to trigger this within 100ms, so use whatever logic you like to detect the block and respond quickly. If you just want something quick and dirty, I would just make it spam F8 keypresses every 50ms for a period of time until you're certain the page has loaded.
EDIT: On further investigation, this page is extremely messy and hostile to anyone with the dev tools open. There is not one but several functions that trigger debugger;and they get called repeatedly on a timer for as long as you're on the page. Running headlessly seems like the best choice, unless you want to continue spamming F8 for the entire session.

Debugger.setPauseOnExceptions for Selenium debugging not working [duplicate]

Everytime I try to access this website and open google-chrome-devtools I am unable to inspect any of the elements through the Inspector as the UI is having an overlay along with a message Paused in debugger.
The upvoted and accepted answer of this discussion says to check the Source tab, check under the Event Listener Breakpoints panel if you've set any breakpoints under 'Mouse'. I have cross checked that none of the Sources -> EventListenerBreakpoint are set.
The upvoted and accepted answer of this discussion says to check if the little octagonal stop/pause sign (at lower left of Chrome "Sources") is colored (blue or purple). I am not sure why do I need to do that additionally for selected websites.
Snapshot:
The upvoted and accepted answer of this discussion speaks about the Manual Steps.
All the solutions seem to point towards the manual process. But this issue seems to me the root cause behind Selenium being unable to getPageSource().
Code trials:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("http://rd.huangpuqu.sh.cn/website/html/shprd/shprd_tpxw/List/list_0.htm");
Output: Chrome opens but doesn't navigates to the url.
So my questions are:
In which case can Paused in debugger error occurs?
Is it an error from the frontend development?
How can I bypass this error during the Automated Tests through Selenium?
In which cases can the Paused in debugger error occur?
Anytime you are accessing this page with the dev tools open. The debugger; line will pause javascript execution, but browsers will ignore it if the dev tools are closed.
Is it an error from the frontend development?
In this case, no--they're deliberately trying to keep you out. The purpose of this function is to pause execution and then redirect your browser to a different page if it takes longer than 100ms to resume. I would speculate that this is designed to interfere with automated crawlers like selenium, because a regular user wouldn't be affected and a human developer can just hack around it.
How can I bypass this error during the Automated Tests through Selenium?
My first recommendation would be to try running Selenium headlessly, if that's an option. If not, use the hotkey to resume execution (F8). You can use whatever method you like for generating a keypress; with the java.awt package it will look something like this:
Robot robot = null;
try
{
robot = new Robot();
}
catch(Exception e)
{
//handle failure
}
robot.keyPress(KeyEvent.VK_F8);
Remember that you have to trigger this within 100ms, so use whatever logic you like to detect the block and respond quickly. If you just want something quick and dirty, I would just make it spam F8 keypresses every 50ms for a period of time until you're certain the page has loaded.
EDIT: On further investigation, this page is extremely messy and hostile to anyone with the dev tools open. There is not one but several functions that trigger debugger;and they get called repeatedly on a timer for as long as you're on the page. Running headlessly seems like the best choice, unless you want to continue spamming F8 for the entire session.

Selenium - watch for an error condition AND run 'happy path' test code

My app displays an error dialog whenever a JavaScript error occurs. This is always a bad sign, so I want to set up my tests so that, if the error dialog appears, it causes the test to fail there and then.
So I'd like to do something like (very much pseudocode!);
// start a new 'guard' thread;
start {
found = this.driver.wait(untilVisible(By.css('.myErrorDialog')), VERY_LONG_TIMEOUT);
if (found) {
// the error dialog appeared! That's bad!
throw();
}
}
// now run the test
login();
clickButton();
testBannerContains();
But I'm having trouble and I think it has to do with the way Selenium schedules actions.
What I've found is that for a single driver, I can only schedule one thing at a time, so the guard I set up early in the test blocks the body of the test from starting.
Is there a better way to handle conditions like 'this should never happen', or a way to create two independent threads in the same test?
So the problem with the code you have is that it immediately runs it and waits for a VERY_LONG_TIMEOUT amount of time for that error dialog to appear. Since it never does, it continues to wait. You have already discovered that is not what you want... ;)
I haven't done anything like this but I think you want a JS event handler that watches for the event that is triggered when the error dialog appears. See the link below for some guidance there.
Can my WebDriver script catch a event from the webpage?
One option would be to watch for that event to fire and then store true (or whatever) in some JS variable. Before leaving a page, check to see if the variable is set to true and if so, fail the test. You can set and get JS variables using JavascriptExecutor. Some google searches should get you all you need to use it.

Webdriver/Selenium Alert window issue

My company wants me to develop a "Visual" GUI style BDD function using JBehave and Selenium, which uses javascript alert/confirm popup window to prompt user what is the exact step the running test reaches, eg:
Given I goto "www.google.com"
When I login
So we want to add Javascript alert window to popup during the automation test, the popped up window has the "OK" button, so when user click the OK button, the test will continue to the next step, and so on...
My issue is: I wrote a javascript func using Selenium's executeScript API which invoke the pop up alert window:
public void stepText(String step) {
executeScript("alert('"+step+"');");
}
So I expect when I click the OK button, the popped up window will disappear and test will continue to next step... But what shocked me is that when I click it, the test throw exception and crashed...
The exception is: selenium.WebDriverException
But I found if I add the following code to make the test automatically detect the alert window and accept it by using the following usual selenium alert handle function:
Alert alert=switchTo().alert();
alert.accept();
This can make the test runs well, so it looks I can NOT manually click the alert (after I manually click, the selenium still can NOT go back to the browser...lost connection to browser?), but the automation alert handle code works...
Of course, we want to let user to manually to click alert window to control the test execution, not the automation handle alert.
I really got stuck here for a while, and did a lot googling to search, but can not find similar example online, I hope you can shed me light on it, since you are much more guru than me on JBehave and Selenium.
I will be much grateful if you can help me out.
Selenium is a browser automation tool, it does not anticipate user's interactions.
Therefore, I'd use a simple Java GUI window to present the user with messages/options. Afterall, you are testing a web application in a browser, but the program itself is Java and has nothing to do with the browser. A usual Swing option dialog should be enough.
JOptionPane.showMessageDialog(null, "Login successful.");
String loginAs = JOptionPane.showInputDialog("Login as:", "admin");
int choice = JOptionPane.showConfirmDialog(null, "Use production data?");
(note that you don't want to invoke this in the EventQueue.invokeLater() block, because you want the dialogs to be blocking)
This way, you won't interact with Selenium or the browser in any way, you won't confuse it and you'll get the user input cleanly.
That said, if you insist on using alerts, I think it's definitely doable, but as of now (June 2013, Selenium 2.33.0), I don't know how:
The issue is not reproducible on IE8. After the executeScript("alert('Something.')"); call, Selenium waits for the call to return something and then proceeds normally. So you're good on IE.
However, with FF21, Selenium fails immediatelly with UnhandledAlertException just as you said.
I tried two obvious solutions:
js.executeScript("alert('something')");
new WebDriverWait(driver, 10)
.pollingEvery(100, TimeUnit.MILLISECONDS)
.ignoring(UnhandledAlertException.class)
.until(ExpectedConditions.not(ExpectedConditions.alertIsPresent()))
.wait();
and
js.executeScript("alert('something')");
boolean alertVisible = true;
while (alertVisible) {
try {
driver.switchTo().alert();
} catch (NoAlertPresentException ignored) {
alertVisible = false;
}
}
Both make FF fail horribly with an internal JavaScript exception. Possibly a bug that might get fixed (please test it, check whether it had been reported and report it if you're interested in it), so I'll leave the solutions here for future generations.
But as I said before, it's possible that it won't get fixed, since Selenium doesn't count on manual user interactions.
Not sure how this behaves in other browsers.

How do I interact with a popup window with Mink, Selenium 2, and Behat?

I am running through an internal site with Behat and for the most part it is going really well. But the problem is that on certain parts of the site we have popup windows that come up to complete an action. In this case we hit a "Withdraw" button and a popup comes up to have you select a reason and save it.
In an ideal world, and if I had actually designed this app, the site wouldn't be using any popup windows. But I am the new guy who is supposed to implement automated functional tests (and I am learning how to do that from the ground up). So I don't really have any say over the site design at this point (though I will push for a lot of changes as time goes by).
I am running Behat with Mink and the Selenium 2 driver on an Ubuntu 12.10 system (will eventually have to run some tests on a Windows environment for testing in IE). I am also using PhantomJS for some of the tests I have setup.
Anyway, does Behat/Mink support working with popup windows somehow through the Selenium 2 driver (or through PhantomJS)? I am early in all of this automation setup and really I am just experimenting with tools. If there is a better tool that can handle this then please let me know.
My primary question is how do I get Behat/Mink to work with the popup window, check a box, fill in a field, and click the save button? I know how to do everything except get it to interact directly with the newly popped up window. Any ideas/suggestions would be welcome.
Thanks!
So it turns out that Mink includes some window switching features, but no way to identify said windows. So I wrote two functions getWindowName() and getWindowNames() that identify the current window and all open windows respectively. I committed these changes to the project in GitHub it seems that my fixes will get implemented soon into the code base.
But with these changes I am able to switch windows no problem.
Link: https://github.com/Behat/Mink/pull/341
By setting the focus of the window we can also name these windows so we can access them again in the future.
Using this method we can easily switch between popup windows and continue testing...
/**
* #Then I switch to popup :name
*
* #param $name
*/
public function iSwitchToPopup($name)
{
$this->iSetMainWindowName();
$this->getSession()->switchToWindow($name);
}
/**
* #Then I set main window name
*/
public function iSetMainWindowName()
{
$window_name = 'main_window';
$script = 'window.name = "' . $window_name . '"';
$this->getSession()->executeScript($script);
}
/**
* #Then I switch back to main window
*/
public function iSwitchBackToMainWindow()
{
$this->getSession()->switchToWindow('main_window');
}