Please note that Sahi's development and support teams do not actively monitor this. This is more a community forum. If you are a Sahi Pro user looking for commercial support, please email support @ sahipro.com with your query.

How to include custom js libraries with sahi

How to use jquerry in sahi .Where should I include the jquerry js file .

Comments

  • 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
  • edited July 2011 Members
    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 ?
  • 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
  • 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>
    
  • 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.
  • edited January 2013 Members
    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?
  • I get the following error:
    "TypeError: r is not a function, it is undefined."

    - - - - -
    with this code:

    _include("includes/jquery-1.11.1.min.sah");

    if (window.jQuery) {
    _alert("loaded");// jQuery is loaded
    } else {
    _alert("not");// jQuery is not loaded
    };
    - - - - -
    I would like to use the jQuery.getJSON() function, and I am failing.

    Any help would be appreciated.

    Thanks.
  • edited May 2016 Members
    Here is a solution: use "eval"

    var $test = '$("button.className")[1]';
    _highlight(eval($test));
    - - -
    Or for a more complicated solution to identify an element from an in-class style:

    var $test = '_link(0, _in($(\'div[style*="background-color: #f9f9f9"]\')[0]))';
    _highlight(eval($test));
  • edited May 2016 Members
    actually the above doesn't work with all functions; a better fix is needed
  • edited May 2016 Members
    so far the best solution is to avoid using variables for the jquery location; this works using jQuery!

    _set($href, _link(0, _in($('div[style*="background-color: #f9f9f9"]')[0])).href)
Sign In or Register to comment.