c# - 如何访问 RunSettings 文件中的 TestRunParameters

标签 c# asp.net coded-ui-tests vs-unit-testing-framework runsettings

通读https://msdn.microsoft.com/en-us/library/jj635153.aspx我创建了一个 .RunSettings 文件,其中包含一些类似于示例的参数:

  <TestRunParameters>
    <Parameter name="webAppUrl" value="http://localhost" />
    <Parameter name="webAppUserName" value="Admin" />
    <Parameter name="webAppPassword" value="Password" />
  </TestRunParameters>

我计划为我们的每个环境创建一个 .RunSettings 文件,其中包含适当的 URL 和凭据,以便在指定的 RunSettings 文件的环境中运行 CodedUI 测试。

我可以看到从命令行引用我可以运行的设置文件:

vstest.console myTestDll.dll /Settings:Local.RunSettings /Logger:trx
vstest.console myTestDll.dll /Settings:QA.RunSettings /Logger:trx

等...

但我看不到任何方法可以说明如何从 codedUI 测试中实际利用 TestRunParameters

我想做的是设置测试初始值设定项,使用 TestRunParameters 来确定在哪里登录,以及使用什么凭据。像这样:

[TestInitialize()]
public void MyTestInitialize()
{

    // I'm unsure how to grab the RunSettings.TestRunParameters below
    string entryUrl = ""; // TestRunParameters.webAppUrl
    string userName = ""; // TestRunParameters.webAppUserName
    string password = ""; // TestRunParameters.webAppPassword

    LoginToPage(entryUrl, userName, password);
}

public void LoginToPage(string entryUrl, string userName, string password)
{
    // Implementation
}

非常感谢有关如何引用 TestRunParameters 的信息!

编辑

/// <summary>
/// Summary description for CodedUITest1
/// </summary>
[CodedUITest]
public class CodedUITest1
{

    public static string UserName = string.Empty;

    [ClassInitialize]
    public static void TestClassInitialize(TestContext context)
    {
        UserName = context.Properties["webAppUserName"].ToString();
        Console.WriteLine(UserName);
    }

    [TestMethod]
    public void CodedUITestMethod1()
    {
        this.UIMap.RecordedMethod1();
        // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
    }

    // Rest of the default class - TestContext instantiation, UI map instantiation, etc
}

我在运行时遇到的异常:

空引用异常

enter image description here

@williamfalconeruk 我已经按照上面的方式更新了我的测试类,但我仍然遇到同样的错误,知道我做错了什么吗?

最佳答案

对于那些使用 Resharper 解决这个问题的人,我发现了修复方法(无需禁用 Resharper):

  1. 转到 Visual Studio 顶部菜单 -> Resharper -> 选项

  2. 找到“工具”部分,展开“单元测试”

  3. 点击“MsTest”。该复选框应处于启用状态,但下方的测试设置文件路径可能为空。如果是,请单击浏览并选择您要使用的运行设置文件。

  4. 点击保存、重建并尝试运行测试,参数现在应该可以工作了。

不知道为什么,只是简单地从测试菜单中选择测试设置文件 -> 测试设置在使用 Resharper 时实际上不起作用,因此需要在 Resharper 选项中直接明确指向该文件。

关于c# - 如何访问 RunSettings 文件中的 TestRunParameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31707415/

相关文章:

asp.net - 如何在电子邮件正文中缩放图像?

c# - 使用 C# 中的互操作将宏插入并运行到 Word 中

c# - 如何在没有 Azure Bot 服务的情况下向 Bot Framework v4 项目添加身份验证?

asp.net - 如何在 ASP.NET MVC 模型中保存选定的 DropDownList 值以进行 POST?

asp.net - 如何使用 OWIN OAuthBearerAuthentication 验证访问 token ?

coded-ui-tests - 编码UI测试: How to run from Web Portal (MS Test Manager's replacement)

c# - 在静态上下文中调用 C# 扩展方法是否有效?

.net - 在编码 UI 中与应用程序的多个实例交互

c# - “int” 和 “uint”/“long” 和 “ulong” 有什么区别?

c# - 如何在 C# 或 VB.Net 中获取没有 WWW 的域名