Asp.net mvc 4 IEDriverServer在使用模拟时引发错误
我正在尝试测试一个ASP.NET MVC4 web应用程序,该应用程序使用模拟同时与不同的用户连接。因此,测试的想法是使用不同的模拟用户执行驱动程序的多个实例。听起来概念上很简单 但是,如果在使用与当前用户不同的用户调用LogonUser后运行IEDriverServer,则在启动时会显示应用程序故障对话框。这是在事件日志中注册的信息:Asp.net mvc 4 IEDriverServer在使用模拟时引发错误,asp.net-mvc-4,selenium,selenium-webdriver,impersonation,Asp.net Mvc 4,Selenium,Selenium Webdriver,Impersonation,我正在尝试测试一个ASP.NET MVC4 web应用程序,该应用程序使用模拟同时与不同的用户连接。因此,测试的想法是使用不同的模拟用户执行驱动程序的多个实例。听起来概念上很简单 但是,如果在使用与当前用户不同的用户调用LogonUser后运行IEDriverServer,则在启动时会显示应用程序故障对话框。这是在事件日志中注册的信息: Faulting application name: IEDriverServer.exe, version: 2.44.0.0, time stamp: 0x
Faulting application name: IEDriverServer.exe, version: 2.44.0.0, time stamp: 0x54496690
Faulting module name: IED7543.tmp, version: 0.0.0.0, time stamp: 0x5449668c
Exception code: 0xc0000005
Fault offset: 0x000000000009d609
Faulting process id: 0x760
Faulting application start time: 0x01d00e3d12f93475
Faulting application path: ...\bin\IEDriverServer.exe
Faulting module path: C:\Users\user\AppData\Local\Temp\IED7543.tmp
Report Id: 5383a54d-7a30-11e4-b39c-000c29b46927
这是我用于模拟的代码。这是基于我发现的所有例子,所以我没有感到惊讶。。。我们还尝试使用具有相同结果的包装:
public class ImpersonationHelper
{
public enum LogonType
{
Interactive = 2,
Network = 3,
Batch = 4,
Service = 5,
Unlock = 7,
Cleartext = 8, // Win2K or higher
NewCredentials = 9 // Win2K or higher
};
public enum LogonProvider
{
Default = 0,
Winnt35 = 1,
Winnt40 = 2,
Winnt50 = 3
};
public enum ImpersonationLevel
{
SecurityAnonymous = 0,
SecurityIdentification = 1,
SecurityImpersonation = 2,
SecurityDelegation = 3
}
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out IntPtr phToken);
[DllImport("kernel32.dll")]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool CloseHandle(IntPtr handle);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel, ref IntPtr hNewToken);
public sealed class Impersonator : IDisposable
{
public string Domain { get; private set; }
public string User { get; private set; }
public string Password { get; private set; }
public LogonType Type { get; private set; }
public LogonProvider Provider { get; private set; }
private WindowsImpersonationContext _context;
private IntPtr _token;
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public Impersonator(string domain, string user, string password,
LogonType type = LogonType.Interactive,
LogonProvider provider = LogonProvider.Default)
{
Domain = domain;
User = user;
Password = password;
Type = type;
Provider = provider;
_token = IntPtr.Zero;
Logon();
}
public void Dispose()
{
Undo();
}
private void Logon()
{
try
{
if (!LogonUser(User, Domain, Password, (int)Type, (int)Provider, out _token))
{
int ret = Marshal.GetLastWin32Error();
throw new Exception(String.Format("LogonUser failed with error code : {0}", ret));
}
_context = WindowsIdentity.Impersonate(_token);
}
catch (Exception exception)
{
var message = exception.Message;
Undo();
}
}
private void Undo()
{
try
{
if (_token != IntPtr.Zero)
{
CloseHandle(_token);
_token = IntPtr.Zero; // Clean _token so Undo() could be called several times
}
if (_context != null)
{
_context.Undo();
_context = null; // Clean _context so Undo() could be called several times
}
}
catch (Exception exception)
{
var message = exception.Message;
// Releasing resources failed...
}
}
}
}
引发异常的代码是:
using (new ImpersonationHelper.Impersonator("WIN-NKLTTMMUEPD", "tester", "tester"))
{
using (IWebDriver driver = new InternetExplorerDriver())
{
driver.Url = _appAddress;
return null;
}
}
有人知道如何防止出现此错误吗?非常感谢您。我无法重现您的
IEDriver
崩溃
然而,由于你在这个问题上悬赏,我不介意花一点时间试图找到一个解决方案。我以前从未实现过Windows身份验证或需要测试模拟,但是我有UI自动化测试的经验
时间限制为一小时:
- 创建了一个使用Windows身份验证的示例Intranet MVC应用程序李>
- 添加了一个新项目,并设置了加载站点的基本单元测试
- 添加了模拟
SafeHandle
,而dispose路径更干净
我写的测试
[Test]
public void GivenImpersonatedUser_IsLoggedInCorrectly()
{
const string domain = "domain"; // . for local machine
const string username = "impersonate.username";
const string password = "strongp@ssw0rd1";
const LogonType logonType = LogonType.Interactive;
using (Impersonation.LogonUser(domain, username, password, logonType))
{
var page = Host.Instance.NavigateToInitialPage<HomePage>();
Assert.That(page.LoginPanel.LoggedInUserName, Is.EqualTo(string.Format("{0}\\{1}", domain, username)));
}
}
Seleno主机配置,使用
我还学到了其他一些东西:
-如果您的用户名或密码不正确,您将收到错误代码1326
。
-您可以使用来确认webdriver进程是使用正确的身份验证令牌启动的
正如我所说,我无法回答您确切的问题,但是请尝试使用上面的内容并进行回复-很高兴能提供进一步帮助。您的答案中包含了大量有价值的信息,可以帮助您不断研究如何进行涉及多个用户的测试。谢谢你的努力!然而,这个错误原来是x64版本驱动程序的一个问题。
public class HomePage : Page
{
public LoginPanel LoginPanel
{
get { return GetComponent<LoginPanel>(); }
}
}
public class LoginPanel : UiComponent
{
public string LoggedInUserName
{
get { return Find.Element(By.Id("login-username")).Text; }
}
}
public static class Host
{
private static readonly InternetExplorerDriver Driver = new InternetExplorerDriver();
public static readonly SelenoHost Instance = new SelenoHost();
static Host()
{
Instance.Run(c =>
{
c.WithRemoteWebDriver(() => Driver);
c.ProjectToTest(new WebApplication(ProjectLocation.FromFolder("WebApplication1"), 61285));
c.UsingLoggerFactory(new ConsoleFactory());
c.UsingCamera("C:\\screenshots");
});
}
var codeBase = assembly.CodeBase;
var uri = new UriBuilder(codeBase);
var path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}