June 1, 2008

WinRunner and why I like TSL


I like TSL. Not because of its lack of classes, structs, function pointers or other fancy eighties C stuff, but because you can write powerful code on a single line. I like it even more since I am now developing my test automation in QTP. Yeah, that's right, the tool that uses crippled Visual Basic Script.

One nice thing in WinRunner is that you can use assignments inside functions or validations like:
for(i = 0; (rc = obj_exists(thisButton = “{class: push_button, location:”i”}”)) == E_OK; ++i)
    button_press(thisButton);
pause(i-- “ button” (i != 1 ? “s were” : “ was”) “ pressed.”);

The three line above contains interesting code, I'll walk through it.

First, this contains an assignment in function:
obj_exists(thisButton = “{class: push_button, location:”i”}”);

And second, an assignment on validation like:
(rc = obj_exists(myObj)) == E_OK
thisButton = “{class: push_button, location:”i”}” is replaced by myObj to make it more readable.

Notice the parenthesis around the assignment; Normally assignments have a lower precedence, that is why we need it.
(Explanation:
If you use code like this:
rc = obj_exists(myObj) == E_OK
first obj_exists(myObj)) == E_OK is evaluated to TRUE or FALSE. Since rc gets that value assigned, rc becomes FALSE in case of a none existence of the object and TRUE otherwise, making the statement as buggy as hell; FALSE and E_OK both have the same value: 0, causing rc getting a E_OK value in case of a not existing myObj.)

The last line contains a short cut for an if else statement:
(validation ? value if true : value if false)
This is great when you want to write fast code in a (log)message. Otherwise the same line would take you at least four lines if you want to state it properly.
1. if (i == 1)
2.    
pause("1 button was pressed.”);
3. else
4.    pause(i "buttons were pressed.");

The code in the example above is nothing worth in the sense of readability, but more to show the power of writing C-like lines of code. I would challenge the same functionality in a Visual Basic script like language. I think it will be 5 times more lines of code and a lot of debugging, since it is easily forgotten that VB(S) functions in "for" statements are evaluated only once. More on this another time.

No comments: