Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Selenium WebDriver测试在连续运行时失败,但在单独运行时成功_C#_Selenium_Selenium Webdriver_Selenium Chromedriver_Socketexception - Fatal编程技术网

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

C#Selenium WebDriver测试在连续运行时失败,但在单独运行时成功,c#,selenium,selenium-webdriver,selenium-chromedriver,socketexception,C#,Selenium,Selenium Webdriver,Selenium Chromedriver,Socketexception,所以我有一个.feature文件,其中添加了4个场景 功能文件 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

所以我有一个.feature文件,其中添加了4个场景

功能文件

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 Unit Test Sessions窗口)中单独运行这些测试时,它们都会成功,但当连续运行时,第一个测试会成功,但任何连续测试都会失败,出现以下异常:

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

如您所见,测试尝试连接到某些本地IP地址,同时应导航到以下的基本URL:

+控制器+方法,取决于测试

我在步骤文件中创建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);
Where address=“”

和BleKeyIndexUrl=“BleKeys/Index”

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

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

不要担心localhost到IP地址的不确定性,localhost通常解析为127.0.0.1


在应用程序代码中使用静态声明常常会导致连续单元测试失败。今天早上我处理了这个问题,在每个单元测试开始时,我不得不在应用程序中重置一个棘手的静态变量。

不要担心本地主机到IP地址的不确定性,本地主机通常解析为127.0.0.1


在应用程序代码中使用静态声明常常会导致连续单元测试失败。今天早上我处理了这个问题,在每次单元测试开始时,我都必须在应用程序的深处重置一个麻烦的静态变量。

我通过初始化构造函数中的属性使RemoteWebDriver成为非静态的,现在它可以正常工作了。谢谢你answering@Jeroen:很抱歉,我无法帮助解决远程计算机上的端口问题。我建议您当前的问题已经发展到需要一个新的SO问题,因为现在的问题是在专用测试机器上配置一致的测试本地端口号。我假设您已经知道dev网站使用的端口号是在web项目的web选项卡中定义的?这是调试/发布的区别吗?我发现开发服务器上的代码不是最新版本。我通过初始化构造函数中的属性使RemoteWebDriver成为非静态的,现在它可以正常工作了。谢谢你answering@Jeroen:很抱歉,我无法帮助解决远程计算机上的端口问题。我建议您当前的问题已经发展到需要一个新的SO问题,因为现在的问题是在专用测试机器上配置一致的测试本地端口号。我假设您已经知道dev网站使用的端口号是在web项目的web选项卡中定义的?这是调试/发布的区别吗?我发现开发服务器上的代码不是最新版本。它现在已更新并已修复。