简体   繁体   English

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

[英]C# Selenium WebDriver tests failing when run consecutively but succeeding when run individually

So I have one .feature file in which I added 4 scenario's. 所以我有一个.feature文件,其中我添加了4个方案。

.feature file .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

Now when I run these tests in Visual Studio (ReSharper Unit Test Sessions Window) individually they all succeed, but when run consecutively the first one succeeds but any consecutive test fails with the following exception: 现在,当我在Visual Studio(ReSharper单元测试会话窗口)中单独运行这些测试时,它们都会成功,但是当连续运行时,第一个成功但任何连续测试都会失败并出现以下异常:

OpenQA.Selenium.WebDriverException : Unexpected error. OpenQA.Selenium.WebDriverException:意外错误。 System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:5595 System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:无法建立连接,因为目标计算机主动拒绝它127.0.0.1:5595

As you can see the test tries to connect to some local IP-adress while it should be navigating to the base URL of: 正如您所看到的,测试尝试连接到某个本地IP地址,同时应该导航到以下基本URL:

http://localhost:58759/ + controller + method depending on the test. http:// localhost:58759 / + controller +方法取决于测试。

I create the WebDriver instance in my steps file. 我在我的步骤文件中创建了WebDriver实例。 Like so: 像这样:

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();
    }

And in my methods I navigate like this: 在我的方法中,我像这样导航:

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

Where Adress = " http://localhost:58759/ " Adress =“ http:// localhost:58759 /

And BleKeyIndexUrl = "BleKeys/Index" 和BleKeyIndexUrl =“BleKeys / Index”

So for some reason the WebDriver navigates to a local IP-adress instead of the localhost adress. 因此,由于某种原因,WebDriver导航到本地IP地址而不是localhost地址。

Edit: After every test i close the driver with: Driver.Quit(); 编辑:每次测试后我都关闭驱动程序:Driver.Quit(); My guess is that for some reason after the first test the Driver.Url property gets lost. 我的猜测是,由于某种原因,在第一次测试后,Driver.Url属性丢失了。

Don't worry about the localhost to IP address ambiguity, localhost typically resolves to 127.0.0.1. 不要担心localhost对IP地址的歧义,localhost通常会解析为127.0.0.1。

Failing consecutive unit tests are frequently caused by use of static declarations in application program code. 连续单元测试失败通常是由于在应用程序代码中使用静态声明引起的。 I dealt with this problem this morning and I had to reset a troublesome static variable deep in my application at the start of each unit test. 我今天早上处理了这个问题,我不得不在每个单元测试开始时在我的应用程序中重置一个麻烦的静态变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当从命令行运行而不是VS时,硒测试(在C#中)失败 - Selenium tests(in C#) failing when run from the command line but not VS 如何在多个浏览器上并行运行Selenium Webdriver C#测试 - How to run Selenium webdriver C# tests on multiple browser in parallel 一起运行时单元测试超时,单独运行时成功吗? - Unit Tests timeout when run together, succeed when run individually? 一些 Nunit 测试在组中失败,但在其他环境中单独运行时通过 - Some Nunit tests are failing in groups but pass when run individually in other env Selenium WebDriver C# NUnit 测试并行失败 - Selenium WebDriver C# NUnit Tests Failing in Parallel 尝试并行运行测试时出现 WebDriver 异常 - WebDriver exception when trying to run tests in parallel 如何在多个远程计算机上并行运行Selenium Webdriver C#测试? - How to run Selenium Webdriver C# tests on multiple remote machines in parallel? 使用C#在多个浏览器中运行Selenium测试 - Run Selenium tests in multiple browsers with C# 为什么我的测试一起运行时失败,但单独通过? - Why do my tests fail when run together, but pass individually? 单元测试在一起运行时失败,单独传递 - Unit Tests fail when run together, pass individually
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM