“The given key was not present in the dictionary.” error when running specflow test

I was wondering if anyone has come across this issue before when running a test via Specflow. In my class I declare the below:

        private IWebDriver driver = ScenarioContext.Current.Get<IWebDriver>();

However, when I run the specflow test it falls over at the above line of code with the below error:

Message: System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. ----> System.Collections.Generic.KeyNotFoundException : The given key was not present in the dictionary.

Is this an error anyone has seen before when running a test through specflow?

Did the IWebDriver property ever get set for the ScenarioContext.Current? The error is saying it’s not there. Trace it back one more step - my guess is that wherever you’re setting that, the driver likely failed to initialize.

1 Like

Thanks Ernie, I managed to get to the bottom of it. Turns out it was my amateurish coding to blame :wink:

1 Like