codedUI地图从源头code产生 [英] CodedUI map generated from source code

查看:134
本文介绍了codedUI地图从源头code产生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有使用的WinForms源$ C ​​$ C文件生成的UIMap的一种方式? (了.Designer.cs)

Is there a way of generating a UIMap using a WinForms source code file? (.Designer.cs)

如果是这样,可以从源生成的文件使用特性(如公共)? (因此不是在C $ cdUI的$使用硬codeD串并生成编译器错误如果控制被删除)。

If so, can the generated file use properties (if public) from the source? (Therefore not using hardcoded strings and generating compiler errors in the CodedUI if a control gets removed).

推荐答案

通过产生过会失去你所有录制的操作生成设计文件。我建议你​​分离出来你的UIMap使用多个。 我一直在使用T4来生成类似的东西使用奎特框架,以便有逻辑分离。因此,它会产生2设计器文件,根据该文件中的3导航,并有可能在codeD的UI测试调用文件。如1 login.Designer.cs - > 2. loginPage.Designer.cs - > 3. Navigation.Designer.cs

Generating a Designer file by generating over it will loose all your recorded actions. I would suggest you separate out your UIMap to use multiple . I've been using a T4 to generate from something similar using the CUITe framework so there is logical separation. So it will generate the 2. designer file, the 3. navigation based on that file, and possibly the Coded UI Test to call the file. such as 1. login.Designer.cs - > 2. loginPage.Designer.cs - > 3. Navigation.Designer.cs

注:如果您使用的是设计师的文件作为输入,这也应该是你的输出,所以它是很清楚,任何人不得更改这些文件。因此,任何试图从这个优化将必须在一个单独的CS文件

Note: If you are using a designer file as your input, this should also be your output, so it is very clear that no one should change these files. Thus any attempt to optimize from this will have to be in a separate cs file.

,以便您可以自定义,让你到第2步模板低于 当然,你将不得不通过添加现有项目

so a template that you can customize to get you to step 2 is below you will of course have to add in to the csproj manually for each file through "Add existing items"

<#@ 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;
    }
#>

这篇关于codedUI地图从源头code产生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