在上面的代码中,我也把等待网页刷新的时间设置成常量。对于在测试代码中使用事先在基准数据库中准备的测试数据,需要一点编程技巧。请先看下面的代码,下面的代码是一段记录通过网页操作创建文章的代码:

public class Blog : UIHelperBase

{


    // 博客的标题

 

    public string Title { get; private set; }


 

    // 博客的超链接

    public string Permalink { get; private set; }


 

 

    // 博客的超链接文本

    public string MenuText { get; private set; }

 

 

    public string Owner { get; private set; }

 

 

    public Blog(TestLibrary settings, string title,

        string permalink, string menutext, string owner)

        : base(settings)

    {


        Title = title;

        Permalink = permalink;

        MenuText = menutext;

 

        Owner = owner;

    }

 

    // 通过网页界面的操作创建一篇新文章

 

    //

    // PostSetting是一个结构,包含了一篇新文章的所有元素,

    // 例如文章标题,内容等等.


    public Post CreatePost(PostSettings settings)

    {

 

        if (settings == null)

 

            throw new CaseErrorException(new ArgumentNullException("settings"));

 

        if (!String.IsNullOrEmpty(settings.Body))


            throw new CaseErrorException("Set post body is not implemented yet!");

        if (settings.PublishDateTime.HasValue)

 

            throw new CaseErrorException("PublishDateTime is not implemented yet!");


 

 

        // selenium这个变量,你可以想象成是一个正在浏览网页的网友的封装

        selenium.Open("/");

 

        selenium.Click("link=Admin");


        selenium.WaitForPageToLoad(TestLibrary.Consts.TimeToWaitForPageToLoad);

        selenium.Click("link=Manage Blogs");


        selenium.WaitForPageToLoad("60000");

        selenium.Click(String.Format("link={0}", Title));


        selenium.WaitForPageToLoad(TestLibrary.Consts.TimeToWaitForPageToLoad);

        selenium.Click("link=New Post");

        selenium.WaitForPageToLoad(TestLibrary.Consts.TimeToWaitForPageToLoad);

 

        selenium.Type("Routable_Title", settings.Title);

        selenium.Type("Tags", settings.Tags);

 

        if (settings.Permalink != null)

 

            selenium.Type("Routable_Slug", settings.Permalink);


        if (settings.DisableNewComments)

            selenium.Click("CommentsActive");

 

        if (settings.PublishSetting == PostSettings.PublishSettings.PublishNow)


            selenium.Click("Command_PublishNow");

        else if ( settings.PublishSetting == PostSettings.PublishSettings.PublishLater )

 

            throw new CaseErrorException("PublishLater is not implemented yet!");


 

 

        selenium.Click("submit.Save");

        selenium.WaitForPageToLoad(TestLibrary.Consts.TimeToWaitForPageToLoad);

 

 

        return new Post(TestSettings, settings, this);

 

    }

}

 

public class PostSettings

{


    public enum PublishSettings

    {

 

        SaveDraft,

        PublishNow,

 

        PublishLater


    }

 

    public string Title { get; set; }


 


    public string Permalink { get; set; }

 

 

    public string Body { get; set; }

 

 

    public string Tags { get; set; }

 

 

    public bool DisableNewComments { get; set; }


 

    public PublishSettings PublishSetting { get; set; }

 

    public DateTime? PublishDateTime { get; set; }


}

 

 

public class Post : UIHelperBase

 

{


// 当初创建文章的原始详细信息

 

    public PostSettings Settings { get; private set; }


 

 

    // 文章的标题 – 从网页上获取

 

    public string Title { get { return selenium.Read(...); } }

 

// 下面省略文章相关的操作若干


// ...

 

    public Post(TestLibrary settings, PostSettings postSettings, Blog blog)


        : base(settings)


    {

        Settings = postSettings;

        ContainerBlog = blog;


}

 

// 下面省略文章相关的操作若干


// ...

 
}