views:

221

answers:

5

I am trying to automate testing of a winform application. I am running it in the same process as the test code, so it is fairly easy to find the .Net controls and simulate user action on them. I got however stuck a bit with a message box (created using the standard MessageBox.Show method). How can I get hold of it and simulate that a button is pressed?

+2  A: 
  • codeplex.com/white - Free

  • testautomationfx.com - Commercial but very good

Ruben Bartelink
+2  A: 

You probably will have to use WinAPI calls (FindWindowEx, ect) and send a messages of LMB down and up to a button handle.

Ray
Generally UI Automation (as in stuff I've linked to) is a higher level way of doing this, even if this is valid [and I gave a +1 on that basis)]
Ruben Bartelink
Could you paste some code? I have never played with win api.
Grzenio
+1  A: 

One of the best free tools is AutoHotKey.

+4  A: 

I'd advise treating the underlying disease rather than the symptom. Take a few minutes to read these

In short, use an interface to separate out all modal dialog pop-ups - which are a pain in the neck for UI test automation. You can then substitute a mock implementation of the interface that does nothing or returns predetermined test values. The real implementation of course pops up the actual dialog modally... something like this (from the 2nd link)

public class UserInterrogator : IUserInterrogator
{
    private Form owner;

    public UserInterrogator(Form owner)
    {  this.owner = owner;    }

    public Font GetFontFromUser()  // member of the IUserInterrogator interface
    {
        FontDialog fd = new FontDialog();
        fd.ShowDialog( owner );
        return fd.Font;
    }
}

The easier approach is of course to write some code that finds the dialog and closes/kills it. I've seen some people have some success with Win32 APIs and NUnitForms ...

Gishu
A: 

You can use autoit script system.

But i am suggest to separate the GUI and implementation, because basic principle of unit testing is "unit", where unit is class that separated from other classes or real world.
This principle give you good class design and help to avoid software eruption and lot of other good stuff..

Avram