Pages

HOME

HOME

Saturday, 11 August 2012

How/When to :Use UITestControl.WaitFor..() method in CodedUI


Today we will see how to use the different Playback.Wait() methods provided in CodedUI.While automating the scripts you may come across scenario like some controls are visible after some actions performed on other controls.viz. On click of search button the the respective gridview is shown(or visible on page). In such cases the control is availability depends on action performed on some other controls; you may face issue like "ControlNotFound()" exception. To overcome or handle such scenario CodedUI provides some Playback.Wait() or UITestControl.WaitForControl....() methods.Let's see one by one the different methods and their purpose.

1.WaitForControlReady :This method Waits for the control to be ready to accept mouse and keyboard input. The engine implicitly calls this API for all actions to wait for the control to be ready before doing any action on it. However, some time in some cases, you may have to call it explicitly.
UITestControl UiTestCtrl = new UITestControl();
                if (UiTestCtrl.WaitForControlReady())
                {
                    Mouse.Click(UiTestCtrl);
                }
Also this method has 1 overload method to give the timeout as input in milliseconds. Default timeout is 60000 i.e. 1 minute
UITestControl UiTestCtrl = new UITestControl();
//Here the timeout is set to 5 seconds
                if (UiTestCtrl.WaitForControlReady(5000))
                {
                    Mouse.Click(UiTestCtrl);
                }


2.WaitForControlEnabledWaits for the control to be enabled. For example, when you want to wait for control to be enabled which is depend on server response then we can use this method to wait. 
UITestControl UiTestCtrl = new UITestControl();
                if (UiTestCtrl. WaitForControlEnabled())
                {
                    Mouse.Click(UiTestCtrl);
                }
Also this method has 1 overload method to give the timeout as input in milliseconds. Default timeout is 60000 i.e. 1 minute
UITestControl UiTestCtrl = new UITestControl();
//Here the timeout is set to 5 seconds
                if (UiTestCtrl. WaitForControlEnabled(5000))
                {
                    Mouse.Click(UiTestCtrl);
                }


3.WaitForControlExists:Waits for the control to appear on User Interface. e.g. you are expecting an error message after you have done the validation of the parameters. The time taken for validation is not fixed. We can use this method to wait for the error message dialog box.
UITestControl UiTestCtrl = new UITestControl();
                if (UiTestCtrl. WaitForControlExist())
                {
                    Mouse.Click(UiTestCtrl);
                }
Also this method has 1 overload method to give the timeout as input in milliseconds. Default timeout is 60000 i.e. 1 minute
UITestControl UiTestCtrl = new UITestControl();
//Here the timeout is set to 5 seconds
                if (UiTestCtrl. WaitForControlExist(5000))
                {
                    Mouse.Click(UiTestCtrl);
                }


4.WaitForControlNotExists :Waits for the control to disappear from the User Interface(Form/web page). As shown above similarly we can validate for WaitForControlNotExists.
5.WaitForControlPropertyEqual(string,Object):This method waits for the specified property of the control to have the given value. For example if we have windows calculator and we want to check 1+2= 3 is true or false.
[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(10000);
                Mouse.Click(btnAdd);
                //Playback.Wait(10000);
                Mouse.Click(btn2);
                Mouse.Click(btnAdd);
              bool isTrue=txtResult.WaitForControlPropertyEqual("DisplayText", "3");
                       //we can explicitly add wait in milliseconds
// txtResult.WaitForControlPropertyEqual("DisplayText", "3",5000);
                    if (txtResult)
                    {
                        //True.
                    }
                    else{
                        //false.
                    }
              }
            catch(Exception e) {
                throw new AssertInconclusiveException(e.Message.ToString());
            }

        }

 6.WaitForControlPropertyNotEqual(string,Object) :Waits for the specified property of the control to have the opposite of a specified value. For example, you wait for the edit box to be not read-only, that is, editable.
 //Here it is given for illustration only
      bool isTrue=txtResult.WaitForControlPropertyNotEqual("ReadOnly", " true ");
                       //we can explicitly add wait in milliseconds
// txtResult.WaitForControlPropertyEqual("ReadOnly ", "true",5000);
                    if (txtResult)
                    {
                        //True.
                    }
                    else{
                        //false.
                    }
  

7.WaitForControlCondition(Predicate<UITestControl>):Waits for the specified predicate returns to be true. This can be used for complex wait operation (like OR conditions) on a given control. For example, you can wait until the status text is or as shown in the following code:

// Define the method to evaluate the condition

private static bool IsOperationSucceed(UITestControl control)
{
        return txtResult.DisplayText == "3" || txtResult.DisplayText == "2";
}

// In test method, wait till the method evaluates to true
txtResult.WaitForControlCondition(IsOperationSucceed);




8.WaitForCondition<T>(T,Predicate<T>): All the above methods are instance methods of UITestControl. While this method is a static method. This method also waits for the specified predicate to be true. e.g. We can wait until the Result text is or until an error/exception message displayed.

// Define the method to evaluate the condition
private static bool IsOperationSucceedOrError(UITestControl[] controls)
{
    
    WinWindow errorDialog = controls[1] as WinWindow;
    return txtResult.DisplayText == "2" || errorDialog.Exists;
}

// In test method, wait till the method evaluates to true
UITestControl.WaitForCondition<UITestControl[]>(newUITestControl[] { txtResult, errorDialog }, IsOperationSucceedOrError);

Other links:
1.How to create CodedUITest
2.How to use Playback.wait()



No comments:

Post a Comment