如何启动Word进行重复的UI测试?

时间:2012-02-29 23:35:34

标签: c# testing ms-word coded-ui-tests

我是一个测试团队,我们正在为Word插件编写测试。我们有一个启动单词方法,我们在每次测试之前重新启动。它适用于单个测试,但是当我们将它们放入有序测试时,它会引发异常。这就是我们之前所拥有的:

if (!Playback.IsInitialized)
{
     Playback.Initialize();
}

// Launch '%ProgramFiles%\Microsoft Office\Office12\WINWORD.EXE'
ApplicationUnderTest wINWORDApplication = ApplicationUnderTest.Launch(exePath, altPath);

if (killAllPreviousWordProcesses == true)
{
       wINWORDApplication.Maximized = true;
}

环顾四周之后,我们发了一篇建议使用Process.Start()而不是ApplicationUnderTest的帖子。这是他们建议的代码。

Process np = Process.Start(@"C:\Windows\System32\Notepad.exe");
while (np.MainWindowHandle == IntPtr.Zero)
  {
    System.Threading.Thread.Sleep(100);
  }

 //This line throws the error
 WinWindow npWindow = UITestControlFactory.FromWindowHandle(np.MainWindowHandle) as WinWindow;

 MessageBox.Show(npWindow.Name);
 ApplicationUnderTest aut = ApplicationUnderTest.FromProcess(np);
 MessageBox.Show(aut.Title);

我们将其更改为使用Word(用“WINWORD”替换字符串,我们也尝试了路径)但现在每次运行方法时都会得到NullReferenceException。我已经检查了所有的null,并确保np.MainWindowHandle不是零,但它仍然给我错误。有任何想法来解决这个或其他建议吗?

堆栈跟踪:

at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.FindFromWindowHandle(IntPtr windowHandle)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl..ctor(IntPtr windowHandle)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FromWindowHandle(IntPtr windowHandle)
at Microsoft.VisualStudio.TestTools.UITesting.UITestControlFactory.FromWindowHandle(IntPtr windowHandle)
at Common.BaseUIMapClasses.BaseUIMap.startWord(Boolean killAllPreviousWordProcesses, Boolean maximizeWord, String exePath, String altPath) in C:\Source1\Common\BaseUIMap.cs:line 170

2 个答案:

答案 0 :(得分:3)

您应该检查this post

答案 1 :(得分:0)

非常感谢Nikola。我看到问题已经解决,但我想分享我有趣的案例。我们假设我有一个测试类,它是基类。基类定义了一些ClassInitialize个动作。测试类具有CodedUITest属性,但基类也具有属性 - [TestClass]。这打破了一切。

[CodedUITest]
public class ProjectTabTests : CodedUIFunctionalTestBase
{ 
    [TestInitialize]
    public override void SetUp()
    {
        base.SetUp();
    }

    [TestMethod]
    public void Test()
    {
    }
}

[TestClass] // this broke everything! remove it!
public class CodedUIFunctionalTestBase
{
    public virtual void SetUp()
    {
        KillProcess(Constanst.ProcessName);

        Playback.PlaybackSettings.SmartMatchOptions = SmartMatchOptions.None;
        Playback.PlaybackSettings.LoggerOverrideState = HtmlLoggerState.AllActionSnapshot; // here I got internal NullReferenceException
        Playback.PlaybackSettings.SearchTimeout = 10000;
        Playback.PlaybackSettings.WaitForReadyTimeout = 10000;
        Playback.PlaybackSettings.ThinkTimeMultiplier = 1;
        Playback.PlaybackSettings.MaximumRetryCount = 3;

        Application = ApplicationUnderTest.Launch(Constants.Application);
    }
}

希望这篇文章可以帮助某人。