[Selenium+java] SSL Certificate Error Handling in Selenium

Source: Internet
Author: User
Tags ssl certificate what is ssl certificate



Original URL: https://www.guru99.com/ssl-certificate-error-handling-selenium.html


SSL Certificate Error Handling in Selenium



SSL (Secure Socket Layer) Certificate ensures secure transformation of data across the server and client application using Strong encryption Standard or digital signature. One has-to-install an SSL certificate or a code signing certificate.






In this tutorial, you'll learn-


    • What is SSL Certificate

    • How Does the SSL Certificate Create a Secure Connection

    • Types of SSL certificates

    • How SSL certificates is verified

    • Types of SSL Certificate Error

    • How to handle SSL Certificate Error using Selenium webdriver

    • SSL Certificate Error Handling in Firefox

    • SSL Certificate Error Handling in Chrome

    • SSL Certificate Error Handling in IE

What is SSL Certificate


SSL (Secure Sockets Layer) is a standard security protocol for establishing a Secure connection between the server and the Client which is a browser.



There is number of benefits of using SSL certificate like,


    • One can increase their users ' and customer ' s trust in order to enhance the business ' growth rapidly
    • These certificates help to secure online transactions and customers sensitive information like Credit-card/debit-card data , etc.
    • Signing certificate tends to get a maximum number of downloads and good reviews from users.


ssl-secured websites begin with https:// and you can see a-lock icon or green address bar If the connection is SE Curely established.



For example, if want to do some transaction via net banking or want to purchase a Mobile phone through e-commerce site such as Flipkart or Amazon.



What happens between the Web Browser and Server


    1. A browser tries to connect with a website secured with SSL. The browser requests the webserver to identify itself
    2. The server sends the browser a copy of its SSL certificate
    3. The browser verifies whether the SSL certificate is genuine. If So, it sends a message to the server
    4. The server sends back a digitally signed acknowledgment to start an SSL encrypted session
    5. The encrypted data is shared between the server and the browser


In doing so, you need to transmit sensitive information such as credits card numbers or login credentials and that have to T Ransmit securely So, it cannot be hacked or intercept.



For example


    1. Type https://netbanking.hdfcbank.com/netbanking/ .
    2. Hit Enter.
    3. You'll see a green address bar in the browser as below:-




How Does the SSL Certificate Create a Secure Connection




    1. Browser sends HTTPS request to the server.
    2. Now servers must provide some identification to Browser to prove the it is trusted. This can is done by sending a copy of it SSL certificate to the browser.
    3. Each Browser have its own list of Trusted CA ' s. Browser checks the certificate root against its list of trusted CAs and that the certificate are unexpired, unrevoked, and That the common name was valid for the website that it was connecting to.
    4. If The browser trusts the certificate, an encrypted session is created between the server and the browser.
    5. Server and Browser can send encrypted messages
Types of SSL certificates


Browser and the server use SSL Certificate mechanism to is able to establish a secure connection. This connection involves verification of three types of certificates.


    • Root
    • Intermediate
    • Server Certificate


Process of getting SSL Certificate



The process of getting SSL certificate includes below steps:-


    1. First, you must create CSR (create a Certificate Signing request) request.
    2. CSR request creates CSR data file, which is sent to SSL certificate issuer known as CA (certificate authority).
    3. The CA uses the CSR data files to create an SSL certificate for your server.
    4. After receiving the SSL certificate, you had to install it on your server.
    5. An intermediate certificate are also needed to being installed which ties yours SSL certificate with CA ' s root certificate.


The below image represent all the three certificate- Root, Intermediate, and Server certificate.





How SSL certificates is verified


SSL works through a combination of programs and encryption/decryption routine that exist on the Web server computer and we b Server Browser.



SSL Certificate basically contains below information.


    1. Subject which is the identity of the website owner.
    2. Validity Information-a Public and a private key.


The Private and public key is uniquely related cryptographic keys (numbers). Whatever is encrypted by a public key could be decrypted by a private key.






When a secure connection was not established between the server and client due to the certificate, following SSL Certificat E error would be manifested.


Types of SSL Certificate Error


