WaitiN is used to write a simple automated login test. It can use a small amount of code to perform login testing for batch accounts.
The Code is as follows:
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using WatiN. Core;
Namespace ConsoleApplication1
{
Class Program
{
[STAThread]
Static void Main (string [] args)
{
List <LoginTester. LoginAccount> Accounts = new List <LoginTester. LoginAccount> ();
Accounts. Add (new LoginTester. LoginAccount () {UserName = "your user account", Password = "aaaaa", ShouldSuccess = false });
Accounts. Add (new LoginTester. LoginAccount () {UserName = "your user account", Password = "", ShouldSuccess = false });
Accounts. Add (new LoginTester. LoginAccount () {UserName = "your user account", Password = "your password", ShouldSuccess = true });
LoginTester tester = new LoginTester ("http://passport.cnblogs.com/login.aspx", "http://home.cnblogs.com", "tbUserName", "tbPassword", "btnLogin ");
Tester. BrowserVisible = true;
Accounts. ForEach (t => tester. ExecuteTest (t. UserName, t. Password, t. ShouldSuccess ));
Console. writeLine ("\ r \ n" );
Console. WriteLine ("************* Test Report Summary ****************");
Console. WriteLine (tester. ReportSummary );
}
Public class LoginTester
{
Public class LoginAccount
{
Public string UserName {get; set ;}
Public string Password {get; set ;}
Public bool ShouldSuccess {get; set ;}
}
Private string loginUrl = string. Empty;
Private string loginSuccessForwaredUrl = string. Empty;
Private string loginButtonName = string. Empty;
Private string userNameFieldName = string. Empty;
Private string passwordFieldName = string. Empty;
Public string ReportSummary {get; private set ;}
Public bool BrowserVisible {get; set ;}
Public LoginTester (string loginUrl, string loginSuccessForwaredUrl, string userNameFieldName, string passwordFieldName, string loginButtonName)
{
This. loginUrl = loginUrl;
This. loginSuccessForwaredUrl = loginSuccessForwaredUrl;
This. userNameFieldName = userNameFieldName;
This. passwordFieldName = passwordFieldName;
This. loginButtonName = loginButtonName;
}
Public void ExecuteTest (string userName, string password, bool loginSuccess)
{
String msg = string. Format ("userName: {0}, password: {1}, expected login: {2}", userName, password, loginSuccess );
Using (IE browser = new IE (this. loginUrl ))
{
Browser. Visible = this. BrowserVisible;
Browser. TextField (Find. ByName (this. userNameFieldName). TypeText (userName );
Browser. TextField (Find. ByName (this. passwordFieldName). TypeText (password );
Browser. Button (Find. ByName (this. loginButtonName). Click ();
Bool loginIsSuccess = browser. Url. IndexOf (this. loginSuccessForwaredUrl, StringComparison. OrdinalIgnoreCase)> = 0;
Msg = string. Format ("{0} \ r \ n {1}", msg, loginIsSuccess = loginSuccess? "Successful": "Failed ");
ReportSummary + = msg + "\ r \ n ";
Console. WriteLine (msg );
}
}
}
}
Source code download