简体   繁体   中英

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 file

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:

OpenQA.Selenium.WebDriverException : Unexpected error. 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

As you can see the test tries to connect to some local IP-adress while it should be navigating to the base URL of:

http://localhost:58759/ + controller + method depending on the test.

I create the WebDriver instance in my steps file. 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/ "

And BleKeyIndexUrl = "BleKeys/Index"

So for some reason the WebDriver navigates to a local IP-adress instead of the localhost adress.

Edit: After every test i close the driver with: Driver.Quit(); My guess is that for some reason after the first test the Driver.Url property gets lost.

Don't worry about the localhost to IP address ambiguity, localhost typically resolves to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM