开发者

How to select date from date fileds using watin framework

Can any one tell me how to select a date in these date fields automatically by using WatiN

(http://www.meanfreepath.com/javascript_calendar/livedemo.html) I have given this site as an example.

I have tried to select a random date as

foreach (SelectList sl in lists)
{
    OptionCollection oc = sl.Options;
    temp = random.Next(oc.Count);
    oc[te开发者_C百科mp].Select();
}

It doesn't work as date fields are not selection lists first of all date fields are not selection lists

Is there any other way to solve this puzzle by clicking on the date field an selecting a random value from it?


You've got a couple options: You can use the Filter method or do a for loop through all the tablecells. I'll be using Filter below as it takes fewer lines of code.

Looking through the code for the Calendar, you should see it is a basic HTML table and that the cells are well defined by CSS classes.

Goal of the example

Pick a random day in the current month

Pseudocode

  • Go to the page
  • Figure out the number of days in the current month
  • Generate a random number base on the number of days in the month
  • Pick the day by filtering on the CSS class and the expected text being the random number

Actual Code

Settings.HighLightElement = false;   //If you don't do this, the calendar colors won't work correctly.
ie = new IE(true);

ie.GoTo("http://www.epoch-calendar.com/javascript_calendar/livedemo.html");
ie.Table("bas_cal_calendar").WaitUntilExists(5);

int totalDaysInMonth = ie.Table("bas_cal_calendar").TableCells.Filter(Find.ByClass(new Regex(@"wkday|wkend|wkday\scurdate"))).Count;

Random random = new Random();
int randomDay = random.Next(1,totalDaysInMonth + 1);

ie.Table("bas_cal_calendar").TableCell(Find.ByClass(new Regex(@"wkday|wkend|wkday\scurdate")) && Find.ByText(randomDay.ToString())).Click();

Note: I'm guessing the CSS class when the current date is a weekend day would be wkend curdate. This would need to be verified and RegExs updated as needed. Note: After you click on a day, the CSS class changes to *cell_selected*.

The above is tested and good using WatiN 2.1 and IE8

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