2

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

  • Fahad, welcome to SQA. Are you asking a question? – user246 Jan 28 '14 at 16:20
  • can you plz add few details like how to invoke ChooseCertificate from webdriver. the handler is not been passed . – jack Jan 20 at 18:08
  • @jack You're unlikely to get a response from someone five years later, friend. :-) – corsiKa Jan 20 at 20:03
1

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);
        }
    }
  • I'm quite a newbye in Java so it's not so easy to use this code that is exactly what I need to. Could someone tall me which libraries I've to import? Thank you very much in advance! – Cesare Jun 17 '15 at 13:55
1

This code is really good, exactly what I was looking for. You just need to install the AutoIT and add it to your project reference. Here is the vb.net version.

Also if you need to call it from a different module:

Sub ClickCertificate()
    Dim cls as new CertificateDialogHandler
    cls.ChooseCertificate("your certificate friendly name", "Windows Security")
    'string "Windows Security" works on Win 7
    'string "your certificate friendly name" needs to be changed to whatever you see on the certificate selection window
End sub



Imports System.Threading
Imports System.Text.RegularExpressions
Imports AutoITX3Lib

Public Class CertificateDialogHandler

    Private autoIt As AutoItX3 = New AutoItX3

    Public Sub ChooseCertificate(ByVal CertificateName As String, ByVal windowTitle As String)
        Thread.Sleep(2)
        Me.autoIt.WinWaitActive(windowTitle, "", 0)
        Dim CurrentCertificate As String = ""
        Me.ViewCertificateInfo(windowTitle)
        CurrentCertificate = Me.GetCertificateIssuedTo

        While True
            If Not CurrentCertificate.Equals(CertificateName) Then
                Me.SelectNextCertificate(windowTitle)
                Me.ViewCertificateInfo(windowTitle)
                Dim newCert As String = Me.GetCertificateIssuedTo
                If newCert.Equals(CurrentCertificate) Then
                    Throw New SystemException("Certificate was not found!")
                Else
                    CurrentCertificate = newCert
                    'TODO: Warning!!! continue Else
                End If

            Else
                Me.PressBtnOK(windowTitle)
                Exit While
            End If


        End While

    End Sub

    Public Sub ConfirmSecurtyException()
        Thread.Sleep(2)
        Me.autoIt.WinWaitActive("Security Alert", "", 0)
        Me.PressBtnYes()
    End Sub

    Public Sub CancelCertificateDialog(ByVal windowTitle As String)
        Thread.Sleep(2)
        Me.autoIt.WinWaitActive(windowTitle, "", 0)
        Me.PressBtnCancel()
    End Sub

    ''' <summary>
    ''' Open certificate details window
    ''' </summary>
    ''' <param name="windowTitle">Title of certificate dialog</param>
    Private Sub ViewCertificateInfo(ByVal windowTitle As String)
        If windowTitle.Equals("Choose a digital certificate") Then
            Me.autoIt.Send("{TAB}", 0)
            Thread.Sleep(1000)
        End If

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

    ''' 
    ''' Extract the certificate name from a "Certificate" window text
    ''' 
    ''' The certificate name
    Private Function GetCertificateIssuedTo() As String
        Dim CertificateWndText As String
        Dim sPattern As String
        Dim SplittedCertificateWndText() As String
        CertificateWndText = Me.autoIt.WinGetText("Certificate", "")
        sPattern = "General" & vbLf & vbLf & "(.+?)" & vbLf
        SplittedCertificateWndText = Regex.Split(CertificateWndText, sPattern)
        Return SplittedCertificateWndText(1)

    End Function

    ''' 
    ''' Determines whether the certificate name is founded 
    ''' in the "Certificate" window text
    ''' 
    ''' 
    Private Function IsFoundedCertificate(ByVal CertificateName As String) As Boolean
        Return (Me.GetCertificateIssuedTo = CertificateName)
    End Function

    ''' 
    ''' Return to the "Choose a digital certificate" window
    ''' and press the button "OK" when the certificate is founded
    Private Sub PressBtnOK(ByVal windowTitle As String)
        If Not windowTitle.Equals("Choose a digital certificate") Then
            Me.autoIt.WinWaitActive("Certificate Details", "", 0)
        End If

        Thread.Sleep(1000)
        Me.autoIt.Send("{ESC}", 0)
        Thread.Sleep(1000)
        Me.autoIt.Send("{TAB}", 0)
        Thread.Sleep(1000)
        Me.autoIt.Send("{ENTER}", 0)
    End Sub

    Private Sub PressBtnCancel()
        'Thread.Sleep(1000);
        'AutoIt.Send("{ESC}", 0);
        Dim i As Integer = 0
        Do While (i < 3)
            Me.autoIt.Send("{TAB}", 0)
            Thread.Sleep(1000)
            i = (i + 1)
        Loop

        Me.autoIt.Send("{SPACE}", 0)
        Thread.Sleep(1000)
    End Sub

    Private Sub PressBtnYes()
        'Thread.Sleep(1000);
        'AutoIt.Send("{ESC}", 0);          
        Dim i As Integer = 0
        Do While (i < 2)
            Me.autoIt.Send("{TAB}", 0)
            Thread.Sleep(1000)
            i = (i + 1)
        Loop

        Me.autoIt.Send("{SPACE}", 0)
        Thread.Sleep(1000)
    End Sub

    ''' 
    ''' Return to the "Choose a digital certificate" window and 
    ''' select next certificate name from the name list
    ''' 
    Private Sub SelectNextCertificate(ByVal windowTitle As String)
        If Not windowTitle.Equals("Choose a digital certificate") Then
            Me.autoIt.WinWaitActive("Certificate Details", "", 0)
        End If

        Thread.Sleep(1000)
        Me.autoIt.Send("{ESC}", 0)
        Thread.Sleep(1000)
        Me.autoIt.Send("{TAB}", 0)
        Thread.Sleep(1000)
        Me.autoIt.Send("{DOWN}", 0)
    End Sub
End Class 
0

enter image description here

You can also achieve this using SikuliX. Here is a link for how to configure SikuliX.

I have added the sample snippet of images for both the certificate and ok button. Then the code can be:

public static void clickCertificate() {
String certificateZkhan = "C:/zkhan.PNG";
String okButton = "C:/ok.PNG";
Screen screen = new Screen();
    try {
        screen.click(certificateZkhan);
        screen.click(okButton);
    } catch (FindFailed findFailed) {
        findFailed.printStackTrace();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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