locked
Unable to cast object of type 'System.String' to type 'System.String[]'. -- Extract Hidden Fields

    Question

  •  

    I am getting this error with VS2008.  I am working with the following, if that sheds any light on the situation:

    1. Insert Call to Web Test
    2. ScriptableParameterPlugIn (UKVSTS.WebTestPlugins.2008)

    I have the same script working without the "Insert Call to Web Test" (Script2)...  I have other scripts that work with "Insert Call to Web Test"...  I checked the execution results of the Script2, and the source of the page returned, there are multiple hidden fields in the response.

     

    Here is the stack trace from the error:

     

    System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.String[]'.
       at Microsoft.VisualStudio.TestTools.WebTesting.Rules.ExtractHiddenFields.Extract(Object sender, ExtractionEventArgs e)
       at Microsoft.VisualStudio.TestTools.WebStress.WebTestInstrumentedTransaction.ExecuteExtractionRuleReferences(ExtractionRuleReferenceCollection extractionRuleReferences, Boolean rulesAreCorrelationRules)

     

    Any thoughts or suggestions on how I should proceed?  I am using the tool for some functional testing & test bed creation.  I would love to get this functionality working, "Insert Call to Web Test" is one of the best features of VS2008 in my opinion.  Thanks in advance for your suggestions!

    Friday, March 14, 2008 3:25 PM

Answers

  • Hello Lewis,

     

    Thanks for your repro steps. They worked for us and we figured out where the problem is.

     

    The issue is not within our code but in the ScriptableParameterPlugin. In a method PreWebTest() they iterate through all the parameters in the WebTest.Context in search for the scriptable parameters and accidentally format all the values, including the ones of the hidden fields, to strings.

     

    This caused the problem of InvalidCastException when we cast the hidden field of String[] that was formatted to be of string type instead.

     

    Here is their code:

     

    foreach (String key in keys)

    {

        e.WebTest.Context[key] = ModifyIfScriptPresent(key, e.WebTest.Context[key].ToString(), null);

    }

     

    What they should be doing is:

     

    foreach (String key in keys)

    {

        string value = e.WebTest.Context[key] as string;

        if (value != null)

        {

            e.WebTest.Context[key] = ModifyIfScriptPresent(key, value, null);

        }

    }

     

    Jason: I hope this would solve your problem. Let me know if there is anything else I can do.

     

    Thanks.

    Monday, April 28, 2008 8:35 PM

