none
WaitForControlCondition with variable Conditions

    Question

  • We can wait for certain events with the WaitForControlCondition(Predicate<UITestControl> conditionEvaluator)

    1. private static bool IsStatusDone(UITestControl control)
        • {
        •     WinText statusText = control as WinText;
        •     return statusText.DisplayText == "Succeeded" || statusText.DisplayText == "Failed";
        • }
        • // In test method, wait till the method evaluates to true
        • statusText.WaitForControlCondition(IsStatusDone);

    Is there a way to pass a parameter to the "IsStatusDone()" method?

    Like:

    1. private static bool IsStatusDone(UITestControl control, string text)
    2. {
    3.     WinText statusText = control as WinText;
    4.     return statusText.HelpText.Contains(text);
    5. }
    6. // In test method, wait till the method evaluates to true
    7. statusText.WaitForControlCondition(IsStatusDone("Succeeded"), 2000);

    Trying something like that gives my the error:

    Error    1    The best overloaded method match for 'Microsoft.VisualStudio.TestTools.UITesting.UITestControl.WaitForControlCondition(System.Predicate<Microsoft.VisualStudio.TestTools.UITesting.UITestControl>, int)' has some invalid arguments.


    http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/3030821-allow-connecting-to-test-controller-using-differen <-- Vote for connecting with any user to Test Contrtoller

    Wednesday, January 23, 2013 2:42 AM

Answers

  • The code you show may work, but I would worry about the use of "static" in lots of places. How about code like this (note that I have compiled but not executed the three code segments below):

    class ParameterisedIsStatusDone
    {
        public ParameterisedIsStatusDone(string t)
        {
            text = t;
        }
    
        private string text;
    
        public bool IsStatusDone(UITestControl control)
        {
            WinText statusText = control as WinText;
            return statusText.HelpText.Contains(text);
        }
    }

    Then in your test write the lines:

    ParameterisedIsStatusDone pisd = new ParameterisedIsStatusDone("Succeeded");
    yourControl.WaitForControlCondition(pisd.IsStatusDone);

    A shorter form that does not need the class but uses an anonymous function would be:

    this.UIMap.UITrackbarTestHarnessWindow.UITrkTestPane.WaitForControlCondition(
        (UITestControl control) =>
        {
            WinText statusText = control as WinText;
            return statusText.HelpText.Contains("Succeeded");
        }
    );

    Regards

    Adrian


    Wednesday, January 23, 2013 5:00 PM

All replies

  • I would suggest using a "closure", see Wikipedia.

    The explanation at http://www.codethinked.com/c-closures-explained seems interesting, the code following "It Has To Close Over It, Son" gives the style of one solution. Web searching for "c# closure" gives many other interesting links.

    The 'static' will probably need to be removed from whatever replaces your "IsStatusDone" method.

    Regards

    Adrian

    Wednesday, January 23, 2013 9:00 AM
  • Not really understanding it (closure paradigm) at the moment.
    Do you know it fits with the WaitForControlCondition?

    Another Idea I thinked about was passing a string to the class containing the evaluating method (IsStatusDone in the example: here).
    So I have at first set the string in the class and than call the evaluation as normal.

    So that the method can access it:

             
    public static class Evaluat
    {
    	privatestaticstring evalString = String.Empty;
    	
    	publicstaticvoid SetEvalString(string evaluateString)
    	{
    		evalString = evaluateString;
    	}
    	
        publicstaticbool IsStatusDone(UITestControl control)
        {
            WinText statusText = control as WinText;
            return statusText.HelpText.Contains(evalString);
        }
    	
    }
     // Code anywhere
    Evaluat.SetEvalString("Succeeded");
    bool isFinalyDone = statusText.WaitForControlCondition(IsStatusDone, 2000);
    Evaluat.SetEvalString(String.Empty);
    Assert.IsTrue(isFinalyDone);




    http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/3030821-allow-connecting-to-test-controller-using-differen <-- Vote for connecting with any user to Test Contrtoller


    • Edited by Victor Zn Wednesday, January 23, 2013 4:33 PM
    Wednesday, January 23, 2013 4:32 PM
  • The code you show may work, but I would worry about the use of "static" in lots of places. How about code like this (note that I have compiled but not executed the three code segments below):

    class ParameterisedIsStatusDone
    {
        public ParameterisedIsStatusDone(string t)
        {
            text = t;
        }
    
        private string text;
    
        public bool IsStatusDone(UITestControl control)
        {
            WinText statusText = control as WinText;
            return statusText.HelpText.Contains(text);
        }
    }

    Then in your test write the lines:

    ParameterisedIsStatusDone pisd = new ParameterisedIsStatusDone("Succeeded");
    yourControl.WaitForControlCondition(pisd.IsStatusDone);

    A shorter form that does not need the class but uses an anonymous function would be:

    this.UIMap.UITrackbarTestHarnessWindow.UITrkTestPane.WaitForControlCondition(
        (UITestControl control) =>
        {
            WinText statusText = control as WinText;
            return statusText.HelpText.Contains("Succeeded");
        }
    );

    Regards

    Adrian


    Wednesday, January 23, 2013 5:00 PM