This is a migrated thread and some comments may be shown as answers.

iterating through a RadListBox

31 Answers 560 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Max
Top achievements
Rank 1
Max asked on 20 Jul 2012, 12:26 PM

I've got a list of entries in a page that I would like to iterate through. I was hoping to do this, without having to record each and every entry, using a count of the items and an iterator.

I modified my code to include a reference to the item in the pages.g.cs file (by inspecting the DOM for the page). Do I need to create an entry under the tstest file?

<d2p1:AutomationStep i:type="d2p1:AutomationStep">
      <d2p1:ContinueOnFailure>false</d2p1:ContinueOnFailure>
      <d2p1:CustomDescription i:nil="true" />
      <d2p1:Description>RadListBoxItem
etc...

31 Answers, 1 is accepted

Sort by
0
Plamen
Telerik team
answered on 25 Jul 2012, 01:06 PM
Hi Max,

The easiest way to iterate through the items in the RadListBox, is to use the 'RadListBox.Items' collection. For example, the following code will iterate through all items, scrolling and clicking on every single one.
 
RadListBox listBox = ActiveBrowser.Find.ById<RadListBox>("RadListBoxSource");
 
foreach (RadListBoxItem lbItem in listBox.Items)
{
    lbItem.ScrollToVisible();
    lbItem.Click();
}

Here's a video demonstrating the execution.

Regards,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Max
Top achievements
Rank 1
answered on 25 Jul 2012, 03:05 PM

That still results in an error that there isn't a definition for the listbox control, warning that it could be a missing using directive or assembly reference.

0
Plamen
Telerik team
answered on 25 Jul 2012, 04:11 PM
Hello Max,

You need to add a using statement and reference to Telerik.WebAii.Controls.Html.dll. Please see this video on how to overcome this problem.

Regards,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Max
Top achievements
Rank 1
answered on 26 Jul 2012, 08:16 AM

The test was recorded using Test Studio. The HTML using directive was already present:

using Telerik.WebAii.Controls.Html;

I've checked in both Test Studio and Visual Studio and the using directive isn't underlined in red, so I don't think it is the reference that is missing.

Edit - double checked this:

---------------------------
Microsoft Visual Studio
---------------------------
A reference to 'C:\Program Files\Telerik\Test Studio 2011.2\Bin\Translators\Telerik.WebAii.Controls.Html.dll' could not be added. A reference to the component 'Telerik.WebAii.Controls.Html' already exists in the project.
---------------------------
OK
---------------------------

0
Plamen
Telerik team
answered on 26 Jul 2012, 10:13 AM
Hello Max,

Can you please send me a sample project where you are experiencing this problem? Just place it into a .zip file and attach it to this ticket. I'll try to fix it and send it back to you. 

Kind regards,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Max
Top achievements
Rank 1
answered on 26 Jul 2012, 10:15 AM
See attached. Please delete from thread once received.
0
Plamen
Telerik team
answered on 26 Jul 2012, 01:10 PM
Hello Max,

Thank you for providing the project.

The problem is that there is conflict between the Telerik.WebAii.Controls.Html and Telerik.WebAii.Controls.Xaml namespaces. To overcome this, you need to remove or comment the using directive for the Xaml namespace("using Telerik.WebAii.Controls.Xaml"). See this short video.

Another option is to keep both of them and use the full path to the RadListBox class in your code. Here's an example:
 
Telerik.WebAii.Controls.Html.RadListBox listBox = ActiveBrowser.Find.ById<Telerik.WebAii.Controls.Html.RadListBox>("RadListBoxSource");
 
foreach (Telerik.WebAii.Controls.Html.RadListBoxItem lbItem in listBox.Items)
{
     lbItem.ScrollToVisible();
     lbItem.Click();
}


I also noticed that you are referencing a missing page element in a coded step in the GetAllExportsWithData test and that is why you are getting the following exception:
c:\Users\peykov\Desktop\GetAllExports_20072012\GetAllExportsWithData.tstest.cs: Line 82: (CS1061) 'GetAllExports_20072012.Pages.GoCompletionsPage' does not contain a definition for 'ctl00_ContentPlaceHolder1_ImportsListBox' and no extension method 'ctl00_ContentPlaceHolder1_ImportsListBox' accepting a first argument of type 'GetAllExports_20072012.Pages.GoCompletionsPage' could be found (are you missing a using directive or an assembly reference?)

