这周的NuGet包:专为Web应用程序的自动化测试服务的FluentAutomation

[原文发表地址] NuGet Package of the Week: FluentAutomation for automated testing of Web Applications

[原文发表时间] 2014-03-04

上周我研究了现有的针对自动浏览器测试的多样化选择。有像PhantomJS的无头的开源引擎“浏览器”和像BrowserStack的云动力的多浏览器的测试工具以及SauceLabs

image

Selenium 是一种黄金标准,不仅提供大量的“驱动程序”而且提供大量的语言绑定用来驱动浏览器。有时浏览器更新如此之快以至于会有一些版本和Selenium不相兼容,但是大多数情况下一旦你植入它,它就能很好的工作。

另一个我一直寻找的选择是FluentAutomation。它是流畅的自动化应用编程接口,支持Selenium和WatiN 连同它们所有的风格和驱动程序。自从Fluient支持Selenium,那就意味着你可以使用Selenium ChromeDriver,IEDriver,远程Web驱动程序甚至于无头的PhantomJS。GitHub上也有 FluentAutomation,当然,和NuGet上的一样

FluentAutomation有了不起的(和不断增长的)文档并且它的API的风格流畅而且有趣。

现在,不是每个人都喜欢一个“流畅的”API所以这可能需要一段时间才能习惯。通常你把你要做的事情列出很多行,虽然它真正只有一行,例如,以下这行:

 I.Open("https://automation.apphb.com/forms")       .Select("Motorcycles").From(".liveExample tr select:eq(0)")        .Select(2).From(".liveExample tr select:eq(1)")        .Enter(6).In(".liveExample td.quantity input:eq(0)")        .Expect                .Text("$197.72").In(".liveExample tr span:eq(1)")                .Value(6).In(".liveExample td.quantity input:eq(0)");
注意这个方法链接和使用CSS选择器的效果一样。

FluentAutomation还有一个很酷的页面对象的理念,用来携带你的潜在的脆弱脚本并且赋予它们更多的结构。页面对象对你的行为,期望行为,以及声明进行分类,并且当一个页面在多个测试中出现时,使你可以重用代码。

例如你有很高的测试水平(这是单元测试,但是你可以使用任何你想要使用的):

 public class SampleTest : FluentTest {       public SampleTest() {         SeleniumWebDriver.Bootstrap(SeleniumWebDriver.Browser.Chrome);       }         [Fact]      public void SearchForFluentAutomation() {          new BingSearchPage(this)              .Go()                         .Search("FluentAutomation")                        .FindResultUrl("https://fluent.stirno.com/blog/FluentAutomation-scriptcs/");    }}
然后你可以用单独的页面对象,使你拥有自己的特定于该页面的公共方法,以及你可以重用的声明。

 public class BingSearchPage : PageObject<BingSearchPage> {     public BingSearchPage(FluentTest test) : base(test) {            Url = "https://bing.com/";                At = () => I.Expect.Exists(SearchInput);       }         public BingSearchResultsPage Search(string searchText) {             I.Enter(searchText).In(SearchInput);               I.Press("{ENTER}");               return this.Switch<BingSearchResultsPage>();        }         private const string SearchInput = "input[title='Enter your search term']";} public class BingSearchResultsPage : PageObject<BingSearchResultsPage> {      public BingSearchResultsPage(FluentTest test) : base(test) {               At = () => I.Expect.Exists(SearchResultsContainer);       }         public BingSearchResultsPage FindResultUrl(string url) {           I.Expect.Exists(string.Format(ResultUrlLink, url));               return this;       }         private const string SearchResultsContainer = "#b_results";        private const string ResultUrlLink = "a[href='{0}']";}
如果你不想,你可以不用定义所有结构和面向对象。你可以用 FluentAutomation轻松的编写脚本并且朝着一个不同的方向。

伴随着 FluentAutomation的 ScriptCS = 用C# 脚本使你的浏览器自动化

我经常在我的Selenium脚本中使用Python。我喜欢创建一个文本文件并且启动脚本,然后运行,调试,继续,所有这些都可以通过命令行实现。感觉简单而轻便。在C#中创建一个DLL并且通常会接着运行单元测试,我也可以用“脚本语言”使之运行的更快。

你可以使用ScriptsCS去实现,因为它提供给你基于无项目的有效地脚本语言C#。结合此方法和 FluentAutomation ,你可能会得到两全其美的方法。

为了能够安装,首先你需要用Windows apt-get打开等效源,赋予独特的名字和拼写Chocolatey。然后你会获得针对 FluentAutomation 的 ScriptCS和安装包。

· 安装Chocolatey-一行安装

· 从你的命令行运行“cinst ScriptCS”使用Chocolatey来安装 ScriptCS

· 现在,像这样获取 FluentAutomation 的ScriptCS脚本包:

    • scriptcs -install FluentAutomation.SeleniumWebDriver
    • scriptcs -install ScriptCs.FluentAutomation

现在,作为一种快速测试,创建一个文件夹并且将名为start.csx的文本文件放进去,文本内容如下:

 var Test = Require<F14N>()     .Init<FluentAutomation.SeleniumWebDriver>()      .Bootstrap("Chrome")       .Config(settings => {                // Easy access to FluentAutomation.Settings values         settings.DefaultWaitUntilTimeout = TimeSpan.FromSeconds(1);        }); Test.Run("Hello Google", I => {        I.Open(https://google.com);});
注意这里没有命名空间,没有类,没有主函数。除了它使用的是C#以外,它仅仅是一个脚本。你可以将“Chrome”改为“IE”或“Firefox”也是一样的,继续进行。

随感:我喜欢Selenium功能,通过 FluentAutomation 被公开…截屏!

// Take Screenshot

I.TakeScreensho("LoginScreen");

如果你不想用ScriptCS,它本身可以作为一个REPL,还有一个专门的 FluentAutomation REPL的开端(read–eval–print loop)。这从根本上说是一个命令提示符,可以使你交互式地探索你的应用程序并且帮助你构建脚本。你仅仅使用"cinst FluentAutomation.Repl"还可以得到作为一个Chocolatey包的Repl

在自动化测试的领域你已经拥有了很多种选择。选择如此多现在没有借口了。选择一个库,选择一种语言并且从今天开始自动化测试你的web应用程序。

相关链接

主办方:非常感谢 GrapeCity的一个部门ComponentOne,因为他们本周赞助了博客。他们的非常流行的.NET控件 ,Studio Enterprise包含数以百计的数据和UI控件,例如网格,图表和报告来提供你所需要的当前或者未来应用程序开发的功能,特点和支持。今天下载试用!

你可能会喜欢