SpecFlow框架中使用的URL变量

SpecFlow框架中使用的URL变量

本文介绍了如何设置要在NUnit/SpecFlow框架中使用的URL变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我在SpecFlow中编写的Test Harness进行扩展,使其更具可扩展性,因此我想在设置中创建一个基本URL变量,可以根据我使用的标志进行设置在Nunit跑步者上因此,如果我以标签的形式发送Test,我希望将某些URL值设置为"http://test/",或者将Development的URL设置为"http://dev/".我知道全局变量在NUnit中没有用,我以前的大多数脚本都是在Perl中进行的,即使那时我也很少使用它.我不确定我是否正确执行了此操作,尽管我可以正确地编译代码,但从未设置URL.我正在做的是检查NUnit运行器何时启动:

I'd like to extend the Test Harness I have written in SpecFlow to be a bit more extensible, so what I would like to do is in the setup create a base URL variable that I can set depending on a flag I use on the Nunit runner. So if I send in Test as a Tag I want some URL value to be set to "http://test/" or for Development to set URL to "http://dev/". I know Global variables are not useful in NUnit, most of my previous scripting was in Perl where even then I used it on rare occasions. I'm not sure I am doing this right, although I get the code to compile without errors the URL never get's set. What I am doing is a check when the NUnit runner starts:

private static testInfoConfiguration myUrl;
public static string baseUrl = string.Empty;
[BeforeFeature("Test")]
public static void BeforeFeature_Test()
{

    myUrl = new testInfoConfiguration();
    baseUrl = myUrl.setBaseUrl("Test");
}

这叫什么:

public class testInfoConfiguration
{
public string setBaseUrl(string envType)
    {
        string envUrl;
        if (envType == "Test")
        {
            envUrl = "http://testweb/";
            return envUrl;
        }
        if (envType == "Prod")
        {
            envUrl = "http://www/";
            return envUrl;
        }
        envUrl = "http://devweb/";
        return envUrl;
    }

然后我想稍后再调用URL变量:

I then want to make a call to the URL variable later on:

[When(@"I access the Web Site")]
public void WhenIAccessTheWebSite()
{
    string kcUrl = baseUrl + "/knowledge/Pages/default.aspx";
    driver.Navigate().GoToUrl(kcUrl);

当我调用URL变量时,它仍然为空.有没有办法做到这一点?我仍在学习C#,NUnit和SpecFlow,所以我可能只是没有在正确的方面寻找问题所在.或者只是真的不了解如何以这种方式设置变量.

When I call the URL variable it's still empty. Is there a way to do this? I'm still learning C#, NUnit and SpecFlow so I am probably just not looking at the right aspect of this to where I am going wrong. Or just really not understanding how to set a variable in this manner.

调整现有代码

推荐答案

在此代码块中:

private static testInfoConfiguration myUrl;
public string baseUrl = "";
[BeforeFeature("Test")]
public static string BeforeFeature_Test()
{

    myUrl = new testInfoConfiguration();
    string baseUrl = myUrl.setBaseUrl("Test");
    return baseUrl;
}

您要定义baseUrl两次:一次在方法范围内,一次在类范围内.

You're defining baseUrl twice: once in the scope of the method, and once in the scope of the class.

在此代码段中:

string kcUrl = baseUrl + "/knowledge/Pages/default.aspx";
driver.Navigate().GoToUrl(kcUrl);

您指的(我假设)是实例字段baseUrl,您从未设置过.

You're referring to (what I assume) is the instance field baseUrl, which you never set.

请尝试以下操作:

private static testInfoConfiguration myUrl;
public static string baseUrl = string.Empty;
[BeforeFeature("Test")]
public static void BeforeFeature_Test()
{
    myUrl = new testInfoConfiguration();
    baseUrl = myUrl.setBaseUrl("Test");
}

这篇关于如何设置要在NUnit/SpecFlow框架中使用的URL变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:16