Suppose you type some HTTPS request in the browser and get a message such as "This connection is untrusted" or the "the SI Te ' s security certificate is not trusted "depending upon the browser you are using. Then such error was subject to SSL certificate error.



Now, if the browser are unable to establish a secured connection with the requested certificate and then the browser would thro W "Untrusted Connection" exception as below and ask the user to take appropriate action.



The types of error likely to see due to certificate in different browsers could be somewhat as this


    1. FireFox -this connection is untrusted
    1. Google Chrome -this site security is not trusted
    1. Internet Explorer (IE) -This security certificate presented by this website is not trusted by a trusted Certif Icate Authority (CA)




How to handle SSL Certificate Error using Selenium webdriver


Suppose we have written some test scripts and while executing the script, we caught in the situation as "untrusted Connect Ion "above then how do we handle the exception purely through automation.



In the such case, we had to adjust our script in such a, the It would take care of the SSL Exception by itself.



The scripts need to is modified according to the type of browser instance we is using. These when desired capabilities comes in picture.



Desired capabilities is used to configure the driver instance of Selenium Webdriver. Through desired capabilities, one can configure all driver instance like Chromedriver, Firefoxdriver, and Internet Explore R.



As of now we don ' t has any specific URL to create the above scenario, but I am providing steps that we can add in the Sel Enium Script to handle the above situation "untrusted Connection."


SSL Certificate Error Handling in Firefox


For handling SSL certificate Error in Firefox, we need to use desired capabilities of Selenium Webdriver and follow the FO Llowing steps.



Step 1): First we need to create a new Firefox profile say "myprofile". You can refer Google to learn ' how to create ' Firefox profile. It's simple and easy.



Step 2): Now access Myprofile in the script as below and create the Firefoxprofile object.


ProfilesIni prof = new ProfilesIni()				
FirefoxProfile ffProfile= prof.getProfile ("myProfile")


Step 3): Now we need to set "setacceptuntrustedcertificates" and " Setassumeuntrustedcertificateissuer"Properties in the fire Fox profile.


ffProfile.setAcceptUntrustedCertificates(true) 
ffProfile.setAssumeUntrustedCertificateIssuer(false)


Step 4): Now use the ' Firefox profile ' in the Firefox driver object.




Note: "Setacceptuntrustedcertificates" and "Setassumeuntrustedcertificateissuer" are capabilities To handle the certificate errors in Web browsers.


SSL Certificate Error Handling in Chrome


For handling SSL error in Chrome, we need the use desired capabilities of Selenium Webdriver. The below code would help you accept all of the SSL certificate in Chrome, and the user would not receive any SSL certificate re lated error using this code.



We need to create instance of Desiredcapabilities class as below:-


DesiredCapabilities handlSSLErr = DesiredCapabilities.chrome ()       
handlSSLErr.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true)
WebDriver driver = new ChromeDriver (handlSSLErr);
SSL Certificate Error Handling in IE


Unlike handling SSL certificates in Chrome Browser and Firefox, in IE, your may has to handle it using JavaScript.



To handle SSL certificate in IE, can handle this situation in both ways,


    1. In this, you'll click the link "Continue to this website (not recommended)". In the following we'll see if handle SSL error in IE.


Observe SSL certificate Error in IE browser you'll find "Continue to this website (not recommended)" link. This link has the ID "override link". You can view the ID in HTML mode using F12.






Click on the link using driver.navigate () method with JavaScript as below:-


Driver.navigate (). to ("Javascript:document.getElementById (' Overridelink '). Click ()");
    1. The second method is quite 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);


The above code would help with handle SSL certificate error in IE.



Summary:


    • SSL (Secure Sockets Layer) is a standard security protocol for establishing Secure connection between the server and the C Lient
    • Browser and the server use SSL Certificate mechanism to is able to establish a secure connection.
    • SSL works through a combination of programs and encryption/decryption routine that exist on the Web server computer and we b Server Browser.
    • When secure connection isn't established between the server and client due to certificate SSL certificate error would OCCU R
    • Need to adjust our script in such a-the-it'll take care of the SSL exception/error by itself through Selenium Web Drive R.


[Selenium+java] SSL Certificate Error Handling in Selenium


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.