You must enable the HTTP proxy before you can start using it. This setting is turned off by default. This can be done either by adding the setting to your .config file or modifying the setting in the Settings object. To add the setting to your .config file add this to your .config file:
<WebAii.Settings
Manager.Settings.Web.UseHttpProxy="true"
/>
To enable this setting in the Settings object, add code to your initialization section like this:
// This will get a new Settings object. If a configuration
// section exists, then settings from that section will be
// loaded
Settings settings = GetSettings();
// Override the settings you want. For example:
settings.Web.UseHttpProxy = true;
// Now call Initialize again with your updated settings object
Initialize(settings, new TestContextWriteLine(this.TestContext.WriteLine));
' This will get a new Settings object. If a configuration
' section exists, then settings from that section will be
' loaded
Dim settings As Settings = GetSettings()
' Override the settings you want. For example:
settings.UseHttpProxy = True
' Now call Initialize again with your updated settings object
Initialize(settings, New TestContextWriteLine(AddressOf Me.TestContext.WriteLine))
Now that we have the HTTP proxy turned on we can start using it. Here's an example of how we would detect that a response coming back from the web server is an image response.
[TestMethod]
public void ImageDetection()
{
// Launch a new browser window
Manager.LaunchNewBrowser(BrowserType.InternetExplorer);
// Add our HTTP response event handler
ResponseListenerInfo li = new ResponseListenerInfo(CheckTypeForImage);
Manager.Http.AddBeforeResponseListener(li);
// Navigate to a website containing images
ActiveBrowser.NavigateTo("http://news.google.com/");
// We don't need the event handler any longer
Manager.Http.RemoveBeforeResponseListener(li);
}
private void CheckTypeForImage(object sender, HttpResponseEventArgs e)
{
Log.WriteLine(String.Format("Request for {0}", e.Response.Request.RequestUri));
if (e.Response.IsImage)
{
Log.WriteLine(String.Format("Image detected; MIME type: {0}",
e.Response.Headers["Content-Type"]));
}
else
{
Log.WriteLine(String.Format("Not an image; MIME type: {0}",
e.Response.Headers["Content-Type"]));
}
}
<TestMethod()> _
Public Sub ImageDetection()
' Launch a new browser window
Manager.LaunchNewBrowser(BrowserType.InternetExplorer)
' Add our HTTP response event handler
Dim li As New ResponseListenerInfo(AddressOf CheckTypeForImage)
Manager.Http.AddBeforeResponseListener(li)
' Navigate to a website containing images
ActiveBrowser.NavigateTo("http://news.google.com/")
' We don't need the event handler any longer
Manager.Http.RemoveBeforeResponseListener(li)
End Sub
Private Sub CheckTypeForImage(ByVal sender As Object, ByVal e As HttpResponseEventArgs)
Log.WriteLine([String].Format("Request for {0}", e.Response.Request.RequestUri))
If e.Response.IsImage Then
Log.WriteLine([String].Format("Image detected; MIME type: {0}", e.Response.Headers("Content-Type")))
Else
Log.WriteLine([String].Format("Not an image; MIME type: {0}", e.Response.Headers("Content-Type")))
End If
End Sub
Let's go one step further. Suppose we want to verify that all of the images on the web page were smaller than 40Kb. We can accomplish this using code like this:
[TestMethod]
public void CheckImageSize()
{
// Add our HTTP response event handler
ResponseListenerInfo li = new ResponseListenerInfo(Check);
Manager.Http.AddBeforeResponseListener(li);
// Launch a new browser window
Manager.LaunchNewBrowser();
// Navigate to a website containing images
ActiveBrowser.NavigateTo("http://my.homepage.com/");
// Check our global flag
Assert.IsFalse(_imageTooLarge, "Some images in page >40k");
}
private volatile bool _imageTooLarge = false;
private void Check(object sender, HttpResponseEventArgs e)
{
if (e.Response.IsImage && Int32.Parse(e.Response.Headers["Content-Length"]) > 40000)
{
_imageTooLarge = true;
}
}
<TestMethod()> _
Public Sub CheckImageSize()
' Add our HTTP response event handler
Dim li As New ResponseListenerInfo(AddressOf Check)
Manager.Http.AddBeforeResponseListener(li)
' Launch a new browser window
Manager.LaunchNewBrowser()
' Navigate to a website containing images
ActiveBrowser.NavigateTo("http://my.homepage.com/")
' Check our global flag
Assert.IsFalse(_imageTooLarge, "Some images in page >40k")
End Sub
Private _imageTooLarge As Boolean = False
Private Sub Check(ByVal sender As Object, ByVal e As HttpResponseEventArgs)
If e.Response.IsImage AndAlso Int32.Parse(e.Response.Headers("Content-Length")) > 40000 Then
_imageTooLarge = True
End If
End Sub
Note: If you are using localhost in the URL of the NavigateTo method it must contain a trailing '.' character like this: ActiveBrowser.NavigateTo("http://localhost./myTestPage") . This is an IE limitation where all URL's must contain a '.' character in them to be recognized properly when a proxy is in place.