Sign In/My Account | View Cart  
advertisement


Listen Print Discuss

Automated GUI Testing
by George Nistorica | Pages: 1, 2, 3, 4

After sending the right combination of keys (Alt+F, O), the code expects that the window containing the Button will pop up. Then it uses FindWindowLike() again, using as a search item the title of the window containing the button (in this case, here). Remember what I said about the windows hierarchy?

Next, it ensures that the Button window has the focus, although this is not entirely necessary at this point. After bringing the window to the front, the code searches for a button in the window (I already know that there's only one button there).

@button = FindWindowLike( $window[0], "", "Button" );

This narrows down the search: "Search for a window of the class Button under the window that has the ID $window[0]," the window having the ID in $window[0] having been previously found by its title.

PushChildButton( $window[0], $button[0], 0.25 );

is here just for the power of example, as it doesn't work for the Tk button. It would work for a native Win32 button.

The trick is that the code can still push it using the mouse! Having the button ID, as returned by FindWindowLike(), the code calls the click_on_the_middle_of_window function.

sub click_on_the_middle_of_window {
    my $window = shift;
 
    print "* Moving the mouse over the window id: $window\n";
 
    my ( $left, $top, $right, $bottom ) = GetWindowRect($window);
 
    MouseMoveAbsPix( ( $right + $left ) / 2, ( $top + $bottom ) / 2 );
 
    sleep(1);
 
    print "* Left Clicking on the window id: $window\n";
    SendMouse("{LeftClick}");
    sleep(1);
}

The function takes a window ID as its parameter, searches its rectangle using GetWindowRect(), and then moves the mouse pointer right in the middle of it with MouseMoveAbsPix().

With the pointer over the button, sending LeftClick presses the button.

Moving Around with the Mouse

As seen earlier, moving the mouse is straightforward: just use MouseMoveAbsPix(). It takes as parameters the coordinates where you want the pointer to be (horizontal and vertical positions) in pixels.

It is useful to use other two functions in conjunction: SendMouse() and GetWindowRect().

SendMouse sends a mouse action to the Desktop. It takes only one parameter: a mouse action such as {LeftDown}, {LeftUp}, or {LeftClick}. For more details, see the function's documentation.

You can also move the mouse wheel using MouseMoveWheel(). It takes a positive or a negative argument, indicating the direction of the motion.

To send an action, you need to know where we send it. Usually you will move the mouse pointer over a window. GetWindowRect() is useful to find the coordinates of a window.

It can be simpler to create a wrapper around these three functions in order to move the mouse pointer over a selected window, and then generate a mouse action, as I did with click_on_the_middle_of_window().

Further Reading

Here are some links you may find useful.