编码UI测试

本文关键字:测试 UI 编码 | 更新日期: 2023-09-27 18:35:55

对不起,这么一个笨拙的帖子!!

我是编码/自动化测试的新手。我有VS 2013 Ultimate,并创建了一个带有断言的CodedUI测试,它运行良好。但是,当断言失败时,测试将停止。我知道这个问题已经被问了很多次,我已经尝试了显示的解决方案,但没有运气。我希望即使在发生故障后也能运行测试。

我正在测试一个 .net 应用程序,在应用程序中我们有几个搜索字段,我想验证如果我搜索单词"test",搜索结果中将包含"test"一词。我对一个没有"测试"一词的单元格进行了断言,正如预期的那样,断言失败了。

我希望测试继续运行,以便我可以搜索所有其他字段,但一旦断言失败,它就会停止。

当我在单元格上做断言时,我使用了"包含"而不是"AreEqual"似乎是有意义的,因为我正在地址的任何部分寻找"测试"(即使它说"1 wetester dr")

下面是 codedUITest1.cs 脚本中的部分代码:

//From the CodedUITest1.cs file 
[TestMethod]
        public void TestAddressSearchFields()
        {            
            this.UIMap.SearchAddressFor_test();
            this.UIMap.Assert_on_FirstAddressCellForTheWord_test();
            this.UIMap.SearchNextThing();
            this.UIMap.SearchAnotherThing();                      
        }
---------------------------------------------------------------------------------------

来自 UIMap.Designer.cs 文件(这是断言的定义)

 public void Assert_on_FirstAddressCellForTheWord_test()
        {
            #region Variable Declarations
            WinCell uIItem800SROLLINGRDCell = this.UIDPSClaimantSearchWindow.UIGrdClaimantSearchWindow.UIDataGridViewTable.UIRow6Row.UIItem800SROLLINGRDCell;
            #endregion
            // Verify that the 'Value' property of '800 S ROLLING RD' cell contains 'test'
            StringAssert.Contains(uIItem800SROLLINGRDCell.Value, this.Assert_on_FirstAddressCellForTheWord_testExpectedValues.UIItem800SROLLINGRDCellValue, "Address does not contain the word '"test'"");

        }

//And then the definition for the cell value
[GeneratedCode("Coded UITest Builder", "12.0.30501.0")]
    public class Assert_on_FirstAddressCellForTheWord_testExpectedValues
    {
        #region Fields
        /// <summary>
        /// Verify that the 'Value' property of '800 S ROLLING RD' cell contains 'test'
        /// </summary>
        public string UIItem800SROLLINGRDCellValue = "test";
        #endregion
    }
I tried the following but it didn't work, once the assertion failed, the test stopped (is it because the failure is not an exception?)
     this.UIMap.SearchAddressFor_test();
                try
                {
                    this.UIMap.Assert_on_FirstAddressCellForTheWord_test();
                }
                catch (Exception theword_test_not_found)
                {
                    throw (theword_test_not_found);
                }
                {
                    Playback.PlaybackSettings.ContinueOnError = true;
                }

对不起这么长的帖子,记住我是全新的,请温柔一点!

编码UI测试

如果你真的想在一个测试中测试多个条件,你可以把这一行添加到测试的顶部。

Playback.PlaybackSettings.ContinueOnError = True;

像这样分解测试可能非常乏味,并且运行起来可能非常耗时。

我会推荐一些东西。 首先,考虑手动编写 CodedUI 测试,而不是使用录制和播放。 这将使您拥有更多可配置、可重用的块。

代码优先是一个很好的开始。

我也有一些基本的例子,展示了一些编写可重用测试的技术。

现在,关于为您描述的每一件事编写测试。 我们发现,如果它们是可恢复的测试,那么在单个测试中测试多个条件是完全可以的。 我仍然建议尽可能多地分解测试。

在您的情况下,我会说,如果到达您正在测试的页面的时间很长,或者满足前提条件的时间很长,或者耦合错误的可能罩子很高,则可以聚合异常。 跟踪并修复名称字段的错误只是为了发现状态字段也具有可由同一测试捕获的错误,这会很烦人。

[TestInitialize]
public void GivenTimeConsumingPreConditions()
{
   // do time consuming steps
}
[TestMethod]
public void WhenManipulatingTabs_ThenSearchFiltersResults()
{
    // depending on how you want to iterate, this is a super simple example
    string[] tabs = new ["Products", "Customers"];
    List<Exception> exs = new List<Exception>(tabs.Length);
    foreach(string tabName in tabs){
        OpenTab(tabName);
        EnterTextIntoSearchBox("test");
        ClickSearch();
        try {
            Assert.IsTrue(AllResultsContain("test"));
        } catch(Exception e) {
            exs.Add(e);
        }
    }
    if(exs.Any()){
        throw new AggregateException(exs);
    }
}

这样做可确保如果测试由于其他原因(例如,选项卡无法打开)而失败,测试仍然会立即失败,因此您只是聚合预期可能组合在一起的异常。(例如,搜索不能在所有选项卡与单个选项卡上正常工作)。

这可能会有所帮助。将您的测试方法标记为预期异常(当您没有找到"测试"词时)并捕获它,然后您可以随心所欲地使用它。以下是我如何处理单元测试中的预期异常 -

    [TestMethod]
    [ExpectedException(typeof(ArgumentException))]
    public void HoofNotesUtilitiesCreateDirectoryTest()
    {
        try
        {
            // Test whether it throws an exception
            // It'd better, * is not a valid directory name
            // when you are creating one
            HoofNotesUtilities.CreateDirectory("*");
        }
        catch (System.ArgumentException e)
        {
            if (e.Message == "Illegal characters in path.")
            {
                // You got the exception that you expected - GREAT
                // Do whatever you need to do with it here. 
                // THEN rethrow it, because the test expects it.
                // If you don't, the test will fail and stop. 
                throw;
            }
            else
            {
                // Wrong exception, fail the test
                Debug.Assert(false);
            }
        }
    }