3

I'm using Selenium Web Driver on Windows 7.

I need to test a web application and I've to use a SSL certificate to enter.

I don't know how to select the right certificate to use my application.

I'm in this situation now

enter image description here

NOTE: I've put also the AutoIt window to add some info.

Here

How to select Security Certificate From security Dialog

I've found a good answer, but I'm quite a newbie in Java so it's not so easy for me to understand how to use this code, that is exactly what I need to.

Could someone tell me, which libraries I've to import? Or some others alternatives?

3 Answers 3

3

I found a solution that requires no browser profile with a client certificate preconfigured but rather test must provide the certificate. The downside is that it is based on PhantomJS Web browser implementation, not on Firefox or Chrome.

You just need to install version 2.1.1 version or later. A client certificate can be provided with the following command-line parameters.

  • --ssl-client-certificate-file: an absolute path of the client certificate file eventually containing a secret key.
  • --ssl-client-key-file: an absolute path of a file containing just a secret key. Only needed in case of separate locations of certificate and key.
  • --ssl-client-key-passphrase: a passphrase for the secret key.

Preparing client certificates

I followed the instructions here to prepare certificate in a proper format:

  1. Extract cert for --ssl-client-certificate-file parameter

    openssl pkcs12 -nokeys -clcerts -in cert.p12 -out src/test/resources/keys/cert.cer 
    
  2. Extract key for --ssl-client-key-file parameter

    openssl pkcs12 -nocerts  -in cert -out src/test/resources/keys/cert.key
    

Marrying with Selenium automated tests

I was able to use it with PhantomJSWebDriver (v1.2.1). which wraps PhantomJS:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setJavascriptEnabled(true);
capabilities.setCapability("takesScreenshot", true);

ImmutableMap<String, String> commandLineArguments = ImmutableMap.<String, String>builder()
    .put("web-security", "false")
    .put("ssl-protocol", "any")
    .put("ignore-ssl-errors", "true")
    .put("webdriver-loglevel", "DEBUG");
    .put("ssl-client-certificate-file", clientCertificateAbsolutePath)
    .put("ssl-client-key-file", clientSecretKeyAbsolutePath)
    .put("ssl-client-key-passphrase", clientSecretKeyPasshphrase)
    .build();

String[] params = commandLineArguments.entrySet().stream()
            .map(e -> String.format("--%s=%s", e.getKey(), e.getValue()))
            .collect(Collectors.toList())
            .toArray(new String[0]);

capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, params);
WebDriver driver = new PhantomJSDriver(capabilities);
2

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!

3
  • This solution does not scale, as you need to install certificate on each machine you're running the test and configure Web browser properly before running the test. Have you found a better way to use Selenium certificates?
    – dzieciou
    Apr 20, 2016 at 14:42
  • Unfortunately no. My tests were very simple proof-of-concept.
    – Cesare
    Apr 20, 2016 at 19:22
  • I found a solution with PhantomJS, see my answer.
    – dzieciou
    Apr 21, 2016 at 12:10
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.