使用c#/selenium设置避免冗余

使用c#/selenium设置避免冗余,c#,selenium,webdriver,C#,Selenium,Webdriver,我刚刚开始通过SeleniumWebDriver学习自动化的基础知识,我正在努力“清理”我编写的测试。在解决方案中,我将每个测试分离到它自己的案例中;然而,由于每个测试用例都有相同的设置和拆卸,很明显,在解决方案中为每个单独的测试用例执行这两项操作是多余的。是否有一种方法可以在所有测试运行之前执行一次设置,在所有测试运行完成之后执行一次拆卸 下面是一个示例测试用例: using System; using System.Text; using System.Text.RegularExpress

我刚刚开始通过SeleniumWebDriver学习自动化的基础知识,我正在努力“清理”我编写的测试。在解决方案中,我将每个测试分离到它自己的案例中;然而,由于每个测试用例都有相同的设置和拆卸,很明显,在解决方案中为每个单独的测试用例执行这两项操作是多余的。是否有一种方法可以在所有测试运行之前执行一次设置,在所有测试运行完成之后执行一次拆卸

下面是一个示例测试用例:

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace SeleniumTests
{
    [TestFixture]
    public class TestCase3Final
    {
    private IWebDriver driver;
    private StringBuilder verificationErrors;
    private string baseURL;
    private bool acceptNextAlert = true;

    [SetUp]
    public void SetupTest()
    {
        driver = new FirefoxDriver();
        baseURL = "http://adam.goucher.ca/parkcalc/index.php";
        verificationErrors = new StringBuilder();
    }

    [TearDown]
    public void TeardownTest()
    {
        try
        {
            driver.Quit();
        }
        catch (Exception)
        {
            // Ignore errors if unable to close the browser
        }
        Assert.AreEqual("", verificationErrors.ToString());
    }

    [Test]
    public void TestCase3()
    {
        driver.Navigate().GoToUrl(baseURL);
        driver.FindElement(By.Id("EntryDate")).Clear();
        driver.FindElement(By.Id("EntryDate")).SendKeys("01/02/2014");
        driver.FindElement(By.Id("ExitDate")).Clear();
        driver.FindElement(By.Id("ExitDate")).SendKeys("01/01/2014");
        driver.FindElement(By.Name("Submit")).Click();
        try
        {
            Assert.AreEqual("ERROR! YOUR EXIT DATE OR TIME IS BEFORE YOUR ENTRY DATE OR TIME", driver.FindElement(By.CssSelector("b")).Text.Trim());
        }
        catch (AssertionException e)
        {
            verificationErrors.Append(e.Message);
        }
    }
    private bool IsElementPresent(By by)
    {
        try
        {
            driver.FindElement(by);
            return true;
        }
        catch (NoSuchElementException)
        {
            return false;
        }
    }

    private bool IsAlertPresent()
    {
        try
        {
            driver.SwitchTo().Alert();
            return true;
        }
        catch (NoAlertPresentException)
        {
            return false;
        }
    }

    private string CloseAlertAndGetItsText() {
        try {
            IAlert alert = driver.SwitchTo().Alert();
            string alertText = alert.Text;
            if (acceptNextAlert) {
                alert.Accept();
            } else {
                alert.Dismiss();
            }
            return alertText;
        } finally {
            acceptNextAlert = true;
        }
    }
    }
}

谢谢

使用基类并为方法指定属性,这样NUnit仍然可以理解基本测试夹具

[TestFixture]
public class TestCase3Final : BaseTestFixture

因此,如果有一个所有其他测试夹具都继承自的基本测试夹具,那么只要它们继承自该夹具,就会为每个夹具调用setup方法

请注意,您的测试来自IDE或一些录制和回放。自动化+适当的“清理测试”之类的东西要求您对所使用的语言有相当的了解,并理解OO原则。

显然,对解决方案中的每个单独测试用例都执行这两种操作是多余的。-即使是冗余的,您也希望避免在单独的单元测试之间保持状态。例如,如果单元测试B在A之后运行时失败,那么最好对每个测试执行设置和拆卸。
public class BaseTestFixture {
 [TestFixtureSetup]
 public void SetupStuff() {
  // set up driver here using a [TestFixtureSetup] attribute
  // it will be run for every class that inherits from it.
 }
}