This is a migrated thread and some comments may be shown as answers.

Log.Writeline returning NullReferenceException

11 Answers 133 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Drew
Top achievements
Rank 1
Drew asked on 28 Mar 2017, 06:39 PM

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

Sort by
0
Drew
Top achievements
Rank 1
answered on 28 Mar 2017, 06:42 PM
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);
            }
        }
}
}
0
Drew
Top achievements
Rank 1
answered on 28 Mar 2017, 06:44 PM
Reformatted the code properly. 
0
Elena
Telerik team
answered on 03 Apr 2017, 08:39 AM
Hi Drew,

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
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Drew
Top achievements
Rank 1
answered on 03 Apr 2017, 05:47 PM

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

0
Drew
Top achievements
Rank 1
answered on 03 Apr 2017, 05:48 PM
Forgot to add. The test I am executing the HTTP parsing method from is the BeaverCreek Homepage test. Is is in it's own test list.
0
Drew
Top achievements
Rank 1
answered on 03 Apr 2017, 05:49 PM
Forgot to add the test I am running the HTTP parsing method from is the Beaver Creek Homepage test. It has it's own test list it executes from.
0
Drew
Top achievements
Rank 1
answered on 03 Apr 2017, 05:49 PM
Forgot to add the test I am running the HTTP parsing method from is the Beaver Creek Homepage test. It has it's own test list it executes from.
0
Elena
Telerik team
answered on 06 Apr 2017, 01:23 PM
Hi 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
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Drew
Top achievements
Rank 1
answered on 25 Apr 2017, 08:42 PM

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?

0
Drew
Top achievements
Rank 1
answered on 25 Apr 2017, 10:39 PM

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);
          }
          
      }

 

0
Elena
Telerik team
answered on 01 May 2017, 07:00 AM
Hello Drew,

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
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
Tags
General Discussions
Asked by
Drew
Top achievements
Rank 1
Answers by
Drew
Top achievements
Rank 1
Elena
Telerik team
Share this question
or