CodedUI map generated from source code

我只是一个虾纸丫 提交于 2020-01-16 00:36:42

问题


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

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).


回答1:


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

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.

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


来源:https://stackoverflow.com/questions/29262201/codedui-map-generated-from-source-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!