gpt4 book ai didi

testing - 没有 UIMAP 的编码 UI 测试

转载 作者:行者123 更新时间:2023-11-28 21:11:44 25 4
gpt4 key购买 nike

我在编码的 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

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

我不知道这是什么问题。请帮助完成这项任务。提前致谢。

最佳答案

Coded UI solution structure

编码的UI扩展文件

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]
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 的工作示例

关于testing - 没有 UIMAP 的编码 UI 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24007382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com