Please see this article on how to resolve this.

If you need further assistance, please let me know.

Regards,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Max
Top achievements
Rank 1
answered on 26 Jul 2012, 01:12 PM
that is quite interesting as the html and xaml assembly references are placed in the code, by default, by test studio!
0
Cody
Telerik team
answered on 30 Jul 2012, 03:46 PM
Hi,

I agree we don't have the best solution regarding using the RadListBox in code. I've filed a bug on this behavior here. Fortunately the fix is pretty easy.

All the best,
Cody
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Max
Top achievements
Rank 1
answered on 30 Jul 2012, 03:51 PM
Thanks. Although the points above still don't allow me to access the items one by one in that fashion. Mostly, I believe because of the control being situated within a DIV and not being built into the codebehind when recording.
0
Cody
Telerik team
answered on 31 Jul 2012, 06:06 PM
Hello,

...the points above still don't allow me to access the items one by one in that fashion....

Could you elaborate on what symptom / problem you are running into? The sample code Plamen sent to you does iterate the list one by one no problem. Can you explain in what way it's not working for you?

I believe because of the control being situated within a DIV...

Our sample page puts the RadListBox into a DIV as you can see below. This makes me believe that's not the cause of any problem.

<div id="RadAjaxPanel1">
    <div id="RadListBoxSource" class="RadListBox RadListBox_Office2010Silver RadListBoxButtonAreaRight RadListBoxScrollable" style="height: 200px; width: 200px;">
        <table cellpadding="0" cellspacing="0" class="rlbButtonAreaRight" style="width: 30px;">
            <tr>
                <td>
                <a class="rlbButton rlbTransferFromDisabled rlbDisabled rlbNoButtonText" title="To Right">
                <span class="rlbButtonBL"><span class="rlbButtonBR">
                <span class="rlbButtonTR"><span class="rlbButtonTL">
                <span class="rlbButtonText"> </span></span></span></span></span></a><a class="rlbButton rlbTransferToDisabled rlbDisabled rlbNoButtonText" title="To Left"><span class="rlbButtonBL"><span class="rlbButtonBR"><span class="rlbButtonTR"><span class="rlbButtonTL"><span class="rlbButtonText"> </span></span></span></span></span></a><a class="rlbButton rlbTransferAllFrom rlbNoButtonText" href="#" title="All to Right"><span class="rlbButtonBL"><span class="rlbButtonBR"><span class="rlbButtonTR"><span class="rlbButtonTL"><span class="rlbButtonText"> </span></span></span></span></span></a><a class="rlbButton rlbTransferAllToDisabled rlbDisabled rlbNoButtonText" title="All to Left"><span class="rlbButtonBL"><span class="rlbButtonBR"><span class="rlbButtonTR"><span class="rlbButtonTL"><span class="rlbButtonText"> </span></span></span></span></span></a></td>
            </tr>
        </table>
        <div class="rlbGroup rlbGroupRight" style="margin-right: 30px;">
            <ul class="rlbList">
                <li id="RadListBoxSource_i0" class="rlbItem">
                <span class="rlbText">Argentina</span></li>
                <li id="RadListBoxSource_i1" class="rlbItem">
                <span class="rlbText">Australia</span></li>
                <li id="RadListBoxSource_i2" class="rlbItem">
                <span class="rlbText">Brazil</span></li>
                <li id="RadListBoxSource_i3" class="rlbItem">
                <span class="rlbText">Canada</span></li>
                <li id="RadListBoxSource_i4" class="rlbItem">
                <span class="rlbText">Chile</span></li>
                <li id="RadListBoxSource_i5" class="rlbItem">
                <span class="rlbText">China</span></li>
                <li id="RadListBoxSource_i6" class="rlbItem">
                <span class="rlbText">Egypt</span></li>
                <li id="RadListBoxSource_i7" class="rlbItem">
                <span class="rlbText">England</span></li>
                <li id="RadListBoxSource_i8" class="rlbItem">
                <span class="rlbText">France</span></li>
                <li id="RadListBoxSource_i9" class="rlbItem">
                <span class="rlbText">Germany</span></li>
                <li id="RadListBoxSource_i10" class="rlbItem">
                <span class="rlbText">India</span></li>
                <li id="RadListBoxSource_i11" class="rlbItem">
                <span class="rlbText">Indonesia</span></li>
                <li id="RadListBoxSource_i12" class="rlbItem">
                <span class="rlbText">Kenya</span></li>
                <li id="RadListBoxSource_i13" class="rlbItem">
                <span class="rlbText">Mexico</span></li>
                <li id="RadListBoxSource_i14" class="rlbItem">
                <span class="rlbText">New Zealand</span></li>
                <li id="RadListBoxSource_i15" class="rlbItem">
                <span class="rlbText">South Africa</span></li>
                <li id="RadListBoxSource_i16" class="rlbItem">
                <span class="rlbText">USA</span></li>
            </ul>
        </div>
        <input id="RadListBoxSource_ClientState" autocomplete="off" name="RadListBoxSource_ClientState" type="hidden" value="{"isEnabled":true,"logEntries":[],"selectedIndices":[],"checkedIndices":[],"scrollPosition":0}">
    </div>
