Batch Account login test function implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.