【问题标题】:NUnit will not run consecutive Selenium Webdriver C# testsNUnit 不会运行连续的 Selenium Webdriver C# 测试
【发布时间】:2016-01-26 21:34:24
【问题描述】:

Selenium Webdriver 2.48、C#、NUnit 2.6.4、Chrome 驱动程序 当我从 NUnit 测试运行器运行我的测试时,如果单独运行,它们都会通过。

如果我选择一个主标题节点,并选择“运行”,组中的第一个测试将运行,其余的将失败。

如果我让测试夹具 [TearDown] 在每次测试结束时关闭驱动程序,则会出现以下错误: “无效的操作异常:没有这样的会话”

如果我让测试夹具 [TearDown] 退出驱动程序,则会出现以下错误: “意外错误。System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:无法建立连接,因为目标机器主动拒绝它 127.0.0.1:13806 在 System.Net.Sockets.Socket.DoConnect(端点 endPointSnapshot,SocketAddress 套接字地址) 在 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& 套接字, IPAddress& 地址, ConnectSocketState 状态, IAsyncResult asyncResult, Exception& 异常)"

使用 driver.Quit() 或 driver.Close() 对结果没有影响 - 仅运行组中的第一个测试。

我已搜索但无法找到解决方案。必须可以通过从最顶层节点运行来运行所有测试,而不必选择每个测试并单独运行它们。任何帮助,将不胜感激。谢谢。迈克尔

这是一个在一个类中有两个测试的示例。我已经从测试中删除了大部分方法,因为它们很长。

using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium;
using NUnit.Framework;
using SiteCore.HousingRepairsLogic;

namespace SiteCore.HousingRepairsTests.DoorsAndWindowsTests
{
[TestFixture]
class DoorsTests
{
    private IWebDriver driver = new     ChromeDriver(@"C:\chromedriver_win32");

    [SetUp]
    public void setup()
    {
        HousingRepairsLogic.Utilities utilities = new Utilities(driver);
        utilities.NavigateToLogin();
    }

    [TearDown]

    public void teardown()
    {
        Utilities utilities = new Utilities(driver);
        utilities.CloseDriver();
    }


    [Test]

    public void LockRepair()
    {

        //Create Instance of the HomePage class
        HomePage homepage = new HomePage(driver);

        homepage.ClickHousingButton();
        homepage.RequestRepairButton();
        homepage.RequestRepairNowButton();
}
  [Test]

    public void ExternalWoodDoorFrameDamaged()
    {


        //Create Instance of the HomePage class
        HomePage homepage = new HomePage(driver);

        homepage.ClickHousingButton();
        homepage.RequestRepairButton();
        homepage.RequestRepairNowButton();

        //Create instance of TenancyPage class

        TenancyPage tenancy = new TenancyPage(driver);
        //proceed with login
        tenancy.ClickYesLoginButton();
        //enter username
        tenancy.EnterMyeAccountUserName();
        //enter password
        tenancy.EnterMyeAccountPassword();
        //click the login button
        tenancy.ClickLoginButton();
}
    }

【问题讨论】:

  • 您可以添加代码吗?具有设置、拆卸和测试方法的类
  • 感谢您的回复。我编辑了原始消息并添加了代码。

标签: selenium-webdriver nunit


【解决方案1】:

您在夹具中初始化驱动程序一次,当它被声明时:

私有 IWebDriver 驱动程序 = new ChromeDriver(@"C:\chromedriver_win32");

然后您的第一个测试运行,使用驱动程序和拆解关闭它。下一个测试不再使用驱动程序。 您需要:在设置中重新初始化驱动程序,或者您需要在夹具拆解中将其关闭。

如果您选择在 setup/teardown 中初始化和关闭,您将看到驱动程序为每个测试启动一个新的浏览器会话。这将确保您的测试彼此独立,但会花费更多的运行时间。

如果您想为所有测试重新使用浏览器会话:将初始化和关闭移动到 TestFixture Setup 和 TestFixture Teardown 方法。

【讨论】:

  • 谢谢,史蒂夫。现在问题已经解决了。我在每次测试中初始化驱动程序并在测试结束时关闭驱动程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-11
  • 2018-05-06
相关资源
最近更新 更多