Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

The problem I would like to solve is as follow:

  1. go to a website which is protected by client certificate authentication using browser
  2. once the security challenge dialog box is shown, select on the dialog
  3. look through the list of certificates I have in my keystore
  4. select the desired certificate and click 'ok'

Is this possible with Selenium/Webdriver?

enter image description here

share|improve this question
    
Fahad, welcome to SQA. Are you asking a question? –  user246 Jan 28 at 16:20
add comment

1 Answer

I have the same issue. Currently my workaround is to use AutoIt. Below is the code that select desired certificate:

public class CertificateDialogHandler
    {
            AutoItX3 autoIt = new AutoItX3(); 

        public void ChooseCertificate(string CertificateName, string windowTitle)
        {
            Thread.Sleep(2);
            autoIt.WinWaitActive(windowTitle, "", 0);
            string CurrentCertificate = "";
            ViewCertificateInfo(windowTitle);
            CurrentCertificate = GetCertificateIssuedTo();

            while (true)
            {
                if (!CurrentCertificate.Equals(CertificateName))
                {
                    SelectNextCertificate(windowTitle);
                    ViewCertificateInfo(windowTitle);
                    string newCert = GetCertificateIssuedTo();
                    if (newCert.Equals(CurrentCertificate))
                        throw new SystemException("Certificate was not found!");
                    else
                    {
                        CurrentCertificate = newCert;
                        continue;
                    }
                }

                else
                {
                    PressBtnOK(windowTitle);
                    break;
                }
            }
        }

        public void ConfirmSecurtyException()
        {
            Thread.Sleep(2);
            autoIt.WinWaitActive("Security Alert", "", 0);

            PressBtnYes();

        }


        public void CancelCertificateDialog(string windowTitle)
        {
            Thread.Sleep(2);
            autoIt.WinWaitActive(windowTitle, "", 0);

            PressBtnCancel();

        }

        /// <summary>
        /// Open certificate details window
        /// </summary>
        /// <param name="windowTitle">Title of certificate dialog</param>

        private void ViewCertificateInfo(string windowTitle)
        {
            if (windowTitle.Equals("Choose a digital certificate"))
            {
                autoIt.Send("{TAB}", 0);
                Thread.Sleep(1000);
            }

            autoIt.Send("{SPACE}", 0);
            Thread.Sleep(1000);
            autoIt.WinWaitActive("Certificate", "", 0);
        }

        /// 
        /// Extract the certificate name from a "Certificate" window text
        /// 
        /// The certificate name
        private string GetCertificateIssuedTo()
        {
            string CertificateWndText;
            string sPattern;
            string[] SplittedCertificateWndText;

            CertificateWndText = autoIt.WinGetText("Certificate", "");
            sPattern = "General\n\n(.+?)\n";
            SplittedCertificateWndText = Regex.Split(CertificateWndText, sPattern);

            return SplittedCertificateWndText[1]; ;
        }

        /// 
        /// Determines whether the certificate name is founded 
        /// in the "Certificate" window text
        /// 
        /// 
        private bool IsFoundedCertificate(string CertificateName)
        {
            return GetCertificateIssuedTo() == CertificateName;
        }

        /// 
        /// Return to the "Choose a digital certificate" window
        /// and press the button "OK" when the certificate is founded
        private void PressBtnOK(string windowTitle)
        {
            if (!windowTitle.Equals("Choose a digital certificate"))
            {
                autoIt.WinWaitActive("Certificate Details", "", 0);
            }

            Thread.Sleep(1000);
            autoIt.Send("{ESC}", 0);

            Thread.Sleep(1000);
            autoIt.Send("{TAB}", 0);
            Thread.Sleep(1000);
            autoIt.Send("{ENTER}", 0);

        }

        private void PressBtnCancel()
        {
            //Thread.Sleep(1000);
            //AutoIt.Send("{ESC}", 0);
            for (int i = 0; i < 3; i++)
            {
                autoIt.Send("{TAB}", 0);
                Thread.Sleep(1000);
            }
            autoIt.Send("{SPACE}", 0);
            Thread.Sleep(1000);
        }
        private void PressBtnYes()
        {
            //Thread.Sleep(1000);
            //AutoIt.Send("{ESC}", 0);          
            for (int i = 0; i < 2; i++)
            {
                autoIt.Send("{TAB}", 0);
                Thread.Sleep(1000);
            }
            autoIt.Send("{SPACE}", 0);
            Thread.Sleep(1000);
        }

        /// 
        /// Return to the "Choose a digital certificate" window and 
        /// select next certificate name from the name list
        /// 
        private void SelectNextCertificate(string windowTitle)
        {
            if (!windowTitle.Equals("Choose a digital certificate"))
            {
                autoIt.WinWaitActive("Certificate Details", "", 0);
            }

            Thread.Sleep(1000);
            autoIt.Send("{ESC}", 0);


            Thread.Sleep(1000);
            autoIt.Send("{TAB}", 0);
            Thread.Sleep(1000);
            autoIt.Send("{DOWN}", 0);
        }
    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.