Get each iteration in its respective table
Hey everybody,
i got a new problem.
I write my scripts and as soon as they finish i have an event handler which writes everything into database line by line. Everything works fine with just 1 iteration, but once the second (for example) iteration starts, everything from the first iteration is also written into the table meant for the second iteration.
That's what i mean:
table_1 content: logs from iteration 1
table_2 content: logs from iteration 1 & 2
That's what i want/need:
table_1 content: logs from iteration 1
table_2 content: logs from iteration 2
For logging i used the script from here --> https://support.smartbear.com/viewarticle/68245/?utm_source=site-search&utm_medium=search-results&utm_campaign=site-search-c&utm_term=scripting+access&_ga=1.146940919.1763155909.1438165004
...and edited it for my purposes like this (maybe there are some easier ways to log the data, but this works for me and im still a beginner :smileyhappy: )-->
//USEUNIT Create_DB // Exporting the log function General_Events_OnStopTest(Sender) { Log.SaveToDisk(); var i, FS; if(Project.Logs.LogItemsCount > 0) { for(i = (Project.Logs.LogItemsCount-1); i < Project.Logs.LogItemsCount; i++) { ExportLogItem(Project.Logs.LogItem(i)); } } else // If the project does not contain log items, // post a message about this to the test log Log.Message("No logs for export."); } // Exporting row data function ExportRow(TableScheme, Caption, ARow) { var i, s; var Child, ChildRow; var ColCount, ChildCount // Getting the number of table columns ColCount = TableScheme.ColumnCount; // Getting the number of child tables ChildCount = TableScheme.ChildCount; s = Caption; //Getting column data for(i = 0; i < ColCount; i++) { //Write data into the array s[i] = aqConvert.VarToStr(ARow.ValueByIndex(i));} //Depending on the caption call the respective function if(((aqString.Find(s[2], '<keyword>')) != -1)) { Testrun_data(s,ARow); } else if(((aqString.Find(s[2], '20')) != -1)) { Log_data(s,ARow); } // Exporting child tables data for(i = 0; i < ChildCount; i++) { Child = ARow.ChildDataByIndex(i); ExportLogData(Child); } // Exporting child rows (if the data is displayed as a tree) for(i = 0; i < ARow.ChildRowCount; i++) { ChildRow = ARow.ChildRow(i); s = aqConvert.VarToStr(i + 1) + "\t"; ExportRow(TableScheme, s, ChildRow); } } // Exporting table data function ExportTable(ALogData) { var TableScheme, Row, i; var s = Array(); // Obtaining the table scheme TableScheme = ALogData.Scheme; // Iterating through table records and exporting table data for(i = 0; i < ALogData.RowCount; i++) { // Obtaining the row object Row = ALogData.Rows(i); s[0] = aqConvert.VarToStr(i + 1) + "\t"; // Exporting the row} ExportRow(TableScheme, s, Row); } } // Exporting log item's data function ExportLogData(ALogData) { var Scheme; Scheme = ALogData.Scheme; if (Scheme.Name == "Performance Counters") return; ExportTable(ALogData); // Exporting table data } // Exporting log items function ExportLogItem(ALogItem) { var i; // Exporting log item's data for(i = 0; i < ALogItem.DataCount; i++) ExportLogData(ALogItem.Data(i)); // Exporting child log items for(i = 0; i < ALogItem.ChildCount; i++) ExportLogItem(ALogItem.Child(i)); } function Testrun_data(s,ARow) { s[0] = 1; s[3] = aqConvert.DateTimeToFormatStr(ARow.ValueByIndex(3),"%Y-%m-%d %H:%M:%S"); s[4] = aqConvert.DateTimeToFormatStr(ARow.ValueByIndex(4),"%Y-%m-%d %H:%M:%S"); var Cmd = ADO.CreateADOCommand(); Cmd.ConnectionString = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=dblog;Uid=dblog;Pwd=dblog;"; Cmd.CommandText = 'INSERT IGNORE INTO testrun (start, stop, devicenumber, description)' + 'VALUES ("'+ s[3] +'","'+ s[4] +'","'+ s[0] +'","Default ' + s[3] +'")' + 'ON DUPLICATE KEY UPDATE stop="'+ s[4] +'"'; Cmd.Execute(); } function Log_data(s,ARow) { //Get script information var curr = Project.TestItems.Current; var cat = curr.Name; var tcId = curr.ElementToBeRun.Caption; //Avoid script for creating data table if((aqString.Find(tcId, 'Create_DB')) != -1) { return; } //Get iteration var iter = curr.Parent.Parent.Iteration; //Convert content to desired form s[0] = 1; s[2] = aqConvert.DateTimeToFormatStr(ARow.ValueByIndex(2),"%Y-%m-%d %H:%M:%S"); if((aqString.Find(s[1], '"')) != -1) { s[1] = aqString.Replace(s[1], '"', '\\"'); } if((aqString.Find(s[6], 'Error')) != -1) {var err_flag = 1;} else {var err_flag = 0;} //Get DB connection var Cmd = ADO.CreateADOCommand(); Cmd.ConnectionString = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=dblog;Uid=dblog;Pwd=dblog;"; Cmd.ParamCheck = false; //Get the latest entry from table testrun Cmd.CommandText = "SELECT max(pk_testrun) FROM `testrun`" var RecSet = Cmd.Execute(); var trCnt = RecSet.Fields("max(pk_testrun)").Value; //Insert into the defined table Cmd.CommandText = 'INSERT IGNORE INTO '+ tbname +' (testcaseid, categorie, devicenumber, description, error_flag, iterations, fk_testrun, LogDateTime)' + 'VALUES ("'+ tcId +'","'+ cat +'","'+ s[0] +'","' + s[1] +'","' + err_flag +'","' + iter +'","' + trCnt +'","' + s[2] +'")'; Cmd.Execute(); if(err_flag == 1) {var t_result = 'FAILED';} else {var t_result = 'OK';} Cmd.CommandText = 'INSERT IGNORE INTO tcid_entries (categorie, testcaseid, result, iterations, fk_testrun, fk_log_id) ' + 'SELECT categorie, testcaseid, "'+ t_result +'", iterations, fk_testrun, pk_log_id FROM '+ tbname +' WHERE testcaseid = "' + tcId +'" AND description = "Starting Script" AND testcaseid NOT LIKE "%Create_DB%"'; Cmd.Execute(); Cmd.CommandText = 'INSERT IGNORE INTO log_err (testcaseid, categorie, devicenumber, description, error_flag, iterations, LogDateTime, fk_log_id, fk_testrun) ' + 'SELECT testcaseid, categorie, devicenumber, description, error_flag, iterations, LogDateTime, pk_log_id, fk_testrun ' + 'FROM '+ tbname +' WHERE error_flag = 1'; Cmd.Execute(); }
Is there a way to start logging into database at the point where the second (or higher) iteration starts?
Thanks for help in advance.
I can't believe it myself, but i did it. After 3 days of trying i found the solution.
I tested it with 2 and 4 iterations and every log goes in its own table.
My solution -->
function ExportLogItem(ALogItem) { var i; // Exporting log item's data for(i = 0; i < ALogItem.DataCount; i++) { if((aqString.Find(ALogItem.Name, 'Iteration')) != -1) { var curr = Project.TestItems.Current.ElementToBeRun; if((aqString.Find(curr.Caption, 'Create_DB')) == -1) { var proIter = Project.TestItems.Current.Parent.Parent.Iteration; if((aqString.Find(ALogItem.Name, proIter)) != -1) { ExportLogData(ALogItem.Data(i)); } else { return; } } } else { ExportLogData(ALogItem.Data(i)); } } // Exporting child log items for(i = 0; i < ALogItem.ChildCount; i++) ExportLogItem(ALogItem.Child(i)); }