麻烦在编码的UI测试中更改当前浏览器 [英] Troubles changing current browser in Coded UI Tests
问题描述
我已经用C#编写了一些自动化的Web测试,它们使用了已编码的UI测试库。
这些测试不是一堆记录下来的动作,它们已经被编码并在IE上执行。
我正在使用Visual Studio 2010,并且试图在Firefox 3.6上启动测试。我已经为此目的安装了必需的组件。
问题是我想选择要用来测试应用程序的浏览器,所以我想更改字段 currentBrowser,但由于是静态成员属性而无法访问此属性。
我尝试使用反射库访问该属性,但是我无法实现这一目标。
这是我当前代码的示例:
BrowserWindow browserWin =新的BrowserWindow();
//复制dll
Assembly testAssembly = Assembly.LoadFile(@ c:\Microsoft.VisualStudio.TestTools.UITesting.dll);
//从刚刚加载的程序集中获取类BrowserWindow的类型
类型BrowserWindowType = testAssembly.GetType( Microsoft.VisualStudio.TestTools.UITesting.BrowserWindow);
对象browserInstance = Activator.CreateInstance(BrowserWindowType,new object [] {});
PropertyInfo BrowserPropertyInfo = BrowserWindowType.GetProperty( CurrentBrowser);
//属性的设置值:public
BrowserPropertyInfo.SetValue(browserInstance, FireFox,null);
//获取属性值:public
字符串值=(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从未修改。
我需要某种特殊权限吗?也许像 ReflectionPermission之类的东西
非常感谢
看起来应该像这样:
BrowserWindow.CurrentBrowser = firefox;
BrowserWindow bw = BrowserWindow.Launch(new Uri( uriAsString));
//此时,如果您已安装当前的跨浏览器
//组件并安装了预期版本的firefox(我有26个)
$ c $,它将启动firefox c>
您说:
问题是我我想选择我要用来测试我的应用程序的浏览器,所以我想更改字段 currentBrowser,但是由于具有静态成员属性,我无法访问此属性。
静态不会阻止访问。这意味着您不需要该对象的实例即可访问属性/方法。如果通过对象浏览器查看该类,则会看到以下内容:
公共静态字符串CurrentBrowser {get;组; }
所以我们知道我们可以使用它,因为它是公开的。我们可以获取值并设置值,因为它包含 get;
和 set;
而没有隐藏访问修饰符。 / p>
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
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.
这篇关于麻烦在编码的UI测试中更改当前浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!