In most Web tests, you want to examine some properties of various elements on a page. This is simple once you get a reference to the page itself.
Executing a simple request.
The simplest way is just execute the TestSession..::.GetPage(String) method
| Visual Basic | Copy Code |
|---|
Dim page as System.Web.UI.Page = session.GetPage("Default.aspx") |
| C# | Copy Code |
|---|
System.Web.UI.Page page = session.GetPage("Default.aspx"); |
This method takes a single parameter which is the page's Url, relative to the application root.
Note that the above code is just a shortcut for the following:
| Visual Basic | Copy Code |
|---|
Dim request As New WebRequest("Default.aspx")
Dim response As WebResponse = session.ProcessRequest(request)
Dim page As Web.UI.Page = response.Page
|
| C# | Copy Code |
|---|
WebRequest request = new WebRequest("Default.aspx");
WebResponse response = session.ProcessRequest(request);
System.Web.UI.Page page = response.Page;
|
The Ivonna.Framework..::.WebRequest class is responsible for preparing the request: setting up the headers and cookies, post data and event handlers.
The WebResponse class encapsulates the response data, such as headers, cookies, status code, and Html output. In most cases, however, it is the Page property that you need.
Now that you have a Page object, you can verify that it works correctly as simple as this:
| Visual Basic | Copy Code |
|---|
Dim label as Label = page.FindControl("HelloLabel")
Assert.AreEqual("Hello", label.Text)
|
| C# | Copy Code |
|---|
Label label = (Label)page.FindControl("HelloLabel");
Assert.AreEqual("Hello", label.Text);
|
Executing a postback.
Another simple scenario is when you get a page, enter some values, click some button, and examine the postback result.
In Ivonna, the postback is invoked using the TestSession..::.ProcessPostback()()() method. As a parameter, it takes the control (or its ID property) that invokes the postback (such as a button). Note that autopostbacks are not implemented in this version.
This method returns a Page object. If you want more control, create a request using the TestSession..::.CreatePostRequest(String) method, modify its properties, process it using the TestSession..::.ProcessRequest(WebRequest) method, and investigate the output (a WebResponse instance).
| Visual Basic | Copy Code |
|---|
Dim session As New TestSession()
Dim testPage As Web.UI.Page = session.GetPage("PostBack.aspx")
Dim NameTextBox As Web.UI.WebControls.TextBox = testPage.FindControl("NameTextBox")
NameTextBox.Text = "John" 'user enters some data
testPage = session.ProcessPostback("HelloButton")
Dim label As Web.UI.WebControls.Label = testPage.FindControl("HelloLabel")
Assert.AreEqual("Hello John", label.Text) 'The entered text should appear here
|
| C# | Copy Code |
|---|
TestSession session = new TestSession();
Page testPage = session.GetPage("PostBack.aspx");
TextBox NameTextBox = (TextBox) testPage.FindControl("NameTextBox");
NameTextBox.Text = "John"; //user enters some data
testPage = session.ProcessPostback("HelloButton");
Label label = (Label) testPage.FindControl("HelloLabel"); //The entered text should appear here
Assert.AreEqual("Hello John", label.Text);
|