如何使用 Selenium 运行我的 NUnit 测试用例以针对不同环境运行? [英] How do I run my NUnit test cases using Selenium to run against different environments?

查看:41
本文介绍了如何使用 Selenium 运行我的 NUnit 测试用例以针对不同环境运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Selenium 编写了 NUnit 测试用例来测试 Web 应用程序.我想针对不同的环境(例如 QA、暂存和生产)运行相同的测试用例,实现这一目标的最简单方法是什么?

I have written NUnit test cases using Selenium to test a web application. And I would like to run the same test cases against different environments (e.g. QA, Staging, & Production) What's the easiest way to achieve that?

推荐答案

NUnit 支持参数化测试装置和参数化测试.所以第一件事是,您是要针对不同的环境运行特定的测试,还是要针对两种环境重新运行整个测试装置?

NUnit supports parametrised test fixtures as well as parametrised tests. So the first thing is that are you going to want to run specific tests against different environments, or is it the entire test fixture will be rerun for both environments?

我这么问是因为这个问题的答案决定了你将在哪里传递参数(环境).如果您只想重新运行整个测试装置,您应该在测试装置级别传递环境,即创建参数化测试装置.如果您只想针对这些环境运行特定测试,则必须将其传递给每个单独的测试用例.下面是我如何处理同一类事情的示例:

I ask because the answer to this determines where you will pass the parameter (the environment). If you are just wanting to rerun the whole test fixture, you should pass the environment in at a test fixture level, that is to create parametrised test fixtures. If you want to run only particular tests against those environment, you'll have to pass it in to each individual test case. An example is below of how I've gone about the same sort of thing:

首先创建一种方法来定义测试可以附加"到什么环境".我建议也许将它推入 app.config 并有一个设置"类和一个枚举:

First create a way to define what 'environment' the tests can 'attach' to. I'd suggest perhaps shoving this into the app.config and have a 'Settings' class and an enum to go with it:

public enum Environment
{
    QA,
    Production,
    Hotfix,
    Development
}

public class Settings
{   
    public static string QAUrl { get { return "some url"; } }

    public static string ProductionUrl { get { return "some url"; } }

    public static string HotfixUrl { get { return "some url"; } }

    public static string DevUrl { get { return "some url"; } }
}

上面的一些 url"将从您的配置文件中读取或硬编码,但您可以随意.

The above "some url" would be read from your configuration file or hardcoded, however you please.

我们现在已经有了一个环境的概念,它是 URL,但它们没有链接在一起或以任何方式相关.理想情况下,您希望为其提供枚举的QA"值,然后它会为您整理出 URL.

We've now got a concept of an environment, and it's URL, but they are not linked together or related in any way. You would ideally want to give it the 'QA' value of your enum and then it will sort out the URL for you.

接下来创建一个基础测试装置,您的所有测试装置都可以从它继承,它保持当前环境.我们还创建了一个 Dictionary,它现在将环境值与其 URL 相关联:

Next create a base test fixture that all your test fixtures can inherit from, which keeps hold of the current environment. We also create a Dictionary that now relates the environment value to it's URL:

public class BaseTestFixture
{
    private Dictionary<Environment, string> PossibleEnvironments 
    {
        get
        {
            return new Dictionary<Environment, string>()
            {
                { Environment.QA, Settings.QAUrl },
                { Environment.Production, Settings.ProductionUrl },
                { Environment.Hotfix, Settings.HotfixUrl },
                { Environment.Development, Settings.DevelopmentUrl },
            }
        }
    }

    private Environment CurrentEnvironment { get; set; }

    public BaseTestFixture(Environment environment)
    {
        CurrentEnvironment = environment;
    }
}

您可能可以使用反射来计算什么 URL 映射到什么 enum 值.

You could probably use Reflection to have it work out what URL's map to what enum value's.

太酷了,我们有一个可以运行的环境.以管理员身份登录您网站的示例测试:

So cool, we've got an environment we can run against. A sample test to go to login as an administrator to your site:

public class LoginToSite
{
    [Test]
    public void CanAdministratorSeeAdministratorMenu()
    {
        // go to the site
        driver.Navigate().GoToUrl("production site");
        // login as administrator
    }
}

