31 Test
(require framework/test) |
(test:keystroke #\A) (test:menu-select "File" "Save")
It is possible to load this portion of the framework without loading the rest of the framework. Use (require framework/test).
Currently, the test engine has primitives for pushing buttons, setting check-boxes and choices, sending keystrokes, selecting menu items and clicking the mouse. Many functions that are also useful in application testing, such as traversing a tree of panels, getting the text from a canvas, determining if a window is shown, and so on, exist in GRacket.
31.1 Actions and completeness
The actions associated with a testing primitive may not have finished when the primitive returns to its caller. Some actions may yield control before they can complete. For example, selecting “Save As...” from the “File” menu opens a dialog box and will not complete until the “OK” or “Cancel” button is pushed.
However, all testing functions wait at least a minimum interval before returning to give the action a chance to finish. This interval controls the speed at which the test suite runs, and gives some slack time for events to complete. The default interval is 100 milliseconds. The interval can be queried or set with test:run-interval.
A primitive action will not return until the run-interval has expired and the action has finished, raised an error, or yielded. The number of incomplete actions is given by test:number-pending-actions.
Note: Once a primitive action is started, it is not possible to undo it or kill its remaining effect. Thus, it is not possible to write a utility that flushes the incomplete actions and resets number-pending-actions to zero.
However, actions which do not complete right away often provide a way to cancel themselves. For example, many dialog boxes have a “Cancel” button which will terminate the action with no further effect. But this is accomplished by sending an additional action (the button push), not by undoing the original action.
31.2 Errors
Errors in the primitive actions (which necessarily run in the handler thread) are caught and reraised in the calling thread.
However, the primitive actions can only guarantee that the action has started, and they may return before the action has completed. As a consequence, an action may raise an error long after the function that started it has returned. In this case, the error is saved and reraised at the first opportunity (the next primitive action).
The test engine keeps a buffer for one error, saving only the first error. Any subsequent errors are discarded. Reraising an error empties the buffer, allowing the next error to be saved.
The function test:reraise-error reraises any pending errors.
31.3 Technical Issues
31.3.1 Active Frame
The Self Test primitive actions all implicitly apply to the top-most (active) frame.
31.3.2 Thread Issues
The code started by the primitive actions must run in the handler thread of the eventspace where the event takes place. As a result, the test suite that invokes the primitive actions must not run in that handler thread (or else some actions will deadlock). See make-eventspace for more info.
31.3.3 Window Manager (Unix only)
In order for the Self Tester to work correctly, the window manager must set the keyboard focus to follow the active frame. This is the default behavior in Microsoft Windows and MacOS, but not in X windows.
In X windows, you must explicitly tell your window manager to set the keyboard focus to the top-most frame, regardless of the position of the actual mouse.
31.4 Test Functions
procedure
(test:button-push button) → void?
button :
(or/c (λ (str) (and (string? str) (test:top-level-focus-window-has? (λ (c) (and (is-a? c button%) (string=? (send c get-label) str) (send c is-enabled?) (send c is-shown?)))))) (and/c (is-a?/c button%) (λ (btn) (and (send btn is-enabled?) (send btn is-shown?))) (λ (btn) (test:top-level-focus-window-has? (λ (c) (eq? c btn))))))
procedure
(test:set-radio-box! radio-box state) → void?
radio-box : (or/c string? regexp? (is-a?/c radio-box%)) state : (or/c string? number?)
procedure
(test:set-radio-box-item! entry) → void?
entry : (or/c string? regexp?)
procedure
(test:set-check-box! check-box state) → void?
check-box : (or/c string? (is-a?/c check-box%)) state : boolean?
procedure
(test:set-choice! choice str) → void?
choice : (or/c string? (is-a?/c choice%)) str : (or/c string? (and/c number? exact? integer? positive?))
procedure
(test:set-list-box! choice str) → void?
choice : (or/c string? (is-a?/c list-box%)) str : (or/c string? (and/c number? exact? integer? positive?))
procedure
(test:keystroke key [modifier-list]) → void?
key : (or/c char? symbol?)
modifier-list : (listof (symbols 'alt 'control 'meta 'shift 'noalt 'nocontrol 'nometea 'noshift)) = null
Note: To send the “Enter” key, use #\return, not #\newline.
#\? #\: #\~ #\\ #\| #\< #\> #\{ #\} #\[ #\] #\( #\) #\! #\@ #\# #\$ #\% #\^ #\& #\* #\_ #\+
procedure
(test:menu-select menu items ...) → void?
menu : string? items : (listof string?)
procedure
(test:mouse-click button x y [modifiers]) → void?
button : (symbols 'left 'middle 'right) x : (and/c exact? integer?) y : (and/c exact? integer?)
modifiers : (listof (symbols 'alt 'control 'meta 'shift 'noalt 'nocontrol 'nometa 'noshift)) = null
On the Macintosh, 'right corresponds to holding down the command modifier key while clicking and 'middle cannot be generated.
Under Windows, 'middle can only be generated if the user has a three button mouse.
procedure
(test:run-interval msec) → void?
msec : number? (test:run-interval) → number?
parameter
(test:current-get-eventspaces func) → void? func : (-> (listof eventspace?))
procedure
(test:new-window window) → void?
window : (is-a?/c window<%>)
procedure
(test:close-top-level-window tlw) → void?
tlw : (is-a?/c top-level-window<%>)
procedure
procedure
procedure
(test:run-one f) → void?
f : (-> void?)
parameter
(test:use-focus-table) → (or/c boolean? 'debug)
(test:use-focus-table use-focus-table?) → void? use-focus-table? : (or/c boolean? 'debug)