Currently I am trying to monitor and log Http Requests through a proxy using the following:
<p>
using
Telerik.TestingFramework.Controls.KendoUI;<br>
using
Telerik.WebAii.Controls.Html;<br>
using
Telerik.WebAii.Controls.Xaml;<br>
using
System;<br>
using
System.Collections.Generic;<br>
using
System.Text;<br>
using
System.Linq;<br><br>
using
System.Web;<br><br>
using
ArtOfTest.Common.UnitTesting;<br>
using
ArtOfTest.WebAii.Core;<br>
using
ArtOfTest.WebAii.Controls.HtmlControls;<br>
using
ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts;<br>
using
ArtOfTest.WebAii.Design;<br>
using
ArtOfTest.WebAii.Design.Execution;<br>
using
ArtOfTest.WebAii.ObjectModel;<br>
using
ArtOfTest.WebAii.Silverlight;<br>
using
ArtOfTest.WebAii.Silverlight.UI;<br><br>
using
ArtOfTest.WebAii.Messaging.Http;
//Needed for Telerik HTTPProxy<br><br>namespace EpicMix<br>{<br> public class HTTPProxy : BaseWebAiiTest<br> {</p><p> <br> public List<Dictionary<string, string>> NavigateToUnauthenticatedUrl(string testUrl)<br> {<br> // Check for HTTP Requests in proxy<br> RequestListenerInfo ensReq = new RequestListenerInfo(checkRequests);<br> Manager.Current.Http.AddBeforeRequestListener(ensReq);<br> <br> <br> // Add our HTTP response event handler. For each response the proxy gets, parse for the Omniture URL's query string parameters<br> ResponseListenerInfo li = new ResponseListenerInfo(GetQueryStringParamatersAndValues);<br> Manager.Current.Http.AddBeforeResponseListener(li);<br> <br> // Navigate to the given URL (passed in from the test case)<br> Manager.Current.ActiveBrowser.NavigateTo(testUrl);<br> <br> // Wait '5000' msec for url:'{testUrl}'<br> Manager.Current.ActiveBrowser.WaitForUrl(testUrl, true, 5000);<br> <br> // Wait for the page to completely load<br> Manager.Current.ActiveBrowser.Frames.WaitAllUntilReady();<br> <br> // We don't need the event handler any longer. Removing it stops listening to responses in the proxy<br> Manager.Current.Http.RemoveBeforeResponseListener(li);<br> Manager.Current.Http.RemoveBeforeRequestListener(ensReq);<br> <br> return ResponseDictionaries;<br> }<br> <br> private void checkRequests(object sender, HttpRequestEventArgs e)<br> {<br> <br> try{<br> Manager.Log.WriteLine(String.Format("Request for {0}", e.Request.RequestUri));<br> }<br> catch (NullReferenceException nre)<br> {<br> Manager.Log.WriteLine("Request E is null" + nre.Message);<br> }<br> }</p><p>}</p><p>}</p>
I have checked and made to make a log in the test settings and have checked the setting to use the HttpProxy in the test list settings, which is why I haven't set it here.
However I am running into the following error which I have attached a picture of:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
at ArtOfTest.WebAii.Design.BaseWebAiiTest.get_Manager()
at EpicMix.HTTPProxy.checkRequests(Object sender, HttpRequestEventArgs e)
11 Answers, 1 is accepted

using
Telerik.TestingFramework.Controls.KendoUI;
using
Telerik.WebAii.Controls.Html;
using
Telerik.WebAii.Controls.Xaml;
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Linq;
using
System.Web;
using
ArtOfTest.Common.UnitTesting;
using
ArtOfTest.WebAii.Core;
using
ArtOfTest.WebAii.Controls.HtmlControls;
using
ArtOfTest.WebAii.Controls.HtmlControls.HtmlAsserts;
using
ArtOfTest.WebAii.Design;
using
ArtOfTest.WebAii.Design.Execution;
using
ArtOfTest.WebAii.ObjectModel;
using
ArtOfTest.WebAii.Silverlight;
using
ArtOfTest.WebAii.Silverlight.UI;
using
ArtOfTest.WebAii.Messaging.Http;
//Needed for Telerik HTTPProxy
namespace
EpicMix
{
public
class
HTTPProxy : BaseWebAiiTest
{
#region [ Dynamic Pages Reference ]
private
Pages _pages;
public
List<Dictionary<
string
,
string
>> ResponseDictionaries =
new
List<Dictionary<
string
,
string
>>();
/// <summary>
/// Gets the Pages object that has references
/// to all the elements, frames or regions
/// in this project.
/// </summary>
public
Pages Pages
{
get
{
if
(_pages ==
null
)
{
_pages =
new
Pages(Manager.Current);
}
return
_pages;
}
}
#endregion
// START TEST VARIABLES
// Omniture URL (partial)
private
string
omnitureUrl = @
".snow.com/b/ss/"
;
// Dictionary to hold the query string parameters and values
private
static
Dictionary<
string
,
string
> dictionary =
null
;
// END TEST VARIABLES
//*******************************************************************************************************************************************************
// Navigate to Url and get Omniture
public
List<Dictionary<
string
,
string
>> NavigateToUnauthenticatedUrl(
string
testUrl)
{
// Check for Ensighten Request
RequestListenerInfo ensReq =
new
RequestListenerInfo(checkRequests);
Manager.Current.Http.AddBeforeRequestListener(ensReq);
// Add our HTTP response event handler. For each response the proxy gets, parse for the Omniture URL's query string parameters
ResponseListenerInfo li =
new
ResponseListenerInfo(GetQueryStringParamatersAndValues);
Manager.Current.Http.AddBeforeResponseListener(li);
// Navigate to the given URL (passed in from the test case)
Manager.Current.ActiveBrowser.NavigateTo(testUrl);
// Wait '5000' msec for url:'{testUrl}'
Manager.Current.ActiveBrowser.WaitForUrl(testUrl,
true
, 5000);
// Wait for the page to completely load
Manager.Current.ActiveBrowser.Frames.WaitAllUntilReady();
// We don't need the event handler any longer. Removing it stops listening to responses in the proxy
Manager.Current.Http.RemoveBeforeResponseListener(li);
Manager.Current.Http.RemoveBeforeRequestListener(ensReq);
return
ResponseDictionaries;
}
private
void
checkRequests(
object
sender, HttpRequestEventArgs e)
{
try
{
Manager.Log.WriteLine(String.Format(
"Request for {0}"
, e.Request.RequestUri));
}
catch
(NullReferenceException nre)
{
Manager.Log.WriteLine(
"Request E is null"
+ nre.Message);
}
}
}
}

