18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

problem with catch block execution

bosshr1043bosshr1043 Members
edited November -1 in Sahi - Open Source
My code snippet looks as follows:

function searchForAirFlightFare($departure,$arrival,$departDate)
{
_stopOnError();
try
{
_setValue(_textbox("DlgAirSearchEntriesForm:tblOriginDestination:0:txtDepartLocationCode"), $departure);

_setValue(_textbox("DlgAirSearchEntriesForm:tblOriginDestination:0:txtArrivalLocationCode"), $arrival);

// _setValue(_textbox("DlgAirSearchEntriesForm:tblOriginDestination:0:txtDepartDate"), $departDate);

_click(_submit("Fare Search"));

} catch (e)
{
_logException(e);
_click(_submit("Clear Search"));

}
_continueOnError();

}
searchForAirFlightFare("HYD", "FRA", "08/31/2009");


I have 3 mandatory fileds----as you see in the code the 3rd _setValue(...) is commented , so in this case when "Fare Search" button is clicked ,application throws an exception for missing mandatory filed.So I have written a Catch block for that.

But when I execute this script in Sahi Controller,catch block is not executed .It clicks Fare Search and stops the script with success.

Hope you help me doing this Try-Catch.

Comments

  • narayannarayan Administrators
    bosshr1043,

    Exceptions thrown by an application's logic will not be caught by Sahi's try catch.

    Sahi's try catch will kick in if the step _click(_submit("Fare Search")); actually threw an exception, which would happen if there was no submit button called "Fare Search" and we were trying to click it.

    In your case, you should just do something like this:
    function searchForAirFlightFare($departure,$arrival,$departDate)
    {
       try 
        {
           _setValue(_textbox("DlgAirSearchEntriesForm:tblOriginDestination:0:txtDepartLocationCode"),  $departure);
        
           _setValue(_textbox("DlgAirSearchEntriesForm:tblOriginDestination:0:txtArrivalLocationCode"), $arrival);
        
    //    _setValue(_textbox("DlgAirSearchEntriesForm:tblOriginDestination:0:txtDepartDate"), $departDate);
           _click(_submit("Fare Search"));
           
           _assert(_isVisible(_div("error_div"))); // Check the error message is visible
           _assertEqual("Error message", _getText(_div("error_div"))); // Check the error message is the one you expect.
    
    //    if you want to clear search only if an error message is visible, you need to use an if condition
    
          if (_condition(_isVisible(_div("error_div")))){
                _click(_submit("Clear Search"));
          }
           
         }
    }
    searchForAirFlightFare("HYD", "FRA", "08/31/2009");
    
  • But as per your solution given in the following forum location:

    http://sahi.co.in/forums/viewtopic.php?id=1004

    which explains that if there is some exception is thrown in "try" block it should catch it and execute "catch" block.

    How far your forum solution is applicable for my code?

    So, can you please listout under which conditions or situations Sahi is able to catch Exception and execute the catch block????

    URGENT Please.
  • narayannarayan Administrators
    Hi bosshr1043,

    As far as Sahi is concerned, an error message shown by an application is valid behaviour. It does not qualify as a runtime exception of Javascript. You will easily be able to find valid cases of javascript exceptions on the net (Sorry, but I am unable to explain that well enough, so I am pointing you to the internet.)

    Regards,
    Narayan
Sign In or Register to comment.