2011-01-10 64 views
0

我正在使用MSTest(Visual Studio)單元測試來運行Selenium來測試網站的功能。我想要做的是能夠將一些配置變量傳遞給我的測試。比如,服務器地址,Selenium瀏覽器類型等等。我一直在嘗試使用TestContext,但除了使用LoadTests來傳遞這些信息外,似乎沒有任何其他用途。Visual Studio將配置值傳遞給UnitTests

我也試過使用Spring.NET,但似乎也沒有幫助。

使用TestContext的任何想法?或者也許別的東西。

謝謝

回答

1

我想我會分享我最終做的。我使用Spring.net將這些設置注入SeleniumSettings類中,

<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd" > 
    <object id="Settings" type="Sample.SeleniumSettings, Sample" singleton="true"> 
    <property name="Server" value="localhost"/> 
    <property name="Port" value="4444"/> 
    <property name="Browser" value="*firefox" /> 
    <property name="Url" value="http://website.com"/> 
    <property name="Email" value="[email protected]"/> 
    </object> 
</objects> 

這會將SeleniumSettings注入Test類的一個名爲Settings的屬性中。測試需要繼承AbstractDependencyInjectionSpringContextTests,並執行;

protected override string[] ConfigLocations 

設置類看起來像這樣;

public class SeleniumSettings 
{ 
    public const string DefaultEmailAddress = "[email protected]"; 
    public const string DefaultServerAddress = "localhost"; 
    public const string DefaultProtocol = "http://"; 
    public const string DefaultEndPoint = "/"; 

    public string Server = DefaultServerAddress; 
    public int Port = 4444; 
    public string Browser = "*firefox"; 
    public string Url = "http://localhost"; 
    public string Email = DefaultEmailAddress; 

    public ISelenium factory() 
    { 
     return new DefaultSelenium(Server, Port, Browser, Url); 
    } 
} 

然後使用SeleniumSettings.factory()來獲得DefaultSelenium對象與運行測試。

Selenium文檔有這方面的一些信息,但它潛伏得太快,並跳過設置這些東西所需的基本信息。

我試圖將DefaultSelenium對象最初注入類中,但是我在Selenium內部崩潰時遇到了問題。它似乎不喜歡被Spring.net注入創建。

我希望這可以幫助別人。

相關問題