6

I have made a Windows Forms app to host my GUI for running selenium tests.

When the user presses the "run test" button, it launches an instance of chromedriver and runs the selected selenium test.

However, let's say that while the test is running, the user decides they want to stop the test, or simply not have the entire test run.

I have a button on the form that calls FixtureTearDown() in case this were to happen. FixtureTearDown is simply:

if (_driver != null) _driver.Quit();

However, if you DO press this "stop test" button, the driver closes, but the selenium test keeps executing the script as if the webdriver were still open, which then throws errors (because it can't find elements because the driver is closed, duh) and causes my Windows Forms app to crash.

So, is there a way to gracefully halt the running of a selenium script? Thanks in advance for the help, and i apologize if this is a dumb question, i am still new to selenium and c# in general.

2
  • 1
    I have never seen the browser remain open after driver.quit(). Which browser are you using? Are you sure your button click event quits the driver? May 7, 2015 at 4:27
  • 1
    The driver and the test scripts are different things. Yes, with driver.quit() you close the driver. But the test script is still running. So you need to stop the test itself. This can be managed by the testing framework that you are using. It should provide a solution to stop a started test script.
    – Twaldigas
    May 7, 2015 at 6:46

2 Answers 2

3

Well, I solved it, but it might not be best practice.

Basically, in each test method I assign that method to a current thread variable.

targetThread = Thread.CurrentThread;

and then, when the user presses the stop test button, I kill the thread. It works like a charm.

public void haltScript()
{
    targetThread.Abort();
}

of course, I'm quitting the driver also, but this seems to work for stopping the execution of the script.

2

Please use driver.close() and driver.quit() both in the end.

driver.close – It closes the the browser window on which the focus is set.

driver.quit – It basically calls driver.dispose method which in turn closes all the browser windows and ends the WebDriver session gracefully.

Reference

9
  • 2
    Short answers without explanation do not really help, can you explain why this would help? Also from what I read here .quit() will close all the windows: stackoverflow.com/questions/15067107/… May 7, 2015 at 11:43
  • I don't think you can driver.quit() after driver.close(). From what remember, it throws an exception. May 8, 2015 at 9:58
  • It is also a way but If you want to kill driver process then both required.
    – Sagar007
    May 8, 2015 at 12:56
  • I may mis-understand but .quit() method has already include .close() method, right?
    – Nam G VU
    Jul 20, 2017 at 2:50
  • @NamGVU If you use one browser then only quite() is better option.
    – Sagar007
    Jul 20, 2017 at 4:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.