Automated regression testing of GUIs written with gtk+



Hi,
There doesn't seem to have been much discussion of automated testing of applications written using gtk+. I have

written a small example which exploits the gtk_key_snooper_install() function. The installed snooper records keypresses storing them in a file. On playback the snooper function substitutes keypresses from the file for the

keypresses actually made by the tester. (So this is not quite automated, unless you put a small lead weight on the Enter key!)

I have two main questions:

1) would it be possible to have a gtk_event_snooper_install() function to allow a similar stunt to be performed on more general events - I wrote such a thing (for Tcl/Tk) about five years ago, it was quite a hairy experience. Any takers?

2) can I get rid of my small lead weight? That is, would something like gtk_main_do_event() or XSendEvent() enable me to truly automate the tests?

Attached is a small example of such a snooper function for those who prefer C to English - it doesn't cope with sending keypresses too fast for the window drawing functions to keep up, but it gives the idea. It is GPL of course.

Richard Shann






------------------------------------------------------------------------

/* snooper function installed by gtk_key_snooper_install (snooper, testfilename)
* if a file exists of name testfilename then it supplies keypresses to substitute
* for the ones typed by the user - ie it "plays back" the test. If not it creates
* a new test by storing each keypress from the user into a file of that name.
* In this version the speed of playback is limited, otherwise keypresses may be sent
* to the wrong window.
*/
#include <stdio.h>
#include <gtk/gtk.h>
static gint snooper(GtkWidget *grab_widget, GdkEventKey *event, gpointer func_data)
{
 static FILE *fp; /* file containing keypress events */
 static gboolean recording = FALSE; /* we are recording keystrokes */
 GdkWindow *window;
 guint32 time;
 gchar *string;
 gchar *testfile = (gchar*)func_data;/* name of test file containing keypresses */
window=event->window;
 time=event->time;
 if(!fp)
   fp=fopen(testfile, "r");
 if(!fp) /* it is a new file, so we are recording a new test */ {
   fp=fopen(testfile, "w");
   recording = TRUE;
 }
 g_assert(fp); /* can't open the file */
 if(recording) {
   fwrite(event,sizeof(*event), 1, fp);
   fwrite(event->string, event->length, 1, fp);
 } else /* playing back keystrokes from file */ {
   fread(event,sizeof(*event), 1, fp);
   string = (gchar*)g_malloc(event->length+1); /* memory leak? - ok for test rig */
   g_assert(string);
   fread(string, event->length, 1, fp);
   *(string+event->length) = 0; /* null terminate */
   /* freshen the stale pointers and time */
   event->window = window;
   event->time = time;
   event->string = string;
 }
return 0; /* allow the keypress to be acted on normally */
}






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]