How do you set the port for ChromeDriver in Selenium? - c#

As some background, I'm playing around with BrowserMob proxy and I am getting blocked by the port settings. So I'd like to know how to set a port by myself.
Server srv = new Server(#"C:\BMob\browsermob\bin\browsermob-proxy.bat");
srv.Start();
Client cln = srv.CreateProxy();
cln.NewHar("BOWZA");
ChromeOptions co = new ChromeOptions();
Proxy seleniumProxy = new Proxy { HttpProxy = cln.SeleniumProxy };
co.Proxy = seleniumProxy;
ChromeDriver cDriver = new ChromeDriver(co);
// What do I do now...?
I just failed to find anything documenting this, sorry

This should work:
ChromeDriverService service= ChromeDriverService.CreateDefaultService(DRIVER_PATH);
service.Port = <PORT>;
IWebDriver WebDriver = new ChromeDriver(service);

This works for me. I'm using webDriverManager as well
ChromeDriverService chromeDriverService = new ChromeDriverService.Builder().usingPort(DesiredPortNumber).build();
Then use it as a parameter when initializing new driver
new ChromeDriver(chromeDriverService , BrowserOptionsManager.getChromeOptions());

Related

Your connection is not secure - using Selenium.WebDriver v.3.6.0 + Firefox v.56

I'm writing tests with Selenium + C# and I face an important issue because I didn't found solution when I test my site with secure connection (HTTPS). All solutions I found on stackoverflow are out of date or doesn't work.
I tried to exercise all solutions from below question:
Selenium Why setting acceptuntrustedcertificates to true for firefox driver doesn't work?
But they did not help me solve the problem
Nor is it the solution of using Nightly FireFox.
Still, when the selenium loading Firfox browser, I see the page: "Your connection is not secure".
Configuration:
Firefox v56.0
Selenium.Firefox.WebDriver v0.19.0
Selenium.WebDriver v3.6.0
my code is:
FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.AcceptUntrustedCertificates = true;
profile.AssumeUntrustedCertificateIssuer = false;
options.Profile = profile;
driver = new FirefoxDriver(FirefoxDriverService.CreateDefaultService() , options , TimeSpan.FromSeconds(5));
Drivers.Add(Browsers.Firefox.ToString() , driver);
Thank for your help!
Updates to my question here:
Note 1: To anyone who has marked my question as a duplicate of this question:
Firefox selenium webdriver gives “Insecure Connection”
I thought that it is same issue, but I need solution for C#, I try match your JAVA code to my above code
First, I changed to TRUE the below statment:
profile.AssumeUntrustedCertificateIssuer = true;
second, I create new FF profile ("AutomationTestsProfile")
and try to use it:
Try 1:
FirefoxProfile profile = new FirefoxProfileManager().GetProfile("AutomationTestsProfile");
try 2:
FirefoxProfile profile = new FirefoxProfile("AutomationTestsProfile");
I Run 2 options, but still the issue exists.
Note 2: I attached screenshot of my problem, it appears when the driver try to enter text to user-name on login page.
I noticed that when I open my site with FF, Firefox displays a lock icon with red strike-through red strikethrough icon in the address bar,
but near the username textbox not appears the msg:
"This connection is not secure. Logins entered here could be compromised. Learn More" (as you writed on the duplicate question),
So maybe there is a different problem?
You are setting the properties on the profile. The FirefoxOptions has a property AcceptInsecureCertificates, set that to true.
Forget the profile, this is what you want:
var op = new FirefoxOptions
{
AcceptInsecureCertificates = true
};
Instance = new FirefoxDriver(op);
For me, the profile setting AcceptUntrustedCertificates was not enough, I also had to set option security.cert_pinning.enforcement_level. My startup looks like
// no idea why FirefoxWebDriver needs this, but it will throw without
// https://stackoverflow.com/questions/56802715/firefoxwebdriver-no-data-is-available-for-encoding-437
CodePagesEncodingProvider.Instance.GetEncoding(437);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var service = FirefoxDriverService.CreateDefaultService(Environment.CurrentDirectory);
service.FirefoxBinaryPath = Config.GetConfigurationString("FirefoxBinaryPath"); // path in appsettings
var options = new FirefoxOptions();
options.SetPreference("security.cert_pinning.enforcement_level", 0);
options.SetPreference("security.enterprise_roots.enabled", true);
var profile = new FirefoxProfile()
{
AcceptUntrustedCertificates = true,
AssumeUntrustedCertificateIssuer = false,
};
options.Profile = profile;
var driver = new FirefoxDriver(service, options);
It works for me for following settings (same as above):
My env:
win 7
firefox 61.0.2 (64-bit)
Selenium C# webdriver : 3.14.0
geckodriver-v0.21.0-win32.zip
==============================
FirefoxOptions options = new FirefoxOptions();
options.BrowserExecutableLocation = #"C:\Program Files\Mozilla Firefox\firefox.exe";
options.AcceptInsecureCertificates = true;
new FirefoxDriver(RelativePath,options);

Selenium 3.5 Options.ToCapabilities is not applying the options to RemoteWebDriver

I had a couple of cmd line options working with Selenium 3.3 as follows:
`DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities = DesiredCapabilities.Chrome();
options.AddArguments("--lang=en-GB");
options.AddArguments("--high-dpi-support=1");
options.AddArguments("--force-device-scale-factor=0.8");
capabilities = options.ToCapabilities() as DesiredCapabilities;
Driver = new RemoteWebDriver(new Uri("WIN10:5566/wd/hub"), capabilities,
TimeSpan.FromSeconds(180));`
However switching to Selenium 3.5.2, these options are no longer being applied even when using new ToCapabilities() as follows:
ChromeOptions options = new ChromeOptions();
options.AddArguments("--lang=en-GB");
options.AddArguments("--high-dpi-support=1");
options.AddArguments("--force-device-scale-factor=0.5");
Driver = new RemoteWebDriver(new Uri("http://WIN10:5566/wd/hub"), options.ToCapabilities(), TimeSpan.FromSeconds(180));
Is there something else I need?
Try replacing
capabilities = options.ToCapabilities() as DesiredCapabilities;
with the following the following:
capability.setCapability(ChromeOptions.CAPABILITY, options);
Then you only need to specify "capability" in the RemoteWebDriver:
RemoteWebDriver(new URL("your url"), capability);
Thanks smit, that would have worked as well. The problem was the chrome driver needed updating (embarrassed emoji here!)

Disabling a popup

I am trying to Disable a popup which keeps showing up, which is a "Chrome Extension".
I am attaching a screenshot and also the code.
What I am trying to do is I want to click the Disable button.
The code I am using is:
public static void Initialize()
{
//Instance = new FirefoxDriver();
//Instance = new InternetExplorerDriver(#"C:\Sele2");
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-popup-blocking");
var basePath = AppDomain.CurrentDomain.BaseDirectory;
Instance = new ChromeDriver(basePath, options);
Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
}
Can someone please help? Thanks in advance
You can add one more arguments as --disable-extensions in your chrome options as below :-
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-popup-blocking","--disable-extensions");
var basePath = AppDomain.CurrentDomain.BaseDirectory;
Instance = new ChromeDriver(basePath, options);
Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));

