This project is read-only.

Loop over controls

Apr 15, 2013 at 5:14 PM
Edited Apr 15, 2013 at 5:21 PM
I am creating a more generic framework for my UI testing. Based on keywords and triggers.

For example when I see a Click action I know this will be for a Button or Link. Is it possible to loop over these two controls with a for loop? Because now I have to use try...catch( try..catch ).

Something like the following, although this example is not allowed:
var list = new List<ICUITe_ControlBase>();
list.Add(CUITe_HtmlHyperlink);
list.Add(CUITe_HtmlButton);

foreach(control in list of controls)
{
control link = screen.Get<control>("Class=button;InnerText=" + identifier);
link.click();
}
Could someone help me out here?
Apr 16, 2013 at 10:15 AM
I'm not sure I exactly understand your requirements, but maybe something like this would be better
List<string> buttonTexts = new List<string>();
buttonTexts.Add("buttonText1");
buttonTexts.Add("buttonText2");

foreach(string buttonText in buttonTexts)
{
ICUITe_ControlBase button = screen.Get<ICUITe_ControlBase>("Class=button;InnerText=" + buttonText);
button.click();
}
Apr 16, 2013 at 5:04 PM
Edited Apr 16, 2013 at 5:04 PM
Thank you for the fast response

The loop should be over the different types of ICUITe_ControlBase (so CUITe_HtmlHyperlink, CUITe_HtmlButton, and other clickable items).

The ICUITe_ControlBase as GET<> did not do the trick for me, unfortunately.
Apr 17, 2013 at 8:16 AM
Here is some sample code for you:
using (TempFile tempFile = new TempFile(
@"<html>
    <head>
        <title>test</title>
    </head>
    <body>
        <a href=""#"">test</a>
        <button>test</button>
    </body>
</html>"))
            {
                CUITe_BrowserWindow.Launch(tempFile.FilePath);
                CUITe_BrowserWindow window = new CUITe_BrowserWindow("test");

                ICUITe_ControlBase a = (ICUITe_ControlBase)window.Get<CUITe_HtmlHyperlink>("InnerText=test");
                a.Click();

                List<Type> list = new List<Type>();
                list.Add(typeof(CUITe_HtmlHyperlink));
                list.Add(typeof(CUITe_HtmlButton));

                MethodInfo getMethodInfo = typeof(CUITe_BrowserWindow).GetMethod("Get");

                foreach(Type t in list)
                {
                    MethodInfo test = getMethodInfo.MakeGenericMethod(t);
                    //window.Get<t>("InnerText=test");
                    ICUITe_ControlBase link = (ICUITe_ControlBase)test.Invoke(window, new object[]{"InnerText=test"});
                    link.Click();
                }

                window.Close();
            }
Apr 17, 2013 at 9:02 AM
Superb, thank you very much
Apr 17, 2013 at 9:59 AM
This works nicely for Click actions, because Click() is in the Interface. Could this trick also be done with HTMLEdit and HTMLTextarea's (because SetText is not part of the interface)
Apr 18, 2013 at 7:06 AM
Hi JGelon,

Here is some sample code for you.

Note that your questions are more C# specific programming questions and not really related to CUITe directly, so I recommend asking these type of questions in the C# msdn forum here.

Thank you for understanding.
using (TempFile tempFile = new TempFile(
@"<html>
    <head>
        <title>test</title>
    </head>
    <body>
        <a href=""#"">test</a>
        <button>test</button>
<input type=""text"" value=""test""/>
    </body>
</html>"))
            {
                CUITe_BrowserWindow.Launch(tempFile.FilePath);
                CUITe_BrowserWindow window = new CUITe_BrowserWindow("test");

                ICUITe_ControlBase a = (ICUITe_ControlBase)window.Get<CUITe_HtmlHyperlink>("InnerText=test");
                a.Click();

                List<Type> list = new List<Type>();
                list.Add(typeof(CUITe_HtmlHyperlink));
                list.Add(typeof(CUITe_HtmlButton));
                list.Add(typeof(CUITe_HtmlEdit));

                MethodInfo getMethodInfo = typeof(CUITe_BrowserWindow).GetMethod("Get");

                foreach(Type t in list)
                {
                    MethodInfo test = getMethodInfo.MakeGenericMethod(t);
                    
                    ICUITe_ControlBase control;

                    if ((t == typeof(CUITe_HtmlEdit)) || (t == typeof(CUITe_HtmlTextArea)))
                    {
                        control = (ICUITe_ControlBase)test.Invoke(window, new object[] { "Value=test" });
                    }
                    else
                    {
                        //window.Get<t>("InnerText=test");
                        control = (ICUITe_ControlBase)test.Invoke(window, new object[] { "InnerText=test" });
                    }

                    control.Click();

                    if (control is CUITe_HtmlEdit)
                    {
                        (control as CUITe_HtmlEdit).SetText("text");
                    }
                    else if (control is CUITe_HtmlTextArea)
                    {
                        (control as CUITe_HtmlTextArea).SetText("text");
                    }

                }

                window.Close();
            }