c# - 如何在运行时提示用户输入——编码的UI自动化测试

标签 c# visual-studio-2010 testing ui-automation coded-ui-tests

Visual Studio Permium 2010 中的编码 UI 测试 - C#

我有一堆编码的 UI 测试,对于其中一个,我想提示用户输入访问代码,稍后将在自动化测试中使用。

这些是数据驱动的测试,但是在测试开始之前访问代码不可用。其中一个自动化步骤是在线注册用户,并通过电子邮件接收访问代码。

我希望在测试期间出现提示,用户可以在自动测试继续之前输入访问代码。

我考虑过查询数据库以获取访问代码,但这不是一个选项,因为代码存储在事件目录中,测试机器无法访问它,所以我不得不这样做。任何建议将不胜感激。谢谢!

最佳答案

也许更好的解决方案是为您的服务器抽象您的电子邮件接口(interface),并在您的测试套件中创建一个模拟电子邮件接口(interface),该接口(interface)可以拦截消息以接收带有访问代码的电子邮件。这样,您也不依赖于网络可用性来运行您的测试。

一些代码来解释我的意思:

interface IEmailAPI{
    void SendMessage(string from, string to, string subject, string body);
}

class RealEmailAPI : IEmailAPI
{
    System.Net.Mail.SmtpClient client;
    public RealEmailAPI(string host, int port)
    {
        client = new System.Net.Mail.SmtpClient(host, port);
    }

    public void SendMessage(string from, string to, string subject, string body)
    {
        client.Send(from, to, subject, body);
    }
}

class MockEmailAPI : IEmailAPI
{
    public string LastAccessCode{get;private set;}
    public void SendMessage(string from, string to, string subject, string body)
    {
        var findCode = new System.Text.RegularExpressions.Regex(""); // whatever this takes
        LastAccessCode = findCode.Match(body).Value;
    }
}

class MyServer
{
    IEmailAPI emailer;
    public MyServer(IEmailAPI emailSystem)
    {
        this.emailer = emailSystem;
    }

    public void SendAccessCode(string to, string code)
    {
        this.emailer.SendMessage("whatever@what.ever", to, "Access Code", code);
    }
}

class TestMyServer
{
    public void TestAPICodeGen()
    {
        var email = new MockEmailAPI();
        var server = new MyServer(email);
        var code = "XYZZY";
        server.SendAccessCode("not.important@discard.com", code);
        Assert.AreEqual(code, email.LastAccessCode);
    }
}

关于c# - 如何在运行时提示用户输入——编码的UI自动化测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18470071/

相关文章:

c# - 为移动浏览器设计网页的技巧?

c# - 为什么 .Contains 很慢?通过主键获取多个实体的最有效方法?

visual-studio-2010 - 有没有一种简单的方法来分发 Access 数据库引擎

python - 排除 coverage 中的 'else' 子句

c# - 如何从 XAML 内部访问嵌套命名空间?

c# - Visual Studio 2010 未在工具箱中显示打印选项卡的 Crystal Report Viewer?

c# - VS 2010 鼠标处理器扩展 - 不工作

visual-studio-2010 - TFS/Visual Studio 2010 : Clear cached password for TFS in Visual Studio 2010, 需要它让我重新登录

node.js - 测试一个使用 promises 的函数

testing - 与 Yii 的持续集成