i

Selenium Step By Step Guide

Handling SSL Certificate

What is an SSL Certificate?

SSL (Secure Sockets Layer) helps in establishing a secure connection between client and server.  SSL certificates ensure to transfer data securely using a digital signature. SSL certificate ensures the security of sensitive information like customer bank details, Credit card details as communication between server and client happens over an encrypted environment.

A web site having an SSL certificate always shows the https:// an “s” is added after the “http” to identify the web site as SSL secured site.

Communication between the Clients and Servers through SSL Certificates:

Let us see how a browser interacts with a server through SSL certificates:

  1. The browser connects to a web server secured with the SSL certificate, and then the browser asks the server to verify it.
  2. The server sends a copy of the SSL certificate to the web browser.
  3. The browser verifies the SSL certificate to check if it is genuine or not; then, the browser sends a confirmation to the server.
  4. The server sends the acknowledgment by sending a copy of Digital signed confirmation, then the SSL encrypted session is created.
  5. Encrypted data is shared between the clients and the server.

In some cases, we face issues with the SSL certification expired or not valid anymore, then the browser starts showing error pages instead of expected web pages. Let us see how popular browsers show errors

  1. Google Chrome:

  1. Firefox:

 

  1. Internet Explorer:

 

We get such situations while writing the Selenium test scripts, and if any Web site SSL certificate is expired, our test suite gets failed without running a single test.

So, let us see how to handle the SSL errors in selenium script by using the DesiredCapabilities class of the Selenium package.

SSL Certification error Handling in the Chrome browser:

We can use the DesiredCapabilities class to set the capability to handle the SSL certificate error. Let us see the sample code below:

package SeleniumTest.SeleniumTest;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.remote.CapabilityType;

import org.openqa.selenium.remote.DesiredCapabilities;

public class SSLCertificationErrorHandling

{

    public static void main( String[] args )

    {

        DesiredCapabilities cap=DesiredCapabilities.chrome();

        cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

        WebDriver driver=new ChromeDriver(cap);

        driver.get(webUrl);

 

    }

}

 

SSL Certificate error resolution in IE web browser:

We can handle SSL certification error situation in IE browser by using DesiredCapabilities class as shown in the below sample code:

package SeleniumTest.SeleniumTest;

 

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.ie.InternetExplorerDriver;

import org.openqa.selenium.remote.CapabilityType;

import org.openqa.selenium.remote.DesiredCapabilities;

public class SSLCertificationErrorHandling

{

    public static void main( String[] args )

    {

      DesiredCapabilities cap=new DesiredCapabilities();

      cap.setCapability("ignoreProtectedModeSettings", true);

      cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

      WebDriver driver=new InternetExplorerDriver(cap);

      driver.get(webUrl);

    }

}

SSL Certification error Handling in Firefox browser:

In Firefox, we need to follow below steps to handle SSL certification error:

  1. Create an object of FirefoxOptions and set the property by using the setCapability() method.

    FirefoxOptions options=new FirefoxOptions();

   options.setCapability("acceptInsecureCerts", true);

  1. Create an object of the DesiredCapabilities class and pass on the “options” object to the setCapability method of DesiredCapabilities class.

  DesiredCapabilities cap=new DesiredCapabilities();

  cap.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);

  1. Create the FirefoxDriver class object and pass the “cap” object to the constructor of FirefoxDriver class. Now we can launch the URL which has SSL certificate error.

WebDriver driver=new FirefoxDriver(cap);

driver.get(WebUrl);

Let us see the complete sample code to handle the SSL certificate error in Firefox browser:

package SeleniumTest.SeleniumTest;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.firefox.FirefoxOptions;

import org.openqa.selenium.remote.CapabilityType;

import org.openqa.selenium.remote.DesiredCapabilities;

public class SSLFirefox

{

    public static void main( String[] args )

    {

        System.setProperty("webdriver.gecko.driver",driverPath);

        FirefoxOptions options=new FirefoxOptions();

        options.setCapability("acceptInsecureCerts", true);

        DesiredCapabilities cap=new DesiredCapabilities();

        cap.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);

        WebDriver driver=new FirefoxDriver(cap);

        driver.get("https://hyperion-ui-test1.kubeodc-test.corp.intranet/#/home");

      

    } 

}

Note: As we do not have any web page available right now to produce SSL certification error, So I have kept the URL section as WebURL in the above examples.