How to use SSL certificates in Selenium Web Driver?

I'm using Selenium Web Driver on Windows 7.

I'm trying to test a website that use authentication and I need to use SSL certificates.

When I use Firefox out of Selenium all works fine but I've noted that the Firefox browser session opened by Selenium doesn't have any certificates registered and so it's clear that it doesn't work.

Here you are the Advanced Preferences when I use Firefox "out" of Selenium

and here you are the same when I use the Firefox sessione opened by Selenium

I've tried to keep the session opened and to register manually the certificate but the browser session doesn't register the certificate.

Here you are my code if could be useful

package myTestProjects;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class GAMOPERA_Test_01 {

private static WebDriver driver = null;

public static void main(String[] args)  throws InterruptedException {

    // Create a new instance of the Firefox driver
    System.out.println("Creo una nuova sessione del browser Firefox ...");
    driver = new FirefoxDriver();

    //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    // It is always advisable to Maximize the window before performing DragNDrop action
    System.out.println("Massimizzo la finestra del browser ...");
    driver.manage().window().maximize();
    Thread.sleep(3000L);

    //Launch the Sistema Piemonte Home Page
    System.out.println("Mi collego a Sistema Piemonte ...");
    driver.get("http://<my_site_url>");
    Thread.sleep(3000L);          

    // Find the element Accedi o 
    System.out.println("Accesso tramite certificato digitale ...");
    driver.findElement(By.xpath("/html/body/div[6]/div/div/div[2]/form/table/tbody/tr[3]/td/input")).click();        

    //driver.findElement(By.className("loginbutton")).click();
    Thread.sleep(3000L); 

    // Print TEST = OK!!
    System.out.println("TEST = OK !!");
    //driver.quit();

        }
}

Any suggestions?

Answers


I've solved!

Surfing on the web I've found this post http://seleniummonk.blogspot.it/p/how-to-handle-ssl-cerificates.html that gave me the solution.

I need to use the "Firefox profile" (I use the default one ...), so I can have all the certificates I need to.

Here you're the new code that works

package myTestProjects;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class GAMOPERA_Test_01 {

private static WebDriver driver = null;

public static void main(String[] args)  throws InterruptedException {

    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile ffProfile = profile.getProfile("default"); 

    // Create a new instance of the Firefox driver
    System.out.println("Creo una nuova sessione del browser Firefox ...");
    driver = new FirefoxDriver(ffProfile);          

    //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    // It is always advisable to Maximize the window before performing DragNDrop action
    System.out.println("Massimizzo la finestra del browser ...");
    driver.manage().window().maximize();
    Thread.sleep(3000L);

    //Launch the Sistema Piemonte Home Page
    System.out.println("Mi collego a Sistema Piemonte ...");
    driver.get("<my_site_url>");
    Thread.sleep(3000L);          

    // Find the element Accedi o 
    System.out.println("Accesso tramite certificato digitale ...");
    driver.findElement(By.xpath("/html/body/div[6]/div/div/div[2]/form/table/tbody/tr[3]/td/input")).click();        

    //driver.findElement(By.className("loginbutton")).click();
    Thread.sleep(3000L); 

    // Print TEST = OK!!
    System.out.println("TEST = OK !!");
    //driver.quit();

        }

}

I hope this could be useful!


You can do that using Proxy. Through DesiredCapabilities you can configure the browser accordingly.

String PROXY = "localhost:8080";

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
     .setFtpProxy(PROXY)
     .setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);

WebDriver driver = new InternetExplorerDriver(cap);

Code taken from SeleniumHQ


First create a profile as I created with "Test" in Firefox using below command in CMD:

"C:\Program Files\Mozilla Firefox\firefox.exe" -P

Then import your certificate in this newly created Firefox Profile with Password used to generate the certificate. In my case this was a P12 extenstion Certificate.

Once done you can use below code in Selenium and Firefox will not popup for Certificate and you will be logged into the Portal or Website.

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile myProfile = allProfiles.getProfile("Test");
myProfile.setPreference("security.default_personal_cert", "Select Automatically");
FirefoxOptions firefoxoptions = new FirefoxOptions();
firefoxoptions.setProfile(myProfile);
WebDriver driver = new FirefoxDriver(firefoxoptions);

Background I am using Firefox 56.0 and Windows 7


Need Your Help

WordPress loop ignore first post of three separate meta_values

wordpress loops metadata advanced-custom-fields

I have a loop that is pulling all of the news out, however there is three main stories set by ACF. These are Main, Secondary and Third. This wouldn't be a problem if there was only one post set to ...

Finding a sql query to get the latest associated date for each grouping

sql tsql

I have a sql table of payroll data that has wage rates and effective dates associated with those wage rates, as well as hours worked on various dates. It looks somewhat like this: