Click here to Skip to main content
15,879,535 members
Articles / Web Development / HTML
Article

Testing HTML Controls with NUnitASP

Rate me:
Please Sign up or sign in to vote.
2.69/5 (5 votes)
5 Aug 20052 min read 38.2K   5   15   5
Usually NUnitASP is used to test web controls(UI) on ASPX page. This artilcle explain how to test HTML controls which are on the ASPX page.

Introduction

This artilce explains how to test HTML Controls which are on ASPX page through NUnitASP. Usually NUnitASP tests the web controls on ASPX page. Even though NUnit has provided enough support to test most of the web controls some times it needs to test HTML controls of ASPX page. NUnit has provided 2 tester objects for HTML controls. In this artilce I just brief thruogh how to test the other HTML controls like Label, Button, Table etc..

NUnitASP uses Response text to test the values of Web Controls or HTML Controls. Response text is usually in HTML format. That means IIS emits HTML format for the web controls in the response. We can take this Response text as advantage and search thruogh the response text and find the value for the required HTML controls.

Testing the Web Controls

Testing web controls with NUnitASP is a straight farword method. In the first step, declare the Tester object for the control which you want to test.

lblHeader = new LabelTester("lblHeading", CurrentWebForm);<BR><BR>     lblRep = new LabelTester("lblRepName", CurrentWebForm);<BR><BR>     txtName = new TextBoxTester("txtUserName",CurrentWebForm);<BR><BR>     btnReport = new ButtonTester("btnRun", CurrentWebForm);<BR><BR>     grdData = new DataGridTester("dgData",CurrentWebForm);<BR>
Next, Call GetPage method of browser, which sends a request to the page.
Browser.GetPage(<A href="http://localhost/GetTrackerReportTest/GetReport.aspx">http://localhost/GetTrackerReportTest/GetReport.aspx</A>);

Now, test the Controls for the required property

AssertVisibility(lblHeader,true);<BR><BR>     AssertVisibility(lblRep,true);<BR><BR>     AssertVisibility(txtName,true);<BR><BR>     AssertVisibility(btnReport,true);<BR><BR>     AssertVisibility(grdData,false);<BR>
Find more details on how to test Web Conrols at http://nunitasp.sourceforge.net/tutorial/index.html

Testing the HTML Controls

Testing process is same for even HTML Controls. Only the thing is we need to find equivalent web control for HTML Control and create object to Web Control tester class with HTML control ID. For example if you want to test HTML input control, create an object to TextBox web control with HTML control id.

Ex: If the following is HTML Control that needs to be tested,

<input type="text" id="HTMLTextBox" value="HTML Textbox Value"

Then you need to create object for TextBoxTester

TextBoxTester txtHTMLName = new TextBoxTester("HTMLTextBox",CurrentWebForm);<BR>

Once, object is created, you can test HTML Control as you are testing the web control.

Simillarly for HTML Label create and object of LabelTester, for HTML Table create and object of DataGridTester.

LabelTester lblHTML = new LabelTester("HTMLLabel", CurrentWebForm);<BR><BR>     DataGridTester HTMLDGTester = new DataGridTester("HTMLTable",CurrentWebForm);<BR>
Remember that, when you are testing HTML Table with DataGridTester object, row index will start from -1 not from 0. Usually, In datagrid case -1 row returns Header row. All the HTML Control must contain ID to test it.
string[] strRow0 = {"11","12","13"};<BR><BR>     string[] strRow1 = {"21","22","23"};<BR><BR>     string[] strRow2 = {"31","32","33"};<BR><BR>     DataGridTester HTMLDGTester = new DataGridTester("HTMLTable",CurrentWebForm);<BR><BR>     AssertEquals("HTML Table Failed at Row1", strRow0, HTMLDGTester.GetRow(-1).TrimmedCells);<BR><BR>     AssertEquals("HTML Table Failed at Row2", strRow1, HTMLDGTester.GetRow(0).TrimmedCells);<BR><BR>     AssertEquals("HTML Table Failed at Row3", strRow2, HTMLDGTester.GetRow (1).TrimmedCells);<BR>

Conclusion

For testing the HTML Controls with NUnitASP, you can also use regullar expressions to find the control id. Once the control id is found in response text, parse the statement for the required value.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I am Suresh Reddy B.V. Currently I am working on ASP.Net, C#.

Comments and Discussions

 
QuestionNunit Test for Asp:MenuControl Pin
Malini8221-Feb-07 23:31
Malini8221-Feb-07 23:31 
GeneralNunit ASP and Infragistics Pin
Tad McClellan7-Aug-05 5:01
professionalTad McClellan7-Aug-05 5:01 
QuestionHuh? Where is the article? Pin
AgeKay5-Aug-05 23:36
AgeKay5-Aug-05 23:36 
AnswerRe: Huh? Where is the article? Pin
Suresh Reddy B.V6-Aug-05 0:46
Suresh Reddy B.V6-Aug-05 0:46 
GeneralRe: Huh? Where is the article? Pin
AgeKay6-Aug-05 9:33
AgeKay6-Aug-05 9:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.