I am using Capybara-Webkit to automate some work on a website.
This is done in the background in a Resque task. After some executions there are quiet a lot webkit_server processes, that weren't closed by Capybara.
So how can I force Capybara to close it at the end?
session = Capybara::Session.new(:webkit)
session.visit URL
session.click_button(BUTTON)
....
You have to use page.quit at end of the each test case to close all the browser instance.
Related
This question already has an answer here:
Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?
(1 answer)
Closed 2 years ago.
I have written a code to automate the login process by passing credentials in python and using selenium web driver. So, once the login is done, then the user will need to carry out some task and then he will be the one closing the browser, on closing the browser manually, is it possible to end the chromedriver process as well?
I dont think it is a good idea closing the browser manually. When you do it, you will get an Exception, maybe with message "timed out connecting to Chrome..." or something, because seleniumdriver instance is still running but chrome has gone.
I strongly suggest replacing this step by driver.quit.
BUT
If you really want to close the browser manually, maybe you can try to KILL the chromedriver process after that.
A demo in Java On Windows, like this:
Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe");
You don't need close manually , you can use close() method in selenium
driver.close();
The Close() method closes the currently open browser window, which has the WebDriver's focus. All other browser windows remain open, and the webdriver's instance remains open and usable too. The Quit() closes all browser windows, and further disposes of the WebDriver instance.
When I run my selenium tests using a chrome browser all my tests cases run fine. When using the phantomjs browser it would appear that the browser session does not get reset after each test case. In my tests cases, I log in as a user to then navigate to certain pages and then logout. A problem occurs when a test case happens to fail. The browser session is not reset so when the next test case begins, the test that failed was unable to logout. This causes all test cases after a single failure to fail.
When searching the internet for a solution to this issue it been known sine 2013. I can't seem to find anything regarding this issue that's recent. Is there any up to date workarounds?
Manually trying to delete the cookies before or after each test case does not appear to work. webDriver.manage().deleteAllCookies();
I'm using phantomjs ver 2.1.1.
First of all PhantomJS is dead, you are better off switching to Headless Chrome or Headless Firefox.
Secondly PhantomJS is a port of Webkit which is not thread safe. This means that if you try and run more than one test in parallel you will see threading issues, to fix this you would need to start multiple instances of PhantomJS and have each GhostDriver instance connect to a different instance of PhantomJS.
The particular problem that you are seeing is that PhantomJS doesn't clear itself down properly, again the only solution would be to kill the initial PhantomJS instance you are running after your test finished and then start up a clean new one, unfortunately that is not supported by GhostDriver.
The final problem is that GhostDriver is dead as well, there was no point in continuing development when PhantomJS died.
TLDR; Use Chrome/Firefox Headless mode instead.
We would love to adopt Nightwatch.js for testing on browsers, but we're stuck on one major caveat: at the time of this writing, Nightwatchjs does not support running different tests using the same browser session. In short, it means that:
Creating the browser session is handled by the Nightwatch module from lib/index.js, in the startSession function;
Killing the browser would correspond to the delete command place in the Selenium action queue in the terminate function of that module;
A new Nightwatch client is created at every test run, which happens every time we load a different test file;
According to this source, it is possible to reuse the current browser session in Selenium, instead of opening a new window.
Has anyone managed to fix this problem in Nightwatch?
Here's the feature request on Github, which was requested on Mar 31, 2014 and is still open.
Another approach would be to circumvent the problem altogether by getting Nightwatch to merge all different files into one Test Suite, but that seems to be harder to solve than the problem with sessions...
Nightwatch now has support for resuing browser sessions. You can simply use the flag --reuse-browser to reuse sessions while running your nightwatch tests.
I'm using the karma-script-launcher to open a virtual machine/browser/url but when the tests are complete I don't know how to close everything.
If I leave the browser open in the virtual machine it eventually stops trying to reconnect - no error message just fails to reconnect. I'm not sure if this is a bug or not. I can't see why you wouldn't want the browser indefinitely trying to reconnect? What ever the reason it leaves me needing to close and re-open the browser each time I run the tests. But if I'm using the script launcher I don't see how this is possible.
Thanks for any help
not quite sure if this covers your answer but I stumbled over your question while looking for a solution to a similar issue, so let me explain - in case someone else encounters this issue.
Was running karma unit tests on codeship ci environment for a nodejs app.
Tests were successful, however the test would keep watching and wouldn't shut down, thus setting the tests to 'passed' - instead it would idle and after a while timeout, setting the tests to 'failed'.
Setting the option singleRun to true within the karma config helped.
Hope this helps anyone who's having the same issue.
Sorry if not topical enough :S
I'm using Capybara with the Selenium webdriver in my testing suite. I've noticed that when all tests are complete, Selenium closes the browser by binding to at_exit. However, this causes an issue for my web application displays a "Are you sure you want to navigate away" dialog onunload (Please don't judge. This is an intranet application and the users specifically requested it.). So when the tests are done my Cucumber scenario fails (even though all my steps pass) because there was an unhandled Javascript confirm dialog. Is there any way to bind after Selenium tries to close the browser and accept the dialog?
Update
I believe I have found the issue with this. It appears that after each test, Capybara resets the browser by clearing all cookies and navigating to about:blank. This is what is causing the onbeforeunload dialog to open (not browser.quit()). I'm cross posting on the Capybara mailing list to try to get help on this and will post anything I find here.
Is it possible for you to switch context at_exit and accept or dismiss the alert. Something like this
at_exit do
page.driver.browser.switch_to.alert.accept
#or
page.driver.browser.switch_to.alert.dismiss
end
If you are using a unit test framework like JUnit or TestNG, then what I do is use Configuration annoations like #AfterTest or #AfterClass to quit the browser. This way, while the test is running the browser is always up. It's not until the #Test block is finished that the test case will close the browser instance.
If you are using Ruby, I assume there is something similar to this.
As I mentioned in the OP update it appears that Capybara navigates to about:blank as the end of each Cucumber scenario. After talking with jnicklas from the Capybara team it appears that the best way to handle this is to implement a Cucumber after handler which navigates away before Capybara tries to and handle the alert there.
After do
# after each test navigate away before Capybara tries to so that we can appropriately handle the onbeforeunload dialog
if #browser
begin
#browser.navigate.to("about:blank")
#browser.switch_to.alert.accept
rescue Selenium::WebDriver::Error::NoAlertPresentError
# No alert was present. Don't need to do anything
end
end
end
Like it was mentioned, calling driver.Quit() should solve the problem, but if the pop-up window is causing an issue that prevents the browser from closing, then eliminate the instance. A simple solution is to wrap your test inside of a using clause.
using (IWebDriver driver = new FirefoxDriver())
{
//Perform your test
driver.Quit();
}