将Ruby和Watir与NUnit集成

OK, I'm hooked. Ruby is the bomb and I'm loving it. We shall see what the future brings, the debugger is iffy, I need to recompile Notepad2 like Wes did with Ruby syntax support, but otherwise I'm digging it.

好吧,我迷上了。 Ruby是炸弹,我很喜欢它。 我们将看到未来带来的情况,调试器尚无定论,我需要像Wes在Ruby语法支持下所做的那样重新编译Notepad2 ,否则我将继续进行挖掘。

The back story is this. I've been trying to find the Holy Grail, or even the 40% Grail of Automated Web UI Testing for at least the last 5 years. We use NUnit for everything, so it'd be cool if whatever we chose could fail NUnit tests.

背后的故事是这个。 至少在过去的五年中,我一直在尝试寻找“圣杯”,甚至40%的自动化Web UI测试。 我们使用NUnit进行所有操作,因此如果我们选择的任何方法都无法通过NUnit测试,那就太酷了。

Lately folks have been trying again all over the next and lots of good stuff has been happening in this space.

最近,人们一直在尝试再次尝试,并且在这个空间中发生了很多好事。

  • NUnitASP - http://nunitasp.sourceforge.net/

    NUnitASP - http: //nunitasp.sourceforge.net/

    • Pros: C# class library for downloading and parsing web pages. Very CSharpy.

      优点: C#类库,用于下载和解析网页。 非常CSharpy。

    • Cons: Nothing has happened with this since November 2004. Have to extend it constantly for more complex controls. Thinks about things in a Control way.

      缺点:自2004年11月以来,此操作未发生任何事情。必须不断扩展它以实现更复杂的控件。 以控制方式思考事物。

    NUnitASP - http://nunitasp.sourceforge.net/

    NUnitASP - http: //nunitasp.sourceforge.net/

  • SAMIE - http://samie.sourceforge.net/

    SAMIE - http://samie.sourceforge.net/

    • Pros: Uses Perl to drive IE around using it's OLE automation interface. Keeps logs, can fill with data from databases, and there's a cheesy but functional WinForms app to help write the scripting language. 

      优点:使用Perl使用OLE自动化界面来驱动IE。 保留日志,可以填充数据库中的数据,并且有一个俗气但功能强大的WinForms应用程序可帮助编写脚本语言。

    • Cons: Uses Perl. LOL.

      缺点:使用Perl。 大声笑。

    SAMIE - http://samie.sourceforge.net/

    SAMIE - http://samie.sourceforge.net/

  • HttpUnit - http://httpunit.sourceforge.net/

    HttpUnit - http://httpunit.sourceforge.net/

    • Pros: Feels good to the Java folks, integrates with JUnit, lower level focused.

      优点:对Java专家感觉很好,与JUnit集成,专注于较低级别。

    • Cons: May not be actively developed, no activity since October 2004.

      缺点:可能没有得到积极开发,自2004年10月以来没有任何活动。

  • IEUnit - http://ieunit.sourceforge.net/

    IEUnit - http: //ieunit.sourceforge.net/

    • Pros: Almost zero installation footprint. Javascript based engine uses the language we were all born knowing. Rich debugging because you can use the Windows Script Debugger. Fairly simple in design. Actively developed. Nice WinForms script helper. Good if you already dig the DOM.

      优点:安装空间几乎为零。 基于Java的引擎使用了我们天生知道的语言。 丰富的调试功能,因为您可以使用Windows脚本调试器。 设计上相当简单。 积极发展。 不错的WinForms脚本助手。 如果您已经研究过DOM,那就很好。

    • Cons: Slightly cheesy handling of model dialogs. Steals focus while running tests.

      缺点:对模型对话框的处理比较随意。 在运行测试时窃取焦点。

  • Selenium - http://selenium.thoughtworks.com/

    Selenium-http : //selenium.thoughtworks.com/

    • Pros: Good for testing cross browser compatibility. Actively worked on. Selenium tests can be expanded to use other languages.

      优点:适用于测试跨浏览器的兼容性。 积极工作。 Selenium测试可以扩展为使用其他语言。

    • Cons: Uses a Fit-like HTML as it's source code. This takes a mind shift and freaks out some folks. Server-side component required.

      缺点:使用类似Fit的HTML作为源代码。 这需要转变思路并吓到一些人。 需要服务器端组件。

    Selenium - http://selenium.thoughtworks.com/

    Selenium-http : //selenium.thoughtworks.com/

And the one I'm currently enamored with is Watir. There's a great comparison of Watir and Selenium by a contributor to both here.:

