2014-02-10 27 views
0

我在C#中編寫了一些自動化Web測試代碼,他們使用Coded UI測試庫。在Coded UI測試中更改當前瀏覽器的問題

這些測試不是一堆錄製的動作,而是在IE上編碼和執行的。

我正在使用Visual Studio 2010,我試圖在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」從來沒有被修改。

我需要某種特殊權限嗎?也許像「性ReflectionPermission」

非常感謝您

回答

2

它應該是這個樣子:

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;和,且沒有隱藏訪問修飾符。