本文介绍了没有UIMAP的编码UI测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在ui编码项目中工作。我试图在不使用UIMAP的情况下对ui测试进行编码。在此要求下,使用c#中的以下代码。
I have working in coded ui project. I have trying to coded ui test without UIMAP.In this requirement using following code in 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
但是代码引发错误,例如
But,Above the code raise the error like
'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?)
我不知道这是什么问题。请帮助完成此任务。
提前致谢。
I don't know what is this issue. Please help for this task.Thanks in Advance.
推荐答案
CodedUIExtension File
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]
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的工作示例
I hope this is working sample for code ui without uimap
这篇关于没有UIMAP的编码UI测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!