6

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.

2 Answers 2

3

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)
            {
                StartIE();
            }

            _ie.Close();
            _ie = null;


            // fill the NetworkCredeitials object that we use for impersonation
            if (string.IsNullOrEmpty(domain))
            {
                alternateUserCredentials = new NetworkCredential(userName, password);
            }
            else
            {
                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;
            proc.Start();

            // 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();
                    System.Threading.Thread.Sleep(1000);

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

                }
            }
        }
    }
3
  • 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
    Commented Jul 12, 2011 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
    Commented Jul 12, 2011 at 6:03
  • 1
    @rodi ... yep that should work Commented Jul 12, 2011 at 12:34
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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