New to Telerik Test Studio? Download 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.

//We scroll to the date picker so that we can see the click occuring. //Pages.WeekEndCourseullesSurMer.WeekendFormFormTag is a definiton which Test Studio 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 initalize 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 corespond 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 coresponds 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