How do I export runtime datatable into excel if any error occurs due to data? - excel

I want to know if I can export a datatable into excel when I get an error due to data while running the scripts.
If i am having 5 records in a sheet, and 2 records processed well, while running the third record my script encounters an error. Am I able to export into excel in that moment?
Errors may occur at any places because of the data.

Your question doesn't explicitly say QTP, but I'm assuming QTP because you used the tag HP-UFT.
I'm not sure what you mean by "when we get error", so I'll explore two possibilites.
1) You're getting an error in the application you are testing; QTP itself is still executing the script.
In this situation, your script should have validation checks (if statements that check to make sure that what you expected to happen did indeed just happen), and if those checks fail, you could immediately do a DataTable.Export(filename) to save the data to disk before QTP ends. Then, the script could continue, or you can add an ExitTest to fail out and stop the test.
Based on your question, I think it's more likely that:
2) You're getting an error in QTP itself. When QTP crashes, it drops any dynamic changes to the DataTable (i.e. if you had done a DataTable.Import(filename) or updated any fields, it would loose that and go back to it's design time DataTable instead)
In this situation, your script is encountering something that is causing QTP itself to stop the script. Perhaps it's hitting an error where an object cannot be found, or some kind of syntax error. You should consider adding defensive statements to check on things before your code reaches the point that this kind of error would occur... For example, perhaps add...
If not Browser("ie").Page("page").WebTable("table").Exists then
FailTestBecause "Can't find table"
End If
...
function FailTestBecause (reason)
Print "Test Failed Because: " & reason
Reporter.ReportEvent micFail, Environment("ActionName"), reason
DataTable.Export(filename)
ExitTest
end Function
Or, you could just use an On Error Resume Next and put in a command to DataTable.Export(filename) immediately after where it is failing...

Related

Is there a way to check if a connection in Excel is valid before refreshing it?

Quick Background:
I have almost no experience with VBA. I started learning this two weeks ago and have made fast progress, but I am lacking a lot of knowledge. I do not know enough to know what I do not know, for lack of a better word. My apologies up front if this is a question that has a very simple solution. I have been scouring Microsoft's VBA documentation with no luck for several days now.
The Problem:
I have a single connection in a main spreadsheet to a spreadsheet on SharePoint that pulls data from inputs by operators on their cell phones. The current way my code is set up, it will refresh that connection before running calculations to ensure that any new data is accounted for.
However, since the connected document's location (or even name) may change in the future due to it being a shared document, I wanted to find a way to test that the connection was valid; i.e. that the correctly named file existed where it was supposed to be.
Currently, if the file name or location do not match, Excel throws an error and my code will stop running. Ideally I would like to have something similar to this:
If connectionIsValid Then
Refresh the connection
Else
MsgBox "Could not refresh connection", vbInformation
*Continue with code instead of throwing an error*
End If
I have tried things like:
If ActiveWorkbook.Connections(1).OLEDBConnection.IsConnected Then
Run Code
End If
But after reading the documentation I realized the above is related to the MaintainConnection Property being true.
and:
On Error GoTo ErrorHandler
*Refresh the sheet*
*Continue code*
Error Handler:
*Don't refresh the sheet*
*Continue code*
Any help or even a pointer in the right direction is greatly appreciated. Thank you for your time.
I'm not sure what is wrong with your second suggestion - using an error handler.
Function checkConnection()
On Error GoTo errorHandler
ActiveWorkbook.Connections.Item(1).Refresh
Exit Function
errorHandler:
MsgBox "Could not refresh connection. " & Err.Description
End Function
I've just added the Exit command there, otherwise it will run the code in the errorHandler label every time, even if there isn't an error.

Detect that automated Chrome page has been closed

I have written code to automate entries into a survey using Excel VBA with selenium controlling chrome.
If the user closes the driven chrome window then currently I get a vba error when the next element can't be found.
I want to write some code to detect the chrome window is no longer present and then close down the vba code smoothly.
My idea was to try to find the tab title and execute the closure code if it did not exist - meaning I got the chrome not available error. The code below seems to work but there is a 13 second wait whilst it looks for the page title. Shortening any of the 4 Timeouts available in selenium basic does not seem to change this wait.
My questions are:
Is there a better way to check if the driven chrome window has closed?
Is there a way to shorten the period of time before the error is thrown?
On Error Resume Next
tabTitle = ch.Title
If Not tabTitle Then
Debug.Print "Closed"
End If
On Error GoTo 0
Many thanks
The only way I know of is to catch UnreachableBrowserException. I am not sure what Selenium version you are using, but in Java, you would surround your code in a try/catch
try {
// do your Excel thing here
} catch (UnreachableBrowserException e) {
// execute your alternate plan
}
The problem is that this could happen at any time, anywhere. You can simply put this everywhere on the code. If this is part of a fully-automated solution, why not simply run your script in headless mode. This will prevent the root cause of the problem which is a user unexpectedly closing the browser.

