dmeehan dmeehan - 2 months ago 8
C# Question

How can I specify a different URL and/or Browser to run my C# WebDriver test against at runtime?

I am using WebDriver and NUnit to create tests in C#. Is there a way to specify at runtime the URL and the Browser without having to change the code and rebuild?

Basically can I run the same tests against different URLs (Dev, QA , UAT etc) and different Browsers without having to recomplile.

Many Thanks.

Answer

After much searching I eventually came up with the solution by combining the best bits of two other incomplete solutions.

Config file should be the same name as your test file dll eg TestFile.dll.config and located in the same folder.

This is the format of your config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <startup>
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
   </startup>
   <appSettings>
     <add key="TestURL" value="http://www.example.com/"/>
     <add key="Browser" value="Firefox"/>
   </appSettings>
 </configuration>

Add a reference and a using statement for System.Configuration and use code like this to access your parameters:

 testURL = ConfigurationManager.AppSettings["TestURL"];
 browser = ConfigurationManager.AppSettings["Browser"];

All improvements, suggestions and comments are welcome.