Sevvy325
Sevvy325

Reputation: 546

Chromedriver closing after test

So my understanding is that calling driver.quit or close is the the proper way to close the driver and associated window.

However when running my tests, it seems that even without calling driver.quit and instead calling pass, that the window still closes.

I'm using python with unittest test cases excuted via pytest. I've also run standard unitests via Pycharm. In all scenarios, the browser closes as described. I want the browser to stay open to where I can debug tests. I could just call sleep(9000) but that seems dumb.

Furthermore, the browser stays open when commenting out quit on some machines, but not on others with the same chromedriver, Chrome version, and code.

Analyzing the chromedriver logs, it seems it registers a QuitAll command, but I have no idea where it could be getting it from. Could the pyc file not be overwritten?

Code for quit:

def tearDown(self):
    pass
    # self.driver.quit()

Upvotes: 9

Views: 15542

Answers (2)

Michael Mintz
Michael Mintz

Reputation: 15381

For maximizing your debugging options with pytest and WebDriver, here's some useful info:

pytest comes with some powerful debugging abilities via the interactive Python debugger (“Debug Mode”), which utilizes the following two libraries: the included pdb library, and the improved (but optional) ipdb library.

Debug Mode in pytest can be triggered in a few different ways:

  • Your test raises an exception after passing the --pdb option to pytest.
  • The moment your test begins after passing the --trace option to pytest.
  • Calling pdb.set_trace() or ipdb.set_trace() from your test after importing pdb or ipdb respectively. (Python versions 3.7+ and newer can also use the breakpoint() method to enter Debug Mode.)

When Debug Mode is activated, the browser window will remain open, and you can see how variables look from the command-line.

(Note that you may need to add -s to your pytest run command to allow breakpoints, unless you already have a pytest.ini file present with addops = --capture=no in it.)

Once you’re in Debug Mode, there are several commands that you can use in order to control and debug tests:

  • n (next): Execute the next line of the current method block.
  • s (step): Step through and execute the next line of the current method block (but if the current method calls another method, go down the stack).
  • c (continue): Leave Debug Mode and continue the test where the current method left off.
  • r (return): Continue running the test until the current method returns.
  • j (jump): Jump to the line number of the current method block.
  • w (where): Show where you are within the current stack trace.
  • u (up): Move up the stack.
  • d (down): Move down the stack.
  • ll (longlist): See the code for the current method block. dir(): List namespace objects.
  • h (help): List all available commands.

Additionally, you can execute any Python code that you want from within Debug Mode.

Not all Debug Modes are the same. If you enter Debug Mode after a test raises an exception, you are in the special “Post Mortem Debug Mode”, which lets you explore the stack of the current stack trace, but you can no longer advance to the next line of the method that you were in because that would end the test immediately.

(This is from the pytest Debug Mode tutorial that I created here: https://seleniumbase.com/the-ultimate-pytest-debugging-guide-2021/)

Upvotes: 0

Lucas Tierney
Lucas Tierney

Reputation: 2563

The service will stop once your script ends due to the code here.

If you want chrome and chromedriver to stay open afterward, you can add the detach option when starting chromedriver:

from selenium.webdriver import ChromeOptions, Chrome
opts = ChromeOptions()
opts.add_experimental_option("detach", True)
driver = Chrome(chrome_options=opts)

Upvotes: 20

Related Questions