C# 手动编码 UI 测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17238253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hand Coding Coded UI Tests
提问by Jeff Finn
Hi I am looking at using Coded UI Tests (CUIT) to test an application. I have tried the recording option and this is not flexible enough for me. If you use it on a different size screen it breaks.
嗨,我正在考虑使用编码 UI 测试 (CUIT) 来测试应用程序。我尝试了录音选项,但这对我来说不够灵活。如果您在不同尺寸的屏幕上使用它,它会损坏。
I know you can hand code the tests but I cannot find any good examples of how to write a basic test. There are examples on here that use CUITe but these posts are from 2011 and I'm not sure how relevant they are anymore with the new upgrades to CUIT from Microsoft.
我知道你可以手工编写测试代码,但我找不到任何关于如何编写基本测试的好例子。此处有使用 CUIT 的示例,但这些帖子来自 2011 年,我不确定它们与 Microsoft 对 CUIT 的新升级的相关性。
These tests need to be integrated with my build environments in Visual Studio 2012 Ultimate, that is why I'm not using Selenium.
这些测试需要与我在 Visual Studio 2012 Ultimate 中的构建环境集成,这就是我不使用 Selenium 的原因。
And Code samples or links to good tutorials would be appreciated, but in particular I am looking for an exampl on how to start hand coding my CUITs
并且代码示例或良好教程的链接将不胜感激,但特别是我正在寻找有关如何开始手动编码我的 CUIT 的示例
采纳答案by Kate Paulk
The Code First coded UI test API project on CodePlex (http://codeduicodefirst.codeplex.com/) includes a project demo that you can download - application and tests. It's designed for building CUIT tests without the dependency on record/playback.
CodePlex ( http://codeduicodefirst.codeplex.com/)上的 Code First 编码 UI 测试 API 项目包括一个项目演示,您可以下载 - 应用程序和测试。它专为构建 CUIT 测试而设计,不依赖于记录/回放。
The biggest thing that you need if you're going to work on a code-only basis is a way to avoid the dependency on the autogenerated object map the CUIT recordings create. The Code-First project uses classes mapped to individual page objects to work around this - you'd need to extend the project code to work with desktop applications if I remember correctly.
如果您打算在纯代码的基础上工作,您需要做的最重要的事情是避免依赖于 CUIT 记录创建的自动生成的对象映射。Code-First 项目使用映射到单个页面对象的类来解决这个问题 - 如果我没记错的话,您需要扩展项目代码以使用桌面应用程序。
(I am not affiliated with this project in any way - it's just the only hand-coding resource other than CUITe that I've found, and CUITe hasn't been updated in a while, the last I saw).
(我与这个项目没有任何关系——它只是我发现的除了 CUITe 之外唯一的手工编码资源,而且 CUITe 有一段时间没有更新了,这是我最后一次看到的)。
回答by bitbonk
Here is a video showing how to do Code First coded UI tests:
这是一个视频,展示了如何进行 Code First 编码的 UI 测试:
回答by Jowen
Not a lot of developers know this, but it's possible to create Code First tests with CodedUI. It's not being advocated, which is bad imo. I consider the Recording option as a fragile one. It uses mouse coords which means you have to recreate the tests when the UI changes...
没有多少开发人员知道这一点,但可以使用 CodedUI 创建 Code First 测试。它没有被提倡,这是不好的imo。我认为录音选项是一个脆弱的选项。它使用鼠标坐标,这意味着您必须在 UI 更改时重新创建测试...
The maintainable way would be to use the Page Object pattern(also used by other popular tools like Selenium). This creates an abstraction of the UI which gives you more flexibility and strong typing.
可维护的方法是使用页面对象模式(也被其他流行的工具如 Selenium 使用)。这创建了 UI 的抽象,为您提供更多的灵活性和强类型。
You get easy, readable and most of all maintainable code:
您将获得简单、可读且最重要的是可维护的代码:
var storeHyperlink = new HtmlHyperlink(_browserWindow);
storeHyperlink.SearchProperties[HtmlHyperlink.PropertyNames.Id] = "StoreLink";
Mouse.Click(storeHyperlink);
回答by Garry
Not sure if someone is still looking to find out how to best hand code the Coded UI tests but imo going to the record and playback route is going to be disappointing later on! Best way is to create an automation framework which defines individual objects which you want to interact with and have the page objects to handle your business logic. If you are testing web applications you can define objects using generic UITestControls or HtmlControls. For example:
不确定是否有人仍在寻找如何最好地手动编码编码 UI 测试,但 imo 进入记录和播放路线以后会令人失望!最好的方法是创建一个自动化框架,它定义要与之交互的单个对象并让页面对象来处理您的业务逻辑。如果您正在测试 Web 应用程序,您可以使用通用 UITestControls 或 HtmlControls 定义对象。例如:
public static UITestControl EditBox_Password
{
get
{
if ( mEditBox_Password == null || ! mEditBox_Password.Exists )
{
mEditBox_Password = new UITestControl (browserWindow );
mEditBox_Password.TechnologyName = "Web";
mEditBox_Password.SearchProperties.Add (UITestControl.PropertyNames.ControlType , "Edit");
mEditBox_Password.SearchProperties.Add ( UITestControl.PropertyNames.Name , "TxtPassword");
}
return mEditBox_Password ;
}
}
If you are testing Windows-based applications then you can define objects using WinControls or WpfControls.
如果您正在测试基于 Windows 的应用程序,那么您可以使用 WinControls 或 WpfControls 定义对象。
I recently bought a book on Amazon (Hand Coding Coded UI) which clearly defines how to setup the framework and create an easily maintainable code. Not sure if it is available in any book store but here is the link on Amazon if you want to have a look
我最近在亚马逊上买了一本书(Hand Coding Coded UI),它清楚地定义了如何设置框架和创建易于维护的代码。不确定它是否在任何书店都有售,但如果您想看一看,这里是亚马逊上的链接
I hope it is helpful.
我希望它有帮助。
Updated: Just googled it and there is a discount code for the book at http://www.arkenstone-ltd.com/testing-with-coded-ui/
更新:刚刚在谷歌上搜索它,这本书的折扣代码位于http://www.arkenstone-ltd.com/testing-with-coded-ui/

