Machine cleanup Framework for UFT/QTP

Hello Friends,

In this post we will discuss about the effective ways to clean up the machine after execution of the scripts.

1.     What is Machine Cleanup Framework?

Machine cleanup framework is a separate module which will be implemented as a functional library within QTP/UFT scripts. This module will enable effective usage of the machine and nightly/automated execution of the script. We must implement this framework, if we plan to integrate our scripts with continuous integration frameworks like Jenkins.

2.     Why Machine Cleanup framework is required?

For a new automation team, priority is to increase the automation coverage as quickly as possible to show more benefits. But what will happen once we have tons of automated scripts in bucket? And the answer to that question is most of the team will spend most of their time for execution of the automated scripts instead of developing new ones. It will become a huge problem if we are not thinking about it when we start.

Solution to the above issue will be, the ability to start nightly executions of the scripts, but to have that ability, scripts must be cleaned up, because a lot of automated scripts will have certain amount of manual interventions. Once we do that, the next big question will be how to make sure that the machine or UFT is ready for the execution.

To be able to do that we have to write the code for killing certain applications closing UFT and taking care of the UFT crashes.

Can we take care of UFT crash? Yes, I can, and by the time you will finish reading this post, you can also.

3.     Implementation

Most of us will think about implementing a machine cleanup framework when it’s too late and they already have thousands of scripts already in place, so here we will take the same case and we will try to implement the code where we don’t have to take care about where to make the function calls.

To implement it. we will create a separate class in a functional library.

Class Environment_Cofig
‘Member variables declaration goes here.
‘Member function definition’s goes here.
End Class

Now, the question is why to implement the class and complicate things, we can simple write functions in the function library, and the answer is, remember a class comes with two inbuilt functions “Constructor” and “Destructor”, which will be called automatically. Constructor will be called automatically when we create the object of the class and destructor will be called when the garbage collector is called for the class. This is exactly we want to do, we want to call a function at the start of the UFT execution to check if the machine is clean and we have to call the same set of functions again at the end of UFT execution.

Now, to see the strength of this kind of framework create a empty test in the UFT and copy the below code in a functional library and associate it with the test.

‘####################################################

Public OEnvironment

Public Function IntializeClass
Set OEnvironment = New Environment_Cofig
End Function

Class Environment_Cofig
‘Constructor of the class
Private Sub Class_Initialize
Msgbox “Class Constructor called”
End Sub

‘Destructor for the class
Private Sub Class_Terminate
Msgbox “Destructor called”
End Sub
End Class

Copy the below code into the action of the test

Msgbox “Test Run Started”

‘Initialize Environment Cleanup class here
IntializeClass

Msgbox “Other stuffs here”

Msgbox “Test Run Ended”
‘####################################################

Run the test and see the sequence of the message boxes. You will notice that the constructor of the class is called as soon as the object is created and destructor of the class is called after the execution ends.

It’s time to see an interesting fact. Now place a break point on the line, where we are displaying the message about other stuffs, and run the test. Now, when the test reaches the break point try to stop the test using the “Stop” button. Test will stop but the destructor will be called, because UFT must release the class object and in the process destructor will be called automatically and that’s the most important feature which we will use while writing the cleanup framework.

Complete implementation should look like this

‘####################################################

Public OEnvironment

Public Function IntializeClass
Set OEnvironment = New Environment_Cofig
End Function

Class Environment_Cofig
‘Constructor of the class, this constructor will be used for any initialization required
Private Sub Class_Initialize
‘Function to check the logs, if any applications were left opened by last UFT execution
‘Function to terminate common applications like word, excel, powerpoint, UFT’s Report viewer, Internet Explorer etc. should be placed here..
‘Set QTP Configurations Here
‘Function to Initialialize application under test should be called here 
Msgbox “Class Constructor called”
End Sub

‘Destructor for the class
‘This function will be called to close any external objects created
Private Sub Class_Terminate
‘Functions to terminate the common and AUT should be placed here
‘Function to delete the open application logs should be placed here
‘Functions for any another miscellaneous tasks like moving results folder to the desired placed should be placed here
Msgbox “Destructor called”
End Sub
End Class

‘####################################################

The most important aspect of this implementation is that the whole framework is implemented and will call the functions automatically just by calling one simple function at the start of the test execution and we don’t have to change any of our existing code to implement the code cleanup functionality.

Important Point : Destructor might not be called if the UFT is killed abruptly by another process, to check that, execute the below command when UFT is at the break point we discussed earlier

taskkill /f /im uft.exe

Replace UFT.exe with QTPro.exe for QTP versions.

After executing this command, UFT will be killed instantly and destructor will not be called and hence to make sure that machine is ready, you have to run the clean up code in constructor also so that machine will be ready before execution.

If you have several hundreds of scripts which handles different types of applications then it will be a good idea to implement an initialize app function within the class, which can have switch cases for the initialization of different types of applications. This function can write logs about the opened applications with their process ID’s. These process ID’s can be used for killing the opened applications by the previous execution.

