Sunday, March 11, 2012

Ajax request (webservice) and WebTest in Visual Studio 2005

Could you provide more details about the situation. specifically:

What is the signature of your method on the server

ok, I have a web service who check if a process is processing in a background thread.

The Web method :

[WebMethod(true)]

publicbool IsProcessing(string windowId,string uniqueId){ //some code}

I inject a javascript script in the page and it is in this script where you found the call to the web service.

=> MvcServices.IsProcessing(_windowId, _uniqueId, OnRequestComplete);

All this work very well when I hit the page and the ajax call give me the right result. The only problem I have is when I when to record in a web test. I want to test the page and how the backgournd processing is working with some users in a load test. So, the first thing I do is record a web test. But, the Web Test Recorder didn't record the ajax request. So, I hit the page using fiddler and save the result in a .webtest file. When I play it in Visual Studio, I have this error. It's seems like if the context parameter was not initialize... Do you think it miss something ?

I convert the .webtest file in a generate code file and it give me this :

WebTestRequest request1 = new WebTestRequest("http://localhost:21815/MvcWebTests/Default.aspx");
request1.FollowRedirects = false;
request1.QueryStringParameters.Add("__wid", windowId, false, false);
request1.QueryStringParameters.Add("__mvcid", uniqueId, false, false);
request1.QueryStringParameters.Add("FakeRequest", switchFakeRequest, false, false);
yield return request1;

WebTestRequest request2 = new WebTestRequest("http://localhost:21815/MvcWebTests/MvcServices.asmx/js");
request2.FollowRedirects = false;
yield return request2;

WebTestRequest request3 = new WebTestRequest("http://localhost:21815/MvcWebTests/MvcServices.asmx");
request3.FollowRedirects = true;
request3.Method = "POST";
request3.QueryStringParameters.Add("mn", "IsProcessing");
FormPostHttpBody request3Body = new FormPostHttpBody();
request3Body.FormPostParameters.Add("windowId", windowId, false);
request3Body.FormPostParameters.Add("uniqueId", uniqueId, false);
request3.Body = request3Body;
yield return request3;

Thank you for your help !


So, could you tell me how I can encode parameter in JSON encoding ? I think the web test namespace (Microsoft.VisualStudio.TestTools.WebTesting) was not build and not think to support this...

request2Body.FormPostParameters.Add("__wid",this.Context["$HIDDEN1.__wid"].ToString());

Thank you !


I'm not too familiar with the Web Test feature, but looking at the code, I see why it's not working: you are trying to pass the parameter using the a=1&b=2 syntax, which is not how Atlas works. Parameters in Atlas are passed JSON encoded.

You can run fiddler on a real (working) Atlas request to see what the request URL and body should look like. But generally, I don't recommend making any assumptions on this, as it is subject to change. It's best to always use the Atlas client libraries to make Atlas web service calls.

David


Actually, WebTestRequest has a Body property which lets you set arbirary body content. Just don't use FormPostParameters.

Atlas has some public serialization methods in the Microsoft.Web.Script.Serialization namespace.

No comments:

Post a Comment