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.

Full path name of Current Executing Script

DonnaMooresDonnaMoores Members
edited November -1 in Sahi - Open Source
Is there any way to get hold of the full path name of the current executing script?

Cheers
Donna
:)

Comments

  • lausserlausser Members
    edited April 2009
    Me too :)
    I am writing my testscripts like this
    var $testname = "app_web_naprax_internal_check_login";
    
    function process_result($test, $result) {
      var $stmt = 'insert into testresults (test, result, time) values(' +
          '\'' + $test + '\', ' + '\'' + $result + '\', sysdate())'       
      var db = _getDB("com.mysql.jdbc.Driver",
          "jdbc:mysql://localhost/sahi", "sahi", "***");
      db.update($stmt);
    }
    
    
    try {
      .....
      // do the checks
      process_result($testname, 'success');
    } catch(e) {
      process_result($testname, 'failure');
    }
    
    and it would be nice if the script would know it's own name (and maybe even the suite) automatically, so i don't need to hard-code it.

    Gerhard
  • narayannarayan Administrators
    You can already use _scriptName() to get the name.

    For the path, here is a small hack.
    function getScriptPath = function(){
        return ("" + ScriptRunner.getScript().getFilePath()). replace(/\\/g, "\\\\");
    }
    
    $path = getScriptPath();
    
    In the coming versions, an API _scriptPath will be added.
  • cheers

    Donna
  • lausserlausser Members
    edited April 2009
    Something strange happens here. The Script does not run with this function. I debugged now for several hours, but no solution...
    ScriptRunner.getScript().getFilePath().replace(/\\/g, "\\\\") does not work.
    var $longpath = ScriptRunner.getScript().getFilePath().toString();
    
    can be executed. typoeof($longpath) even says it it a string. Also $longpath.search(/\\/) says there's a backslash at position 2 (C:\..). I can _debug($longpath(0, 4)) and i see C:U (C:\Users..)
    But as soon as i do something else with this string, it fails. Especially the replace.
    Does anybody have an idea whats wrong with this string? Or with me?

    Gerhard

    UPDATE: I'm at home now and cannot test it. Can it be that ...getFilePath() returns a Java-String which must first be converted to a JavaScript-String? Like this:
    return String("" + ScriptRunner.getScript().getFilePath()). replace(/\\/g, "\\\\");
    
    UPDATE2: the ""+ already does this...
  • narayannarayan Administrators
    Yes Gerhard, ScriptRunner.getScript().getFilePath() does return a Java string which is converted to a js string by concatenating with an empty js string.
  • Thanks, Narayan. It's working now.

    I organise my tests with a subdirectory for each suite.
    scripts/
      suite1/
        suite1.suite
        test1.sah
        test2.sah
      suite2/
        suite2.suite
        test1.sah
    ...
    
    and the following snippet gives me the suite name and the test name.
    var $scriptPath = ("" + ScriptRunner.getScript().getFilePath());
      if ($scriptPath.search(/\\/)) { // Windows
        var $arrScriptPath = $scriptPath.split(/\\/);
      } else {
        var $arrScriptPath = $scriptPath.split(/\//);
      }
      var $nagiosTestScript = $arrScriptPath.pop().replace(/\.sah/, '');
      var $nagiosTestSuite = $arrScriptPath.pop();
    
    Cheers,
    Gerhard
Sign In or Register to comment.