NUnit will not run consecutive Selenium Webdriver C# tests
Selenium Webdriver 2.48 , C#, NUnit 2.6.4, Chrome Driver When I run my tests from the NUnit test runner, they all pass if run individually.
If I select a main heading node, and select "Run", the first test in the group will run, the rest will fail.
If I have the test fixture [TearDown] close the driver at the end of each test, the following error occurs: "Invalid OPeration Exception: No such session"
If I have the test fixture [TearDown] quit the driver, the following error occurs: "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:13806 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)"
Using either driver.Quit() or driver.Close() makes no difference to the result - only the first test in the group running.
I have searched but not been able to find a solution. It must be possible to run all tests by running from the top-most node, rather than having to select each test and run them individually. Any help would be appreciated. Thanks. Michael
Here is an example which has two tests in the one class. I have removed most of the methods from the tests as they are very long.
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();
}
}
You initialize the driver once in the fixture, when it is declared:
private IWebDriver driver = new ChromeDriver(@"C:chromedriver_win32");
Then your first test runs, uses the driver and the teardown closes it. The next test no longer has use of the driver. You need to either: reinitialize the driver in the setup, or you need to close it in the fixture teardown.
If you chose to initialise and close in the setup/teardown you will see that the driver starts a new browsersession for every test. This will make sure that your tests are independant of eachother, but it will cost a lot more runtime.
If you want to re use the browsersession for all the tests: move the initialization and closure to TestFixture Setup and TestFixture Teardown methods.
链接地址: http://www.djcxy.com/p/63420.html