Pages

HOME

HOME

Friday, 27 July 2012

How/When :To use Playback.Wait() method in CodedUI

The CodedUI provides one useful method when we want to wait for sometime till we get some operation(e.g. action - like onclick of submit button if some sql query get fired and then success message we show on some label at that time we have to wait for sometime ) completed  then we can call the Playback.Wait(int thinkTimeMilliseconds) explicitly.The Playback.Wait() internally calls Thread.Sleep().


This method is under :
Namespace:  Microsoft.VisualStudio.TestTools.UITesting
Assembly:  Microsoft.VisualStudio.TestTools.UITesting (in Microsoft.VisualStudio.TestTools.UITesting.dll)

The properties supported by Playback class are :
Name
Description
IsInitialized
This property indicates whether playback has been initialized.
IsSessionStarted
This property indicates whether the playback session has started.
PlaybackSettings
This property gets the current playback settings.


The methods supported by Playback class are:
Method Name
Description
Initialize
This method initializes playback for the UITest.
Wait
This method makes the playback pause for a specified period of time.
Cancel
This method cancels the playback of the current operation.
Cleanup
This method performs clean up after the UITest playback.
StartSession
This method starts a new session for playback.
StopSession
This method stops the current playback session.
EncryptText
This method ncrypts a given text for passing to playback as password property.
Example:


Please ref How to Create CodedUI test for the background for the below method.

   [TestMethod]

   public void CheckAdditionFunctionality()

   {

       try

         {

                //add using Microsoft.VisualStudio.TestTools.UITesting.WinControls; namespace

                LaunchCalculator();

                //Here playback.wait() method added just for understanding purpose for first time user..

                //to understand the actions automated in slow

         WinButton btn1 = this.UIMap.UICalculatorWindow.UIItemWindow.UIItem1Button;

         WinButton btn2 = this.UIMap.UICalculatorWindow.UIItemWindow1.UIItem2Button;

         WinButton btnAdd = this.UIMap.UICalculatorWindow.UIItemWindow2.UIAddButton;    
WinButton btnEquals = this.UIMap.UICalculatorWindow.UIItemWindow3.UIEqualsButton;
WinText txtResult = this.UIMap.UICalculatorWindow.UIItem0Window.UIItem0Text;

                Mouse.Click(btn1);
                //Playback.Wait(5000);

                Mouse.Click(btnAdd);
                //Playback.Wait(5000);

                Mouse.Click(btn2);
                // Playback.Wait(5000);

                Mouse.Click(btnAdd);

                //playback will wait for 10000 thinkTimeMilliseconds

                Playback.Initialize();
                Playback.Wait(10000);

                if (txtResult.DisplayText.Equals("3"))

                {

                    MessageBox.Show("Passed");

                }

                else {

                    MessageBox.Show("Failed");

                }

            }

                           catch(Exception e) {

                throw new AssertInconclusiveException(e.Message.ToString());

            }

           finally{
                 Playback.Cleanup();
            }
}