locked
Calling javascript function in webtest RRS feed

  • Question

  • Hi,
    I have to pass a parameter in webtest but that is not available in response to capture.
    I can generate that value using a javascript function. But I have no idea how to pass a string to a javascript function and retreive a value from that function to use it in webtest.
    Can somebody help me on this issue..


    Thanks in advance
    vv
    Monday, March 23, 2009 5:48 AM

Answers

  • In this case, you can write a custom web request plug-in. In the PreRequest method of the plug-in, computer userID+Timestamp in the same way as Javascript does, then update the post parameter. For  example:

     

    using System;  
    using System.Collections.Generic;  
    using Microsoft.VisualStudio.TestTools.WebTesting;  
     
    namespace RequestPluginNamespace  
    {  
        public class MyWebRequestPlugin : WebTestRequestPlugin  
        {  
            public string FormParameterName  
            {  
                get;  
                set;  
            }  
     
            public override void PreRequest(object sender, PreRequestEventArgs e)  
            {  
                FormPostHttpBody body = e.Request.Body as FormPostHttpBody;  
                if (body != null)  
                {  
                    string newValue = string.Empty;  
                    // Computer newValue.  
     
                    for(int i=0;i<body.FormPostParameters.Count;i++)  
                    {  
                        if(body.FormPostParameters[i].Name==FormParameterName)  
                        {  
                            body.FormPostParameters[i].Value = newValue;  
                            break;  
                        }  
                    }  
                }  
            }  
        }  

    Please mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by Bill.Wang Friday, March 27, 2009 9:44 AM
    • Marked as answer by Bill.Wang Monday, March 30, 2009 6:27 AM
    Thursday, March 26, 2009 9:20 AM

All replies

  • Hi vv
     

    Please take a look at Web tests work at the HTTP layer. Web test records communition between a browser and web server. During a web test, Javascript contained in the web page is not executed.

     

    Sometimes we pass a parameter to a javascript function to compute a value, then the parameter is thrown away and the value is posted to the web server. When we record a web test, the value should be recorded as part of the web request. Visual Studio use  the recorded web requests to drive the web server.

     

    If you are in a different situation, could you let us know why you want to invoke a javascript function in the web test? We might be able to find a better solution.


    Please mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, March 25, 2009 7:44 AM
  • Hi Bill,

    The value is recorded in the webtest . It is sent in one of the requests as a form post parameter.
    But the authentication is failing when the same value is passed to different users.( That value is generated by a java script file in the application using userID+Timestamp).


    Wednesday, March 25, 2009 2:44 PM
  • In this case, you can write a custom web request plug-in. In the PreRequest method of the plug-in, computer userID+Timestamp in the same way as Javascript does, then update the post parameter. For  example:

     

    using System;  
    using System.Collections.Generic;  
    using Microsoft.VisualStudio.TestTools.WebTesting;  
     
    namespace RequestPluginNamespace  
    {  
        public class MyWebRequestPlugin : WebTestRequestPlugin  
        {  
            public string FormParameterName  
            {  
                get;  
                set;  
            }  
     
            public override void PreRequest(object sender, PreRequestEventArgs e)  
            {  
                FormPostHttpBody body = e.Request.Body as FormPostHttpBody;  
                if (body != null)  
                {  
                    string newValue = string.Empty;  
                    // Computer newValue.  
     
                    for(int i=0;i<body.FormPostParameters.Count;i++)  
                    {  
                        if(body.FormPostParameters[i].Name==FormParameterName)  
                        {  
                            body.FormPostParameters[i].Value = newValue;  
                            break;  
                        }  
                    }  
                }  
            }  
        }  

    Please mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by Bill.Wang Friday, March 27, 2009 9:44 AM
    • Marked as answer by Bill.Wang Monday, March 30, 2009 6:27 AM
    Thursday, March 26, 2009 9:20 AM