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.

23 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
  8. Interesting post! This is really helpful for me. I like it! Thanks for sharing!

    Webseiten Gestaltung L├╝denscheid

    ReplyDelete
  9. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
    selenium training in bangalore|
    python training in bangalore|

    ReplyDelete
  10. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, have a nice weekend!

    data science training in bangalore

    ReplyDelete
  11. I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.
    "Devops Training in Chennai"

    ReplyDelete
  12. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    hadoop training in chennai

    ReplyDelete

  13. The website is looking bit flashy and it catches the visitors eyes. A design is pretty simple .
    office 2010 professional plus key deutsch

    ReplyDelete
  14. It is really a great work and the way in which you are sharing the knowledge is excellent.Thanks for your informative article

    selenium training in chennai|

    ReplyDelete
    Replies
    1. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

      Java Training In Bangalore

      Delete
  15. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site."Devops Training in Bangalore"

    ReplyDelete
  16. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    hadoop training in chennai
    big data training in chennai

    ReplyDelete
  17. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
    Besant technologies Marathahalli

    ReplyDelete
  18. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, have a nice weekend!

    Data Science Training in Bangalore

    ReplyDelete