Thursday 10 January 2013

Coding Firefox in C# in real-time using Selenium's Firefox driver

The best way to write and debug Selenium Web Automation scripts is to be able to be able to write code snippets in real time (in a REPL)

This post will show how I just did that for the TeamMentor’s UnitTests environment that Michael Hidalgo is working on.

In VisualStudio 2010, I added (via Nuget) the FluentSharp C# REPL to the TeamMentor.UnitTests.Automation project

image

This allows me to use the {target object}.script_Me() function:

image

which when executed gives us a C# REPL, with the {target object} passed as a parameter:

image

The error that we get (above) is because the this object is actually a generic type, and the automatic type conversion (of the name used in the script_Me window) creates an invalid variable name (for a workaround see 'extra info' section at the end of this post )

In this scenario, what we actually want is to get the driver object, so we can do this:

image

Note how the the driver object/variable is THE live Selenium Firefox Driver:

image

Which we can now use to directly control/manipulate/invoke Firefox (with full Code Complete support)

image

The screenshot above shows google.com open from the C# REPL Environment.

Which means that we are coding Firefox in C# in real-time using Selenium's Firefox driver :)


EXTRA INFO: converting generic types using a dynamic proxy

To solve the problem caused by the use of an Generic in the {target object}.script_Me() call, start by adding the special tag //O2Tag_SetInvocationParametersToDynamic

image

And couple references/usings later, we now have a script access to the UnitTest live object :)

image

Note how we also have direct access to the other methods in the UnitTest class (and code complete works)

image