Thursday, 10 January 2013

OpenQA.Selenium.DriverServiceNotFoundException on Chrome

While trying to running a TeamMentor UnitTest in Chrome I got this error:


image_thumb[1]

Using the trick shown in Coding Firefox in C# in real-time using Selenium's Firefox driver, let’s run this in under firefox to see where this chrome dll should be (i.e. I’m looking for what is the ‘current directory’)

image

Here is what is in this location:

image

let’s take a look at the this object location

image

which has the compiled dll of the current UnitTest:

image

Clearly this is not where we should be looking at

So, I guess the next step is to download the ChromeDriver:

image

Which unziped looks like this:

image

Using the technique described in Using VisualStudio C# REPL to quickly find issue let’s see if we can set the Path Dynamically.

Here is the current path:

image

Trying to run the ChromeDriver.exe directly doesn’t work (because it is not being resolved in the current Path)

image

Here is a temp location for the ChromeDriver.exe:

image

Testing adding a new path:

image

Here is the code that updates the current (process) Environment Path and starts ChromeDriver.exe directly:

image

Next we add this code to the CreateDriver method:

image

and the unit test will execute :)

image

Add the option to detect when this is a chrome test:

image

which means that that code will not be executed when we are running the tests in Firefox:

image

Final step is to remove the hard dependency on the ChromeDriver.exe, and download it on first run into a temp folder.

Open the C# REPL again, and define a temp folder inside O2’s temp folder (daily based):

image

This is where we want the ChromeDriver.exe file to exist:

image

Here is the code that downloads the ChromeDriver.exe (if not there)

image

Add this code to the CreateDriver method (with a bit of refactoring using Lambda methods)

image

If the ChromeDriver.exe file doesn't exist (note the empty directory) it will be downloaded on first run:

image

After the run the ChromeDriver.exe will be in the right location:

image

It will need to authorized on first run (if you have a firewall enabled)

image

And all is working as it should (the UnitTest will successfully run on Chrome)

This code still needs quite a bit of refactoring but it already solves a really hard problem (which is to remove a hardcoded dependency on ChromeDriver.exe and prevent us from needing to add ChromeDriver.exe to GitHub)
Post a Comment