How to Handle SSL Certificate Issue In Java Selenium Program?

Handle SSL Certificate Issue: How to Handle SSL Certificate Issue In Java Selenium Program?

You may have come across a situation where when you try to open some webpage manually, those are opening fine. Still, when trying to open the same site using Selenium WebDriver, you will get an error stating: “This Connection is Untrusted.”

When we try to open manually a URL on our system browser that time the browser already imported the required certificate that’s why we are not getting any error but when we try to open the same URL with selenium webdriver that time Selenium webdriver creates a fresh profile for each run and as the profile doesn’t have the SSL certificate so when you try to open using WebDriver you are getting this error screen.

What Is SSL Security Certificate?

SSL (Secure Sockets Layer) is a standard security protocol that is used to establish an encrypted connection between the server and client, which is a browser.

What is the untrusted certificate?

When you try to access a site that has the security certificate installed at that time, the certificate will help to determine whether the website you are visiting is the site that it claims to be. If there is any problem with the certificate, then that time, you will get an alert message or screen saying ‘This Connection Is Untrusted.’

Types of SSL Security Certificate Error

  • Firefox: This connection is untrusted.
  • Google Chrome: This site security is not trusted.
  • Internet Explorer: This security certificate presented by this website was not trusted by a trusted certificate authority (CA).

How to Handle SSL Certificate Error In Firefox?

As we have mentioned that sometimes the firefox profile doesn’t have the certificate. That’s why we are getting the SSL: certificate error. SO to resolve this issue, we can create a Firefox profile manually, and after that, we can use the same profile with our selenium webdriver. So to resolve this follow the below steps:

  • Create a firefox Profile
  • Set the setAcceptUntrustedCertificates() method as true and setAssumeUntrustedCertificateIssuer() method to false

Selenium Automation Script For Firefox:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class SSLCertificate
{
   public static void main(String[] args)
   {
      //It create firefox profile
      FirefoxProfile profile=new FirefoxProfile();
      // This will set the true value
      profile.setAcceptUntrustedCertificates(true);
      // This will open firefox browser using above created profile
      WebDriver driver=new FirefoxDriver(profile);
      driver.get("pass the url as per your requirement");
   }
}

By following the above step, you can access the URL without any certificate issue.

How to Handle SSL Certificate Error In Chrome & IE?

Above we have seen how we can fix the certificate issues for the firefox browser, and now we are going to see how we can fix such issues in the chrome & IE browser also. In the Firefox browser, we have taken the help of a firefox profile, but for the chrome & IE browser, we need to take the help of Desired capabilities to get and accept the SSL certificate error on run time.

How To Test SSL Certificate?

We are going to discuss how we can handle the SSL Security Certificate error for different browsers like Chrome Browser, IE Browser, and Firefox Browser.

Steps for Chrome Browser:

  • Create An object for Desiredcpabilities class
  • Set the ACCEPT_SSL_CERTS as true
  • Open the chrome browser with the capability
// Create object of DesiredCapabilities class
DesiredCapabilities cap=DesiredCapabilities.chrome();

// Set ACCEPT_SSL_CERTS variable to true
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);


// Set the driver path
System.setProperty("webdriver.chrome.driver","Chrome driver path");


// Open browser with capability
WebDriver driver=new ChromeDriver(cap);

Steps for IE Browser:

Like the Chrome browser, we need to follow the same process to handle the certificate issue for the IE browser.

// Create object of DesiredCapabilities class
DesiredCapabilities cap=DesiredCapabilities.internetExplorer();

// Set ACCEPT_SSL_CERTS variable to true
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

// Set the driver path
System.setProperty("webdriver.ie.driver","IE driver path");

// Open browser with capability
WebDriver driver=newInternetExplorerDriver(cap);
package com.selenium.basics;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SSLCertificateIssue 
{
   public static void main(String[] args) 
   {
      DesiredCapabilities cap=new DesiredCapabilities();
      cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
      WebDriver driver=new FirefoxDriver();
      driver.get("http://www.cacert.org/");
      driver.close();
   }
}

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment