locked
how to capture winforms Message box's text using coded UI test

    問題

  • I have 2 text boxes (Bank code, Bank name). There are three type of messages will be displayed. First one is for mandatory checking (Plz enter bank code or Plz enter bank name). Second one is Bank code already exists. Third one is record saved successfully.

    When i do the data driven test first time CUIT identifies the message box text and writes the assert as well. after that the CUIT is unable to search the message box or unable to close the message box.

    this is my test data

    Bank Name,Display Name,Active,Message

    A,BANK A,TRUE,Records Saved Successfully

    ,BANK B,TRUE,Enter Bank Name

    C,,TRUE,Enter Display Name

    D,BANK D,FALSE,Records Saved Successfully

    Coded UI Test

    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Book111.csv", "Book111#csv", DataAccessMethod.Sequential), DeploymentItem("TestProject6\\Book111.csv"), TestMethod]
            public void CodedUITestMethod1()
            {
                // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
                // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463

                this.UIMap.BNKINSParams.UITxtBankNameEditText = TestContext.DataRow[0].ToString();
                this.UIMap.BNKINSParams.UITxtDisplayNameEditText = TestContext.DataRow[1].ToString();

                this.UIMap.BNKINS();
                this.UIMap.CHKMSGExpectedValues.UITxtMessageEditText = TestContext.DataRow[3].ToString();
                this.UIMap.CHKMSG(); // To assert the expected Vs Actual here
                this.UIMap.CLOSEBNK(); // After the assert to close the message box

     

    public void CHKMSG()
            {
                #region Variable Declarations
                WinEdit uITxtMessageEdit = this.UITxtMessageWindow.UITxtMessageEdit;
                #endregion

                // Verify that 'txtMessage' text box's property 'Text' equals 'Bank Name Already Exists
                Assert.AreEqual(this.CHKMSGExpectedValues.UITxtMessageEditText, uITxtMessageEdit.Text);
    }


    public void CLOSEBNK()
            {
                #region Variable Declarations
                WinClient uIPanel1Client = this.UIItemWindow1.UIPanel1Window.UIPanel1Client;
                WinButton uIOKButton = this.UIOKWindow.UIOKButton;
                WinButton uIExitButton = this.UIToolbarControl1Window.UIToolStrip1ToolBar.UIExitButton;
                #endregion

                // Click 'panel1' client
                Mouse.Click(uIPanel1Client, new Point(198, 10));

                // Click 'Ok' button
                Mouse.Click(uIOKButton, new Point(17, 17));

                // Click 'Exit' button
                Mouse.Click(uIExitButton, new Point(18, 14));
            }

    Plz help on this.
    2012年3月6日 上午 09:22

解答

  • The idea is to change the code to be something like the following :

    [TestInitialize()]
    public void MyTestInitialize()
    {       
       this.UIMap.OPENBNK();
    }

    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Book111.csv", "Book111#csv", DataAccessMethod.Sequential), DeploymentItem("TestProject6\\Book111.csv"), TestMethod]
    public void CodedUITestMethod1()
    {
       this.UIMap.BNKINSParams.UITxtBankNameEditText = TestContext.DataRow[0].ToString();
       this.UIMap.BNKINSParams.UITxtDisplayNameEditText = TestContext.DataRow[1].ToString();
       this.UIMap.UpdateValues();    //this.UIMap.BNKINS();
       this.UIMap.CHKMSGExpectedValues.UITxtMessageEditText = TestContext.DataRow[3].ToString();
       this.UIMap.CHKMSG();
    }

    [TestCleanup] public void MyTestCleanup() { this.UIMap.CLOSEBNK(); }

    
    



    Issamo san

    2012年3月6日 上午 11:47
  • BaraneetharanR,

    You will need to update the search properties for that control to reflect the changed property from "BankMaster" to "Bank Master".

    The absolute best way to do this is using the Visual Studio Feature Pack 2 available @http://msdn.microsoft.com/en-us/vstudio/ff655021.aspx

    To get started fast, I have uploaded a video on my blog which gives a quick tutorial on how to use the Coded UI Test Editor @ http://windotnet.blogspot.com/2011/07/coded-ui-test-editor-is-visual-studio.html

    The following instructions will be able to solve your issue:

    After installing, open your test project.  (You may get key not present in dictionary error, if so, unload the project and reload).

    Double click the uimap file (.uitest extension) containing the control you wish to change.  Find the control in the heirarchy which appears on the right under UI Control Map.  Right click it and select properties of press F4.  Click the ... to the right of Search Properties (Collection) in the properties window that just opened.  Then change the value of the name property.

    This directly modifies the .uitest file and as such regenerates all of the recorded code, but with your new changes applied!

    To remove out of date controls, double click the uimap file and find the control in the hierarchy on the right under UI Control Map.  Right click and choose delete.

    OR

    If you are bold, simply examine the .uitest file in an xml editor (XMLPAD) and change the value you wish to change and delete the node containing the control you wish to remove.  But be very careful to backup and maintain proper XML structure when doing this!

    Please mark posts which answere you question(s) as answered and open a new thread for new questions.  Issamo-San appears to have answered your original question.

    Cheers,

    Paul

    2012年3月27日 下午 02:31

所有回覆

  • I have 2 text boxes (Bank code, Bank name). There are three type of messages will be displayed. First one is for mandatory checking (Plz enter bank code or Plz enter bank name). Second one is Bank code already exists. Third one is record saved successfully.

    When i do the data driven test first time CUIT identifies the message box text and writes the assert as well. after that the CUIT is unable to search the message box or unable to close the message box.

    this is my test data

    Bank Name,Display Name,Active,Message

    A,BANK A,TRUE,Records Saved Successfully

    ,BANK B,TRUE,Enter Bank Name

    C,,TRUE,Enter Display Name

    D,BANK D,FALSE,Records Saved Successfully

    Coded UI Test

    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Book111.csv", "Book111#csv", DataAccessMethod.Sequential), DeploymentItem("TestProject6\\Book111.csv"), TestMethod]
            public void CodedUITestMethod1()
            {
                // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
                // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463

                this.UIMap.BNKINSParams.UITxtBankNameEditText = TestContext.DataRow[0].ToString();
                this.UIMap.BNKINSParams.UITxtDisplayNameEditText = TestContext.DataRow[1].ToString();

                this.UIMap.BNKINS();
                this.UIMap.CHKMSGExpectedValues.UITxtMessageEditText = TestContext.DataRow[3].ToString();
                this.UIMap.CHKMSG(); // To assert the expected Vs Actual here
                this.UIMap.CLOSEBNK(); // After the assert to close the message box

     

    public void CHKMSG()
            {
                #region Variable Declarations
                WinEdit uITxtMessageEdit = this.UITxtMessageWindow.UITxtMessageEdit;
                #endregion

                // Verify that 'txtMessage' text box's property 'Text' equals 'Bank Name Already Exists
                Assert.AreEqual(this.CHKMSGExpectedValues.UITxtMessageEditText, uITxtMessageEdit.Text);
    }


    public void CLOSEBNK()
            {
                #region Variable Declarations
                WinClient uIPanel1Client = this.UIItemWindow1.UIPanel1Window.UIPanel1Client;
                WinButton uIOKButton = this.UIOKWindow.UIOKButton;
                WinButton uIExitButton = this.UIToolbarControl1Window.UIToolStrip1ToolBar.UIExitButton;
                #endregion

                // Click 'panel1' client
                Mouse.Click(uIPanel1Client, new Point(198, 10));

                // Click 'Ok' button
                Mouse.Click(uIOKButton, new Point(17, 17));

                // Click 'Exit' button
                Mouse.Click(uIExitButton, new Point(18, 14));
            }

    Plz help on this.
    2012年3月6日 上午 09:25
  • Hello,

    In order to force the test to close the messageBox whatever the assert result is, you've to put the CLOSEBNK() call into the testCleanup method, and remove it from CodedUITestMethod1.

    [TestCleanup]
    public void MyTestCleanup()
    {
      this.UIMap.CLOSEBNK();
    }


    Issamo san

    • 已提議為解答 Issamo-san 2012年3月6日 上午 11:20
    • 已取消提議為解答 Issamo-san 2012年3月6日 上午 11:20
    • 已提議為解答 Issamo-san 2012年3月7日 上午 10:57
    2012年3月6日 上午 09:46
  • Hi,

    Thanks for the reply. I have moved the this.UIMap.CLOSEBNK(); method into test cleanup part. Now the test run.
    But the second data row is failed. how do i overcome this issue.

    Error :

    TestProject6.BNKINSERT.CodedUITestMethod1 threw exception:
    Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException: The playback failed to find the control with the given search properties. Additional Details:
    TechnologyName:  'MSAA'
    ControlType:  'Window'
    AccessibleName:  'Bank Master'
    ClassName:  'WindowsForms10.RichEdit20W'
     ---> System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component.

    2012年3月6日 上午 11:15
  • Hello

    Could you share the code behind mathode BNKINS()


    Issamo san

    2012年3月6日 上午 11:20
  • using System;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    using System.Windows.Input;
    using System.Windows.Forms;
    using System.Drawing;
    using Microsoft.VisualStudio.TestTools.UITesting;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.VisualStudio.TestTools.UITest.Extension;
    using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;


    namespace TestProject6
    {
        /// <summary>
        /// Summary description for BNKINSERT
        /// </summary>
        [CodedUITest]
        public class BNKINSERT
        {
           
            public BNKINSERT()
            {
            }

            [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Book111.csv", "Book111#csv", DataAccessMethod.Sequential), DeploymentItem("TestProject6\\Book111.csv"), TestMethod]
            public void CodedUITestMethod1()
            {
                // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
                // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463

                this.UIMap.BNKINSParams.UITxtBankNameEditText = TestContext.DataRow[0].ToString();
                this.UIMap.BNKINSParams.UITxtDisplayNameEditText = TestContext.DataRow[1].ToString();

                this.UIMap.BNKINS();
                this.UIMap.CHKMSGExpectedValues.UITxtMessageEditText = TestContext.DataRow[3].ToString();
                this.UIMap.CHKMSG();
                //this.UIMap.CLOSEBNK();
               
            }

            #region Additional test attributes

            // You can use the following additional attributes as you write your tests:

            ////Use TestInitialize to run code before running each test
            //[TestInitialize()]
            //public void MyTestInitialize()
            //{       
            //    // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
            //    // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463
            //}

            ////Use TestCleanup to run code after each test has run
            [TestCleanup]
            public void MyTestCleanup()
            {
                // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
                // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463
                this.UIMap.CLOSEBNK();
            }

            #endregion

            /// <summary>
            ///Gets or sets the test context which provides
            ///information about and functionality for the current test run.
            ///</summary>
            public TestContext TestContext
            {
                get
                {
                    return testContextInstance;
                }
                set
                {
                    testContextInstance = value;
                }
            }

           
            private TestContext testContextInstance;

            public UIMap UIMap
            {
                get
                {
                    if ((this.map == null))
                    {
                        this.map = new UIMap();
                    }

                    return this.map;
                }
            }

            private UIMap map;
        }
    }

    2012年3月6日 上午 11:26
  • This is not the BNKINS() implementation ;).

    Issamo san

    2012年3月6日 上午 11:28
  • Sorry. This is BNKINS()

    public void BNKINS()
            {
                #region Variable Declarations
                WinButton uIStaticMasterDataButton = this.UIWealthManagementSuitWindow.UIPnlExplrWindow.UIStaticMasterDataButton;
                WinMenuItem uIBankMasterMenuItem = this.UIWealthManagementSuitWindow.UIMnuMainMenuMenuBar.UIMiscellaneousMenuItem.UIBankMasterMenuItem;
                WinButton uIAddButton = this.UIWealthManagementSuitWindow.UIP00035Window.UIToolStrip1ToolBar.UIAddButton;
                WinEdit uITxtBankNameEdit = this.UIGrbBankDetailsWindow.UITxtBankNameWindow.UITxtBankNameEdit;
                WinEdit uITxtDisplayNameEdit = this.UIGrbBankDetailsWindow.UITxtDisplayNameWindow.UITxtDisplayNameEdit;
                WinCheckBox uIActiveCheckBox = this.UIGrbBankDetailsWindow.UIActiveWindow.UIActiveCheckBox;
                WinButton uISaveButton = this.UIToolbarControl1Window.UIToolStrip1ToolBar.UISaveButton;
                #endregion

                // Click 'Static / Master Data' button
                Mouse.Click(uIStaticMasterDataButton, new Point(94, 18));

                // Click 'Miscellaneous' -> 'Bank Master' menu item
                Mouse.Click(uIBankMasterMenuItem, new Point(65, 10));

                // Click 'Add' button
                Mouse.Click(uIAddButton, new Point(26, 12));

                // Type 'A' in 'txtBankName' text box
                uITxtBankNameEdit.Text = this.BNKINSParams.UITxtBankNameEditText;

                // Type '{Tab}' in 'txtBankName' text box
                Keyboard.SendKeys(uITxtBankNameEdit, this.BNKINSParams.UITxtBankNameEditSendKeys, ModifierKeys.None);

                // Type 'BANK A' in 'txtDisplayName' text box
                uITxtDisplayNameEdit.Text = this.BNKINSParams.UITxtDisplayNameEditText;

                // Select 'Active' check box
                uIActiveCheckBox.Checked = this.BNKINSParams.UIActiveCheckBoxChecked;

                // Click 'Save' button
                Mouse.Click(uISaveButton, new Point(31, 9));
            }

    2012年3月6日 上午 11:35
  • The idea is to change the code to be something like the following :

    [TestInitialize()]
    public void MyTestInitialize()
    {       
       this.UIMap.OPENBNK();
    }

    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\Book111.csv", "Book111#csv", DataAccessMethod.Sequential), DeploymentItem("TestProject6\\Book111.csv"), TestMethod]
    public void CodedUITestMethod1()
    {
       this.UIMap.BNKINSParams.UITxtBankNameEditText = TestContext.DataRow[0].ToString();
       this.UIMap.BNKINSParams.UITxtDisplayNameEditText = TestContext.DataRow[1].ToString();
       this.UIMap.UpdateValues();    //this.UIMap.BNKINS();
       this.UIMap.CHKMSGExpectedValues.UITxtMessageEditText = TestContext.DataRow[3].ToString();
       this.UIMap.CHKMSG();
    }

    [TestCleanup] public void MyTestCleanup() { this.UIMap.CLOSEBNK(); }

    
    



    Issamo san

    2012年3月6日 上午 11:47
  • Hi Issamo,

    Thanks a lot.

    Now it is working fine and the tests execute very fast. third row is failed because of CUIT unable to find the message panel title "Bank Master". In this secnario it was displayed as "BankMaster" (ie. Without space)

    2012年3月7日 上午 10:54
  • Hi BaraneetharanR,

    Does this issue has been resolved? If it does you could mark a reply as answer, otherwise please provide us the latest status about this issue.


    Tony Xiao [MSFT]
    MSDN Community Support | Feedback to us

    2012年3月27日 上午 09:55
  • BaraneetharanR,

    You will need to update the search properties for that control to reflect the changed property from "BankMaster" to "Bank Master".

    The absolute best way to do this is using the Visual Studio Feature Pack 2 available @http://msdn.microsoft.com/en-us/vstudio/ff655021.aspx

    To get started fast, I have uploaded a video on my blog which gives a quick tutorial on how to use the Coded UI Test Editor @ http://windotnet.blogspot.com/2011/07/coded-ui-test-editor-is-visual-studio.html

    The following instructions will be able to solve your issue:

    After installing, open your test project.  (You may get key not present in dictionary error, if so, unload the project and reload).

    Double click the uimap file (.uitest extension) containing the control you wish to change.  Find the control in the heirarchy which appears on the right under UI Control Map.  Right click it and select properties of press F4.  Click the ... to the right of Search Properties (Collection) in the properties window that just opened.  Then change the value of the name property.

    This directly modifies the .uitest file and as such regenerates all of the recorded code, but with your new changes applied!

    To remove out of date controls, double click the uimap file and find the control in the hierarchy on the right under UI Control Map.  Right click and choose delete.

    OR

    If you are bold, simply examine the .uitest file in an xml editor (XMLPAD) and change the value you wish to change and delete the node containing the control you wish to remove.  But be very careful to backup and maintain proper XML structure when doing this!

    Please mark posts which answere you question(s) as answered and open a new thread for new questions.  Issamo-San appears to have answered your original question.

    Cheers,

    Paul

    2012年3月27日 下午 02:31