C# 更改编码UI测试中的当前浏览器时出现问题

C# 更改编码UI测试中的当前浏览器时出现问题,c#,coded-ui-tests,system.reflection,C#,Coded Ui Tests,System.reflection,我已经用C#编写了一些自动web测试,它们使用编码的UI测试库 这些测试不是一堆记录的动作,它们是在IE上编码和执行的 我正在使用VisualStudio2010,并尝试在Firefox 3.6上启动测试。我已经为此安装了所需的组件 问题是,我想选择用于测试应用程序的浏览器,因此我想更改字段“currentBrowser”,但我无法访问此属性,因为它是静态成员属性 我曾尝试使用反射库访问该属性,但无法实现 这是我当前代码的一个示例: BrowserWindow browserWin = new

我已经用C#编写了一些自动web测试,它们使用编码的UI测试库

这些测试不是一堆记录的动作,它们是在IE上编码和执行的

我正在使用VisualStudio2010,并尝试在Firefox 3.6上启动测试。我已经为此安装了所需的组件

问题是,我想选择用于测试应用程序的浏览器,因此我想更改字段“currentBrowser”,但我无法访问此属性,因为它是静态成员属性

我曾尝试使用反射库访问该属性,但无法实现

这是我当前代码的一个示例:

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);
我也尝试过这一行代码:

typeof(BrowserWindow).InvokeMember("set_CurrentBrowser", BindingFlags.InvokeMethod | BindingFlags.DeclaredOnly |BindingFlags.Public | BindingFlags.Static, null, bw, args,null,null,null);
但是属性的值“currentBrowser”从未被修改

我需要某种特殊权限吗?也许是“反思许可”


非常感谢

它应该是这样的:

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)
你说:

问题是,我想选择用于测试应用程序的浏览器,因此我想更改字段“currentBrowser”,但我无法访问此属性,因为它是静态成员属性

静态不阻止访问。这意味着您不需要该对象的实例来访问属性/方法。如果通过对象浏览器查看该类,您将看到:

public static string CurrentBrowser { get; set; }
所以我们知道我们可以访问它,因为它是公开的。我们可以获取值并设置值,因为它包含
get
设置没有隐藏访问修饰符