在测试代码中硬编码测试数据(5)

发表于:2012-09-11来源:不祥作者:知平软件点击数: 标签:自动化测试
下面是TestLibrary的完整源代码: public class TestLibrary { public UserHelper UserHelper { get ; private set ; } public BlogHelper BlogHelper { get ; private set ; } public CommentHelper Comm

  下面是TestLibrary的完整源代码:

public class TestLibrary

{

    public UserHelper UserHelper { get; private set; }

 

    public BlogHelper BlogHelper { get; private set; }

 

    public CommentHelper CommentHelper { get; private set; }

 

    public Blog DefaultBlog { get; private set; }

 

    public Post DefaultPost { get; private set; }

 

    public ISelenium Selenium { get; private set; }

 

    public string SiteUrl { get; private set; }

 

    public class Consts

    {

        public const string TimeToWaitForPageToLoad = "30000";

 

        public const string AdminUserName = "administrator";         

 

        public const string ContributorUser = "Contributor1";

 

        public const string AuthorUser = "Author1";

 

        public const string ModeratorUser = "Moderator1";

 

        public const string EditorUser = "Editor1";

 

        public const string CommonPassword = "0123456";

 

        public const string AdminPassword = "0123456";

 

        public const string DefaultSeleniumHost = "localhost";

 

        public const int DefaultSeleniumPort = 4444;

 

        public const string DefaultBrowser = "*firefox";

 

        public const string DefaultSite = "http://localhost:30320";

    }

 

    public TestLibrary(ISelenium selenium)

    {

        this.UserHelper = new UserHelper(this);

        this.BlogHelper = new BlogHelper(this);

        this.CommentHelper = new CommentHelper(this);

        Selenium = selenium;

 

        InitialDefaultSiteDate();

    }

 

    private void InitialDefaultSiteDate()

    {

        DefaultBlog = new Blog(this, "Default Test Blog", "default-test-blog", "Default Test Blog", Consts.AdminUserName);

        DefaultPost = new Post(this, new PostSettings()

        {

            Title = "Default Test Post",

            Permalink = "default-test-post",

            Body = "This is for web site testing purpose.",

            Tags = "Test",

            PublishSetting = PostSettings.PublishSettings.PublishNow

        },

        DefaultBlog);

    }

 

    public static TestLibrary SetupTest(TestContext testContext)

    {

        if (testContext != null && testContext.DataRow != null && testContext.DataRow.Table.Columns.Contains("seleniumHost"))

        {

            return SetupTest(testContext.DataRow["seleniumHost"].ToString(),

                Int32.Parse(testContext.DataRow["seleniumPort"].ToString()),

                testContext.DataRow["browser"].ToString(),

                testContext.DataRow["site"].ToString());

        }

        else

        {

            return SetupTest(Consts.DefaultSeleniumHost, Consts.DefaultSeleniumPort,

                Consts.DefaultBrowser, Consts. DefaultSite);

        }

    }

 

    public static TestLibrary SetupTest(string seleniumHost, int seleniumPort,

        string browser, string site)

    {

        var selenium = new DefaultSelenium(

            seleniumHost, seleniumPort, browser, site);

        selenium.Start();

 

        return new TestLibrary(selenium) { SiteUrl = site };

    }

 

    public void Shutdown()

    {

        try

        {

            Selenium.Stop();

        }

        catch (Exception)

        {

            // Ignore errors if unable to close the browser

        }

    }

}

原文转自:http://www.ltesting.net