Monday, 8 July 2013

Dealing with Selenium WebDriver Driver.Quit crashes (Where chromedriver.exe is left open)

We recently came across a problem with Selenium not quitting the webdriver and this would then lock a file that was needed on the build server to run the builds.

We were using Driver.Quit() but this sometimes failed and would leave chromedriver.exe running. I looked around and found this was a common issue that many people were having. We (I say we, as we came to the solution through paired programming), came up with the following, that would encapsulate the driver.quit inside a task and if this task takes longer than 10 seconds, then it will clean up any processes started by the current process, in the case of the issue on the build server, it would kill any process started by Nunit.

[AfterTestRun]
        public static void AfterTestRun()
        {
            var nativeDriverQuit = Task.Factory.StartNew(() => Driver.Quit());
            if (!nativeDriverQuit.Wait(TimeSpan.FromSeconds(10)))
            {
                CleanUpProcessByInheritance();
            }
       }

        private static void CleanUpProcessByInheritance()
        {
            var currentProcessPid = Process.GetCurrentProcess().Id;
            foreach (var process in Process.GetProcesses())
            {
                using (var mo = new ManagementObject("win32_process.handle='" + process.Id.ToString(CultureInfo.InvariantCulture) + "'"))
                {
                    mo.Get();
                    var parentPid = Convert.ToInt32(mo["ParentProcessId"]);

                    if (parentPid == currentProcessPid)
                    {
                        process.Kill();
                    }
                }
            }
        }

I've read quite a bit around this subject and it seems to be a common issue, that the driver.quit can leave the chromedriver.exe open, hence the need for the above method. Since implementing this fix we've not seen the locked file anymore on the build server, so hopefully it's solved. If not then I'll be sure to update this blog with (hopefully) a new workaround.

11 comments:

  1. Your information about Selenium scripts is really interesting. Also I want to know the latest new techniques which are implemented in selenium. Can you please update it in your website? Selenium testing training in Chennai

    ReplyDelete
  2. I have read your blog its very Interesting. Thanks for sharing. ERP Providers in Chennai | ERP in Chennai. We provide best quality cost effective ERP software applications and end to end enterprise solutions to all the industry verticals with the affordable cost.

    ReplyDelete
  3. Really Nice Blog. Thank you for Sharing. We are the best erp software providers in chennai. For more details call +91 9677025199 or email us on info@bravetechnologies.in.
    ERP in Chennai

    ReplyDelete
  4. really the way of explanation about selenium concept was so superb.. Software Testing Training in Chennai | Selenium Training in Chennai

    ReplyDelete
  5. Awesome blog with informative content. Thank you for blogging such a excellent blog. Dot Net Training in Chennai | Java Training in Chennai

    ReplyDelete
  6. Great blog.you put Good stuff.All the topics were explained briefly.so quickly understand for me...
    Software Testing Training in Bangalore
    AWS Training in Chennai

    ReplyDelete
  7. Thanks for your informative post. Your article helped me a lot to understand the future..Web Designing Training Institutes in Chennai | Web Designing Training Institutes in Velachery.

    ReplyDelete