New to Telerik Test Studio Dev EditionStart a free 30-day trial

AJAX Calendar - Random Date Selection

I would like to select a random date from an Ajax Calendar.

Solution

The code below will show you how to select a random date from the Calendar on this site.

C#
    //We scroll to the date picker so that we can see the click occurring. //Pages.WeekEndCourseullesSurMer.WeekendFormFormTag is a definition which Test Studio Dev automatically generated.
    Pages.WeekEndCourseullesSurMer.WeekendFormFormTag.ScrollToVisible(ScrollToVisibleType.ElementBottomAtWindowBottom);
                
    //The Date Picker is actually an HTML List. We use the class name days to locate it.
    HtmlUnorderedList calendar = Find.ByExpression<HtmlUnorderedList>(new HtmlFindExpression("class=days"));
                
    //We initialize a collection to store all "available" dates from the calendar.
    LinkedList<HtmlListItem> availableDates = new LinkedList<HtmlListItem>();
                
    //Go through all the dates in the calendar. The dates are actually HTML List items.
    foreach (HtmlListItem i in calendar.Items)
    {
        //Green dates belong to class "promotion" while the purple one belongs to class "active".
        if (i.CssClass.Equals("promotion") || i.CssClass.Equals("active"))
        {
            //If the date checks out as "available" we add it to the list.
            availableDates.AddLast(i);
        }          
    }
    Random r = new Random();
                
    //We generate a random number which will correspond to an item in our list.
    int randomNum = r.Next(availableDates.Count);
                
    //l will be the date we're going to select in the Calendar.
    LinkedListNode<HtmlListItem> l = availableDates.First;
                
    //This loop moves up the linked list until we reach the node that corresponds to our randomly generated number.
    while (randomNum != 0)
    {
        l = l.Next;
        randomNum--;
    }
    
    //Click on the node after we reach it in the list.
    l.Value.MouseClick();
In this article
Solution
Not finding the help you need?
Contact Support