Sunday, July 31, 2011

MVC Multiple Post buttons in one page

Anyone who has worked in MVC knows how clicking a post button will submit the page, and invoke the action method. It can get a bit annoying if you want to do different things based on different buttons being clicked. However, thanks to html there is a way around it.


To do this, we have to figure out which button was clicked. This can be done in the following way:

Make two buttons with the same “name” but different “value”
e.g

<input type="submit" id="GetResults" name="SubmitButton" value="Apply" />
<input type="submit" id="UpdateResults" name="SubmitButton" value="Inform" />

In the controller action, add the following input parameter (string SubmitButton)
Inside the controller action, the value of the variable “SubmitButton” will either be “Apply” or “Inform” depending upon which submit button was clicked.

After this, a simple “if” condition such as the one given below will do the job:

If(SubmitButton == "Apply")
{
                Do this
}
Else if (SubmitButton == "Inform")
{
                Do that
}

Saturday, July 30, 2011

Typical problems with WatiN

In my last post I discussed about creating a Hello World application in WatiN. In this post I will focus on typical problems that can entail the usage of WatiN.

WatiN Not Executing

If your application gives a strange error message and does not execute, go to references and see if you have the reference "Interop.SHDocVw" added. If it is added, then it is no co-incidence that your code is not running, delete it and try again, it should work now.

If you are using a 64-bit machine with .NET Framework 4, you have to change the properties of your project in order to make your application work. As discussed in this blog (from step 7 onwards), you need to go to the properties of your project (by right clicking on your project in solution explorer) and under the text "target framework", change the value of dropdown to ".NET Framework 4". Recompile and it should work now.

Working with Firefox instead of Internet Explorer

Just like you use the code "IE browser = new IE()", you could also use "Firefox browser = new Firefox()" and start working with Firefox rather than Internet Explorer. However, this will come with its own share of problems.

To the best of my knowledge, to date WatiN is incompatible with Firefox 4, so you need to revert to Firefox 3.6 (or any other version of Firefox 3) in order to let WatiN work successfully with Firefox.

Sometimes the problems do not end here, and you get a JSSH error. There is nothing alarming about this error, and if you get it, all you need to do is download JSSH Firefox extension, which could also be downloaded manually from here, and your problem will end.

If your problems do not end here, feel free to inform me of your problems in the comment box below.

Advice for WatiN-Firefox users

As an alternate to Internet Explorer's development tools, Firefox has an add-on called Firebug, which is very easy to use and extremely powerful. While using it you just need to right click on any part of the webpage and click on Firebug to get all the information you need.

Test application for a web project

Once we have a fully functional website it is very common to add features to it without testing it thoroughly enough and getting on with the release. This can leave some bugs unnoticed which could prove to be embarrassing for a developer. However with applications that perform automated testing for you, you can sit back and relax while your application does the testing and gives you the results.
Thanks to WatiN it is not only possible but very easy as well. For an introduction to WatiN you could go to WatiN Home and learn more about it.

Anyways, to create a Hello World application in WatiN you need to create a new console application in .NET.
In that application right click on References (which can be found in the Solution Explorer), and click add Library package reference, over there you can search for WatiN and download it. If you do not have the option of adding a library package reference, you can download it manually from here and add it to your project.
Once this is done, you can use the following code to create a basic application that will go to Google, search for "Hello World" and click the search button to get results.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WatiN.Core;


namespace WatiNTest
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            IE browser = new IE("http://www.google.com");
            browser.TextField(Find.ByName("q")).TypeText("Hello World");
            browser.Button(Find.ByName("btnG")).Click();
        }           
    }
}

In this code, there is a keyword [STAThread] on top of void main, this is essential for our application to successfully communicate with Internet Explorer.
In the first line of code, I created a new instance of Internet Explorer (browser), and opened google in it.
In the second line of code, I used WatiN's built in function to find the text box named "q", and type "Hello World" in it. The question here is how I found out the name of google's search textbox. Simple, if you open google on Internet Explorer and press F12, you will open Internet Explorer's development tools. The page should now look like this:

 


In developer tools, there is an icon under the tab "HTML" that looks like a cursor of a mouse. If you click on it, and then click on the google search textbox, it will give you a lot of information about that textbox like this:

 

Notice here that the information about that textbox is highlighted in blue. If you go through this, you can see that the name of this textbox is "q".

Once you have done this, you can now insert anything in the search box, which in the example given above is "Hello World". After that if you follow the same technique, you can find the name of the button "google search" and click it (as done in the example).
 
There you go, your hello world testing application is now complete. You can add more things to it, such as after the code you can add a line of code that clicks the button "next page" and shows page 2 of results. (WatiN has been designed in a way that if you click "google search" and in the next line you write the code to click "results page 2" button, it will click "google search" wait for the page to load and once it is loaded click on page 2 button).

I hope it works without any problems, however, you do not need to worry if you face problems doing it. My next post will focus more on them.