没有UIMAP的编码UI测试

时间:2014-06-03 04:53:51

标签: testing coded-ui-tests

我在编码的ui项目中工作过。我试图在没有UIMAP的情况下对ui测试进行编码。在此要求中使用c#中的以下代码。

[TestMethod]
public void CodedUITestMethod1()
{
    var app = ApplicationUnderTest.Launch("C:\\Windows\\System32\\calc.exe", "%windir%\\System32\\calc.exe");
    WinWindow calWindow = app.SearchFor<WinWindow>(new { Name = "Calculator" },new { ClassName = "CalcFrame" });
    WinButton buttonAdd = calWindow.Container.SearchFor<WinButton>(new { Name = "Add" });
    WinButton buttonEqual = calWindow.Container.SearchFor<WinButton>(new { Name = "Equals" });
    WinButton button1 = calWindow.Container.SearchFor<WinButton>(new { Name = "1" });
    WinButton button2 = calWindow.Container.SearchFor<WinButton>(new { Name = "2" });
    WinButton button3 = calWindow.Container.SearchFor<WinButton>(new { Name = "3" });
    WinText txtResult = calWindow.Container.SearchFor<WinText>(new { Name = "Result" });

    //do all the operations
    Mouse.Click(button2);
    Mouse.Click(buttonAdd);
    Mouse.Click(button3);
    Mouse.Click(buttonEqual);

    //evaluate the results
    Assert.AreEqual("5", txtResult.DisplayText);

    //close the application
    app.Close();
}

我已经提到了以下dll&#39;

Microsoft.VisualStudio.QualityTools.CodedUITestFramework
Microsoft.VisualStudio.QualityTools.UnitTestFramework
Microsoft.VisualStudio.TestTools.UITest.Common
Microsoft.VisualStudio.TestTools.UITest.Extension
Microsoft.VisualStudio.TestTools.UITesting

但是,在代码之上会引发错误,如

'Microsoft.VisualStudio.TestTools.UITesting.ApplicationUnderTest' does not contain a definition for 'SearchFor' and no extension method 'SearchFor' accepting a first argument of type 
'Microsoft.VisualStudio.TestTools.UITesting.ApplicationUnderTest' could be found (are you missing a using directive or an assembly reference?)

我不知道这是什么问题。请帮忙完成这项任务。 在此先感谢。

1 个答案:

答案 0 :(得分:2)

Coded UI solution structure

CodedUIExtension File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UITesting;

namespace CodedUITest
{
public static class CodedUIExtension
{

    public static T SearchFor<T>(this UITestControl _this, dynamic searchProperties, dynamic filterProperties = null) where T : UITestControl, new()
    {
        T ctrl = new T();
        ctrl.Container = _this;

        IEnumerable<string> propNames = ((object)searchProperties).GetPropertiesForObject();
        foreach (var item in propNames)
        {
            ctrl.SearchProperties.Add(item, ((object)searchProperties).GetPropertyValue(item).ToString());
        }
        object s = filterProperties;

        if (s != null)
        {
            propNames = ((object)filterProperties).GetPropertiesForObject();
            foreach (var item in propNames)
            {
                ctrl.SearchProperties.Add(item, ((object)filterProperties).GetPropertyValue(item).ToString());
            }
        }

        return ctrl as T;


    }

    private static IEnumerable<string> GetPropertiesForObject(this object _this)
    {
        return (from x in _this.GetType().GetProperties() select x.Name).ToList();
    }

    private static object GetPropertyValue(this object _this, string propName)
    {
        var prop = (from x in _this.GetType().GetProperties() where x.Name == propName select x).FirstOrDefault();
        return prop.GetValue(_this);
    }



}

}

TestMethod的

[TestMethod]
    public void CodedUITestMethod1()
    {

        try
        {
            //ProcessStartInfo processStartInfo = new ProcessStartInfo(@"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CodedUITestBuilder.exe ");
            //processStartInfo.Arguments = @"/standalone";
            //ApplicationUnderTest app = ApplicationUnderTest.Launch(processStartInfo);

            ApplicationUnderTest app = ApplicationUnderTest.Launch("C:\\Windows\\System32\\calc.exe", "%windir%\\System32\\calc.exe");
            WinWindow calWindow = new WinWindow();
            calWindow = app.SearchFor<WinWindow>(
                /* pass search properties */
 new { Name = "Calculator" },
                /*pass filter properties if needed */
  new { ClassName = "CalcFrame" });

            WinButton buttonAdd = calWindow.Container.SearchFor<WinButton>(new { Name = "Add" });
            WinButton buttonEqual = calWindow.Container.SearchFor<WinButton>(new { Name = "Equals" });
            WinButton button1 = calWindow.Container.SearchFor<WinButton>(new { Name = "1" });
            WinButton button2 = calWindow.Container.SearchFor<WinButton>(new { Name = "2" });
            WinButton button3 = calWindow.Container.SearchFor<WinButton>(new { Name = "3" });
            WinText txtResult = calWindow.Container.SearchFor<WinText>(new { Name = "Result" });

            //do all the operations
            Mouse.Click(button2);
            Mouse.Click(buttonAdd);
            Mouse.Click(button3);
            Mouse.Click(buttonEqual);

            //evaluate the results
            Assert.AreEqual("5", txtResult.DisplayText);

            //close the application
            app.Close();

        }
        catch (Exception e)
        {

        }
    }

我希望这是没有uimap的代码ui的样本