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.

if statemenets that check for null or undefined are failing

handytomhandytom Members
edited November 2009 in Sahi - Open Source
Hi

All tests were run in Firefox 3.0.

Basically I only want to do some verification checks on a checkbox if and only if it exist in the DOM

This works for me in SAHI verison 20080305
$client = {
   'test':'novalue',
};

if(_checkbox('clip')!==null){
  if($client.clip!==undefined){
    _assertTrue(_checkbox('clip').checked);
   }else{
    _assertFalse(_checkbox('clip').checked);
   }    
}


However in SAHI version 20090521 I get this error:
_assertFalse(_checkbox('clip').checked);
TypeError: _sahi._checkbox("clip") is null
()@http://10.0.100.201/_s_/spr/concat.js:2548
()@http://10.0.100.201/_s_/spr/concat.js:2548
@http://10.0.100.201/_s_/spr/concat.js:2429
Click for browser script [object Object] at Nov 3, 2009 3:25:41 PM
So it looks like the null check is getting skipped.

When I test my code in the record window I do not get an error just a return value of
[object Object]
If I reverse the !==null to a ===null then I get the error so it seems to work there just not in my scripts.

And in SAHI version 20091023 I get this error:
_assertFalse(_checkbox('clip').checked);
TypeError: _sahi._checkbox("clip") is undefined
()@http://10.0.100.201/_s_/spr/concat.js:2465
()@http://10.0.100.201/_s_/spr/concat.js:2465
@http://10.0.100.201/_s_/spr/concat.js:2307
Click for browser script at Nov 3, 2009 3:32:46 PM
So it has not changed from null to undefined warning.

When I update my code to check for undefined it refuses to work.
if(typeof(document.getElementsByName('clip')[0])!== 'undefined'){
  if($client.clip!==undefined){
    _assertTrue(_checkbox('clip').checked);
   }else{
    _assertFalse(_checkbox('clip').checked);
   }    
}

Any ideas on what is going wrong here and how I can fix it? Would really love to upgrade from my 2008 version.

Cheers
Tom

Comments

  • Hi,

    When I ran the below code by setting http://sahi.co.in/demo/formTest.htm as start url, it is working fine in 20091023 release.

    $client = {
    'test':'novalue',
    };

    if(_checkbox('c1')!==null){
    if($client.c1!==undefined){
    _assertTrue(_checkbox('c1').checked);
    }else{
    _assertFalse(_checkbox('c1').checked);
    }
    }

    Please try it, by checking and unchecking the checkbox c1 over there.

    Let us know, if you still have any probs

    Thanks,
    Venky
  • Hi Venky,

    Thanks for the super quick response.

    You code does work how ever your scenario does not reproduce my problem. I only want to run checks on the checkbox if it exists. A checkbox named 'c1' does exist on that page better to try this code.
    $client = {
       'test':'novalue',
    };
    
    if(_checkbox('c')!==null){
      if($client.c!==undefined){
        _assertTrue(_checkbox('c').checked);
       }else{
        _assertFalse(_checkbox('c').checked);
       }
    }
    

    In 20080305 both my _asserTrue an d_assertFalse check are correctly skipped as checkbox[c] does not exist which gives this output in the logfile
    Starting script
    Stopping script
    

    however in 20091023 _asserFalse is tested and fails because checkbox[c] does not exist which give this output in the logfile
    Starting script
    _assertFalse(_checkbox('c').checked);
    TypeError: _sahi._checkbox("c") is undefined
    ()@http://sahi.co.in/_s_/spr/concat.js:2465
    ()@http://sahi.co.in/_s_/spr/concat.js:2465
    @http://sahi.co.in/_s_/spr/concat.js:2307
    
    Click for browser script at Nov 4, 2009 1:56:01 PM
    Stopping script
    

    I only want to do my _asserts checks if the element exists on the page. How can I replicate the 20080305 result in the 20091023 version?

    Thanks
    Tom
  • I also tried to incorporate _assertExists into my if statement but still cannot get it to work as I hoped.
    $client = {
       'test':'novalue',
    };
    if(_assertExists(_checkbox('c'))){
      if($client.c!==undefined){
        _assertTrue(_checkbox('c').checked);
       }else{
        _assertFalse(_checkbox('c').checked);
       }
    }
    

    In the RECORD window using the TEST button this returns [Assertion Failed]
    while in the Playback window the scripts throw the i snot defined error same as before
  • dpdp Members
    edited November 2009
    Hi Tom,

    The following code works for me using Sahi 20090902 and for all practical reasons should work on your version too.

    I have used the API _condition within the if condition. I have changed "!==" to "!=". This code will work from within a script but will throw an error if you try it on the controller.
    $client = {
       'test':'novalue',
    };
    
    if(_condition(_checkbox('c')!=null)){
      if($client.c!==undefined){
        _assertTrue(_checkbox('c').checked);
       }else{
        _assertFalse(_checkbox('c').checked);
       }
    }
    


    Please also note that _assert*** APIs cannot be used in a conditional statement as you tried in your last example. Asserts are used to assert something and if it fails, throws an exception.

    Hope this helps

    Cheers
    dp
  • Hey dp,

    Just to let ya know that your solution works brilliantly.

    Thanks a million for your help and that applies to Venky aswell.

    Delighted to be able to use the latest and greatest version of SAHI now.

    Cheers guys
    Tom
Sign In or Register to comment.