参考:
http://ptfrontline.wordpress.com/2010/06/22/overriding-lr-functions-with-custom-code/
Most people do not know that LR supports overriding existing functions with custom ones in a very easy way.
I had a situation where I had a pre-made script that had fixed think-time values for the lr_think_time() calls, and they of course were completely wrong. So my problem was that I had a lot of lr_think_time() places and I didn’t want to go through them all myself.
In most cases a search-and-replace would do the trick, but I stumbled upon the overriding of functions and implemented it that way instead, making my changes to the script stay in one place only instead of manipulating all the actions in the script.
Remember: DO NOT CALL THE OVERRIDDEN FUNCTION INSIDE THE NEW CODE!!! – You’ll just end up with an endless loop, and terminate in stack-errors or similar…
Here’s an empty script where I’ve overridden the lr_think_time() function with a custom one:
01 |
#define lr_think_time my_think_time // This define overrides lr_think_time() with my_think_time() |
03 |
double my_think_time( double sec) |
08 |
x = ((50.0+ rand ()%100)/100)*sec; |
10 |
lr_force_think_time( x ); |
One could get creative and start overriding all sorts of functions with this. An example would be to set specific headers for specific web_url() calls depending on the params. You could override the web_url() call, examine the parameters and add any needed HTTP headers before doing a web_custom_request() instead ..
Enjoy!
-----------
软件性能测试工作室:提供性能测试咨询、培训和项目指导
(QQ: 2225045276 E-Mail: Testing_is_believing@126.com)