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.

_getDB

chejnesschejness Members
edited November -1 in Sahi - Open Source
Hi Team,


I am unable to read data from database by using the
_getDB option:

My sahi.bat is:

cd /d %~dp0
SET EXT_CLASS_PATH=..\extlib\db\mysql-connector-java-5.0.4-bin.jar;C:\sahi\extlib\classes12.jar;
SET SAHI_CLASS_PATH=..\lib\sahi.jar;..\extlib\rhino\js.jar;..\extlib\apc\commons-codec-1.3.jar;
java -classpath C:\sahi\extlib\classes12.jar;../lib/sahi.jar net.sf.sahi.Proxy

my code is:

var db = _getDB("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@host:port:sid", "usrname", "pwd");
var $rs = db.select("select * from emptable where emp_name = 'chej'");
_wait(1000);
_alert($rs);
_assertEqual("nes", $rs[0]["emp_name"]);

Please let me know what is the error.

Comments

  • narayannarayan Administrators
    Assuming your oracle driver is in C:\sahi\extlib\classes12.jar, your sahi.bat should be:

    cd /d %~dp0
    SET EXT_CLASS_PATH=C:\sahi\extlib\classes12.jar;
    SET SAHI_CLASS_PATH=..\lib\sahi.jar;..\extlib\rhino\js.jar;..\extlib\apc\commons-codec-1.3.jar;
    java -classpath %EXT_CLASS_PATH%;%SAHI_CLASS_PATH% net.sf.sahi.Proxy


    If you also need mysql driver, use

    SET EXT_CLASS_PATH=..\extlib\db\mysql-connector-java-5.0.4-bin.jar;C:\sahi\extlib\classes12.jar;
  • I still get this error:

    _alert(db.select("select * from emptable where emp_name = 'chej'"));
    TypeError: 'db' is undefined
    No trace available
    Click for browser script [object Object] at Oct 27, 2009 10:08:36 AM
  • VenkyVenky Members
    edited October 2009
    you can try it as,

    var $db = _getDB("Driver","Jdbc url","username","password");
    _alert($db.select("select * from emptable where emp_name = 'chej'"));

    When you write
    _alert(db);
    Sahi will search for the variable db on the browser (and will not get it)

    When you write
    _alert($db);
    Sahi will evaluate $db on the proxy and pass it to _alert on the browser.
  • Hi team,

    I used the following code:

    var $db = _getDB("Driver","Jdbc url","username","password");
    _alert($db.select("select * from emptable where emp_name = 'chej'"));


    Got yhe following error:

    _alert([object Object]);
    SyntaxError: Expected ']'
    No trace available
    Click for browser script [object Object] at Oct 27, 2009 2:22:10 PM

    Any help?
  • narayannarayan Administrators
    chejness,

    You are right, there is a bug in Sahi in the last 2 builds which is causing the alert to fail. Your alert would work if you added an empty string to it like this:


    var $db = _getDB("Driver","Jdbc url","username","password");
    var $resultAsString = "" + $db.select("select * from emptable where emp_name = 'chej'");
    _alert($resultAsString);

    But I guess this bug would not bother you since you would be iterating over the array and using those variables elsewhere rather than alerting them.

    You can expect a build in a day or two with a fix for this. Email me if your need is urgent and I will email you the needed files.

    Regards,
    Narayan
  • Hi Team,

    Changed the code to :

    var $db = _getDB("Driver","Jdbc url","username","password");
    var $resultAsString = "" + $db.select("select * from emptable where emp_name = 'chej'");
    _alert($resultAsString);

    This time i get an empty result!!

    Where as
    _alert($db.select("select * from emptable where emp_name = 'chej';");

    I got "Undefined"

    The database is working fine.
    But i am unable to retrive and assert the values through Sahi.
    Any help?
  • Any update on this error?
  • dpdp Members
    I would suggest you to try out the following so that we can figure out where it goes wrong.

    Create an excel sheet by name testSheet.xls. Create a column by title "Name". Enter few rows of data. Then run the following code segment and let me know how it goes. (Please update the path to the excel sheet appropriately). Assuming that you are working on a Windows machine, the below code should work fine

    var db = _getDB("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=C:\\sahi_20090902\\sahi\\scripts\\testSheet.xls;readOnly=false", "", "");
    $rs = db.select("select * from [Sheet1$]");
    _alert($rs.length);
    _assertEqual("dp", $rs[0]["Name"]);
    
  • Hi Team,

    I gave the following code:

    var db = _getDB("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)}", "DBQ=C:/sahi/scripts/3GDibbler/testSheet.xls;readOnly=false", "", "");
    $rs = db.select("select * from [Sheet1$];");
    _alert($rs.length);
    _assertEqual("dp", $rs[0]["Name"]);


    And the error message:

    JSERROR SyntaxError: syntax error at Nov 2, 2009 3:22:25 PM

    Also,
    1. What jars need to be placed in the classpath for the above code to work?
    2. Please verify if the syntax mentioned above for _getDB is correct.
  • dpdp Members
    There is something wrong in the syntax. I would suggest you to copy and paste what I have posted and just change the path to the file
  • VenkyVenky Members
    edited November 2009
    In windows, It does not require any extra jar files for accessing the Excel file if you had installed Java.

    Sahi gives JSError mostly when
    1. The database is not accessible (or) problem with connecting to database.
    2. Any driver related errors (or) Driver is not recognizable

    And also try this,

    _getDB("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)}", "DBQ=C:\\sahi\\scripts\\3GDibbler\\testSheet.xls;readOnly=false", "", "");

    For more details on how to access Excel datas using Sahi, Please refer http://sahi.co.in/w/working-with-excel-sheets

    Which version of Sahi you are using?

    Please try it with the latest release

    Let us know if you still have any problems.
  • Hi Team,

    I am using the Sahi V3 (2009-10-23)

    Used the following code:
    _getDB("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)}", "DBQ=C:\\sahi\\scripts\\3GDibbler\\testSheet.xls;readOnly=false", "", "");

    Still getting the error:
    JSERROR
    SyntaxError: syntax error at Nov 2, 2009 6:21:12 PM

    Any help?
  • dpdp Members
    Did you try the code that I posted?

    The code that you wrote and the one posted by Venky has an error. Both of you tried to split the second parameter into two!!! The second parameter is a single string including the Driver, DBQ and readonly.
  • Yes dp, you are right. Thanks to dp and sorry for the mistake.

    Hi Chejness, Please try it as dp mentioned and let us know whether it helps or not.
  • Hi Team,

    I used the code as given by dp:
    var db = _getDB("sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=C:\\sahi\\scripts\\3GDibbler\\testSheet.xls;readOnly=false", "", "");
    $rs = db.select("select * from [Sheet1$]");
    _alert($rs.length);
    _assertEqual("dp", $rs[0]["Name"]);

    Got the same error!!
    JSERROR
    SyntaxError: syntax error at Nov 3, 2009 9:13:30 AM

    What is the workaround for this?
    Any help? :(
  • dpdp Members
    What do you see on the console?
  • Nothing:(
  • Hi team,

    Any workaround to access db through scripts?
  • narayannarayan Administrators
    Chejness,

    DB, Excel access seems to be working fine for the rest of us. One new thing that came up was that if the data had newlines in it, a JSERROR was being thrown. You could have a look at this: http://sahi.co.in/forums/viewtopic.php?id=1075 to see if the suggested workaround fixes your problem.

    If not, please email your sample excel sheet and script which fails, and we will help you fix your problem.

    Regards,
    Narayan
Sign In or Register to comment.