I
came across situation where I have to write test case in which user will select date from calendar and that selected date
and format has to asset in test case. Below DatePickerField class is useful to
set date and format in JQuery Datepicker
using System;
using WatiN.Core;
namespace
Twenty57.Stadium.TestHelpers.WatiNElements
{
public class DatePickerField : Control<TextField>
{
public override
WatiN.Core.Constraints.Constraint
ElementConstraint
{
get { return Find.ByClass("hasDatepicker");
}
}
public DateTime?
Date
{
get
{
if (Element.Text == null)
return null;
var date = Eval("{0}('getDate').toUTCString()",
ElementReference);
return DateTime.Parse(date);
}
set
{
var javascriptDate = "null";
if (value.HasValue)
{
var date = value.Value;
javascriptDate = string.Format("new Date({0}, {1}, {2})", date.Year,
date.Month - 1, date.Day);
}
CallDatepicker("'setDate', {0}",
javascriptDate);
}
}
public string
DateFormat
{
get { return
GetOption("dateFormat"); }
}
public bool
Enabled
{
get
{
var isDisabled = CallDatepicker("'isDisabled'");
return !bool.Parse(isDisabled);
}
}
public string
GetOption(string option)
{
return CallDatepicker("'option',
'{0}'", option);
}
private string
ElementReference
{
get
{
return string.Format("window.jQuery({0}).datepicker",
Element.GetJavascriptElementReference());
}
}
private string
CallDatepicker(string script, params object[] args)
{
var theScript = string.Format(script,
args);
return Eval("{0}({1})",
ElementReference, theScript);
}
private string
Eval(string script, params
object[] args)
{
return Element.DomContainer.Eval(string.Format(script, args));
}
}
}
[Test]
public void
TestSetDate()
{
this.browser.GoTo(this.appURL
+ "/Page1");
this.browser.WaitForComplete();
DateTime dateToValidate = new DateTime(2013,
06,
11);
string dateText = dateToValidate.ToString(Config.DateFormat);
string controlId = "Page1_DatePicker1";
DatePickerField datePicker = this.browser.Control<DatePickerField>(Find.ById(controlId));
Assert.IsNotNull(datePicker, string.Format("DatePicker
with Id [{0}] not found.", controlId));
Assert.IsTrue(datePicker.Enabled, "DatePicker should be enabled.");
datePicker.Date = dateToValidate;
Assert.AreEqual(dateText,
datePickerTextField.Text);
Assert.AreEqual(JQueryUIDatePickerHelper.GetDateFormat(),
datePicker.DateFormat, "Date format is
incorrect.");
}
JQueryUIDatePickerHelper is class which maps C# date format with
JQuery Datepicker format.
public static class JQueryUIDatePickerHelper
{
public static string GetDateFormat()
{
string currentFormat = Config.DateFormat;
currentFormat = currentFormat.Replace("dddd",
"DD");
currentFormat = currentFormat.Replace("ddd",
"D");
if
(currentFormat.Contains("MMMM"))
{
currentFormat = currentFormat.Replace("MMMM",
"MM");
}
else if
(currentFormat.Contains("MMM"))
{
currentFormat = currentFormat.Replace("MMM",
"M");
}
else if
(currentFormat.Contains("MM"))
{
currentFormat = currentFormat.Replace("MM",
"mm");
}
else
{
currentFormat = currentFormat.Replace("M",
"m");
}
currentFormat = currentFormat.Contains("yyyy")
? currentFormat.Replace("yyyy", "yy") : currentFormat.Replace("yy", "y");
return currentFormat;
}
}