Let me know, if you have any questions, i will try to answer them.

Thanks,
Sumeet Singh Kushwah

10 responses to “Machine cleanup Framework for UFT/QTP

  1. Smart solution especially for other automation tools like Selenium. I’ve seen similar implementations in other languages as well. For QTP though I have to ask why not use the recovery sceanario? If a script breaks to the point that QTP can’t continue then a clean up function can be called to do everything you’ve described and more. I’ve set mine to go so far as write special logs, reported failed steps to QTP report and automatically create QC defects.

    Also, the startup function that kills open apps to prepare the PC for a new script execution can be called by any framework so that isn’t new.

    I guess I don’t see the value add in a class method when the recovery scenario does the same thing.

    Jim

    Like

    • Hi Jim,

      The whole point of implementing class is that we don’t have to worry about the calling functions, yes we can insert function calls, but the code will not be easily manageable with function calls and if you have couple of thousand scripts then you will end up inserting a lot of function calls, multiple times and might mess up with the scripts.

      Second most important thing is that there are two types of errors which UFT will throw, One for which we can implement recovery scenarios like a popup message box, but what if we have database connection failed issue, you cant write recovery scenario for that case and UFT will throw error for it. Even if you have recovery function, it will not be called.

      So to handle this situation we will need cleanup script to be called and if you don’t have class then you must have the code placed at so many places just to capture all of the errors and close the scripts.

      Also, I don’t want my applications to remain open and wait for next UFT execution for cleanup, because there will be issues like user id is blocked due to which on other remote machines, scripts execution will be block, and i don’t want to come back and reset things as far as possible.

      we can see the benefits out of this, if we have multiple machines running several scripts every night testing multiple apps and system stability.

      Thanks,
      Sumeet Singh Kushwah

      Like

      • Hi Sumeet,

        I think I understand what your saying. To be clear, the recovery scenario is not limited to pop up windows. It can be used for anything including database calls. More importantly, it is not coded for specific situations but is used as a catch all. If QTP can’t continue then you need a clean up script triggered by the recovery scenario. If QTP doesn’t break then standard exit and close functions will take care of closing applications.

        Inserting clean up function calls is what you need to do for your class call which is no different than what I suggested in my post. Any good framework would have one location for the function call so you don’t have to insert it in a hundred different places. In my situation it is in the recovery scenario and not in any test script.

        I understand what your doing so architecturally speaking both approaches are the same. I was looking for the benefit to a class solution that a recovery scenario doesn’t provide and respectfully, I’m not seeing it.

        Jim

        Like

  2. Hi Jim,

    I can understand what you are trying to explain and i was doing the same things before, its just the type of issues i encountered. i might have missed something and you might be able to through some light on it,

    Lets say i have one single line written in an action

    Msgbox Cint(1/0)

    Syntax is correct but, this will through divide by zero

    Then i added the recovery scenario like this
    Trigger Event – Test Run Error
    Select test run error – Any Error
    Recovery operation – Function call in a function library

    and ran the code and UFT still throws error and recovery scenario is not called why ?

    Thanks,
    Sumeet Singh Kushwah

    Like

    • It’s not called and I don’t care that it’s not called. Handled errors should not trigger clean up scripts. Clean up scripts should be built into the framework as a class or a function, either works as intended.

      Un handled errors should trigger clean up scripts, error messages, logs, ect. Class does it and so does recovery. There is no significant different in either approach.

      The reason I distinguish handled and Un handled is that failed steps should be like failed checkpoints. Failures are reported but the test keeps running.

      QTP has been offering this for years. I think yours is a great idea, I was just asking for benefit beyond recovery scenario.

      Like

  3. In my opinion, using class destructor as described in this article will be more efficient in terms of execution time verses using recovery scenario. Also, same class can be used to handle as many error conditions as the developers want, whereas for recovery scenario we need to handle each error condition separately. As far as QTP is concerned, custom coding is always little more beneficial than using the “user friendly” features.

    Like

  4. Like parantapsamajdar I only prefer it as a solution so as to not be tied into a tools specific functionality. We currently maintain 3 different automation tools in our company, but use the one framework. Yes, they’re all in different languages (QTP vbs, Ranorex C# and Selenium java) however you can use the same generic cleanup scripts across all tools within your framework.
    It’s the same with other mechanisms such as reporters. We wrote our own to maintain a consistent look and feel. Our services and load testing tools also use the same reporter.

    Like

  5. Pingback: Implementing CI using Jenkins and UFT | Automation Insights·

  6. When I use your solution and my destructor is called, UFT goes to the vbscript debugger (another window all together) and I cannot seem to get the code to run. However, it seems to work fine in the constructor.

    Like

    • Hi Russ,

      UFT will open the script debugger sometimes, but you can right click and close it. Once its closed, UFT will generate the results and end the executions. Script debugger will only appear, if you are running UFT in visible mode, if UFT is launched using AOM or Jenkins, it will work fine.

      Let me know, if you have any other questions.

      Like

Leave a comment