Thursday, April 18, 2013

How to book IRCTC tatkal tickets using Selenium IDE?

       It is observed that the IRCTC server becomes very busy in the morning at 10am due to many people across India trying to book Tatkal tickets.

The major challenges we face while booking Tatkal tickets are:
  • We cannot login to the site first.
  • If we are successful logging in, then server response slows down.
  • If we select a train, it’s hard to get the passenger information page.
  • Payment never goes through.
  • Finally, session expires many times.
In fact, Selenium IDE is no special. It won't communicate with the server via any special channel but it overcomes almost all problems mentioned above to a good extent by just speeding up the manual work.

How?


We will write a IRCTC suite with 3 to 5 test cases with the help of IDE's record option and play them back whenever we want to book tickets and also whenever your session expired in the meantime.
You need not type-in the username and password again and again, IDE does that work for you.
You need not type-in the From, To fields and need not select date, quota etc,. again and again, IDE does that work for you.
You need not type-in the Fill the passenger information and Billing Details again and again, IDE does that work for you.

Steps:



I give below 2 sets of explanation. One for techies who wants to do it from the scratch like record the steps and playback them later and another set for Laymen who just want to play back directly by using the selenese commands/code I give.

Make sure you have Firefox and the add-on Selenium IDE open and ready. (See how to add/install Selenium IDE to Firefox)

For Techies and Testers.


1. Create a new test suite and a test case. Name the suite as  "IRCTC" and test case as "IrctcLogin"
Now, press the record button and open the URL in firefox browser
"https://www.irctc.co.in/". Type username and password and click "Login".

The selenese code looks like this:


 2. Add another test case as "Select Train", keep the record button pressed type From, To, select date, Quota and click on SUBMIT.

The selenese code looks like this:





3. Now turn off the record button and choose the train and class and click on book. We are not adding any test case for this action and it is highly variable and people have their own choice of looking at and selecting train and class.


4. Add another test case as "Fill Tatkal Details", keep the record button pressed type Passenger details like Name, Age, Sex, berth preference, IDCard type and Number and click the checkbox "Upgrate ...." and provide mobile number.

The selenese code looks like this: 




5. Recheck the passenger list and precede to payment. We can also add a test case for the payment method but considering the different types of payment methods such as "Credit Card", "Debit Card" etc and the sensitivity of the billing information I would not recommend this.


Once you have the code and commands handy, be ready at 9:58am online and login to IRCTC.
Use and RUN the  above test cases one by one by selecting the test case and pressing RUN button. Don't run all the test cases at once, you may end up failing at any command.

For Laymen and Normal users (Simple method)


1. Create a new test suite and a test case. Name the suite as "IRCTC" and test case as "IrctcLogin".

Copy below code:
 -----------------------------------------------------
<tr>
                <td>open</td>
                <td>https://www.irctc.co.in/</td>
                <td></td>
</tr>
<tr>
                <td>type</td>
                <td>name=userName</td>
                <td>McKiran</td>
</tr>
<tr>
                <td>type</td>
                <td>name=password</td>
                <td>***********</td>
</tr>
<tr>
                <td>clickAndWait</td>
                <td>id=button</td>
                <td></td>
</tr>
  -----------------------------------------------------

Now, Go to source sections and locate the html tag sequence like below:

---------------------------------
</thead><tbody>

</tbody></table>
---------------------------------

Now paste the code between the tags <tbody> and </tbody>.

Note: paste the code like this for all other test cases followed below. And
comeback to Table view and edit the Username and Password.


2. Add another test case as "Select Train".
Copy paste the below code:
  -----------------------------------------------------
<tr>
                <td>type</td>
                <td>name=stationFrom</td>
                <td>sc</td>
</tr>
<tr>
                <td>type</td>
                <td>name=stationTo</td>
                <td>ngp</td>
</tr>
<tr>
                <td>click</td>
                <td>id=calendar_icon1</td>
                <td></td>
</tr>
<tr>
                <td>click</td>
                <td>link=21</td>
                <td></td>
</tr>
<tr>
                <td>select</td>
                <td>name=quota</td>
                <td>label=Tatkal</td>
</tr>
<tr>
                <td>clickAndWait</td>
                <td>name=Submit</td>
                <td></td>
</tr>
  -----------------------------------------------------
Note: Goto Table view and edit the From, To, Date.


3. Now choose the train and class and click on the link ‘book’.


4. Add another test case as "Fill Tatkal Details",

Copy paste the below code:
  -----------------------------------------------------
<tr>
                <td>type</td>
                <td>name=passengers[0].passengerName</td>
                <td>Barfi Sharma</td>
</tr>
<tr>
                <td>type</td>
                <td>name=passengers[0].passengerAge</td>
                <td>28</td>
</tr>
<tr>
                <td>select</td>
                <td>name=passengers[0].passengerSex</td>
                <td>label=Male</td>
</tr>
<tr>
                <td>select</td>
                <td>name=passengers[0].berthPreffer</td>
                <td>label=Lower</td>
</tr>
<tr>
                <td>select</td>
                <td>name=passengers[0].idCardType</td>
                <td>label=Pan Card</td>
</tr>
<tr>
                <td>type</td>
                <td>name=passengers[0].idCardNo</td>
                <td>ABCD09152P</td>
</tr>
<tr>
                <td>type</td>
                <td>name=passengers[1].passengerName</td>
                <td>Priyanka</td>
