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.

How to include custom js libraries with sahi

WebTesterWebTester Members
edited November -1 in Sahi - Open Source
How to use jquerry in sahi .Where should I include the jquerry js file .

Comments

  • narayannarayan Administrators
    Hi WebTester,

    If your web application already uses jquery, you do not need to do anything. You can use jquery accessors in your code.


    Example script:
    _navigateTo("http://jquery.com/");
    _assertFalse(_isVisible($("p.neat")[0]));
    _click(_link("Run Code"));
    _assertTrue(_isVisible($("p.neat")[0]));
    


    If your application does not use jQuery,
    1) Copy the jquery.js and other files to sahi/htdocs/spr directory
    2) Add a script tag for jquery.js in sahi/config/inject_top.txt, so that it looks like:
    <script src="/_s_/spr/domainfix.js"></script>
    <script src='/_s_/spr/concat.js' id='_sahi_concat'></script>
    <script src='/_s_/spr/jquery.js'></script>
    ...
    

    Restart Sahi and it should work for you.

    Regards,
    Narayan
  • tejasbhojaktejasbhojak Members
    edited July 2011
    okay so we can write jQuery code inside a sahi function.
    so we can write entire script like

    _sahi(
    //jquery expression

    //YUI expresssion

    //any javascript expression

    )
  • hi,
    I have included jquery.js as described and I am trying the following
    jQuery.post("http://myapp:9080/ws/", 
       function(data) { 
         alert("Data Loaded: " + data); }
      );
    
    However I am getting
    ReferenceError: "jQuery" is not defined.  at Sep 2, 2011 3:11:29 PM
    

    Should I define it as a sahi function
    or is there any other reason to fail ?
  • narayannarayan Administrators
    Could you specify how you have added the js file, and how you are invoking that jQuery function?
  • Hi ,

    the lib is here: {SAHI_HOME}\htdocs\spr\jquery-1.6.2.min.js

    I am trying to run jQuery as here:
    jQuery.post("http://myapp:9080/ws/", function(data) { alert("Data Loaded: " + data); });
    Lech
  • narayannarayan Administrators
    Hi Lech,

    Did you include it in inject_top.txt or in any other way?

    Regards,
    Narayan
  • Hi Narayan,
    This is my inject_top.txt
    Note, I am using sahi 20101103
    <!--SAHI_INJECT_START-->
    <script>/*<![CDATA[*//*---->*/__sahiDebugStr__="";__sahiDebug__=function(s){__sahiDebugStr__+=(s+"\n");};/*--*//*]]>*/</script>
    <script src="/_s_/spr/domainfix.js"></script>
    <script src='/_s_/spr/concat.js' id='_sahi_concat'></script>
    <script src="/_s_/spr/jquery-1.6.2.min.js"></script>
    
  • narayannarayan Administrators
    Hi Lech,

    When you include the jquery library via inject_top.txt, it will be available on the browser. You would be able to use it like:

    _click(jQuery("id"));

    but not use it directly in a script. If you want to actually post data using jquery from a script, copy jquery-1.6.2.min.js to your scripts folder, rename jquery-1.6.2.min.js as jquery-1.6.2.min.sah and use _include to include it in your script.

    Regards,
    Narayan
  • narayan wrote:
    Hi WebTester,

    If your web application already uses jquery, you do not need to do anything. You can use jquery accessors in your code.


    Example script:
    _navigateTo("http://jquery.com/");
    _assertFalse(_isVisible($("p.neat")[0]));
    _click(_link("Run Code"));
    _assertTrue(_isVisible($("p.neat")[0]));
    


    If your application does not use jQuery,
    1) Copy the jquery.js and other files to sahi/htdocs/spr directory
    2) Add a script tag for jquery.js in sahi/config/inject_top.txt, so that it looks like:
    <script src="/_s_/spr/domainfix.js"></script>
    <script src='/_s_/spr/concat.js' id='_sahi_concat'></script>
    <script src='/_s_/spr/jquery.js'></script>
    ...
    

    Restart Sahi and it should work for you.

    Regards,
    Narayan


    Hi Narayan,

    Our site is already using jquery, but still i got an error,
    ReferenceError: "$" is not defined.
    
    and
    ReferenceError: "jQuery" is not defined.
    

    Moreover, i also tried injecting the jquery but still got the same error results.
    And i also tried,
    narayan wrote:
    Hi Lech,

    When you include the jquery library via inject_top.txt, it will be available on the browser. You would be able to use it like:

    _click(jQuery("id"));

    but not use it directly in a script. If you want to actually post data using jquery from a script, copy jquery-1.6.2.min.js to your scripts folder, rename jquery-1.6.2.min.js as jquery-1.6.2.min.sah and use _include to include it in your script.

    Regards,
    Narayan

    but, got no good result as well.


    Thanks.
  • vijayvijay Members
    edited January 2013
    Did Settings in

    <script src="/_s_/spr/domainfix.js"></script>
    <script src='/_s_/spr/concat.js' id='_sahi_concat'></script>
    <script src='/_s_/spr/jquery.js'></script>
    My Application IS Using Jquery




    HTML CODE:: AM NOT ABLE TO PICK THIS CUSTOMID ATTRIBUTE THRU JQUERY

    <input type="text" customid="dtbill" > hello </input>

    //(1) ERROR = "$" is not defined
    var $d = _getText($("dtbill"));
    _log($d);



    //(2)
    //from http://api.jquery.com/attribute-starts-with-selector/
    $('input[customid="dtbill"]').val('21/2/2012');


  • Hello Narayan,

    I followed your advice in your first post and got JQuery calls working when the scripts are run in IE. However, the library isn't included when I run the scripts in Firefox (v30.0). Any suggestions?
Sign In or Register to comment.