How to print a user's error message when lr script fails

I am new to loadrunner. I know error handling part. But recently I came across a situation in which I use a parameter unique each iteration setting. This data is exuastive in nature i.e I cannot use them again.
Now in my script getting error where where it shouldn't be. Probably at the breaking point of the application. So I want to print that parameter whenever it fails anywhere in the script.
Any idea how to achieve this ?
Thanks in advnace.

Cannot Run Visual Basic Editor Because of a Syntax error on Application.Calculate

I have a VBA Excel model which I have separated into two separate workbooks:
InputsWB, which contains all the inputs for the model, and
RunnerWB, which contains the bulk of the VBA code (and all the class modules live here).
Depending on the needs of the user, the InputsWB can either call the macro in the RunnerWB or the RunnerWB can call multiple versions of the InputsWB. The detail doesn't matter for this question but the two workbooks need to be separated.
Mysteriously, after the split I sometimes get a Run-time 1004 error with the message Cannot Run Visual Basic Editor Because of a Syntax error. If I Debug + F5 then the code runs until it encounters the next Application.Calculate.
I have seen many similar questions while recording macros, or on Application.Calculate. I think mine is related to the Application.Calculate, but those answers don't explain why this happens. Some reasons I can think off, but cannot confirm online is:
There are Modules in the two workbooks with the same names (although I've changed the modules names in the InputsWB but still get the error on occasion.
There are VBA functions in the two workbooks with the same names
Are there any other reasons why I could be getting these errors?
UPDATE (2019/12/02)
I finally realised why I got this error. I am calling a macro in my RunnerWB from my InputsWB. However, there was a duplicate function declared in my RunnerWB. Usually, this would give a Compile error: ambigious name detected when the macro was initiated from ThisWorkbook. However, because it was initiated from another workbook I got the Syntax error explained above.
UPDATE (2020/07/22)
I am now getting this error again on Application.Calculate right at the end of the run. Which breaks me is that I use Application.Calculate many times during the run.
UPDATE (2020/07/23)
I found the issue that caused the bug at last.
I had a sub called Main in a Module called Main
One of the buttons in my workbook referred to this button, and because the names are the same it links it as Main!Main
Later I renamed the module to MainSub. The button still worked, but something inside VBA was broken which was triggered in some Application.Calculate executions.
Conclusion
Never give your sub the same name as your module in VBA, as this may cause problems down the line.

Capture DataFeedConnection errors using vbscript

I have a data feed connection in excel. I'm trying to refresh a data feed connection using vbscript. Here's how I try to refresh the connection.
Set oWorkbook = oExcel.Workbooks.Open('<excel file name/path>')
oWorkbook.Connections(1).Refresh
The total number of records returned by the data feed(OData) is actually huge and the conection isn't stable. Hence, I often get the error stating that "The connection is closed" while trying to refresh the connection from excel manually. A snapshot of the error below. Whenever I get this error, I try refreshing the connection again and this results in successful refresh most of the times.
I want to achieve the same result in vbscript as well. When I refresh the connection using vbscript, I get the same error and my script stops running as its unable to capture and handle the error.
In the documentaion I see that I can capture errors only if its OLEDBError or ODBCError. I'm not able to find the similar ways to capture DataFeedConnection error. Is there any way to capture the DataFeedConnection errors?
Edit: To update you with more information on the vbscript, the script picks up multiple files from the current folder, refresh the first or second connection depends on the file name and then compare certain values before and after refresh to make sure the refresh has happened successfully and then decide saving or closing the file based on the comparison results. The reason for comparison is there are some times refresh brings only partial data.
How about the following, adding an "On Error GoTo" then using a msgbox to display the error description:
Sub Foo()
On Error GoTo WhatHappened
'Your code here
WhatHappened:
MsgBox Err.Description
End Sub

Resources