Troubles changing current browser in Coded UI Tests

扶醉桌前 提交于 2019-12-11 11:39:16

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/21672724/troubles-changing-current-browser-in-coded-ui-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!