Mạo danh là khó khăn và tôi đã không bao giờ có thể nhận được IE để chạy như một bối cảnh người dùng với WatiN. Trong quá khứ, tôi đã triển khai phiên bản khác của trang web đang được thử nghiệm với xác thực cơ bản được bật và sau đó đăng nhập qua hộp thoại.
Có một cái nhìn tại các blog sau để biết thêm thông tin và mẫu mã:
http://blogs.msdn.com/jimmytr/archive/2007/04/14/writing-test-code-with-impersonation.aspx
http://blogs.msdn.com/shawnfa/archive/2005/03/21/400088.aspx
Edit: Tôi đã làm việc này ngày hôm nay. Bí quyết là bạn cần tách riêng việc khởi chạy IE và tự động hóa IE vì bạn không thể chỉ làm cả hai trong một lần truy cập.
Khởi chạy đầu tiên tức là sử dụng System.Diagnostics.Process. Một khi bạn đã đưa ra IE sau đó bạn có thể sử dụng mã từ here để đính kèm và nói chuyện với IE sử dụng impersionation
Dưới đây là đoạn code
[TestMethod]
public void TestMethod()
{
SecureString password = new SecureString();
password.AppendChar('p');
password.AppendChar('a');
password.AppendChar('s');
password.AppendChar('s');
password.AppendChar('w');
password.AppendChar('o');
password.AppendChar('r');
password.AppendChar('d');
ProcessStartInfo psi = new ProcessStartInfo();
psi.UserName = "localtest";
psi.Password = password;
psi.UseShellExecute = false;
psi.LoadUserProfile = true;
psi.FileName = "c:\\Program Files\\Internet Explorer\\iexplore.exe";
psi.Arguments = "about:blank";
Process proc = new Process();
proc.StartInfo = psi;
proc.Start();
t.Join();
proc.Kill();
}
private static void DoWorkAs(object o)
{
User u = o as User;
IntPtr hToken = IntPtr.Zero;
IntPtr hTokenDuplicate = IntPtr.Zero;
if (Win32.LogonUser(u.UserName, u.Domain, u.Password, 2 /*LOGON32_LOGON_INTERACTIVE*/, 0 /*LOGON32_PROVIDER_DEFAULT*/, out hToken))
{
if (Win32.DuplicateToken(hToken, 2, out hTokenDuplicate))
{
WindowsIdentity windowsIdentity = new WindowsIdentity(hTokenDuplicate);
WindowsImpersonationContext impersonationContext = windowsIdentity.Impersonate();
// domain\username
Console.WriteLine(" Thread 2 : " + WindowsIdentity.GetCurrent().Name);
IE ie = IE.AttachToIE(Find.ByUrl("about:blank"));
ie.GoTo(@"http://www.google.com/");
ie.TextField(Find.ByName("q")).TypeText("WatiN");
ie.Button(Find.ByName("btnG")).Click();
Assert.IsTrue(ie.ContainsText("WatiN"));
ie.GoTo("about:blank");
//revert
impersonationContext.Undo();
Console.WriteLine(WindowsIdentity.GetCurrent().Name);
}
}
if (hToken != IntPtr.Zero) Win32.CloseHandle(hToken);
if (hTokenDuplicate != IntPtr.Zero) Win32.CloseHandle(hTokenDuplicate);
}
public class User
{
public User(string u, string d, string p)
{
Domain = d;
UserName = u;
Password = p;
}
public string UserName;
public string Domain;
public string Password;
}
public class Win32
{
// P/Invoke snask
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool LogonUser(
string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
out IntPtr phToken);
[DllImport("advapi32.dll", SetLastError = true)]
public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, int
SECURITY_IMPERSONATION_LEVEL, out IntPtr DuplicateTokenHandle);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool CloseHandle(IntPtr hHandle);
}
Mã này cần có một cấu trúc lại, và won'work trên Vista với IE7 , vì một lỗi IE đã được sửa trong IE8.
Cảm ơn Bruce. Điều này đã cho chúng tôi một giải pháp cho phép chúng tôi tiến lên phía trước. Tôi muốn xem WatiN thêm một số loại hỗ trợ mạo danh trong tương lai. –
Có thiếu mã nào ở đây không? 'T' được định nghĩa ở đâu và bạn đã bao giờ gọi DoWorkAs() ở đâu? –
@ Derek Tôi đã nói mã cần một yếu tố tái .. Nguồn được bao gồm trong khung nguồn mở của tôi ở đây http://testingstax.codeplex.com/SourceControl/changeset/view/6390#73028 –