Reputation: 12423
I have globally installed Selenium Standalone:
npm install selenium-standalone -g
selenium-standalone install --singleDriverInstall=chrome
I have also bastardised the code from this page into this dotnet core / XUnit test:
using System.Threading;
using System;
using Xunit;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Chrome;
using System.Diagnostics;
namespace XunitTestLib.Unit
{
public class BasicBrowserTest : IDisposable
{
public Process _process;
public IWebDriver Browser { get; }
public BasicBrowserTest()
{
_process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "selenium-standalone",
Arguments = "start --drivers=chrome",
UseShellExecute = true
}
};
_process.Start();
var options = new ChromeOptions();
// options.AddArgument("--headless");
options.AddArgument("--disable-gpu");
options.AddArgument("--no-sandbox");
options.AddArgument("--ignore-certificate-errors");
options.AddArgument("--allow-insecure-localhost");
options.AddArgument("--acceptInsecureCerts=true");
options.AddArgument("--proxy-server='direct://'");
options.AddArgument("--proxy-bypass-list=*");
options.SetLoggingPreference(OpenQA.Selenium.LogType.Browser, LogLevel.All);
Thread.Sleep(4000);
Browser = new RemoteWebDriver(options);
}
public void Dispose()
{
Browser.Dispose();
_process.CloseMainWindow();
}
}
}
I am trying to start up selenium-standalone with ChromeDriver so that I can driver the browser to perform browser testing.
I am not hosting my own web application - this is only test run tests against a site which is already running.
The Sleep is to allow selenium to start up in the shell process.
However, I get this output when run with dotnet test --filter DisplayName="Homepage"
:
[xUnit.net 00:00:04.88] Homepage [FAIL]
Failed Homepage
Error Message:
System.InvalidOperationException : Unable to create new service: ChromeDriverService
Build info: version: '3.141.0', revision: '2ecb7d9a', time: '2018-10-31T20:22:52'
System info: host: 'horacepc', ip: '191.191.191.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_201'
Driver info: driver.version: unknown (SessionNotCreated)
Stack Trace:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(Uri remoteAddress, ICapabilities desiredCapabilities, TimeSpan commandTimeout)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(Uri remoteAddress, ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(DriverOptions options)
at XunitTestLib.Unit.BasicBrowserTest..ctor() in C:\git\core\XunitTestLib\Unit\BasicBrowserTest.cs:line 46
Total tests: 1. Passed: 0. Failed: 1. Skipped: 0.
Test Run Failed.
The line the error is indicating is this line: Browser = new RemoteWebDriver(options);
My understanding is that the ChromeDriver is attempting to connect to selenium via an HTTP connection, but is failing to start/initialise.
I don't understand why it is failing.
Upvotes: 0
Views: 3209
Reputation: 12423
The answer turns out to be including the required list of NuGet packages. The blog post from Scott Hanselman documents these:
dotnet add package "Selenium.Support"
dotnet add package "Selenium.WebDriver"
This third package is required for the code in the post above to work:
dotnet add package "Selenium.WebDriver.ChromeDrive"
Upvotes: 0
Reputation: 25743
This doesn't answer your question about using selenium-standalone
specifically, but in my experience, it may be unnecessary if you just want to run them on your local machine.
Instead, if you download Chromedriver from http://chromedriver.chromium.org/downloads and then instead of using new Process
you can do:
var driverPath = "C:\\MyWebDrivers";
var driverExecutableFileName = "chromedriver.exe";
var options = <whateveryouhadabove>;
using (var service = ChromeDriverService.CreateDefaultService(driverPath, driverExecutableFileName))
{
using (var driver = new RemoteWebDriver(service.ServiceUrl, options.ToCapabilities()))
{
// control chrome here!
}
}
Behind the scenes, ChromeDriverService.CreateDefaultService
will be creating a "Web Driver" compatible interface via chromedriver.exe
.
Upvotes: 1