如何以特定域用户身份运行Internet Explorer Selenium测试?

时间:2015-01-20 12:52:27

标签: asp.net-mvc selenium selenium-webdriver windows-authentication specflow

我有一个ASP.NET MVC网站,它使用Windows身份验证来控制访问。我希望有一个specflow selenium测试,通过尝试以非授权用户身份访问该站点来检查配置是否正确。

由于我们使用域帐户来控制访问权限,因此没有用户名/密码登录屏幕。浏览器会自动将当前用户的凭据传递给站点。

因此,对于我的Selenium测试,我需要能够以特定用户身份运行Internet Explorer。

我发现了许多关于Windows模拟的文章,我可以在测试运行期间切换到我的测试用户(使用http://support.microsoft.com/kb/306158中的代码)。但是,如果我创建一个InternetExplorerDriver,它会使用我的凭据而不是测试用户启动Internet Explorer(尽管这个问题和答案表明它应该可以正常运行https://sqa.stackexchange.com/questions/2277/using-selenium-webdriver-with-windows-authentication)。

我还可以明确地启动Internet Explorer进程作为我的测试用户,但是我无法看到将InternetExplorerDriver绑定到已经运行的Internet Explorer进程的方法,所以这可能是一个死胡同。

我的代码基本上来自上面的MSDN页面。在调试器中我可以看到WindowsIdentity.GetCurrent()。Name是" testUser"在测试的所有步骤中。

namespace MyProject.Specs
{
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using TechTalk.SpecFlow;

[Binding]
public class AuthorisationSteps
{
    public const int LOGON32_LOGON_INTERACTIVE = 2;
    public const int LOGON32_PROVIDER_DEFAULT = 0;
    private static WindowsImpersonationContext impersonationContext;
    private static IWebDriver driver;

    [BeforeScenario]
    public static void impersonateUser()
    {
        if (!impersonateValidUser("testUser", "testDomain", "password"))
        {
            throw new Exception();
        }
        driver = new InternetExplorerDriver();
    }

    [AfterScenario]
    public static void cleanupUser()
    {
        undoImpersonation();
        driver.Quit();
    }

    [Given(@"I am an unauthorised user")]
    public void GivenIAmAnUnauthorisedUser()
    {
        var temp = WindowsIdentity.GetCurrent().Name;
    }

    [When(@"I go to the home page")]
    public void WhenIGoToTheHomePage()
    {
        var temp = WindowsIdentity.GetCurrent().Name;
        driver.Navigate().GoToUrl(BaseUrl);
    }

    [Then(@"I should see an error page")]
    public void ThenIShouldSeeAnErrorPage()
    {
        var temp = WindowsIdentity.GetCurrent().Name;
        Assert.That(driver.Title.Contains("Error"));
    }

    [DllImport("advapi32.dll")]
    public static extern int LogonUserA(String lpszUserName,
                                        String lpszDomain,
                                        String lpszPassword,
                                        int dwLogonType,
                                        int dwLogonProvider,
                                        ref IntPtr phToken);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int DuplicateToken(IntPtr hToken,
                                            int impersonationLevel,
                                            ref IntPtr hNewToken);

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool RevertToSelf();

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    public static extern bool CloseHandle(IntPtr handle);

    private static bool impersonateValidUser(String userName, String domain, String password)
    {
        WindowsIdentity tempWindowsIdentity;
        var token = IntPtr.Zero;
        var tokenDuplicate = IntPtr.Zero;

        if (RevertToSelf())
        {
            if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
                LOGON32_PROVIDER_DEFAULT, ref token) != 0)
            {
                if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                {
                    tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                    impersonationContext = tempWindowsIdentity.Impersonate();
                    if (impersonationContext != null)
                    {
                        CloseHandle(token);
                        CloseHandle(tokenDuplicate);
                        return true;
                    }
                }
            }
        }
        if (token != IntPtr.Zero)
        {
            CloseHandle(token);
        }
        if (tokenDuplicate != IntPtr.Zero)
        {
            CloseHandle(tokenDuplicate);
        }
        return false;
    }

    private static void undoImpersonation()
    {
        impersonationContext.Undo();
    }
}

}

8 个答案:

答案 0 :(得分:2)

