Question

I have coded some automated web tests in C#, they use the Coded UI Test Library.

These tests are not a bunch of recorded actions, They have been coded and executed on IE.

I am using Visual Studio 2010 and I'm trying to launch my tests on Firefox 3.6. I have already installed the required components for this purpose.

The problem is that I want to choose the browser I want to use to test my applications, so I want to change the field "currentBrowser" but I cannot have access to this property because is a static member property.

I have tried to use Reflection library to access the property, but I cannot achieve it.

This is an example of my current code:

BrowserWindow browserWin = new BrowserWindow();

// Copy the dll 
Assembly testAssembly = Assembly.LoadFile(@"c:\Microsoft.VisualStudio.TestTools.UITesting.dll");

// get type of class BrowserWindow from just loaded assembly
Type BrowserWindowType = testAssembly.GetType("Microsoft.VisualStudio.TestTools.UITesting.BrowserWindow");

object browserInstance = Activator.CreateInstance(BrowserWindowType, new object[] {});
PropertyInfo BrowserPropertyInfo = BrowserWindowType.GetProperty("CurrentBrowser");   

//set value of property: public 
BrowserPropertyInfo.SetValue(browserInstance,"FireFox", null);
// get value of property: public 
string value = (string)BrowserPropertyInfo.GetValue(browserInstance, null);

I have also tried this line of code:

typeof(BrowserWindow).InvokeMember("set_CurrentBrowser", BindingFlags.InvokeMethod | BindingFlags.DeclaredOnly |BindingFlags.Public | BindingFlags.Static, null, bw, args,null,null,null);

But the property's value "currentBrowser" never is modified.

Do I need some kind of special permissions? Maybe something like "ReflectionPermission"

Thank you very much

Was it helpful?

Solution

It should look something like this:

BrowserWindow.CurrentBrowser = "firefox";
BrowserWindow bw = BrowserWindow.Launch(new Uri("uriAsString"));
// At this point, it should launch firefox if you have the current cross-browser
// components installed and the expected version of firefox (I have 26)

You said:

The problem is that I want to choose the browser I want to use to test my applications, so I want to change the field "currentBrowser" but I cannot have access to this property because is a static member property.

Static does not prevent access. It means you do not need an instance of that object to access the property/method. If you look at the class via the object browser you will see this:

public static string CurrentBrowser { get; set; }

So we know we have access to it because it's public. We can get the value and set the value because it contains get; and set; with no hiding access modifiers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top