</tr>
<tr>
                <td>type</td>
                <td>name=passengers[1].passengerAge</td>
                <td>25</td>
</tr>
<tr>
                <td>select</td>
                <td>name=passengers[1].passengerSex</td>
                <td>label=Female</td>
</tr>
<tr>
                <td>select</td>
                <td>name=passengers[1].berthPreffer</td>
                <td>label=Lower</td>
</tr>
  -----------------------------------------------------
Note: Go to Table view and edit the passenger details such as Name, Age, Sex, Berth, IDCard etc. I mentioned 2 passengers above with IDCard details of one passenger.


5. Recheck the passenger list and precede to payment.


Recheck and edit the above 3 test cases and be ready at 9:58am online and login to IRCTC.
Use and RUN the  above test cases one by one by selecting the test case and pressing RUN button. Don't run all the test cases at once, you may end up failing at any command.
Whenever you get session expired or you end up waiting for infinite time simply select the first test case and click on RUN then you will get a new session in seconds and continue running next test cases.

P.S: By looking at the above theory, you may feel that its a long procedure but if you setup the above code prior to booking the tickets and be ready at 10am to book tatkat then it is very much useful in getting confirmed tickets.
User comments welcome.

18 comments:

  1. Thanks Kiran. it's veru useful info. Keep posting ...........

    ReplyDelete
  2. is the money transaction safe through this?

    ReplyDelete
    Replies
    1. I can not guarantee it as safe, I just gave it as a demo. But, if you use your own PC (no others have access to it) and wants to use this method then it is OK to use provided u delete the info immediately after the transaction.

      Delete
  3. It can not avoid crashing.seems to be useless

    ReplyDelete
    Replies
    1. If you expect an ideal solution for crashing then it should come from IRCTC only. My posts just save your time in typing the same information repeatedly.

      Delete
  4. Hi chandra ,

    This method seems fine.
    But what if some page loading fails .
    say after the step 2 . because at the time of tatkal it fails most of the time and we cannot predict the time it takes for the load. so do you suggest clickand wait option.

    can we have some verifications like checking some texts to confirm whether the page is loaded properly.

    ReplyDelete
    Replies
    1. Yes, wherever a page load is involved it is advised to use 'clickAndWait'. We have used at at our first 2 test cases and we will manually select the trains and fill the passenger information by running 3rd test case. If a page load fails after sometime we have no other option than running the test case again.

      And yes, adding verifications is a good approach to confirm if the page loaded or not.

      Delete
  5. can u provide the exact code for booking tatkal tickets for a particular train no in a particular class

    ReplyDelete
    Replies
    1. Ok, I posted one more article on what you requested. See it here : http://www.seleniumhq.in/2013/10/how-to-book-irctc-tatkal-tickets-using.html

      Delete
  6. Hi Chandra,

    It's a nice work around but i have a doubt how you are going to handle the capcha...

    ReplyDelete
    Replies
    1. I am not handling Capcha using selenium, I never explored it and I am busy these days. We have many tutorials and articles online, one can search in Google, read and implement.

      Delete
  7. hi kiran,

    Good job!!! really awesome ...

    How to save all test case in one html file , every time i going " file " and open browse each test case selecting one by one and running the file , is there any way. pl help me

    ReplyDelete
    Replies
    1. You can actually get all this info at selenium site in their documentation.
      Ok,, here is the answer. You can save all your test cases in a test suite. Once you compile your test cases you can go to File -> Save Test Suite. This give give you a save dialog that prompts you to save your test cases first and test suite at the end. Do select the file type as "HTML Files" (optional).

      So next time you open the IDE, simply click File-> Open Test Suite and select your suite name which automatically loads all test cases associated with it.

      Delete
  8. This is useful !! Is there any other tool which serves same purpose ??

    ReplyDelete
    Replies
    1. We have WebDriver, QTP, UFT, Test Complete etc. Go and explore ma friend........

      Delete
  9. Hi Kiran,

    You have done a very good job!.....But I think it won't work successfully during tatkal time. Please check the below why I'm saying like this.

    1) IRCTC may have performance issue beacause even with high speed internet, Hit the site at peak hours You will get to know that server response time is very slow down..page taking lot of time to complete load.
    2) Captcha is not easy to implement. For this it require image recognization convert to text format.(just think about gmail account creation, it will ask for Captcha to stop automatic creation from Bots). Selenium won't support this and require third party tools.
    3)Some Ajax elements like drop down values are also there in IRCTC pages. These could be handle using selenium with some technical skills but during the tatkal time, Script not possible to proceed the flow.
    4) Once session fails, not possible to get it back(active).

    My suggestion is better to depend on our manual process than automatic script.
    But during the normal time of IRCTC, This article will be helpful to speed up the manual work.

    Thanks,
    Prabhat.B

    ReplyDelete
    Replies
    1. Hey Prabhat,
      Thats really a detailed info on the issues we face during tatkal time. As you said this post is helpful to speed up the manual work. But a correction; its useful not just at normal time but also at tatkal time.
      Because, this tool doesn't care when it is being done, it just cares where it is. It just fills the text boxes, selects the options and re-logs you in immly (if session expires) etc thus saves your time in filling info manually no matter if its a normal booking or tatkal.

      Delete
  10. how to irctc form using selenium webdriver.

    ReplyDelete