我有一个Windows网站,它使用ASP.NET身份验证来控制访问。我想有一个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();
}
}}
发布于 2016-06-02 00:01:09
我们有许多企业客户端将Windows身份验证用于面向intranet的应用程序,并且我们开始运行许多Selenium测试以进行确认、回归等。
我们从Steven的回答中提取了有用的代码,并将其重构为一个可重用的类,类似于其他对我们无效的Impersonate帖子,因为我们希望测试既可以在本地开发中工作,也可以作为Visual Studio Team System发布过程的一部分进行部署。
uri方法在本地不起作用,使用Win32本机方法的模拟方法也不起作用。
这个成功了,所以它就在这里。
使用Steven的代码重构为helper的测试示例
[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!");
}
}
}helper类
// 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;
}
}
}
}也许这会帮助其他人解决同样的问题。
发布于 2015-11-12 15:56:18
这实际上是可能的。我遇到了和你完全一样的问题。基本上,以下是您需要执行的步骤。
Process driverProcess;string driverPath;// Selenium的IE驱动的路径。新信息= ProcessStartInfo ProcessStartInfo(driverPath) { UserName = "UserName",//用户名。SecureString=新密码(),//用户的密码。UseShellExecute = false,LoadUserProfile = true,Arguments = "about:blank“};//在后台线程启动驱动线程Thread startThread = new Thread( () => { try { driverProcess = Process.Start(info);driverProcess.WaitForExit();} catch { //关闭进程。} }) { IsBackground = true };startThread.Start();
DesiredCapabilities.InternetExplorer());
//完成后关闭进程。if (driverProcess != null) { //释放托管资源if (!driverProcess.HasExited) { driverProcess.CloseMainWindow();driverProcess.WaitForExit(5000);//等待if (!driverProcess.HasExited) { driverProcess.Kill();} driverProcess.Close();} driverProcess.Dispose();driverProcess = null;}
发布于 2015-02-11 01:04:57
此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();在支持文章中有更多关于模拟特定用户的代码。
https://stackoverflow.com/questions/28045870
复制相似问题