Few websites and Test environment will be running web application with a invalid certificates causing browser to ask users interactions on confirming whether to visit the side or not. This mechanism will warm users to rethink before visiting a particular insecure site. During testing this can be little tricky, as our first line of code which is calling DRIVER class to visit a site which will not be executed until SSL confirmation is received from users. Fortunately selenium handles these confirmation, so we can truly create an automated tested case even on these sites.
CODE
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.util.concurrent.TimeUnit;
public class Manage_SSL_certificates {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","src\\Geko_driver\\chromedriver.exe");
DesiredCapabilities ch = DesiredCapabilities.chrome();
//ch.acceptInsecureCerts();
ch.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
ch.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
ChromeOptions c = new ChromeOptions();
c.merge(ch);
WebDriver driver = new ChromeDriver(c);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
}
EXPLANATION
Use Class "DesiredCapabilities", then by accessing its "setCapability" method we can either accept insecure connection as well as confirm the certificate by passing appropriate below arguments,
Arguments accepting for insecure connection - (CapabilityType.ACCEPT_INSECURE_CERTS, true);
Arguments accepting for SSL Certificate - (CapabilityType.ACCEPT_SSL_CERTS, true);
These operation may vary based on each browsers so we have to create a reference variable for browser option and then merge it with the DesiredCapabilities.
ChromeOptions c = new ChromeOptions();
c.merge(ch);
Finally passing the reference variable of the Browser option the driver Class,
WebDriver driver = new ChromeDriver(c);