我们如何让它转到特定的 URL?让我们稍微修改一下我们的基类...

How do we get this to go to the specific URL? Let's modify our base class a little...

public class BaseTestFixture
{
    private Dictionary<Environment, string> PossibleEnvironments 
    {
        get
        {
            return new Dictionary<Environment, string>()
            {
                { Environment.QA, Settings.QAUrl },
                { Environment.Production, Settings.ProductionUrl },
                { Environment.Hotfix, Settings.HotfixUrl },
                { Environment.Development, Settings.DevelopmentUrl },
            }
        }
    }

    private Environment CurrentEnvironment { get; set; }

    protected string CurrentEnvironmentURL 
    { 
        get 
        { 
            string url;
            if (PossibleEnviroments.TryGetValue(CurrentEnviroment, out url))
            {
                return url;
            }

            throw new InvalidOperationException(string.Format("The current environment ({0}) is not valid or does not have a mapped URL!", CurrentEnviroment));
        }
    }

    public BaseTestFixture(Environment environment)
    {
        CurrentEnvironment = environment;
    }

            public BaseTestFixture()
            {
            }
}

我们的基类现在可以告诉我们,取决于我们所处的环境,去哪个页面......

Our base class now can tell us, depending on what environment we are in, what page to go to...

所以我们现在有了这个测试,继承自我们的基础:

So we now have this test, inheriting from our base:

public class LoginToSite : BaseTestFixture
{
    [Test]
    public void CanAdministratorSeeAdministratorMenu()
    {
        // go to the site
        driver.Navigate().GoToUrl(CurrentEnvironmentURL);
        // login as administrator
    }
}

然而,这很好,但上面的内容无法编译......为什么?我们实际上还没有给它一个环境,所以我们必须传入一个......

However, that's great, but the above won't compile...why? We are not actually giving it an environment yet so we must pass one in...

[TestFixture(Environment.QA)]
public class LoginToSite : BaseTestFixture
{
    [Test]
    public void CanAdministratorSeeAdministratorMenu()
    {
        // go to the site
        driver.Navigate().GoToUrl(CurrentEnvironmentURL);
        // login as administrator
    }
}

太好了,它现在已经传入了环境,URL 的检查等现在都在后台为您完成了……但是这 仍然 不会编译.既然我们在使用继承,就得有一个构造函数来为我们传递下去:

That's great, it now has the environment passed in, the checking of the URL etc are all done in the background for you now...however this still won't compile. Since we are using inheritance, we have to have a constructor to pass it down for us:

    public LoginToSite(Environment currentEnvironment)
    {
        CurrentEnvironment = currentEnvironment;
    }

等等.

对于具体的测试用例,这个稍微简单一些,拿我们之前的测试用例:

As for specific test cases, this is a little easier, taking our test case from earlier:

public class LoginToSite
{
    [TestCase(Environment.QA)]
    public void CanAdministratorSeeAdministratorMenu(Environment environment)
    {
        // go to the site
        driver.Navigate().GoToUrl("production site");
        // login as administrator
    }
}

哪个会在环境中传递到那个特定测试用例.然后你需要一个新的 Settings 类来为你做环境检查(和我以前做的类似):

Which would pass in the environment into that specific test case. You would then need a new Settings class of some sort, to do the environment checking for you (in a similar way as I did before):

public class EnvironmentHelper
{
    private static Dictionary<Environment, string> PossibleEnvironments 
    {
        get
        {
            return new Dictionary<Environment, string>()
            {
                { Environment.QA, Settings.QAUrl },
                { Environment.Production, Settings.ProductionUrl },
                { Environment.Hotfix, Settings.HotfixUrl },
                { Environment.Development, Settings.DevelopmentUrl },
            }
        }
    }

    public static string GetURL(Environment environment)
    {
        string url;
        if (PossibleEnviroments.TryGetValue(environment, out url))
        {
            return url;
        }

        throw new InvalidOperationException(string.Format("The current environment ({0}) is not valid or does not have a mapped URL!", environment));
    }
}

这篇关于如何使用 Selenium 运行我的 NUnit 测试用例以针对不同环境运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