在测试代码中硬编码测试数据
作者:网络转载 发布时间:[ 2012/9/12 9:13:14 ] 推荐标签:
在上面的代码中,我也把等待网页刷新的时间设置成常量。对于在测试代码中使用事先在基准数据库中准备的测试数据,需要一点编程技巧。请先看下面的代码,下面的代码是一段记录通过网页操作创建文章的代码:
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;
}
// 下面省略文章相关的操作若干
// ...
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11