I am trying to use the Selenium HtmlUnit driver in C# tests. As far as I know, the only way to use the HtmlUnit driver in C# is through Selenium server and the RemoteWebDriver:
var driver = new OpenQA.Selenium.Remote.RemoteWebDriver(
OpenQA.Selenium.Remote.DesiredCapabilities.HtmlUnitWithJavaScript());
However, I also need to use NTLM authentication. Using the non-remote driver in Java, it can apparently be configured like this:
WebDriver driver = new HtmlUnitDriver() {
protected WebClient modifyWebClient(WebClient client) {
// Does nothing here to be overridden.
DefaultCredentialsProvider creds = new DefaultCredentialsProvider();
creds.addNTLMCredentials("userName", "password", null, -1, "myComputerName", "myDomain");
client.setCredentialsProvider(creds);
return client;
}
}
(Source: https://groups.google.com/forum/#!topic/webdriver/ktIWIs5m0mQ)
But this obviously does not solve my problem since I am using C#. How can I do that ? (I can use Chrome successfully, but I would like to use HtmlUnit for speed).
Thanks !
In order to pass credentials you need to overload the modifyWebClient of the HtmlUnitDriver, as you saw in the discussion link1.
For the .NET developer the only way to use the HtmlUnitDriver is via the RemoteWebDriver, and based on the discussion HtmlUnit wrapper for .NET2 the developers chose not to expose all of the HtmlUnit driver classes:
I'm loathe to take any more dependencies in the .NET
bindings... If you're dead-set on using HtmlUnit as your headless browser of choice, you can always use it via the RemoteWebDriver
Therefore you cannot use NTLM, or any other credential method, with the RemoteWebDriver.
If you were willing to do and maintain the work you could convert all the HtmlUnit code as detailed in the second link of #JasonPlutext's answer3.
The original sample appears to be from the selenium FAQ.
Linked to from Is there an HtmlUnitDriver for .NET? here on SO.
Related
Host machine: Windows 10 with VS 2017, Selenium (.NET) & SpecFlow+
I've got a Mojave MacOS with Safari v12 on the network that I need to run my test scripts on.
I'm running Selenium C# scripts on it using RemoteWebDriver but they are failing because v12 uses the latest W3C protocols.
SafariDriver can be started using the "--legacy" switch.
SafariDriverService has a "UseLegacyProtocol" but can't be passed in RemoteWebDriver (example below).
Is there a way to activate the switch by:
a) Passing it through RemoteWebDriver?
b) Merging it as a capability with the options and passing it through RemoteWebDriver?
c) Configuring the switch in a json file for use with Selenium Grid v3?
This is to work with C# code.
Code examples I already have:
var sOptions = new SafariOptions();
sOptions.Proxy = null;
var sService = SafariDriverService.CreateDefaultService();
sService.Port = xxxx;
sService.UseLegacyProtocol = true;
Browser = new SafariDriver(sService, sOptions,
TimeSpan.FromSeconds(PageTimeout));
var rOptions = new SafariOptions();
Browser = new RemoteWebDriver(new Uri("http://xx.xxx.xx.xx:xxxx/wd/hub"), rOptions);
Thanks
Legacy protocol is no longer supported in shipping versions of Safari/safaridriver, so I think this question can be closed.
You can use the following code to merge the capabilities into the SafariOptions:
SafariOptions options = new SafariOptions();
options.merge(capabilities);
But there is still a bug, Safaridriver can't handle Proxy, it is still an open ticket in there backlog.
I need to setup phantomjs to use a proxy. I have found many examples for java, javascript/node.js, etc. But none for c# using selenium. I need to set a global proxy and be able to change it without restarting the driver. I believe it would be under driver.ExecutePhantomJS() but I also cant find what phantomjs script I would run to change the global proxy.
When creating the service:
OpenQA.Selnium.Proxy myproxy = new Proxy();
myproxy.httpProxy = ip +":"+port;
driverservice.AddAdditionalCapability(CapabilityType.Proxy, myproxy);
After driver is running to change the proxy:
driver.ExecutePhantomJS("phantom.setProxy('"+ip+"', "+port+", 'http', '', '');")
So basically I am trying to automate the web. I'm doing this by using Selenium and PhantomJS driver.
In addition to this I am doing it using a proxy. This is where the problems start.
Without the proxy it's working just fine. Using it doesn't. I'm having big trouble trying to debug this.
This is what I am using now to set the proxy:
PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService();
service.ProxyType = "http";
service.AddArgument(string.Format("--proxy=", proxyL));
I feel like I have tried every single Google result regarding this and none of the different variants have worked so far. If there is anyone who's got experience with using the PhantomJS diver with a proxy I would love some help.
EDIT:
I am now using this code to set the proxy:
options.AddAdditionalCapability(CapabilityType.Proxy, new Dictionary<string, string>
{
{"proxyType", "none"},
{"httpProxy", proxyL}
});
The console window dows show that I'm using the proxy. But it still isn't.
I am trying to use OperaDriver for selenium.
IWebDriver wd = new OperaDriver();
but using OpenQA.Selenium.Opera; does not exist. I can not find it in C# dll. I am using v2.5 drivers. Chroom, Ie, Firefox exist but no Opera. Where can i get it?
There is no native OperaDriver in the .NET bindings. Opera decided to implement their driver in Java, and not to implement the RemoteWebDriver JSON wire protocol. As such the only way to use the Opera driver from .NET is to use the .NET RemoteWebDriver class, along with an instance of the standalone Java Selenium server, which is available on the project downloads page.
Assuming you are on Windows:
The Operadriver is written in Java and not suported directly in C#, as it is mainatined not by the Selenium project team but by Opera.
To use it, you have to run the standalone Selenium webserver (from console on windows) before starting the test. get it here
you need to set the OPERA_PATH to point to your opera.exe file. Start the server with the command:
java -jar selenium-server-standalone-2.33.0.jar
i use a small bat for these two tasks:
SET OPERA_PATH="C:\Progra~2\Opera\opera.exe"
cd C:\pathToSeleniumJarFile
C:\Progra~2\Java\jre7\bin\java.exe -jar selenium-server-standalone-2.33.0.jar
C#:
testing with remotewebdriver object in your C# code to connect to it.
switch (WebBrowser)
{
case Browser.Chrome:
// chromedriver.exe has to be in the debug folder
ChromeOptions chrome = new ChromeOptions();
chrome.AddArguments("--ignore-certificate-errors");
webDriver = new ChromeDriver(chrome);
break;
...
case Browser.Opera:
//note: set OPERA_PATH environment variable (in cmd or global)
DesiredCapabilities opera = DesiredCapabilities.Opera();
opera.SetCapability("opera.profile", #"C:\OperaProfile");
webDriver = new RemoteWebDriver(opera);
break;
default:
throw new NotImplementedException();
if you want to manipulate the profile of the opera client (e.g. to accept untrusted certificates etc) you need to set
opera.SetCapability("opera.profile", #"C:\OperaProfile");
Copy an existing Profile to a location of your choice, here C:\OperaProfile.
==> Avoid spaces in all the pathes <==
How can I control Opera Driver https://github.com/operasoftware/operadriver/ (or even Opera the browser itself) on Windows from within the .NET framework?
My goal is to make something similar to Browsershots.org
The easiest way to do this would be to use the RemoteWebDriver, which requires the Selenium server to be running. The code would look something like the following (Warning: untested code).
// Assumes the Selenium server is running on port 4444 on localhost.
// Note that the Opera() method of the DesiredCapabilities class is
// not included in 2.0rc2 (the currently available binary release),
// but does exist in the trunk.
IWebDriver driver = new RemoteWebDriver("http://localhost:4444/wd/hub", DesiredCapabilities.Opera());