Thanks for all details shared and excuse us for the delay.
I reviewed the coded solution you have provided but I couldn't reproduce the same behavior against a public accessible page. If you could provide an executable sample we could determine what the issue is.
Based on the exception you shared I understand that the catch part is executed from the provided code. This probably means that the string e.Request.RequestUri is null against the tested application but against bing.com it returns value and it is listed in the log.
Please find attached a sample project with the executable code against www.bing.com. You could debug your own in Visual Studio in order to understand what is not correct.
Please let me know if I could be helpful with anything. Thanks!
Regards,
Elena Tsvetkova
Telerik by Progress

Hey Elena,
Thanks for your help. Attached is my sample project. I tried comparing code to your project but am still ending up with the error. I think it has to do with the fact that I'm running the NavigateToUNauthenticatedUrl method from a different test. Please take a look and see if you can verify the NullException that I'm seeing on your end.
Drew



Thank you for the sample project you shared with me.
I was able to find out what causes the issue. In the HTTPProxy class where the Log.WriteLine is failing with NullReference you should add Manager.Current in front to specify the current threading. Please refer to the attached screenshot and edit the project accordingly.
Please let me know if that works for you as well.
Thanks for your cooperation!
Regards,
Elena Tsvetkova
Telerik by Progress

Hey Elena,
So this next question is why I wanted to capture the requests in the first place. But I'm trying to intercept and change one of the requests. Do you know how I would do that exactly? using the same checkRequests method? Or do you have a doc I can look at to see how it'd be done?

Hey Elena, I've basically made this method to try and intercept a certain http request and change it before goes and gets and brings back info. Basically the idea is to listen to all the requests until CheckRequests spots the ensightenPath one. Then change it and it continues to make the call and returns a different set of info than it normally would.
private
string
omnitureUrl = @
".snow.com/b/ss/"
;
private
string
ensightenPath = @
"nexus.ensighten.com/vailresorts/prod/serverComponent.php"
;
// Dictionary to hold the query string parameters and values
private
static
Dictionary<
string
,
string
> dictionary =
null
;
// END TEST VARIABLES
//*******************************************************************************************************************************************************
// Navigate to Url and get Omniture
public
List<Dictionary<
string
,
string
>> NavigateToUnauthenticatedUrl(
string
testUrl)
{
Assert.IsNotNull(Manager.Current.Http,
"Class is null"
);
// Check for Ensighten Request
RequestListenerInfo ensReq =
new
RequestListenerInfo(checkRequests);
Manager.Current.Http.AddBeforeRequestListener(ensReq);
// Add our HTTP response event handler. For each response the proxy gets, parse for the Omniture URL's query string parameters
ResponseListenerInfo li =
new
ResponseListenerInfo(GetQueryStringParamatersAndValues);
Manager.Current.Http.AddBeforeResponseListener(li);
// Navigate to the given URL (passed in from the test case)
Manager.Current.ActiveBrowser.NavigateTo(testUrl);
// Wait '5000' msec for url:'{testUrl}'
Manager.Current.ActiveBrowser.WaitForUrl(testUrl,
true
, 5000);
// Wait for the page to completely load
Manager.Current.ActiveBrowser.Frames.WaitAllUntilReady();
// We don't need the event handler any longer. Removing it stops listening to responses in the proxy
Manager.Current.Http.RemoveBeforeResponseListener(li);
Manager.Current.Http.RemoveBeforeRequestListener(ensReq);
return
ResponseDictionaries;
}
public
void
checkRequests(
object
sender, HttpRequestEventArgs e)
{
try
{
string
intercept = e.Request.RequestUri;
if
(intercept.Contains(ensightenPath)){
//Manager.Current.Log.WriteLine(e.Request.RequestUri);
string
substringRequest = e.Request.RequestUri.Replace(
"nexus.ensighten.com"
,
"nexus-test.ensighten.com"
);
e.Request.RequestUri = substringRequest;
Manager.Current.Log.WriteLine(e.Request.RequestUri);
}
}
catch
(NullReferenceException nre)
{
Manager.Current.Log.WriteLine(
"Request E is null"
+ nre.Message);
}
}
Thanks for the additional details.
The scenario you described seems to be closer to API testing since you would like to manipulate the http requests. Therefore I would like to introduce Test Studio for APIs if you have missed the product. It is still in beta version however has plenty of features already and might be of further help for you.
The API tests could be easily run from within a web test to integrate both approaches to complete the requirements. I hope this could be also helpful to you.
On the other hand I would like to share an article for you providing further details about the HTTP proxy and how to use it.
I hope you will find what is missing in the above details. Please let me know in case further assistance is required.
Regards,
Elena Tsvetkova
Telerik by Progress