All replies

  • Hello,

     

    Can you send us your webtest plus as much information as you can (the whole test project could be good) so we can take a look and see if we can repro the error?

     

    Thanks.

     

    Wednesday, March 19, 2008 5:37 PM
  • Sorry I forgot to include my email from the last post: duatle@microsoft.com

     

     

     

     

    Sunday, March 30, 2008 6:49 PM
  • I too am having this problem when using Insert Call to Web Test and the ScriptableParameterPlugIn (if I use the plugin in the original webtest, that is, without the Insert Call to Web Test, I do not see this error). I am hoping that Neil is able to track this down and post an updated version of the plugin.

    Sunday, March 30, 2008 10:56 PM
  • my C# is rusty.. but isn't that error basically indicating that the system is attempting to cast a string to a string-array?

    Monday, March 31, 2008 5:46 AM
  • Hello Jason,

     

    Haven't heard back from you on this problem, but we tried and could make the exception happen. However, it's not the exact repro, and in our case we think it is really a minor issue.

     

    Please do send us your code if you still need us to investigate what happens and give suggestions.

     

    Thanks.

     

     

     

    Wednesday, April 23, 2008 2:36 PM
  •  

    Hey,

     

    Sorry about that.  Is there any way I can send to you privately?  I am running these tests against a pre-production code base.  Legal might have a problem with me posting to a public forum.

     

    Thanks,

     

    jason

    Wednesday, April 23, 2008 2:40 PM
  • Can you send it to my MS email: duatle@microsoft.com?

     

    Thanks

     

    Thursday, April 24, 2008 11:18 PM
  • Just to pipe in, I am able to reproduce the error by performing the following:

     

    Create a new webtest.

    Navigate to www.microsoft.com

    Search for today's date (ex: 24 Mar 2008)

    Navigate to www.msn.com

    Search for today's date (ex: 24 Mar 2008)

    Stop recording

     

    Playback the webtest. It will probably fail but not with the "Unable to cast object..." error.

     

    Add the UKVSTS ScriptableParameterPlugin (http://www.codeplex.com/UKVSTS/Wiki/View.aspx?title=UKVSTS%20Web%20Test%20Plug%20In&referringTitle=Home)

     

    Change the QueryString Parameters for both date searches to

    <%= DateTime.Now.ToString ("dd+MMM+yyyy") %>

     

    Run the test. Again it will probably fail but not with the "Unable to cast object..." error.

     

    Now extract the requests for microsoft.com to another webtest with both "Extracted test inherits test level properties" and

    "Copy test level plug-ins and validation rules" checked.

     

    Do the same for the msn.com requests.

     

    Run the parent test.

     

    I am getting the "Request failed: Unable to cast object of type 'System.String' to type 'System.String[]'" error on the www.msn.com request. Hopefully you will too.

     

    Thanks.

    Saturday, April 26, 2008 12:06 AM
  • Hello Lewis,

     

    Thanks for your repro steps. They worked for us and we figured out where the problem is.

     

    The issue is not within our code but in the ScriptableParameterPlugin. In a method PreWebTest() they iterate through all the parameters in the WebTest.Context in search for the scriptable parameters and accidentally format all the values, including the ones of the hidden fields, to strings.

     

    This caused the problem of InvalidCastException when we cast the hidden field of String[] that was formatted to be of string type instead.

     

    Here is their code:

     

    foreach (String key in keys)

    {

        e.WebTest.Context[key] = ModifyIfScriptPresent(key, e.WebTest.Context[key].ToString(), null);

    }

     

    What they should be doing is:

     

    foreach (String key in keys)

    {

        string value = e.WebTest.Context[key] as string;

        if (value != null)

        {

            e.WebTest.Context[key] = ModifyIfScriptPresent(key, value, null);

        }

    }

     

    Jason: I hope this would solve your problem. Let me know if there is anything else I can do.

     

    Thanks.

    Monday, April 28, 2008 8:35 PM
  • All,

     

    This worked for me!  I give out many thanks to Lewis for keeping the thread going...  I was in a release (successful yesterday!!!).  I rebuilt today with the change and we are all good here.  Thanks, Duat!

     

    Wednesday, April 30, 2008 6:12 PM
  • Duat:

     

    I downloaded Change Set 14357 from the UKVSTS site, incorporated your suggested changes, compiled and installed the DLL. Although it fixes the initial error of Unable to cast object of type 'System.String' to type 'System.String[]', I am getting the following error for a scripted Context Parameter at the start of a test. The error is:

     

    Request failed: Exception in PreWebTest event: Script execution error "Object reference not set to an instance of an object."

    at UKVSTS.WebTestPlugins.ScriptedParamDetails.InvokeScript(PreRequestEventArgs e) in C:\UKVSTS-14357\UKVSTS\WebTestPlugins\UKVSTS.WebTestPlugins\ScriptedParamDetails.cs:line 319
    at UKVSTS.WebTestPlugins.ScriptableParameterPlugIn.ModifyIfScriptPresent(String name, String value, PreRequestEventArgs e) in C:\UKVSTS-14357\UKVSTS\WebTestPlugins\UKVSTS.WebTestPlugins\ScriptableParameterPlugIn.cs:line 261
    at UKVSTS.WebTestPlugins.ScriptableParameterPlugIn.PreWebTest(Object sender, PreWebTestEventArgs e) in C:\UKVSTS-14357\UKVSTS\WebTestPlugins\UKVSTS.WebTestPlugins\ScriptableParameterPlugIn.cs:line 56
    at Microsoft.VisualStudio.TestTools.WebTesting.WebTest.OnPreWebTest(PreWebTestEventArgs e)
    at Microsoft.VisualStudio.TestTools.WebTesting.DeclarativeWebTest.OnPreWebTest(PreWebTestEventArgs e)
    at Microsoft.VisualStudio.TestTools.WebTesting.WebTest.InvokePreWebTest()
    at Microsoft.VisualStudio.TestTools.WebStress.WebTestCaseVariation.Start()

     

    I am hoping that you can shed some light on this. Thanks.

    Sunday, May 04, 2008 11:52 AM
  • Neil just posted a build with the fix on the UKVSTS Codeplex site. He also added an enhancement to allow scripted values to be used as a filename in a file upload param (Thanks Neil).

     

    I have a number of different PDF files of various sizes that I've renamed 1.pdf through 10.pdf and added to the project. Using the ScriptedParameterPlugin, the test is randomly selecting a file to upload with the following:

     

    <%= String.Concat(Math.Round((decimal)(DateTime.Now.Ticks % 10) + 1).ToString(), ".pdf") %>

    Saturday, May 17, 2008 1:13 AM