webdriver 日期控件的处理
http://www.cnblogs.com/liu-ke/p/4200736.html
http://blog.csdn.net/wanglha/article/details/44620627
http://www.yunjuu.com/info/271596.html
http://www.cnblogs.com/tobecrazy/p/4206742.html
http://jarvi.iteye.com/blog/1477837
http://www.cnblogs.com/devtesters/category/657772.html
在构建自动化套件的过程中,日期操作是一件很重要也很频繁的事情。有的日期控件的div层级结构复杂,同一个类型的日期控件在多个子系统中的表现形式也大相径庭。多数工程师为了避免重复的工作,会封装抽象一个方法或公共的库,还有部分会封装成单独的日期类库。
控件
下面是去啊的日期控件
下面是相关的HTML
当然,有的日期控件元素的id为对应的日期。
代码逻辑
针对这种双日期的格式,我们如何在代码中进行处理输入?
1. 输入目标日期。
2. 判断目标日期与当前日期的差值
- 目标日期的年小于当前日期的年
l 小于的情况同大于(如下)
- 目标日期的年等于当前日期的年
l 月份相差除以2,计算步长(即向后翻页的次数)
- 目标日期的年大于当前日期的年
l 计算当年剩余月份数量
l 计算从下一年开始到目标日期的月份
l 月份相加计算出总额并除以2,得出向后翻页的次数。
3. 循环点击翻页。
4. 找到相应的html标签,点击即可。
最终版的代码
internal static void InternalSelectDeliveryDate(IWebDriver driver, DateTime date)
{
DateTime expDate = ConvertToComparableDate(date);
DateTime curDate = ConvertToComparableDate(DateTime.Now);
int clicks = ((expDate.Year - curDate.Year) * 12 + (expDate.Month - curDate.Month)) / 2;
Func<By, IWebElement> exp = (condition) => { return InternalFindElement(driver, condition, TimeSpan.FromSeconds(1)); };
By by = By.ClassName("month_next");
// We currently initialize the wrapDiv, previous, next element every time.
// To avoid the invalid state exception.
if (clicks < 0)
{
clicks = (-1) * clicks;
by = By.ClassName("month_prev");
}
for (int i = 0; i < clicks; i++)
{
IWebElement navButton = exp(by);
navButton.Click();
}
IWebElement targetLinkElem = InternalFindElement(driver, By.Id(expDate.ToString("yyyy-MM-dd")), TimeSpan.FromSeconds(1));
targetLinkElem.Click();
}

浙公网安备 33010602011771号