views:

324

answers:

2

I am using WatiN (2.0.10.928) with C# and Visual Studio 2008 to test a SSL secured website that requires a certificate. When you navigate to the homepage a "Choose a digital certificate" dialog is displayed and requires that you select a valid certificate and click the 'OK' button.

I'm looking for a way to automate the certificate selection so that every time a new test or fixture is executed (and my browser restarts) I don't have to manually interfere with the automated test and select the certificate. I've tried using various WatiN Dialog Handler classes and even looked into using the Win32 API to automate this but haven't had much luck.

I finally found a solution but its adds another dependency to the solution (a third party library called AutoIT). Since this solution isn't ideal but does work and is the best I could find, I will post the solution and mark it as the answer but I am still looking for an 'out of the box' WatiN solution that is more consistent with the rest of my code and test fixtures.

Thanks for your responses!

A: 

The best solution I could find so far was posted here: http://andrey-zhukov.blogspot.com/2009/10/recently-i-wanted-to-choose-digital.html

As stated in the post, it requires a reference to the AutoIT library: http://www.autoitscript.com/autoit3/index.shtml

MoMo
If you have multiple certificates you need to choose from, you may need to add parts of this solution to the Accepted Answer solution.
MoMo
+2  A: 

In my situation I have exactly one certificate attached, so I have to pick up the one and only existing on the list, so I have really simple DialogHandler for this - it only clicks on the button if it cans handle the dialog:

public class CertificateChoosingHandler : BaseDialogHandler
{
    public override bool HandleDialog(Window window)
    {
        new WinButton(1, window.Hwnd).Click();
        return true;
    }

    public override bool CanHandleDialog(Window window)
    {
        return window.StyleInHex == "94C808CC";
    }
} 

AFAIR this solution won't work in Windows 7.

EDIT: I forgot about something useful. When I found that this solution is not working in Windows 7, I discovered very interesting option in IE Internet Options somewhere in "Custom Level": Don’t prompt for client certificate selection when no certificates or only one certificate exists. So I have added my site to trusted sites and edited settings, and there is no need now for me to use this DialogHandler, but it still can be used even if no dialog appears. If it is not clear, what I wrote, here is how to Enable Prompt for Certificate in Internet Explorer to show certificate dialog.

prostynick
Thanks for the reply! Your situation is similar to mine. In fact, I had modified my original solution to just click OK. I'm using your solution because it is an 'out of the box' WatiN solution and it doesn't have any 3rd party dependencies.
MoMo