如何进行编码UI测试以选择C#中的最新文件夹

时间:2012-06-29 11:08:19

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

我正在编写一个编码的ui测试,用于对每晚创建的文件夹进行操作。我希望它的工作方式是测试将选择最新的文件夹。

我该怎么做?断言?

从我使用主应用程序打开的文件夹对话框中选择文件夹。然后将文件夹的名称放在主应用程序的文本框中,并扫描补丁。

 //Expand 'Computer' -> 'network drive (X:)' -> 'great-granparent' -> 'granparent' -> 'parent' -> 'folder' tree item
 uIfolder_TreeItem.Expanded = this.Browse_For_FolderParams.UIfolder_TreeItemExpanded;

使用nXu的代码我试过这个:

 DirectoryInfo root = new DirectoryInfo(@"X:\great-granparent\granparent\parent");
        DirectoryInfo[] folders = root.GetDirectories();

        DirectoryInfo newest = folders[0];
        foreach (var dir in folders)
        {
            if (dir.CreationTime.CompareTo(newest.CreationTime) > 0)
                newest = dir;
        }


        //Expand 'Computer' -> 'network drive (X:)' -> 'great-granparent' -> 'granparent' -> 'parent' -> 'folder' tree item
        newest = this.Browse_For_FolderParams(newest);

结束代码

        DirectoryInfo root = new DirectoryInfo(@"X:\great-granparent\granparent\parent");
        DirectoryInfo[] folders = root.GetDirectories();

        DirectoryInfo newest = folders[0];
        foreach (var dir in folders)
        {
            if (dir.CreationTime.CompareTo(newest.CreationTime) > 0)
                newest = dir;
        }


        uIfolder_TreeItem.SearchProperties["Name"] = newest.Name;

        //Expand 'Computer' -> 'network drive (X:)' -> 'great-granparent' -> 'granparent' -> 'parent' -> 'folder' tree item
        uIfolder_TreeItem.Expanded = this.Get_FolderParams.UIfolder_TreeItemExpanded;

感谢您的帮助:)

1 个答案:

答案 0 :(得分:3)

使用DirectoryInfo class,它具有CreationTime,LastAccessTime和LastWriteTime属性。 例如,要在C:\ root上选择上次创建的文件夹,请使用以下代码:

DirectoryInfo root = new DirectoryInfo(@"C:\");
DirectoryInfo[] folders = root.GetDirectories();

DirectoryInfo max = folders[0];
foreach (var dir in folders)
{
    if (dir.CreationTime.CompareTo(max.CreationTime) > 0)
        max = dir;
}

// Last created directory is max