Programatically finding a message box and generati

2020-07-23 06:51发布

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?

6条回答
Animai°情兽
2楼-- · 2020-07-23 07:01

If you know the caption (and it is unique) you can loop through Application.OpenForms to find it.

查看更多
在下西门庆
3楼-- · 2020-07-23 07:02

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 ...

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-07-23 07:06

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..

查看更多
Viruses.
5楼-- · 2020-07-23 07:11

One of the best free tools is AutoHotKey.

查看更多
够拽才男人
6楼-- · 2020-07-23 07:15

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

查看更多
Fickle 薄情
7楼-- · 2020-07-23 07:19
  • codeplex.com/white - Free

  • testautomationfx.com - Commercial but very good

查看更多
登录 后发表回答