C#Selenium WebDriver连续运行时测试失败但单独运行时成功

时间:2017-08-16 09:44:58

标签: c# selenium selenium-webdriver selenium-chromedriver socketexception

所以我有一个.feature文件,其中我添加了4个方案。

.feature文件

Feature: BleKeys beheren
Het beheren van BLE-keys
d.m.v. de WebInterface.

@notfs
Scenario: BLE-key toevoegen aan database
    Given I'm at the BleKey/Create page.
    And I have entered acceptable BLE-data.
    When I press Create
    Then The BLE-key should be added to the database.

@notfs
Scenario: BLE-key data aanpassen
    Given There is a BleKey in the database.
    And I'm at the BleKey/Edit page of that BleKey
    When I edit the MAC-Adress
    And I edit the Conditional Report
    And I edit the Flag
    And I edit the Distance
    And I edit the Reference
    And I edit the ExtraCfg
    And I press Save
    Then The BleKey should have changed correctly.

@notfs
Scenario: BLE-key data verwijderen
    Given There is a BleKey in the database.
    And I navigate to the Delete page of that BleKey
    When I press Delete
    Then The BleKey should be deleted.

@notfs
Scenario: BLE-key data van 1 sleutel bekijken
    Given There is a BleKey in the database.
    And I navigate to the Details page of that BleKey
    Then The correct data of that BleKey should be displayed

现在,当我在Visual Studio(ReSharper单元测试会话窗口)中单独运行这些测试时,它们都会成功,但是当连续运行时,第一个成功,但任何连续测试都会失败并出现以下异常:

  

OpenQA.Selenium.WebDriverException:意外错误。 System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:无法建立连接,因为目标计算机主动拒绝它127.0.0.1:5595

正如您所看到的,测试尝试连接到某个本地IP地址,同时应该导航到以下基本网址:

  

http://localhost:58759/ +控制器+方法取决于测试。

我在我的步骤文件中创建了WebDriver实例。像这样:

public class BleKeyBeherenSteps
    {
        public static RemoteWebDriver RemoteWebDriver = new ChromeDriver();
        public WebinterfaceSelenium SeleniumTest = new 
        WebinterfaceSelenium(RemoteWebDriver);
        public Navigate Navigate = new Navigate(RemoteWebDriver);
        public Check Check = new Check(RemoteWebDriver);
        public Edit Edit = new Edit(RemoteWebDriver);
        public Delete Delete = new Delete(RemoteWebDriver);
        private readonly BeheerContext _db = new BeheerContext();
    }

在我的方法中,我像这样导航:

Driver.Navigate().GoToUrl(Adress + BleKeyIndexUrl);

Adress =“http://localhost:58759/

BleKeyIndexUrl =“BleKeys / Index”

因此,出于某种原因,WebDriver导航到本地IP地址而不是本地主机地址。

编辑:每次测试后我都会关闭驱动程序:Driver.Quit(); 我的猜测是,在第一次测试之后,由于某种原因,Driver.Url属性会丢失。

1 个答案:

答案 0 :(得分:3)

不要担心localhost对IP地址的歧义,localhost通常会解析为127.0.0.1。

连续单元测试失败通常是由于在应用程序代码中使用静态声明引起的。今天早上我处理了这个问题,我不得不在每个单元测试开始时在我的应用程序中重置一个麻烦的静态变量。