SSL CERTIFICATE ERROR HANDLING IN SELENIUM WEBDRIVER FOR CHROME,IE AND FIREFOX BROWSER
Some time we get these SSL certificate errors or notification in Selenium WebDriver when we try to execute our scripts, but Selenium WebDriver has provided us to give capabilities to handle this certificate error in Chrome Driver/Firefox /IE browsers.
Here I am posting the code that would help you in resolving the SSL error mainly in Chrome Browser
DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver = new ChromeDriver(capability);
Above script will help you in accepting all the SSL certificate in Chrome and by doing so you would not get any SSL error while executing your code.
While in Firefox following code will work.
In Firefox you need to apply this code
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.openqa.selenium.firefox.FirefoxProfile;
import
org.openqa.selenium.firefox.internal.ProfilesIni;
public
class
acceptSSLCerti
{
public
static
void
main(String[] args) {
//Class ProfilesIni details
ProfilesIni allProfiles =
new
ProfilesIni();
// Use FirefoxProfile Constructor
FirefoxProfile myProfile = allProfiles.getProfile("CertificateIssue");
myProfile.setAcceptUntrustedCertificates(
true
);
myProfile.setAssumeUntrustedCertificateIssuer(
false
);
WebDriver Driver =
new
FirefoxDriver(myProfile);
Driver.get("WebSite URL were you are facing this Certificate error"
);
}
}
For IE browser we can use following code, But in IE we have two way to handle this problem..
Case 1: In this we will click on “Continue to this website (not recommended)” and for the same we are going to use document.elementById(“Id”)
So let’s see the code
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Test;
public class HandlingSSLErrorInIE{
private WebDriver driver;
@Test
public void TestSSLerror() {
//Download IEDriver exe and past in project
System.setProperty("webdriver.ie.driver","IEDriverServer.exe");
driver = new InternetExplorerDriver();
//Open WebSite for time being let's take http://xyz.com
driver.get("https://xyz.com");
//now we are going to use javascipt ,This will click on "Continue to this website (not recommended)" text and will //push us to the page
driver.navigate().to("javascript:document.getElementById('overridelink').click()");
}
Case 2: Second method is pretty similar to Chrome SSL Handling code
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
System.setProperty("webdriver.ie.driver","IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver(capabilities);
So by following all the above code snippet we can handle SSL Certificate Error in all major browser like IE, Firefox and Chrome browser.
If you like this post then please like and share this post!!
No comments:
Post a Comment