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.

Checking if a variable is defined within a script

AbePetrilloAbePetrillo Members
edited January 2012 in Sahi - Open Source
Hi there,

I've tried many variations on this but with no success.

Checking if variable doesn't exist (works):
  if(!$myVar)
  {
    //do stuff, works!
  }

Checking if variable exists (does not work):
  if($myVar)
  {
    //do stuff, does not run this code at all
  }
  if(typeof $myVar === "string")
  {
    //do stuff, doesnt work with variables set to a string
  }
  if((typeof $myVar === "string")==true)
  {
    //do stuff, still doesn't work
  }

Given
$myVar = {street_one: "My street"}
  if($myVar.hasOwnProperty('street_one'))
  {
    //do stuff, still doesn't work
  }

Comments

  • Hi AbePetrillo,

    Here is a sample code:
    var $text = _getText(_link("SSL Manager"));
    _alert($text);
    if(typeof $text != "undefined")
      {
       _click(_link("SSL Manager"));
      }
    

    Check if this solves your issue.

    Regards
    Sumitra
  • AbePetrilloAbePetrillo Members
    edited January 2012
    This doesn't seem to work either...

    Actual lines:
    _log($ee.street_one, "success");
     _log((typeof $ee.street_one), "success");
     if(typeof $ee.street_one != "undefined"){
      _setValue(_textbox("person[addresses][0][street_one]"), $ee.street_one); 
    }
    

    skips the if statement.
    log:
    my street at 18-Jan-2012 12:31:28
    _log("my street", "success"); at 18-Jan-2012 12:31:28
    string at 18-Jan-2012 12:31:28
    _log((typeof "my street"), "success"); at 18-Jan-2012 12:31:28
    
  • AbePetrilloAbePetrillo Members
    edited January 2012
    This may be to do with comparing strings. Similiar problem here:

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

    EDIT:

    Found out what the problem is, I have to put the code to run on a new line otherwise it is skipped, as follows:

    This fails:
    if(1==1){_alert("success")}
    

    This works:
    if(1==1){
      _alert("success");
    }
    
Sign In or Register to comment.