</div>

If you can explain the symptom you are running into I'll be glad to assist overcoming whatever the hurdle is you are running into.

Greetings,
Cody
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Max
Top achievements
Rank 1
answered on 01 Aug 2012, 08:30 AM

In short, the iterator doesn't work through the items in the RadListBox element.

Please could you demonstrate code which iterates through the control on your sample page?

I have tried adding the RadListBox element to the test elements using the DOM explorer. I have tried accessing this ByID, ByName and ByExpression. None of which I've managed to get working.

My original question really was about adding the element to the script, if it isn't automatically recorded. This still seems to be the issue.

0
Cody
Telerik team
answered on 01 Aug 2012, 10:00 PM
Hello Max,

Plamen already did that in his response dated July 25. He included the code and a video demonstrating how the code runs. If this approach is not working on your web application, we need to have a look and figure out what is causing the difference in behavior.

All the best,
Cody
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Max
Top achievements
Rank 1
answered on 02 Aug 2012, 08:52 AM

Hi,

I previously submitted the code I'm working on, to demonstrate. I followed the advice and commented out the XAML using directive.

However, I don't think the actual listbox is being added to the test resources in the actual test - this has been what I've been trying to explain:

Element was found by backup search only! We recommend editing the element to correct the primary find expression.
Primary Find Expression: href=/GoCompletions/Application/ImportExport/Imports.aspx?mode=GoCompletions,tagname=a
Element was found by backup search: xpath=/html[1]/body[1]/form[1]/div[4]/div[2]/div[1]/ul[1]/li[1]/ul[1]/li[9]/a[1]
'02/08/2012 08:58:17' - 'NotRun' : 14. RadListBoxItem('ctl00_ContentPlaceHolder1_ImportsListBox_i0'): 'Activities' action -> 'Select'
'02/08/2012 08:58:18' - 'Fail' : 15. [SelectRadListBox_CodedStep] : New Coded Step
------------------------------------------------------------
Failure Information: 
~~~~~~~~~~~~~~~
Exception thrown executing coded step: '[SelectRadListBox_CodedStep] : New Coded Step'.
InnerException:
System.NullReferenceException: Object reference not set to an instance of an object.
   at GetAllExports_20072012.SelectRadListBox.SelectRadListBox_CodedStep() in c:\Users\maxrussell\Documents\Test Studio Projects\GetAllExports_20072012\SelectRadListBox.tstest.cs:line 83

0
Max
Top achievements
Rank 1
answered on 02 Aug 2012, 09:44 AM

in your example:

<div id="RadAjaxPanel1">
    <div id="RadListBoxSource" class="RadListBox RadListBox_Office2010Silver RadListBoxButtonAreaRight RadListBoxScrollable" style="height: 200px; width: 200px;">
and this can be reached using:

RadListBox listBox = ActiveBrowser.Find.ById<RadListBox>("RadListBoxSource");
whereas I've got:

<div class="Column35pct">
<div id="ctl00_ContentPlaceHolder1_ImportsListBox" class="RadListBox RadListBox_WebBlue RadListBoxScrollable" style="height:420px;width:90%;">
but this line doesn't find the item:

RadListBox listBox = ActiveBrowser.Find.ById<RadListBox>("ct100_ContentPlaceHolder1_ImportsListBox");

0
Cody
Telerik team
answered on 02 Aug 2012, 10:48 PM
Hello Max,

...but this line doesn't find the item...

Interesting. I cannot think of any reason why that wouldn't work. I'd like to look at this problem on your computer via GoToMeeting. Let me know what time zone you are in and your availability and I'll setup the meeting. I'm in Austin, TX which is Central Time (GMT -6).

Greetings,
Cody
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Max
Top achievements
Rank 1
answered on 03 Aug 2012, 09:47 AM
My availability isn't good until Tuesday of next week. GMT.
0
Cody
Telerik team
answered on 03 Aug 2012, 01:55 PM
Hi,

No problem for me. Just let me know when you're available. Also, if it would help overcome the timezone difference, I can ask one of my other team members located in Sofia, Bulgaria to meet with you. I think they are much closer to your timezone and could meet during your regular business hours.

Greetings,
Cody
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Max
Top achievements
Rank 1
answered on 07 Aug 2012, 08:19 AM
This might be best tomorrow. Via Sofia.
0
Stoich
Telerik team
answered on 07 Aug 2012, 03:54 PM
Hello Max,
I've went ahead and sent you a meeting request for tomorrow. Go ahead and update the time if it doesn't work for you.

All the best,
Stoich
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Stoich
Telerik team
answered on 10 Aug 2012, 11:21 AM
Hi Max,
I'm glad we were able to workout the issue. To sum it up the problem was as follows:
You were looking for the RadListBox element in code by its ID:
RadListBox rlb = ActiveBrowser.Find.ById<RadListBox>("RadListBoxId");
we managed to determine that Test Studio cannot identify the element as a RadListBox for whatever reason. So we reworked the code like this:
Element e = ActiveBrowser.Find.ById("RadListBoxId");
RadListBox rlb = e.As<RadListBox>();
and this worked.

I spent some time researching this. I did not experience the same problem in my environment.
Try out the following code on your end:
 
RadListBox rlb = ActiveBrowser.Find.ById<RadListBox>("RadListBoxSource");
Log.WriteLine("Found this:"+rlb.ToString());
 
foreach (RadListBoxItem rli in rlb.Items) {
 rli.MouseClick();  
}

This worked fine on my end. Interestingly at first this wouldn't compile because RadListBox is an ambiguous reference between Telerik.WebAii.Controls.Html.RadListBox and Telerik.WebAii.Controls.Html.RadListBox.Xaml.

I had to add the following lines of code to make it work:
using RadListBox = Telerik.WebAii.Controls..Html.RadListBox;
using RadListBoxItem = Telerik.WebAii.Controls.Html.RadListBoxItem;

Did you use the same lines as well? Making a reference to the wrong element (the XAML Listbox instead of the HTML) might cause problematic behavior.

Greetings,
Stoich
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Max
Top achievements
Rank 1
answered on 10 Aug 2012, 12:08 PM

Yes - that works for me, however I was previously told to comment out the XAML using directive, which also works (there is a ticket raised to address the namespace confusion I think):

using Telerik.WebAii.Controls.Html;
//using Telerik.WebAii.Controls.Xaml;
0
Stoich
Telerik team
answered on 13 Aug 2012, 12:09 PM
Hi Max,
I was not aware. Commenting out using Telerik.WebAii.Controls.Html; is a different way of achieving the same effect: getting rid of the ambiguous reference.

All the best,
Stoich
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Max
Top achievements
Rank 1
answered on 04 Oct 2012, 01:47 PM
Cody raised an issue for this:
http://www.telerik.com/support/pits.aspx#/public/test-studio/12090

No fix has been scheduled, is one going to be scheduled?

thanks
0
Cody
Telerik team
answered on 04 Oct 2012, 04:18 PM
Hi Max,

No there is not going to be a code change about this. We have concluded that this behavior is by design. Because there is a RadListBox control in both HTML and Silverlight, you must be intelligent enough when dealing in code to handle this issue, either by commenting out the "using xxxx" statement that you don't need or by using the correct explicit name space qualifier "Telerik.WebAii.Controls..Html.RadListBox" as Stoich proposed. Either approach will overcome the compile error you can get.

Greetings,
Cody
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Max
Top achievements
Rank 1
answered on 05 Oct 2012, 07:23 AM
Okay thanks - kind of goes against what was said earlier in this thread, as it does seem strange to default to having both, if there is a clash in the namespace from a default recording, perhaps that should be documented?
0
Cody
Telerik team
answered on 05 Oct 2012, 02:50 PM
Hello Max,

"...kind of goes against what was said earlier in this thread..."

I understand the sentiment and yes I admit it is a change in direction from me. After we talked about it more with our software developers we all came to the same decision that the approach we're currently taking is the best approach for these reasons:
  • When you start writing code we, Telerik, have no idea if you will be writing code for a Silverlight application or a HTML application.
  • Since we cannot automatically detect this, we have to either include both "using xxxx" statements or neither. We concluded it would be better to include both.
  • When you "customize step in code" our code generation will generate code that uses the fully qualified namespace which is compatible with having both "using xxxx" statements. We're expecting customers to follow after this pattern.

The reason you're running into trouble is that you're trying to use the shorthand method of not specifying the fully qualified namespace. We consider this an advanced programmer technique and expect such people to be intelligent enough to be able to handle it, and I know you are an intelligent person.

 Greetings,
Cody
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Humberto
Top achievements
Rank 1
answered on 22 Mar 2013, 11:31 PM
Hello,

Sorry to hijack this post but it does seem similar to something I am trying to do.  However I am interested to know if it's possible to do this in the Pages.g.cs file, within the Pages class?

Declaractively detect/find a List or a Collection of Menu items?

Something along the lines of:

 public List<ArtOfTest.WebAii.Controls.HtmlControls.HtmlAnchor> MenuItems
{
get { return Get<List<ArtOfTest.WebAii.Controls.HtmlControls.HtmlAnchor>>("tagname=a"); }
}

These are children of a Menu UI element:
 public HtmlControl Menu
{
get { return Get<HtmlDiv>("id=ctl00_RadMenu1"); }
}

So basically I am trying to find the menu sub items and iterate over them clicking on each one.


I realize that the "user actions" I would not do in the Pages class...but what about the declaration?
0
Cody
Telerik team
answered on 26 Mar 2013, 06:57 PM
Hello Humberto,

There are a couple of problems with what you describe you want to try to do:
1) The Pages.g.cs file is very frequently regenerated by Test Studio (nearly constantly)
- Everytime you open your test project
- Anytime you add/delete an element including adding/deleting test steps
- Anytime you run a test
- If using our VS plug-in, anytime you build the project in VS

Any changes you try to make to this file will be overwritten/discarded.

2) The get<> function is restricted to returning a single element that is either a HtmlControl object or derives from the HtmlControl class. It cannot be used to return anything else, including a List.

If you implement it in your own code (instead of trying to place it in Pages.g.cs) you could do the equivalent like this:

public System.Collections.ObjectModel.ReadOnlyCollection<ArtOfTest.WebAii.Controls.HtmlControls.HtmlAnchor> MenuItems
{
    get
    {
        return Find.AllByExpression<HtmlAnchor>("id=ctl00_RadMenu1","|","tagname=a");
    }
}

The above find expression says find the element having the ID ctl00_RadMenu1 and then find and return a list of all the anchors underneath/attached to that element.

Regards,
Cody
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Humberto
Top achievements
Rank 1
answered on 26 Mar 2013, 08:25 PM
Hello again Cody,

Why use a ReadOnlyCollection and not just a List<HtmlAnchor> ?

Also, I'm getting an error when I try to iterate over the anchor elements, something about:

System.NotSupportedException : Find.All is currently not supported for expressions that have a hierarchal dependency

Thanks again Cody.
0
Cody
Telerik team
answered on 26 Mar 2013, 08:41 PM
Hi,

System.NotSupportedException : Find.All is currently not supported for expressions that have a hierarchal dependency

Sorry about that. I forgot about that limitation. Here's updated code to correct that:

public System.Collections.ObjectModel.ReadOnlyCollection<ArtOfTest.WebAii.Controls.HtmlControls.HtmlAnchor> MenuItems
{
    get
    {
        RadMenu theMenu = Find.ById<RadMenu>("ctl00_RadMenu1");
        return theMenu.Find.AllByTagName<HtmlAnchor>("a");
    }
}


Why use a ReadOnlyCollection and not just a List<HtmlAnchor> ?

All of the Find.AllByxxxx methods return a ReadOnlyCollection. The compiler complains it cannot implicitly convert from ReadOnlyCollection to List.

All the best,
Cody
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
Tags
General Discussions
Asked by
Max
Top achievements
Rank 1
Answers by
Plamen
Telerik team
Max
Top achievements
Rank 1
Cody
Telerik team
Stoich
Telerik team
Humberto
Top achievements
Rank 1
Share this question
or