Check All Checkboxes on a page

9 posts, 0 answers
  1. Tyler
    Tyler avatar
    4 posts
    Member since:
    Sep 2011

    Posted 10 Oct 2011 Link to this post

    Hello,

    I am working on creating a test where I need to check every checkbox that appears on a page.  This page is driven by rule validation from data entered on other pages, so the number of check boxes on the page could vary from 1 to 200 depending on the test case that is being run.  The checkboxes are displayed in an HtmlTable, on per body row.  The only difference in the id of the checkboxes is the bolded part: uwciRepeater_ctl01_checkBox_Approve.  Based on what number it is, it will show ctl00, ctl01, ctl02, etc.

    I attempted changing expression in the find logic on the item to remove the ctl01 part and using a Do-While, but this still selects just the first checkbox.   I have been attempting different coded steps to no success.  I played with the idea of recording a huge if...then statement within the test, but I feel like that would be a large amount of steps and not the most optimal solution.  I was wondering if anyone would have an example of a coded step that could solve this issue.

    Thank you,

    Tyler
  2. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 11 Oct 2011 Link to this post

    Hello Tyler,
        this is done fairly easily in code:
    foreach (Element e in Find.AllByTagName("Input"))
                {
     
                    if (e.InputElementType == InputElementType.CheckBox)
                    {
                        HtmlControl hc = e.As<HtmlControl>();
                        hc.ScrollToVisible();
                        hc.MouseClick();
                    }
     
                }

    I tried this code against this page:
    http://www.tizag.com/htmlT/htmlcheckboxes.php
    and it worked flawlessly except that checkboxes that are already checked become unchecked.

    Also, one thing to consider is that this will not work for checkboxes inside of Frames. Instead you'll need to search directly into a Frame in order to perform the same actions.
     i.e. Frame.Find.AllByTagName("Input")...

    Best wishes,
    Stoich
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Tyler
    Tyler avatar
    4 posts
    Member since:
    Sep 2011

    Posted 11 Oct 2011 Link to this post

    Hello Stoich,

    Thank you very much for the code - this worked very well.  I wanted to let you know this behavior that I saw - when doing hc.MouseClick() I observed that it only checked about 1/2 to 2/3 of the boxes on the page, and it was random in what boxes it checked.  I got the solution to work using hc.Click(), so all is good, but I wanted to bring that to your attention in case there is any sort of bug with MouseClick(). 

    Thanks again for the quick response,

    Tyler
  4. Stoich
    Admin
    Stoich avatar
    1 posts

    Posted 12 Oct 2011 Link to this post

    Hello Tyler,
       interesting. MouseClick() attempts to simulate a real user click on an element. On the other hand Click() works directly on the DOM structure of the app. Perhaps the clicks are occurring in very rapid succession. This could leads to problems with the simulate real click (i.e. MouseClick() ) since a real user would not be able to click around to rapidly.

    If you're interested in getting to the bottom of this you can introduce a fixed delay in the test and check out whether that resolves the issue.
    A fixed delay in code will look like this:
    System.Threading.Thread.Sleep(1000); //Fixed delay - takes miliseconds as a parameter
    You can put this like before you invoke the MouseClick() function.

    Regards,
    Stoich
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Muhammad
    Muhammad avatar
    3 posts
    Member since:
    Feb 2013

    Posted 04 Nov 2014 in reply to Stoich Link to this post

    Hi Stoich,
    can you please translate in vb? because mine project is in vb.
    Thanks you so much
  6. Muhammad
    Muhammad avatar
    3 posts
    Member since:
    Feb 2013

    Posted 04 Nov 2014 in reply to Stoich Link to this post

    can you please translate in vb?
  7. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 04 Nov 2014 Link to this post

    Hi Muhammad,

    You can use our free code converter to convert code between C# and VB.

    Regards,
    Cody
    Telerik
     
    The New Release of Telerik Test Studio Is Here! Download, install,
    and send us your feedback!
  8. Jonathan
    Jonathan avatar
    23 posts
    Member since:
    Jan 2012

    Posted 07 Nov 2015 Link to this post

    Hi,

     

    I have a follow up question.  I only want to check all the checkboxes where the id = gridUserRoleList and the checkbox was unchecked.  Any help in C# would be appreciated.  Thanks!

  9. Ivaylo
    Admin
    Ivaylo avatar
    750 posts

    Posted 12 Nov 2015 Link to this post

    Hello Jonathan,

    I am providing you below the code you've requested:

    var checkBoxes = Manager.ActiveBrowser.Find.AllByExpression<HtmlInputCheckBox>("id=gridUserRoleList").Where((cb) => !cb.Checked);
    Manager.Log.WriteLine(checkBoxes.Count().ToString());


    Regards,
    Ivaylo
    Telerik
     
    The New Release of Telerik Test Studio Is Here! Download, install,
    and send us your feedback!
Back to Top