从源代码生成的CodedUI映射

时间:2015-03-25 17:15:30

标签: .net coded-ui-tests

有没有办法使用WinForms源代码文件生成UIMap? (了.Designer.cs)

如果是这样,生成的文件是否可以使用来自源的属性(如果是公共的)? (因此,如果删除控件,则不使用硬编码字符串并在CodedUI中生成编译器错误。)

1 个答案:

答案 0 :(得分:1)

通过生成一个Designer文件生成一个将丢失所有记录的操作。我建议你将你的UIMap分开使用multiple。 我一直在使用T4从类似的东西使用CUITe框架生成,因此存在逻辑分离。因此它将生成2.设计器文件,基于该文件的3.导航,以及可能的Coded UI Test来调用文件。例如1. login.Designer.cs - > 2. loginPage.Designer.cs - > 3. Navigation.Designer.cs

注意:如果您使用设计器文件作为输入,这也应该是您的输出,因此很明显没有人应该更改这些文件。因此,任何从此优化的尝试都必须在一个单独的cs文件中。

所以您可以自定义一个模板以进入第2步 您当然必须通过“添加现有项目”为每个文件手动添加到csproj

<#@ template debug="true" hostSpecific="true" #>
    <#@ output extension=".cs" #>
    <#@ Assembly Name="System.Core" #>

    <#@ import namespace="System" #>
    <#@ import namespace="System.IO" #>
    <#@ import namespace="System.Collections.Generic" #>

    <# 
    string path  = @"C:\Users\username\projectpath";
    foreach(string file in GetDesignerCSFiles(path))
    {
        List<string> controls = new List<string>();
        //get the controls protected global::... someControl;
        string [] properties = System.IO.File.ReadAllLines(file);
        string commentedOut = @"//";

        //controls you want 
        List<string> btnControl = new List<string>();
        //find the lines that have your controls
            foreach (string propertyName in properties) 
            {

                if(propertyName.Contains(commentedOut))
                    continue;
                if(str.Contains("btn"))
                {
                    //trim to just get the name of the control
                    btnControl.Add(str);
                }
            }

        }
        //get your class name assuming it's the same as the file name 
        foreach(string str in url.Split(' '))
        {   
            foreach(string s in str.Split('.'))
            {
                className = s ;
                break;
            }
        }


        GenerateFile(file,controls);


    }
    #>
//get the file and controls. set up your file content

<#+ void GenerateFile(string url, List<string> controls)
{

    string className ="";
    if(true)//generate file
    {

        foreach(string str in url.Split('/'))
        {   
            foreach(string s in str.Split('.'))
            {
                className = s ;
                className += "Page";
                break;
            }
        }

        #>

using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
using CUITe.Controls.WinControls;
namespace Project.Form
{


         public class <#=className#> : CUITe_WinWindow
        {
            public new string sWindowTitle = "<#=className#>";

            <#+
            foreach(string control in controls)
            {
                //add in your list of controls
                //such as public CUITe_WinButton btnLogin { get { return Get<CUITe_WinButton>("Id~<#=control#>"); } }
                #>

                <#+
            }
            #>
        }
}
        <#+
        SaveOutput(className + ".Designer.cs"); 
        #>
    <#+
    }
}
#>

<#+
  void SaveOutput(string outputFileName)
  {
      string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
      string outputFilePath = Path.Combine(templateDirectory, outputFileName);
      File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString()); 

      this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
  }
#>
//get the files
<#+
    public List<string> GetDesignerCSFiles(string path)
    {

        List<string> files = new List<string>();

        try
        {
            string[] fileEntries = Directory.GetFiles(path);
            foreach (string fileName in fileEntries)
                files.Add(fileName);
            string [] subdirectoryEntries = Directory.GetDirectories(path);
            foreach(string subdirectory in subdirectoryEntries)
                files.AddRange(GetDesignerCSFiles(subdirectory));
        }
        catch(Exception e)
        {
            throw new Exception("exception occurred in tt generation getting file",e );
        }
        return files;
    }
#>