在上面的代碼中,我也把等待網頁刷新的時間設置成常量。對于在測試代碼中使用事先在基準數據庫中準備的測試數據,需要一點編程技巧。請先看下面的代碼,下面的代碼是一段記錄通過網頁操作創建文章的代碼:
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; }
// 下面省略文章相關的操作若干 // ...
} |