![](/img/trans.png)
[英]Troubles changing value of CurrentBrowser variable in BrowserWindow in Coded UI Tests
[英]Troubles changing current browser in Coded UI Tests
我在C#中編寫了一些自動化Web測試,他們使用Coded UI測試庫。
這些測試不是一堆記錄的動作,它們已在IE上編碼和執行。
我正在使用Visual Studio 2010,我正在嘗試在Firefox 3.6上啟動我的測試。 我已經為此安裝了所需的組件。
問題是我想選擇我想用來測試我的應用程序的瀏覽器,所以我想更改字段“currentBrowser”但我無法訪問此屬性,因為它是一個靜態成員屬性。
我曾嘗試使用Reflection庫來訪問該屬性,但我無法實現它。
這是我當前代碼的一個示例:
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”之類的東西
非常感謝你
它應該看起來像這樣:
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;
並set;
沒有隱藏訪問修飾符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.