views:

391

answers:

2

Hi,

I'm doing some automation work and can make my way around a site & post to HTML forms okay, but now I'm up against a new challenge, Ajax forms.

Since there's no source to read, I'm left wondering if it's possible to fill in an Ajax form progamatically, in C#. I'm currently using a non-visible axWebBrowser.

Thanks in advance for your help!

+1  A: 

Have you looked at using Selenium. AFAIK, you can write the test cases in C# and I know our testers have successfully used it before to UI Test a Ajax enabled ASP.NET site

http://seleniumhq.org/

Eoin Campbell
Thanks for the link.
Gregg Cleland
+5  A: 

Yes, but I recommend using a different approach to requesting/responding to the server pages including the regular pages, and the AJAX handler pages.

In c#, try using the WebRequest/WebResponse or the more specialized HttpWebRequest/HttpWebResponse classes.

Ajax is no more than a "fancy" name for a technology that allows Javascript to make HTTP requests to a server which usually implements some handlers that produce specialized, light-weight content for the Javascript caller (comonly encoded as JSON).

Therefore in order to simulate AJAX calls, all you have to do is inspect your target application (the web page that you want to "post" to) and see what format is used for the AJAX communications - then replicate the page's Javascript behavior from C# using the WebREquest/WebResponse classes.

See Firebug - a great tool that allows you to inspect a web page to determine what calls it makes, to which pages and what those pages respond. It does a pretty good job at inspecting AJAX calls too.

Here's a very simple example of how to do a web request:

HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create("http://www.mysite.com");

using (HttpWebResponse resp = (HttpWebResponse)wReq.GetResponse())
{
   // NOTE: A better approach would be to use the encoding returned by the server in
   // the Response headers (I'm using UTF 8 for brevity)
   using (StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
   {
       string content = sr.ReadToEnd();
       // Do something with the content
   }
}

A POST is also a request, but with a different method. See this page for an example of how to do a very simple post.

EDIT - Details on Inspecting the page behavior with Firebug

What I mean by inspecting the page you're trying to replicate is to use a tool (I use Firebug - on Firefox) to determine the flow of information between the page and the server.

With Firebug, you can do this by using the "Net" and "Console" panels. The Net panel lists all requests executed by the browser while loading the page. While the "Console" will list communications between the page and the server that take place after the page has loaded. Those communications that take place after the page has loaded are essentially the AJAX calls that you'll want to replicate (Note: Network monitoring has to be enbled in Firebug for this to work)

Check out Michael Sync's tutorial to learn more about Firebug and experiment with the Console panel to learn more about the AJAX requests.

Regarding "replicate the page's behavior from C# using the WebRequest/WebResponse" - what you have to realize is that like I said earlier, the Javascript AJAX call is nothing more than an HTTP Request. It's an HTTP Request that the Javacript makes "behind the scenes", or out-of-band, to the web server. To replicate this, it is really no different than replicating a normal GET or a normal POST like I showed above. And this is where Firebug comes in to play. Using it you can view the requests, as the Javascript makes them - look at the Console panel, and see what the Request message looks like.

Then you can use the same technique as above, using the HttpWebRequest/HttpWebResponse to make the same type of request as the Javascript does, only do it from C# instead.

Gregg, I hope this clarifies my answer a little bit but beyond this I suggest playing with Firebug and maybe learning more about how the HTTP protocol works and how AJAX works as a technology.

Miky Dinescu
Take a look at Wireshark to see what's going on on the wire, down to packet level. It has excellent filtering/searching tools.
tomfanning
It's great to hear that it's possible and thanks for the sample HttpWebRequest code. If you have time, could you provide example code (or link) for:1) "inspect your target application (the web page that you want to "post" to) and see what format is used for the AJAX communications"and 2) "replicate the page's Javascript behavior from C# using the WebRequest/WebResponse classes"
Gregg Cleland
I added some more info to the answer. I hope it helps!
Miky Dinescu
Miky D, Thanks very much for the extra info. It's nudged me in the right direction.
Gregg Cleland