PAGE -42 :How to handle SSL certificate error in Selenium WebDriver using Java

How to handle SSL certificate error in Selenium WebDriver using Java

FireFox Untrusted Connection
  1. import org.openqa.selenium.WebDriver;  
  2. import org.openqa.selenium.firefox.FirefoxDriver;  
  3. import org.openqa.selenium.firefox.FirefoxProfile;  
  4.   
  5. public class Firefoxuntrustconnection {  
  6.   
  7.     public static void main(String[] args) throws InterruptedException {  
  8.   
  9.         // Create Firefox Profile  
  10.         FirefoxProfile profile = new FirefoxProfile();  
  11.         // Accept Untrusted Certificates  
  12.         profile.setAcceptUntrustedCertificates(true);  
  13.         //Intialize Forfox driver  
  14.         WebDriver driver = new FirefoxDriver(profile);  
  15.         //Maximize browser window         
  16.         driver.manage().window().maximize();  
  17.         // Go to desired Untrusted website  
  18.         driver.get("http://www.google.com");  
  19.         Thread.sleep(5000);  
  20.         //close Firefox browser  
  21.         driver.quit();  
  22.     }  
  23.   
  24. }  


SSL certificate error handling for Google Chrome
  1. import org.openqa.selenium.WebDriver;  
  2. import org.openqa.selenium.chrome.ChromeDriver;  
  3. import org.openqa.selenium.remote.CapabilityType;  
  4. import org.openqa.selenium.remote.DesiredCapabilities;  
  5.   
  6. public class ChromeSSLcertificateerrorhandling {  
  7.   
  8.     public static void main(String[] args) throws InterruptedException {  
  9.         DesiredCapabilities capability = DesiredCapabilities.chrome();  
  10.         // Accept SSL certificate   
  11.       capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);  
  12.         //set the system property for Chrome   
  13.       System.setProperty("webdriver.chrome.driver""chromedriver.exe");  
  14.         //create Google Chrome instance  
  15.         WebDriver driver = new ChromeDriver(capability);  
  16.          //Maximize browser window         
  17.         driver.manage().window().maximize();    
  18.        // Go to desired Untrusted website    
  19.         driver.get("http://www.google.com");          
  20.         Thread.sleep(5000);  
  21.         //close Chrome browser   
  22.         driver.quit();  
  23.     }  
  24.   
  25. }  


SSL certificate error handling for IE
  1. import org.openqa.selenium.WebDriver;  
  2. import org.openqa.selenium.ie.InternetExplorerDriver;  
  3. import org.openqa.selenium.remote.CapabilityType;  
  4. import org.openqa.selenium.remote.DesiredCapabilities;  
  5.   
  6. public class IESSLcertificateerrorhandling {  
  7.   
  8.     public static void main(String[] args) throws InterruptedException {  
  9.   
  10.         //set the system property for IE   
  11.       System.setProperty("webdriver.ie.driver""IEDriverServer.exe");  
  12.         //create IE instance  
  13.         WebDriver driver = new InternetExplorerDriver();  
  14.         // Go to desired Untrusted website  
  15.         driver.get("http://www.google.com");  
  16.         //Maximize browser window         
  17.         driver.manage().window().maximize();  
  18.         driver.navigate().to("javascript:document.getElementById('overridelink').click()");  
  19.   
  20.         Thread.sleep(5000);  
  21.         //close IE browser  
  22.         driver.quit();  
  23.   
  24.     }  
  25.   
  26. }  

No comments:

About Me

My photo
Pune, Maharastra, India
You can reach me out at : jimmiamrit@gmail.com