NUNIT 3.10.1 C#硒

时间:2019-01-16 18:07:48

标签: c# selenium-webdriver nunit

我只能执行第一种测试方法。即使代码正确,所有后续测试方法也无法执行。请参阅附件图像以获取错误消息。使用test.sdk(15.8.0),NUNIT(3.10.1),Selenium.WebDriver(3.13.0),Selenium.IEDriverServer.win64(3.9.0),Selenium.InternetExplorer.WebDriver(3.3.0){{3 }}

using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using System;
using OpenQA.Selenium.Interactions;
using System.Threading;

namespace Tests
{
    public class LandingPage
    {
        IWebDriver driver = new InternetExplorerDriver("C:\\Users\\M\\Desktop\\SL\\SLAutomation\\Core\\CoreLandingPage\\CoreLandingPage\\CoreLandingPage\\Drivers\\");

        [SetUp]
        public void Initialize()
        {
            driver.Navigate().GoToUrl("http://www.google.com");
            Console.WriteLine("Opened URL");
        }

        [Test]
        public void TestCase1()
        {
            Assert.That(2+2, Is.EqualTo(4));
            Console.WriteLine("Test case 1");
        }


        [Test]
        public void TestCase2()
        {
            Assert.That(2 * 2, Is.EqualTo(4));
            Console.WriteLine("Test case 2");
        }


        [TearDown]    
        public void CleanUp()
        {
            driver.Close();
            Console.WriteLine("Closed Browser");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您需要使用标记为[SetUp]的方法Initialize()实例化驱动程序。发生错误是因为在TestCase1()的末尾,调用CleanUp()并且驱动程序已关闭。然后TestCase2()出现并调用Initialize(),但驱动程序不再存在。您可以通过注释driver.Close();中的CleanUp()行来验证这一点。

您的代码应该看起来更像

public class LandingPage
{
    IWebDriver driver;

    [SetUp]
    public void Initialize()
    {
        driver = new InternetExplorerDriver("C:\\Users\\M\\Desktop\\SL\\SLAutomation\\Core\\CoreLandingPage\\CoreLandingPage\\CoreLandingPage\\Drivers\\");
        driver.Navigate().GoToUrl("http://www.google.com");
        Console.WriteLine("Opened URL");
    }
    ...