I'm porting some Selenium JUnit tests to a .NET project (tests for a MVC project).
I have following line in Java:
WebDriver driver = new HtmlUnitDriver();
I ported it to .NET like this:
IWebDriver driver = new RemoteWebDriver(DesiredCapabilities.HtmlUnit());
In Java this works perfectly to test a Spring application, but in .NET it throws an Exception:
Test method TDD.Tests.Acceptatie.TestLoginScreen.LoginScreenTest threw exception:
OpenQA.Selenium.WebDriverException: Unexpected error.
System.Net.WebException: Can't connect to remote server --->
System.Net.Sockets.SocketException:
Can't connect because target computer actively refused the connection 127.0.0.1:4444
NOTE: the error message is translated from dutch so probably not the exact message in english.
I have added the Selenium.Webdriver NuGet package. The project structure is the following:
The MVC project is configured to run on localhost:8080 and I also tried to set the webdriver to that ip:
IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:8080"), DesiredCapabilities.HtmlUnit());
I also tried to use the loopback address instead of 'localhost'.
Anyone knows what could cause this problem and how to fix this?
Thanks in advance!
The syntax to invoke the RemoteWebDriver Class is as follows:
public RemoteWebDriver(Uri remoteAddress, ICapabilities desiredCapabilities)
where:
remoteAddress
Type: System.Uri
URI containing the address of the WebDriver remote server (e.g. http://127.0.0.1:4444/wd/hub).
desiredCapabilities
Type: OpenQA.Selenium.ICapabilities
An ICapabilities object containing the desired capabilities of the browser.
As per the error...
OpenQA.Selenium.WebDriverException: Unexpected error.
System.Net.WebException: Can't connect to remote server --->
System.Net.Sockets.SocketException:
Can't connect because target computer actively refused the connection 127.0.0.1:4444
It seems your program/script is unable to connect to the WebDriver Remote Server.
As per your second code trial http://localhost:8080 is your Application Server but not the WebDriver Remote Server e.g. Selenium Grid Hub
. Hence your program show the error. If you are using Selenium Grid change the line as:
IWebDriver driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"), DesiredCapabilities.HtmlUnit());
Finally, a quick look at the API Docs of Inheritance Hierarchy of OpenQA.Selenium.Remote.RemoteWebDriver shows the following hierarchy where HtmlUnit
is not present:
However as per the discussion Is there an HtmlUnitDriver for .NET? your first code trial should have worked:
IWebDriver driver = new RemoteWebDriver(DesiredCapabilities.HtmlUnit());
If you want to use the Firefox Implementation you can:
IWebDriver driver = new RemoteWebDriver(DesiredCapabilities.HtmlUnitWithJavaScript());