Tuesday 4 August 2015

How to handle SSL certificate error in Selenium WebDriver using Java

//FireFox Untrusted Connection

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

public class Firefoxuntrustconnection {

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

        // Create Firefox Profile
        FirefoxProfile profile = new FirefoxProfile();
        // Accept Untrusted Certificates
        profile.setAcceptUntrustedCertificates(true);
        //Intialize Forfox driver
        WebDriver driver = new FirefoxDriver(profile);
        //Maximize browser window      
        driver.manage().window().maximize();
        // Go to desired Untrusted website
        driver.get("http://www.google.com");
        Thread.sleep(5000);
        //close Firefox browser
        driver.quit();
    }

}

SSL certificate error handling for Google Chrome

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 ChromeSSLcertificateerrorhandling {
    public static void main(String[] args) throws InterruptedException {
        DesiredCapabilities capability = DesiredCapabilities.chrome();
        // Accept SSL certificate
      capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        //set the system property for Chrome
      System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
        //create Google Chrome instance
        WebDriver driver = new ChromeDriver(capability);
         //Maximize browser window      
        driver.manage().window().maximize(); 
       // Go to desired Untrusted website 
        driver.get("http://www.google.com");       
        Thread.sleep(5000);
        //close Chrome browser
        driver.quit();
    }

}
 

SSL certificate error handling for IE   

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 IESSLcertificateerrorhandling {
    public static void main(String[] args) throws InterruptedException {

        //set the system property for IE
      System.setProperty("webdriver.ie.driver", "IEDriverServer.exe");
        //create IE instance
        WebDriver driver = new InternetExplorerDriver();
        // Go to desired Untrusted website
        driver.get("http://www.google.com");
        //Maximize browser window      
        driver.manage().window().maximize();
        driver.navigate().to("javascript:document.getElementById('overridelink').click()");

        Thread.sleep(5000);
        //close IE browser
        driver.quit();
    }
}

No comments:

Post a Comment