-1

I am developing an automatic web unit test with Selenium WebDriver. I know that I can use the driver.close() and driver.quit() method to close the browser and to release the chromedriver from the memory after the test but it's not good for me because I want that the Chrome browser will stay open so I will be able to continue to work on the site from the place the automatic test finished.

The problem is, that when I am closing the chrome browser manually, the chromedriver.exe staying in the memory of the machine and not terminated so in the next test i will have another chromedriver.exe in the memory and so on.

Will be great if i can tell to the chromedriver.exe to terminate itself when the last chrome insistence will be closed. As i explained, i can not do it grammatically because the test method already ended.

I know that i can loop over all the process each time i am executing the test and kill any active webdriver process but i don't like that way.

Just to make things clear, I want automation until a specific point and then to continue manually, after the automation part completed. On that point, i can not access the instance of the driver to use driver.quit() or driver.close() method. I am using Microsoft.NET TestMethod.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Omtechguy
  • 3,321
  • 7
  • 37
  • 71
  • You want to close the browser *manually* when you run *autotests*? Why? If you don't want to re-open browser for each test-case then open it for test-run and call `driver.quit()` once the last test-case executed – Andersson Jan 17 '19 at 17:36
  • @Andersson Thanks for the response. As i explained in my question. i want automation until a specific point and then to continue manually, after the automation part completed. – Omtechguy Jan 17 '19 at 18:00
  • 1
    In this case it's not an *automated unit tests*. However you can use, for instance, in Python `input()` to wait for user input (like pressing Enter button) to continue to run script. Anyway, I'm sure that this is X-Y problem and you're doing something wrong :) – Andersson Jan 17 '19 at 19:01
  • @Omtechguy You mentioned about _Microsoft.NET TestMethod_. Are you open to consider answers in other language bindings e.g _Java_ or _Python_? – undetected Selenium Jan 17 '19 at 19:20
  • @DebanjanB - Sure, thanks – Omtechguy Jan 17 '19 at 21:04
  • If I understand you correctly, your test script finishes - exits, and you continue using the browser for manual checks. So at the end (of the manual tests) you cannot close the driver processes programmatically - because you don't have a running program. In the same time, " i can loop over all the process each time i am executing the test and kill any active webdriver process but i don't like that way." - you don't want to do it as the first step of a future run. In that case - again, if I understood you - *when* do you want to do that? Considering you don't have a code of yours running... – Todor Minakov Jan 18 '19 at 08:21
  • @todor i want to unload chromewebdriver when i close the browser – Omtechguy Jan 18 '19 at 10:19
  • That's given - the question is - how you want to do it? If you want to do it with some code, as I understand it you don't have any program of yours running. If I **have not** understand you correctly, and you have code that's running - then the question boils down to "How to kill a process in c# / vb.net / etc" - correct? Otherwise - if you want to end them manually, just open the task manager and kill the processes there. – Todor Minakov Jan 18 '19 at 10:50

1 Answers1

1

A couple of things:

  • driver.close(): It is used to close the current page or the browser (if it is the only page/tab) which is having the focus.

  • driver.quit() : It is used to call the /shutdown endpoint and subsequently the WebDriver and WebBrowser instances are destroyed gracefully closing all the pages/tabs/windows.

  • You can find a detailed discussion in about the difference between driver.close() and driver.quit() in this discussion
  • Even after invoking driver.quit() if the WebDriver / Web Browser still stays in memory you can kill them with brute force following the discussion discussion
  • Once your Automated Tests are completed to terminate only the ChromeDriver binary and keep the Chrome Browser stay open you can use use Python raise Keyword and you can use the following (Python based) solution:

  • Code Block:

    from selenium import webdriver
    import psutil
    
    driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get('https://www.google.com/')
    try:
        raise ValueError('Test Execution completed ... Killing the ChromeDriver instance only')
    except Exception as error:
        print('Caught this error: ' + repr(error))
        PROCNAME = "chromedriver.exe" # or geckodriver.exe or IEDriverServer.exe
        for proc in psutil.process_iter():
            # check whether the process name matches
            if proc.name() == PROCNAME:
                proc.kill()
    print("Only ChromeDriver instance was killed and Chrome Browser instance left open")
    
  • Console Output:

    Caught this error: ValueError('Test Execution completed ... Killing the ChromeDriver instance only',)
    Only ChromeDriver instance was killed and Chrome Browser instance left open
    
  • Here you can find a detailed discussion on Manually raising (throwing) an exception in Python

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Why are you providing PhantomJS (no longer maintained and OP stated he's using chromedriver) and python code and links when OP clearly stated, "I am using Microsoft.NET"? – JeffC Jan 18 '19 at 15:41
  • @JeffC Possibly you missed to check the related discussion just below the question as `@Omtechguy You mentioned about Microsoft.NET TestMethod. Are you open to consider answers in other language bindings e.g Java or Python?` and `@DebanjanB - Sure, thanks`. Moreover the answers within the discussion `PhantomJS web driver stays in memory` are generic and applies to all the _WebDriver_ variants – undetected Selenium Jan 18 '19 at 15:49
  • That's why the question should be updated with all the relevant data. PhantomJS is not the same as chromedriver though... – JeffC Jan 18 '19 at 17:33
  • why do you raise an exception just to catch it? totally unnecessary – Corey Goldberg Jan 18 '19 at 21:10