http://qs1969.pair.com?node_id=131146

brianarn has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Sometimes I feel guilty asking questions in this forum, as I'm of the mindset that "If I can't find it on my own, something's wrong" - but I'm not finding it, so here goes.

I'm being asked to do some 'Quality Assurance' on some applications, and it's pretty routine stuff for each test - so I'd like to try and write a script to make my life easier.

I'd like to write a script (preferably with some sort of GUI - TK is okay, or Win32::GUI as I'm stuck to Win systems for this) that will do some key capture, as well as take of the I/O of some of the different windows (for example, simulate the click of a mouse button in a certain form).

I've heard the term 'gremlin' used before for something like this - that essentially sends random clicks and keystrokes to a GUI app - I'm not necessarily wanting to just hammer away at this, but having it take care of my mundane processes would be great. :)

TIA,
~Brian

Replies are listed 'Best First'.
Re: Application for 'Quality Assurance'
by jmcnamara (Monsignor) on Dec 12, 2001 at 15:21 UTC

    If you wish to test a Windows application then you should have a look at Win32::GuiTest. It allows you to send keystrokes and mouse events to Windows applications.

    An impressive module.

    Update: Just to clarify, Win32::GuiTest uses SendKeys as described by clemburg below.

    --
    John.

Re: Application for 'Quality Assurance'
by clemburg (Curate) on Dec 12, 2001 at 15:34 UTC

    The functionality you are looking for when sending is called SendKeys on Windows. I don't know about capturing output from GUI programs, but I would probably try to cut and paste it into the clipboard, and from there to a logfile opened in notepad or something like this.

    For an extremely simple-minded approach to this, you might try this little sendkeys script (in VB Script) I wrote a while ago to automate little tasks in GUI apps, e.g., when working from a Makefile (hacking Visual Basic in Emacs is crap without the usual compile buffer ... ).

    The syntax for special keys can be found on pages 213-214, table 7.2 of Windows Script Host. I added a directive {DELAY milliseconds}, where milliseconds is an integer number of milliseconds to sleep. This can be used to make the script wait for a certain time, which is vital when automating GUI programs on Win32 platforms.

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

Re: Application for 'Quality Assurance'
by bmccoy (Beadle) on Dec 12, 2001 at 07:59 UTC
    Is this something you can use Expect for, to automate test scenarios? You may not even need to use a GUI, just a defined set of tests that hit every public function of your software and/or API.

    -- Brett

    Go not to the Elves for counsel, for they will say both no and yes

      Well, if it's simple API testing one's after, the Test::Harness module will do that perfectly well. Better are the Test-Unit suite of modules which do more solid unit testing, but Test::Harness is easier to use, and every CPAN module and its mother uses it, so there's plenty of examples.

      If you're trying to approximate real-world situations, repeat after me:

      "Nothing compares to real-world data."

      Don't try to test what happens in the real world. Beta it and use that data instead. Nothing you do will more closely approximate how your application is used.

Re: Application for 'Quality Assurance'
by em (Scribe) on Dec 12, 2001 at 09:14 UTC
    What kind of application are you trying to test? Web based or regular binary application (i.e. Word or Excel)? If you are testing a web application, you can write Perl scripts (using LWP) to GET/POST pages and do things like check for minimum page size or required text. It is possible to script IE with Perl or VBScript to automate some testing. If you are testing binary application, you'll need something to send events (i.e. key presses/mouse clicks) to the application. If your company can afford it, it might be better to consider purchasing an automated testing tool (WinRunner from Mercury Interactive or SilkTest from Segue) and use that instead of Perl. I'm stuck in a situation where I need to do testing, but the company I work for can't/won't purchase the tools I need to effectively test the application. I'm using Perl to do what I can, but I'd be more effective if I had the right tool for the job.
      If you are looking to automate tests of web-based applications, you might have a look at the HTTP::Monkeywrench (Mw). Mw lets you simulate a set of "clicks" around a website, passing parameters to forms, testing the output against regular expressions, etc. Written by the good folks at cnation. I have just released a beta module called HTTP::TestEngine to sourceforge that provides a macro recorder utility for Mw. Basically you can just click around your site, and TestEngine will record your clicks, the parameters you submit etc. You can then write a Mw script to "playback" your session. Requires mod_perl and a couple of modules from CPAN. -Chris Brooks
Re: Application for 'Quality Assurance'
by Deal-a-Neil (Initiate) on Dec 12, 2001 at 20:16 UTC
    Brian - whilst the others help you with your quality assurance and testing issues, I think it would not be such a bad idea for me to help you work through these feelings of guilt. ;-)
Re: Application for 'Quality Assurance'
by Anonymous Monk on Dec 13, 2001 at 00:58 UTC
    Does win32::GuiTest support java? I read the docs for that module, and it looks like it would do EXACTLY what I need, if it supports java (i.e. the ability to push named buttons, etc.).

    I would go try it myself, but I am not sure what the syntax would be in the windows world. And I am on a Linux box right now and it would be a PITA to get to a windows box... but it would be worth it if I could script out all of the events!

Re: Application for 'Quality Assurance'
by Rudif (Hermit) on Dec 13, 2001 at 05:03 UTC
    I played recently with Win32::GuiTest. A neat module by Ernesto Guisado, already mentioned by monks. Great for sending random keystrokes and mouse clicks to your application. It does not offer, however, any help with learning the mouse moves or clicks (neither do I :-).

    The script below is a demo monkey test, that beats on the Windows Calculator app.

    When you start this kind of script on your machine, you may have a problem: it takes over your mouse cursor when sending the mouse clicks. If you programmed it for an hour's worth of clicks, started it, and then you changed your mind, how would you stop it?

    I kludged a solution based on the Term::ReadKey module, which I wrapped in my ReadKeypress module, below. The script frequently checks whether you pressed the Esc key, and to do that, it activates the comand window from which you launched the script, and calls the nonblocking ReadKeypress::escape().

    If anyone knows of a more elegant solution for this problem, please let me know.

    Rudif

    Anyway, here goes the demo

Re: Application for 'Quality Assurance'
by Anonymous Monk on Dec 13, 2001 at 05:16 UTC
    Another third party Windows app that you may find useful is AutoIt (http://www.hiddensoft.com/AutoIt/).

    I don't think it can do the key capturing that you're after though. It's scripting is quite powerful and flexible and can accomodate repetitive tasks quite well. It has a good user base of support and active discussion groups too. Even though it's not Perl, it would not be hard to have a Perl script to generate the content of AutoIt scripts.