简体   繁体   English

如何使用Watin框架从日期字段中选择日期

[英]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 谁能告诉我如何使用WatiN在这些日期字段中自动选择一个日期

( http://www.meanfreepath.com/javascript_calendar/livedemo.html ) I have given this site as an example. http://www.meanfreepath.com/javascript_calendar/livedemo.html )我已经以这个网站为例。

I have tried to select a random date as 我试图选择一个随机日期作为

foreach (SelectList sl in lists)
{
    OptionCollection oc = sl.Options;
    temp = random.Next(oc.Count);
    oc[temp].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. 您有两个选择:可以使用Filter方法或对所有表单元格进行for循环。 I'll be using Filter below as it takes fewer lines of code. 我将在下面使用“ Filter ,因为它需要较少的代码行。

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. 查看Calendar的代码,您应该看到它是一个基本的HTML表,并且该单元格由CSS类很好地定义。

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 通过过滤CSS类来选择一天,期望的文本是随机数

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 . 注意 :我猜当前日期是周末的CSS类可能会比较糟糕 This would need to be verified and RegExs updated as needed. 这需要进行验证,并根据需要更新RegExs。 Note : After you click on a day, the CSS class changes to *cell_selected*. 注意 :单击一天后,CSS类更改为* cell_selected *。

The above is tested and good using WatiN 2.1 and IE8 上面经过测试,使用WatiN 2.1和IE8效果良好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM