Interview Questions

WinRunner: How to access the last window brought up?

Mercury WinRunner FAQ


(Continued from previous question...)

WinRunner: How to access the last window brought up?

Q; WinRunner: How to access the last window brought up? [set_window("{class: window, active: 1}");
rc = win_get_info("{class: window, active: 1}", property, result);
Is there something or some script that can determine the LAST WINDOW DISPLAYED or OPENED on the desktop and in order to use that information to gather the label.


there are a couple of solutions, depending on what you know about the window. If you know distinguishing characteristics of the window, use them and just directly describe the gui attributes. I assume that you do not have these, or you would likely have already done so.
If not, there is a brute force method. Iterate over all of the open windows prior to the new window opening and grab their handles. After your new window opens, iterate again. The 'extra' handle points to your new window. You can use it in the gui description directly to manipulate the new window. As I said, a bit brutish, but it works. You can use the same technique when you have multiple windows with essentially the same descriptors and need to iterate over them in the order in which they appeared.
Any object (or window) can be described by it's class and it's iterator. Ask yourself, if I wanted to address each of the individuals in a room and had no idea what their names were, but would like to do so in a consistent way would it not be sufficient to say - 'person who came into the room first', 'person who came into the room second', or alternately 'person who is nearest the front on the left', 'person who is second nearest the front on the left'. These are perfectly good ways of describing the individuals because we do two things: limit the elements we want to describe (people) and then give an unambiguous way of enumerating them.
So, to apply this to your issue - you want to do an 'exist' on a dynamically described element (window, in your case). So you make a loop and ask 'window # 0, do you exist', if the answer is yes, you ask for the handle, store it and repeat the loop.
Eventually you get to window n, you ask if it exists, the answer is no and you now have a list of all of the handles of all of the existing windows.. You should note that there will be n windows ( 0 to n-1, makes a count of n).
You may need to brush up on programmatically describing an object (or window), the syntax is a little lengthy but extremely useful once you get the feel for it. It really frees you from only accessing objects that are already described in the gui map.
Try this as a starting point, you'll need to add storing & sorting the handles yourself:
i = 0;
finished = FALSE;
while (finished == FALSE)
{
if (win_exists("{class: window, location: \"" & i & "\"}\"") == E_OK )
{
win_get_info("{class: window, location: \"" & i & "\"}\"", "handle", handle);
printf(" handle was " & handle);
i ++;
}
else
{
finished = TRUE;
}
}

(Continued on next question...)

Other Interview Questions