这实际上是可行的。我遇到了你遇到的确切问题。基本上,这是您需要执行的步骤。

  1. 使用后台其他用户的凭据手动启动浏览器驱动程序

    Process driverProcess;
    string driverPath; // The path to Selenium's IE driver.
    ProcessStartInfo info = new ProcessStartInfo(driverPath)
    {
        UserName = "UserName", // The user name.
        Password = new SecureString(), // The password for the user.
        UseShellExecute = false,
        LoadUserProfile = true,
        Arguments = "about:blank"
    };
    // Start the driver in background thread
    Thread startThread = new Thread(
        () => {
            try
            {
                driverProcess = Process.Start(info);
                driverProcess.WaitForExit();
            }
            catch
            {
                // Close the process.
            }
        })
    {
        IsBackground = true
    };
    startThread.Start();
    
  2. 使用远程Web驱动程序挂接手动启动的浏览器驱动程序实例。

    var remoteDriver = new RemoteWebDriver(Uri("http://localhost:5555"), DesiredCapabilities.InternetExplorer());
    
  3. 完成后,请记得关闭/退出/终止驱动程序进程和浏览器实例。

    // Close the process when done.
    if (driverProcess != null)
    {
        // Free managed resources
        if (!driverProcess.HasExited)
        {
            driverProcess.CloseMainWindow();
            driverProcess.WaitForExit(5000);
            // Kill the process if the process still alive after the wait
            if (!driverProcess.HasExited)
            {
                driverProcess.Kill();
            }
    
            driverProcess.Close();
        }
    
        driverProcess.Dispose();
        driverProcess = null;
    }
    

答案 1 :(得分:2)

我们有许多使用Windows身份验证的企业客户端用于面向Intranet的应用程序,我们开始运行许多Selenium测试以进行确认,回归等。

我们从Steven的回答中获取了有用的代码,并将其重构为一个类似于其他Impersonate帖子的可重复使用的类,这些帖子对我们来说并不适用,因为我们想要测试在开发中本地工作,并作为Visual Studio Team System发布过程的一部分进行部署。

uri方法在本地不起作用,也没有使用Win32本机方法模拟方法。

这个就是这样的。

使用重构为帮助程序的Steven代码的测试示例

[TestMethod]
public void ThisApp_WhenAccessedByUnathorizedUser_ShouldDisallowAccess()
{
    string userName = "ThisAppNoAccess";
    string password = "123456";
    string domainName = Environment.MachineName;
    using (new Perkins.Impersonator(userName, domainName, password))
    {
        // - Use Remote Web Driver to hook up the browser driver instance launched manually.
        using (var driver = new RemoteWebDriver(new Uri("http://localhost:9515"), DesiredCapabilities.Chrome()))
        {
            var desiredUri = Helper.Combine(Helper.BaseURL, "/ThisApp/#/appGrid");
            TestContext.WriteLine("desiredUri: {0}", desiredUri);
            driver.Navigate().GoToUrl(desiredUri);
            Helper.WaitForAngular(driver);
            var noPermissionNotificationElement = driver.FindElementByXPath("//div[@ng-show='!vm.authorized']/div/div/div/p");
            var showsNoPermissionNotification = noPermissionNotificationElement.Text.Contains("You do not have permissions to view ThisApp.");
            Assert.AreEqual(true, showsNoPermissionNotification, "The text `You do not have permissions to view ThisApp.` is not being displayed!");
        }
    }
}

助手类

// Idea from http://stackoverflow.com/a/34406336/16008
// - Launch the browser driver manually with other user's credentials in background
public class Perkins
{
    public class Impersonator : IDisposable
    {
        Process _driverProcess = null;
        string _driverPath = @"chromedriver.exe";
        /// <summary>
        /// Impersonates the specified user account by launching the selenium server under that account.  Connect to it via RemoteWebDriver and localhost on port 9515.
        /// </summary>
        /// <remarks>
        /// We may later want to enhance this by allowing for different ports, etc.
        /// </remarks>
        /// <param name="userName">Name of the user</param>
        /// <param name="domainName">Name of the domain or computer if using a local account.</param>
        /// <param name="password">The password</param>
        public Impersonator(string userName, string domainName, string password)
        {
            ProcessStartInfo processStartInfo = new ProcessStartInfo(_driverPath);
            processStartInfo.UserName = userName;
            System.Security.SecureString securePassword = new System.Security.SecureString();
            foreach (char c in password)
            {
                securePassword.AppendChar(c);
            }
            processStartInfo.Password = securePassword;
            processStartInfo.Domain = domainName; // this is important, mcollins was getting a 'stub received bad data' without it, even though rglos was not
            processStartInfo.UseShellExecute = false;
            processStartInfo.LoadUserProfile = true; // this seemed to be key, without this, I get Internal Server Error 500
            Thread startThread = new Thread(() =>
            {
                _driverProcess = Process.Start(processStartInfo);
                _driverProcess.WaitForExit();
            })
            { IsBackground = true };
            startThread.Start();
        }
        public void Dispose()
        {
            // - Remember to close/exit/terminate the driver process and browser instance when you are done.
            if (_driverProcess != null)
            {
                // Free managed resources
                if (!_driverProcess.HasExited)
                {
                    _driverProcess.CloseMainWindow();
                    _driverProcess.WaitForExit(5000);
                    // Kill the process if the process still alive after the wait
                    if (!_driverProcess.HasExited)
                    {
                        _driverProcess.Kill();
                    }
                    _driverProcess.Close();
                }
                _driverProcess.Dispose();
                _driverProcess = null;
            }
        }
    }
}

也许这会帮助其他人解决同样的问题。

答案 2 :(得分:0)

similar question指向此Microsoft support article的链接。基本上你需要

System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext = 
((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();
IWebDriver webDriver = new InternetExplorerDriver();
// do your stuff here.
impersonationContext.Undo();

支持文章中有关于模仿特定用户的其他代码。

答案 3 :(得分:0)

你有几台旧电脑吗?还是某些虚拟机的容量?

如果是,请构建Selenium Grid设置,并将其配置为自动登录为所需的域用户,并将其设置为非域用户。 http://code.google.com/p/selenium/wiki/Grid2

答案 4 :(得分:0)

当我为基于Web的应用程序进行自动化项目时,我遇到了同样的问题,这需要窗口身份验证。但是,我已经使用firefox实现了这一点,以下是实现它的步骤。

FIREFOX SETUP

  1. 打开你的系统和类型的运行对话&#39; firefox.exe -p&#39; (在运行此命令之前关闭你的FIFOFOX浏览器)http://www.wikihow.com/Create-a-Firefox-Profile
  2. 点击创建个人资料并将名称命名为
  3. 选择已创建的个人资料并启动浏览器并打开附加管理器(工具 - 附加)
  4. 搜索&#39; AutoAuth&#39;并安装它。它会要求重新启动,做到这一点
  5. 一旦FIREFOX重新启动,打开网址就会要求您进行身份验证
  6. 输入用户名和密码 - 提交信息,FIREFOX会要求您记住密码
  7. 点击记住,它将在FIREFOX简介中保存密码
  8. 复制创建的FIREFOX配置文件并将其保存到需要的文件夹
  9. 使用FIREFOX驱动程序在您的硒写入调用上面创建的配置文件并通过相同的URL,它不会要求认证对话
  10. 这在我的项目中非常成功。

答案 5 :(得分:0)

我们在2年内对IE和Chrome使用https://stackoverflow.com/a/31540010/3489693方法。它工作正常

答案 6 :(得分:0)

因此,问题试图规避的问题似乎与NTLM自动登录有关。见Google Chrome and NTLM Auto Login Using Windows Authentication

上面的解决方案对我不起作用,因为自动登录会成功地与我系统上的任何用户进行身份验证,因此我用于模拟的用户并不重要。

但是,我注意到您可以通过将localhost替换为任何其他域名(例如本地IP地址)来超越自动登录。不需要假冒:))

答案 7 :(得分:0)

这可能/可能不起作用。

  • 尝试在“ CHROME”中启动您的网站。
  • 按F-12,转到“应用程序”选项卡-> Cookies->单击您的站点链接。在左侧查找代表您的会话ID的内容,可以是JSESSIONID或代表用户会话的类似内容,然后复制该内容。
  • 现在打开Internet Explorer
  • 按F-12并通过在控制台窗口中运行此命令手动创建JSESSIONID(或类似键)

document.cookie =“ JSESSIONID =您的会话ID-来自Chrome”

  • 点击播放按钮以执行脚本
  • 刷新浏览器