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.

As suggested on StackOverflow I am posting this question here as well.

I am having trouble how to implement the Given step (with SpecFlow for example) of a logged in user role throught Web UI automation (with WatiN for example).

Let's say I have the following feature:

Feature: Site usage overview
    As an admin
    I want to view a site usage overview
    So that I know how many visitors my site attracts

This feature uses the admin user role. One possible scenario would be:

Scenario: Admin should see site usage
    Given I am logged in as an admin
    When I navigate to the Site usage overview page
    Then I should see the site usage statistics

This is just as an example. The scenario could also be written with clicking buttons or links and validating some text on the page or something, but I'm not going into that discussion.

What I want to know is how to implement this Given step. I see a couple of options:

  • Writing all the stuff for logging in as some admin (go to login page, log in as admin)
  • Verifying some loggid in state that is visible on every page (currently logged in as ...)

But what if the authentication mechanism is Windows Authentication (Integrated Security / Active Directory) or something similar in other environments and there is no visible indication of your logged in status and you need to be logged in for every page in the whole site?

I hope I am missing something and someone can point me in the right direction.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

To make this change for "As an admin" you will need to change the code that is generated in specflow. This would be the best place for this change IMHO.

As Lyndon suggests, the code that I have written for WatiN should work, but it really depends on how the application is developed.

My other answer copied below.

What I found to solve the "run test as user x in .net" problem I needed to perform a two step process.

Step 1: Fire up the process I want to test as the alternate user.
Step 2: Use impersonation on the call to allow my test harness to access the process from the other user.

This was done for IE in WatiN, but I am guessing that the code will be similar, just calling your chosen test exe.

using System;
using System.Configuration;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Security;
using System.Net;
using System.Security.Principal;
using System.Reflection;
using System.IO;

 public static void SetAlternateUser(string userName, string password, string domain)
        // thread safe singleton code
        lock (threadSafeLock)
            if (_ie == null)

            _ie = null;

            // fill the NetworkCredeitials object that we use for impersonation
            if (string.IsNullOrEmpty(domain))
                alternateUserCredentials = new NetworkCredential(userName, password);
                alternateUserCredentials = new NetworkCredential(userName, password, domain);

            // Prepare to launch
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.UserName = userName;
            psi.Password = SecurePassword(password);
            if (!string.IsNullOrEmpty(domain))
                psi.Domain = domain;
            psi.UseShellExecute = false;
            psi.LoadUserProfile = true;
            psi.FileName = "c:\\Program Files\\Internet Explorer\\iexplore.exe";
            psi.Arguments = "about:blank";

            // launch
            Process proc = new Process();
            proc.StartInfo = psi;

            // Time to become an imposter
            hToken = IntPtr.Zero;
            hTokenDuplicate = IntPtr.Zero;

            if (Win32.LogonUser(alternateUserCredentials.UserName, alternateUserCredentials.Domain, alternateUserCredentials.Password, 2 /*LOGON32_LOGON_INTERACTIVE*/, 0 /*LOGON32_PROVIDER_DEFAULT*/, out hToken))
                if (Win32.DuplicateToken(hToken, 2, out hTokenDuplicate))
                    windowsIdentity = new WindowsIdentity(hTokenDuplicate);
                    impersonationContext = windowsIdentity.Impersonate();

                    _ie = IE.AttachToIE(Find.ByUrl("about:blank"));

share|improve this answer
Thanks. So basically what you do is start up IE with the required user and attach WatiN (my current choice for Web UI Automation) to that instance of IE? –  Rodi Jul 12 '11 at 6:01
And for Forms (/Custom) Authentication? Would you just log in using the login page/form in the Given step? (and of course first have seperate tests that verify you can login) –  Rodi Jul 12 '11 at 6:03
@rodi ... yep that should work –  Bruce McLeod Jul 12 '11 at 12:34
add comment

Although I haven't had a chance to try it in this particular circumstance, you could try the answer that I received to a similar question.

If it is just as simple as logging in as the admin, I would first ensure that you can log in as an admin, add that code to your setUp, and then do a verification in the Given

share|improve this answer
add comment

Your Answer


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.