Selenium Grid in C#

I'm trying to run my tests in another local machine, but i always end up failing! I've seen videos implementing successfully in JAVA, but I'm trying to do it through c#.
Any Ideas are most appreciated!
public class Driver
{
public static IWebDriver Instance { get; set; }
public static void Initialize()
{
IWebDriver driver;
driver = new ChromeDriver();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability(CapabilityType.BrowserName, "chrome");
capabilities.SetCapability(CapabilityType.Platform, new Platform(PlatformType.Windows));
driver = new RemoteWebDriver(new Uri("http://localhost:4446/wd/hub"), capabilities);
}
public static void Close()
{
Instance.Dispose();
Instance = null;
}
You should use remote uri of your grid instance instead of local and ensure that you have chrome installed at least on one of your nodes. If you want to use selenium grid locally start local hub first using selenium-server-standalone.jar. You should use info from here
Also you don't need this code:
driver = new ChromeDriver();` - you need RemoteWebDriver directly
For me this code works perfectly:
var uri = 'uri_to_your_grid_hub';
var capabilities = new ChromeOptions().ToCapabilities();
var commandTimeout = TimeSpan.FromMinutes(5);
var driver = new RemoteWebDriver(new Uri(uri),capabilities,commandTimeout)

selenium change firefox proxy to autoDetect c#

I search to change the default proxy of firefox on firefoxDriver call.
I work in c#.
I know in java you do :
FirefoxProfile ff = new FirefoxProfile();
ff.setPreference("network.proxy.type", ProxyType.AUTODETECT.ordinal());
FirefoxDriver ffD = new FirefoxDriver(ff);
I tried :
FirefoxProfile ff = new FirefoxProfile();
ff.SetPreference ("network.proxy.type",ProxyKind.AutoDetect.ToString ());
var driver = new FirefoxDriver(ff);
I just forgot whats ordinal. After that.
ff.SetPreference ("network.proxy.type",(int)ProxyKind.AutoDetect);

Categories