以编程方式启动时,编码的UI测试无法找到WPF控件

时间:2016-06-27 15:56:46

标签: c# wpf visual-studio coded-ui-tests

在构建编码的UI地图时,我指定了需要启动的应用程序,如下所示。

Coded UI Test Map

当我运行以下测试时,编码的UI测试通过,能够找到我指定的控件。在这种情况下,它是ListViewItem

[TestMethod]
public void UserOpensAnExistingDiary()
{
    this.UIMap.OpenExistingDiary();
}

public void OpenExistingDiary()
{
    #region Variable Declarations
    WpfListItem uIPenAppsLogicModelsDiListItem = this.UIPENWindow.UIDiariesGroup.UIItemList.UIDiaryGroup.UIPenAppsLogicModelsDiListItem;
    WpfWindow uIDiaryEditorWindow = this.UIDiaryEditorWindow;
    #endregion

    // Launch '%LOCALAPPDATA%\Pen\app-5.0.6018.18517\Pen.Apps.Desktop.exe'
    ApplicationUnderTest penAppsDesktopApplication = ApplicationUnderTest.Launch(this.OpenExistingDiaryParams.ExePath, this.OpenExistingDiaryParams.AlternateExePath);

    // Double-Click 'Pen.Apps.Logic.Models.DiaryModels.Diary' list item
    Mouse.DoubleClick(uIPenAppsLogicModelsDiListItem, new Point(76, 72));

    // Wait for 1 seconds for user delay between actions; Click 'Diary' window
    Playback.Wait(1000);
    Mouse.Click(uIDiaryEditorWindow, new Point(590, 25));
}

如果我删除Launch UI操作,并以编程方式启动应用,则测试无法找到ListViewItem。唯一的区别是我删除了Launch操作,并将以下代码添加到我的测试中,因此他们会在启动窗口的情况下初始化。

[TestInitialize]
public void Setup()
{
    string appPath = ApplicationPath.GetApplicationPath();
    var app = ApplicationUnderTest.Launch(appPath);
}

有谁知道为什么会这样?

1 个答案:

答案 0 :(得分:1)

您提供的示例令人困惑的是哪些有效,哪些无效。此外,使用UI地图使得查看正在发生的事情非常困难。请添加一个失败的测试方法,并包含

的UI Map代码
this.UIPENWindow.UIDiariesGroup.UIItemList.UIDiaryGroup.UIPenAppsLogicModelsDiListItem

我的预感是,在测试失败的情况下,被测应用程序不会被用作搜索限制容器。

我会做的是改变:

[CodedUITest]
public class TestingClass
{
    WpfWindow containingWindow;
    [TestInitialize]
    public void Initialize()
    {
        this.containingWindow = ApplicationUnderTest.Launch(appPath);
    }

    [TestMethod]
    public void Test1()
    {
        WpfListItem toClick = new WpfListItem(this.containingWindow);
        // look in the UI map to see what it is doing for search properties
        // and take the simplest sub-set that makes sense
        toClick.SearchProperties.Add("AutomationId", "SomeId");

        Mouse.Click(toClick); // do not need point, typically

       /*

       //You may need to include more levels of searching,
       //but you can see what you need from the UI Map

       WpfTable table = new WpfTable(this.containingWindow);
       table.SearchProperties.Add("AutomationId", "myTableId");

       WpfListViewItem itemToClick = new WpfListViewItem(table);
       itemToClick.SearchProperties.Add("Name", "Some name");
       */
    }
}

这里的要点是列表项目正在启动窗口,因为它的容器似乎在您当前的情况下不会发生。