我现在最着迷的是Watir。 这里的贡献者对Watir和Selenium进行很好的比较

  • Watir (Pronounced "Water") Web Application Testing in Ruby - http://wtr.rubyforge.org/

    的Watir(发音为“水”)的Web应用测试在Ruby中- http://wtr.rubyforge.org/

    • Pros: Actively being worked on. Uses Ruby, the language du jour. Can run tests asychronously. Simple, elegant, fast. Ruby's interactive shell makes it easy to develop tests (more on this later in this post). Great support for client side things like script events, and even mouse rollovers.

      优点:积极地工作。 使用Ruby,即dujour语言。 可以异步运行测试。 简单,优雅,快速。 Ruby的交互式外壳使开发测试变得容易(本文稍后会对此进行更多介绍)。 对客户端事件(例如脚本事件,甚至鼠标悬停)的强大支持。

    • Cons: Haven't found any yet, but I'm sure they are out there. Very focused on the DOM.

      缺点:尚未找到任何东西,但我确定它们在那里。 非常关注DOM。

    Watir (Pronounced "Water") Web Application Testing in Ruby - http://wtr.rubyforge.org/

    的Watir(发音为“水”)的Web应用测试在Ruby中- http://wtr.rubyforge.org/

Ok, just to level set if you made it this far: Watir is an open source functional testing library for automated tests to be developed and run against a web browser. Cool?

好的,就目前而言,只要设定水平即可: Watir是一个开源功能测试库,用于针对要在Web浏览器上开发和运行的自动化测试。 凉?

Here's an example that visits Google, searches for "Programming Ruby" and confirms that it found what it was looking for. The stuff after the # are comments.

这是一个访问Google,搜索“ Programming Ruby”并确认其已找到所需内容的示例。 #之后的内容是注释。

require 'watir' # the watir controllerinclude Watir
test_site =
'http://www.google.com'ie = IE.newie.goto(test_site)ie.text_field(:name, "q").set("pickaxe") # q is the name of the search fieldie.button(:name, "btnG").click # "btnG" is the name of the Search buttonif ie.contains_text("Programming Ruby")   puts "Test Passed. Found the test string: 'Programming Ruby'. Actual Results match Expected Results."else  puts "Test Failed! Could not find: 'Programming Ruby'" end

要求 瓦特尔 #watir控制器包括 瓦蒂尔test_site = “ http://www.google.com” = IE浏览器 转到( test_site ) text_field (: name “ q” )。 设置( “ pickaxe” ) #q是搜索字段的名称 按钮(:名称 “ btnG” )。 点击 #“ btnG”是“搜索”按钮的名称如果 contains_text ( “编程Ruby” ) “测试通过。找到测试字符串:'Programming Ruby'。实际结果与预期结果匹配。” 其他 “测试失败!找不到:'编程Ruby'” 结束

Very simple. So, Patrick and I wanted to integrate this into our NUnit tests, some of which already use Cassini for self-contained tests, and others are more complex but ultimately result in a web site. We wanted to do something like this from our existing infrastructure:

很简单。 因此,帕特里克和我想将其集成到我们的NUnit测试中,其中一些已经使用Cassini进行独立测试,而其他一些则更为复杂,但最终导致了网站的出现。 我们想在现有基础架构中执行以下操作:

[TestFixture]
    public class WatirTest 
    {
        public WatirTest(){}
 
        [Test]
        public void FirstTest()
        {
            WatirAssert.TestPassed("ibcexample.rb");
        }
    }

This was the Goal. We talked about adding [WatirTest] attributes or extending a new WatirTestCase and even a whole new series of Assertions, but we didn't see any reason to be tricky, other than to be tricky. We opted for simple simple simple.

这就是目标。 我们讨论了添加[WatirTest]属性或扩展新的WatirTestCase甚至是全新的断言系列,但是除了棘手之外,我们没有发现其他任何棘手的原因。 我们选择了简单简单简单。

public class WatirAssert 
{
    public static void TestPassed(string rubyFileName)
    {
        string output = String.Empty;
        using(Process p = new Process())
        {
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = "ruby.exe";
            p.StartInfo.Arguments = rubyFileName;
            p.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
            p.Start();
            output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
        }
        Console.Write(output);
        Trace.Write(output);
        Regex reg = new Regex(@"(?<tests>\d+) tests, (?<assertions>\d+) assertions, 
(?<failures>\d+) failures, (?<errors>\d+) errors",
            RegexOptions.Compiled);
        Match m = reg.Match(output);
        try
        {
            int tests = int.Parse(m.Groups["tests"].Value);
            int assertions = int.Parse(m.Groups["assertions"].Value);
            int failures = int.Parse(m.Groups["failures"].Value);
            int errors = int.Parse(m.Groups["errors"].Value);
 
            if (tests > 0 && failures > 0) 
            {
                Assert.Fail(String.Format("WatirAssert: Failures {0}", failures));
            }
            else if (errors > 0) 
            {
                Assert.Fail(String.Format("WatirAssert: Errors {0}", errors));
            }
        }
        catch(Exception e)
        {
            Assert.Fail("WatirAssert EXCEPTION: " + e.ToString());
        }
    }
}

Using this, and armed with increasing knowledge of Ruby we started writing test cases (one per use case) for a large bank as a good test of this concept. These tests would run at the very end of the build process. After the build, test, docs, etc, we'd run the smoke (or integration) tests using Watir.

使用此工具,并随着对Ruby的了解越来越多,我们开始为大型银行编写测试用例(每个用例一个),作为对该概念的良好测试。 这些测试将在构建过程的最后运行。 构建,测试,文档等之后,我们将使用Watir运行冒烟(或集成)测试。

UPDATE: Your Ruby classes have to derive from the Ruby Unit Testing framework for this to work automatically. So your "template" would be (Note: the class name has an initial capital letter):

更新:您的Ruby类必须从Ruby Unit Testing框架派生,才能自动工作。 因此,您的“模板”应为(注意:类名具有首字母大写):

require 'watir'
include Watirrequire 'test/unit'

需要“ watir” 包括瓦蒂尔要求“测试/单位”

class TestExampleTemplate < Test::Unit::TestCase   def test_search
    ie = IE.new
    ie.goto("
http://www.google.com")
    ie.text_field(:name, "q").set("pickaxe")
    ie.button(:value, "Google Search").click
    assert(ie.contains_text("Programming Ruby, 2nd Ed."))
  end
end

类Test ExampleTemplate <Test :: Unit :: TestCase def test_search 即= IE.new ie.goto(“ http://www.google.com ”) ie.text_field(:name,“ q”)。set(“ pickaxe”) ie.button(:value,“ Google搜索”).click assert(ie.contains_text(“ Programming Ruby,2nd Ed。”)) 结束结束

Now, referring back to the syntax:

现在,回到语法:

ie.text_field(:name, "q").set("pickaxe") # q is the name of the search fieldie.button(:name, "btnG").click # "btnG" is the name of the Search button

text_field (: name “ q” )。 设置( “ pickaxe” ) #q是搜索字段的名称 按钮(:名称 “ btnG” )。 点击 #“ btnG”是“搜索”按钮的名称

You may think it'll be a hassle to do a lot of View|Source work and figure out the names and IDs of these textboxes and buttons. However, even though Watir only runs IE (remember it wraps IE's COM stuff) FireFox presented a great way to avoid all that poking through HTML source. Yes, it's the greatest thing FireFox brought to the Web Developer (besides Venkman), it's the Web Developer Extension.

您可能会认为,麻烦的是执行大量View | Source工作并弄清楚这些文本框和按钮的名称和ID。 但是,即使Watir只运行IE(记住它包装了IE的COM内容),FireFox还是提供了一种避免通过HTML源代码进行戳戳的好方法。 是的,这是FireFox带给Web开发人员的最伟大的事情(除了Venkman之外),它是Web开发人员扩展

Bring up your site in FireFox and from the Web Developer toolbar, click Forms|Display Form Details. Notice as in the screen shot below you'll get the IDs and Names of all your form elements. Then code your Watir script to them. Shiny.

在FireFox中打开您的站点,然后从Web Developer工具栏上,单击Forms | Display Form Details 。 请注意,如下面的屏幕快照所示,您将获得所有表单元素的ID和名称。 然后将您的Watir脚本编码给他们。 闪亮

Webdeveloperformsdisplay

If you want get Ruby and Watir and start playing:

如果您想获得Ruby和Watir并开始玩:

  • Download Ruby for Windows

    下载Ruby for Windows

  • Download Watir and run install.rb

    下载Watir并运行install.rb

  • Go into C:\watir_bonus\examples and run googleSuite_test.rb

    进入C:\ watir_bonus \ examples并运行googleSuite_test.rb

  • Then, look at articleExample.rb and make some changes to test your own site.

    然后,查看articleExample.rb并进行一些更改以测试您自己的站点。

  • Enjoy. Feel free to integrate into your NUnit tests if you like using our code above. If you expand it, let us know.

    请享用。 如果您喜欢使用我们上面的代码,请随时集成到您的NUnit测试中。 如果您将其扩展,请告诉我们。

翻译自: https://www.hanselman.com/blog/integrating-ruby-and-watir-with-nunit

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值