WatiN:How to use the automated test run result as I am unable to get the way of Reporting the result of the test run

允我心安 提交于 2019-12-11 06:18:53

问题


I have automated a module with WATIN and now I want the test result to be displayed as PASS/FAIL status etc(REPORTING), Is there any functionality in Watin to do my required action.

Like I have a code like

    public static void TestSelectPatientLink()

    {

        try

        {

            Link lnkSelectPatientb = ie.Link(Find.ByTitle("Search patient"));
            lnkSelectPatientb.Blur();
            lnkSelectPatientb.ClickNoWait();

        }

        catch (Exception ex)

        {

            Console.WriteLine(ex.ToString());

        }

    }

How to get the report as what happened when this code runs in VS 2010, Is it failing or passing, If it fails, the error etc, How to report these things.

**FYI I am using Nunit with WatiN


回答1:


No, there isn't any functionality built into WatiN to support this, but GarethStephenson's post was correct. You can write an NUnit test that will give you a pass/fail.

First of all, for IE to work with NUnit you need to add the following to your app.config

<configSections>
  <sectionGroup name="NUnit">
    <section name="TestRunner" type="System.Configuration.NameValueSectionHandler"/>
  </sectionGroup>
</configSections>

<NUnit>
  <TestRunner>
    <!-- Valid values are STA,MTA. Others ignored. -->
    <add key="ApartmentState" value="STA" />
  </TestRunner>
</NUnit>

Here's an example test. It loads up the google home page, grabs some elements and asserts they exist: -

using WatiN.Core;
using NUnit.Framework;

namespace ConsoleApplication1
{
    [TestFixture]
    public class AutomatedTests
    {
        [Test]
        public void DoGoogleTest()
        {
            using (IE browser = new IE())
            {
                browser.GoTo("www.google.co.uk");

                Div logoDiv = browser.Div("hplogo");
                Assert.IsTrue(logoDiv.Exists, "Logo div does not exist");

                TextField searchText = browser.TextField("lst-ib");
                Assert.IsTrue(searchText.Exists, "Search text field does not exist");

                Button searchBtn = browser.Button(Find.ByName("btnK"));
                Assert.IsTrue(searchBtn.Exists, "Search button does not exist");

                Button nonExistantButton = browser.Button("garbagegarbagegarbage");
                // This will cause the test to fail because the link doesn't (shouldn't!) exist.
                // Comment it out and the test should pass
                Assert.IsTrue(nonExistantButton.Exists, "Non-existant button does not exist");
            }
        }
    }
}

Unfortunately, NUnit doesn't automatically integrate with Visual Studios Test View/Test List windows. Your options are: -

  • Install Visual NUnit
  • Install Gallio (which will integrate and is good for reporting)
  • Run your tests externally (which can be done through VS)

The above code gives me the result: -

ConsoleApplication1.AutomatedTests.DoGoogleTest:
  Non-existant button does not exist
  Expected: True
  But was:  False

If you comment out the last line you get no errors reported.

If you need any more info then let me know. HTH!

EDIT Added link for Visual NUnit extension for VS2010




回答2:


You could always convert that method into a Unit Test Method, using NUnit or the like.

[Test]
public void TestSelectPatientLink()
{
    try
    {
        Link lnkSelectPatientb = ie.Link(Find.ByTitle("Search patient"));
        lnkSelectPatientb.Blur();
        lnkSelectPatientb.ClickNoWait();
    }
    catch (Exception ex)
    {
        // Capture the error screen so you can see what went wrong
        ie.CaptureWebPageToFile("Error.jpg");
        // Fail the test, use the unit testing framework's reporting to get your pass/fail
        Assert.Fail(ex.ToString());
    }
}



回答3:


Subscribing to nunit events(testFinished, testStarted) may help. But it's requires developing addon for nunit Br /Vitalii



来源:https://stackoverflow.com/questions/7685263/watinhow-to-use-the-automated-test-run-result-as-i-am-unable-to-get-the-way-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!