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.

output script results to db

ghillie14ghillie14 Members
edited November -1 in Sahi - Open Source
I assume its possible to change the source code to output the script results to a db, maybe an excel workbook or mysql db instead of the .htm output? Would you recommend me doing so? Can you give me any advice on what files to look at? We want to be able to view the results in a database so we can have a system send an email if any tests fail in an overnight running of our system. In order to do that, we need the results stored in a database.

I've played with java 5 years ago, so my java is a little rusty, but I've been looking in the record file in the source code.

Am I taking the long way to a solution, is there an easier way to record the result: pass/fail of scripts to a database other than reading the .htm files and dumping those into a db?

Thank you very much for your help and replies

Comments

  • I've been looking at the report file, not the record file.

    There must be a more efficient way to input test results into a database instead of reading a text file and passing that value into a db?

    Starting a testing project and am wondering what is the best way to go with this, so any input or advice is greatly appreciated. Thanks!
  • Finished. I changed the src code to log to a txt file AND to a database. So now we have another system that looks at the database to see the test results and sends an email containing the fail/error test result's information to an admin. All called from batch files and scheduled to run during the night. Complete automated testing system works great!
  • diikeediikee Members
    ghillie14,

    can you share what you had to change to log results to a db??

    thanks.
  • I'll try to help as much as possible, I didn't document any of the changes for db logging.

    public void generateTestReport() {
            int i = 0;
            for (Iterator iterator = listReporter.iterator(); iterator.hasNext();) {
                SahiReporter reporter = (SahiReporter) iterator.next();
                reporter.generateTestReport(this);
                if (i >= 1) {
                    reporter.generateTestReport_Db(this);
                }
                
                i++;
            }
        }
    
    Sahi, report, Sahireporter.java...generateTestReport_Db(Report report){}
    try {
                                System.out.println("result = " + result.type.getName() );
                                Queries query = new Queries();
                                query.selectOPD(dto);
    }
    
    It then calls
    public void selectOPD(SahiDTO dto) throws Exception{
            
            try {        
                Connection jdbcConnection = net.sf.sahi.report.DBManager.getConnection();
                Statement  sqlStatement = jdbcConnection.createStatement();    
    
                StringBuffer sqlBuffer = new StringBuffer("INSERT INTO test_results (");
                 sqlBuffer.append("test_result, ");
                 sqlBuffer.append("test_name, ");
                      
                  sqlBuffer.append(") VALUES ('");
             
                sqlBuffer.append(dto.getTest_result());
                sqlBuffer.append("', '");   
                sqlBuffer.append(dto.getTest_name());
                sqlBuffer.append("', '");
    
                sqlBuffer.append("')");              
               
                sqlStatement.executeUpdate(sqlBuffer.toString());       
                
                jdbcConnection.close();
                
            }
            catch (Exception e){
                //logger.error("Exception occured updating the Table: " + e.toString());
                throw e;
            }    
        }
    
    Then it reads a properties file for the jdbc connection string.

    Hope this helps, I can post more code if necessary. Also sorry about the formatting, not sure how this is going to turn out.
  • I've left out a few .java files that aren't necessary, but I can post a more complete explanation if interested.
  • diikeediikee Members
    Yes Ghillie,

    go ahead and post all the .java files, am sure somebody else will make use of them.

    Now,
    do these code go into my script or Sahi config file??
  • You will need to download the source code. Within src\net\sf\sahi load these files into your java IDE, I prefer eclipse. I would not recommend changing the src files unless you are comfortable with java code. Once you make these changes, export the java src code into a jar file and replace the new jar file with the original sahi.jar. You will have to add a mysql connector to the build path along with ant and xmlrpc, I also added jmock.

    The code does not go into the scripts or in a config file but in the sahi.jar file.

    A brief explanation of logging results to a database is as follows.

    Sahi.report.java
    - generateTestReport() is called.

    Sahi.sahireporter.java
    -generateTestReport(Report report) is called.
    - I created another method, generateTestReport_db(Report report)
    public void generateTestReport_Db(Report report ) {  
             
            if (report.listResult != null && report.listResult.size() >0) {
               
                for (int i = 1; i <= (report.listResult.size()-1); i++) {
                    
                    TestResult result = (TestResult) report.listResult.get(i);
    
                    if (result.type.getName() != ("INFO")) {
                        
                        SahiDTO dto = new SahiDTO();  
                        Calendar date = Calendar.getInstance();
                        dto.setTest_day(date.get(Calendar.DAY_OF_YEAR ));
                        dto.setTest_result(result.type.getName() );
                        dto.setTest_name (report.getTestSummary().getScriptName() );
                        int step_num = (report.getTestSummary().getSteps());
                        dto.setTest_step_number (i + " / " + step_num);    
                        dto.setTest_log_file (report.getTestSummary().getLogFileName() );
                        } 
                        catch (IOException e) {
                        }                    
                                     
                        try {
                                System.out.println("result = " + result.type.getName() );
                                Queries query = new Queries();
                                query.selectOPD(dto);
                        }
                        catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        
                    } // end if - FAIlURE or ERROR
                                           
                }  // end if
            }  
        }
    
    I load the test result data into a DTO (data transfer object) and pass the DTO to the database through selectOPD(SahiDTO DTO) {}

    I then load the jdbc string and parameters from a properties file that I placed in the config folder.
    DebugConnectString=jdbc:mysql://localhost:3306/testdb
    DBusername=root
    DBpassword=password
    
    For error logging, I have system.out.println, which is easy to see where the code is breaking. Hope this helps.
  • will this be making its way into the standard Sahi release at any point?
Sign In or Register to comment.