6

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?

screenshot: "insecure Connection" tab

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
C. Mar
  • 139
  • 1
  • 2
  • 16

3 Answers3

12

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);
Jota Alava
  • 136
  • 2
  • 4
  • Thanks Jota, been looking for this solution for ages! – alex Jan 31 '18 at 11:41
  • Thank you! It Works. But I don't understand why I need to set it to true, if in the FirefoxProfile Class documentation they writes "Set to true by default"? – C. Mar Jan 31 '18 at 12:16
3

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);
BurnsBA
  • 4,347
  • 27
  • 39
0

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);

Xin Cai
  • 161
  • 3
  • 5