Selenium SendKeys到两个jquery-ui datepicker文本框间歇性地没有选择输入的日期

时间:2014-07-09 08:33:29

标签: jquery date selenium textbox datepicker

我是Selenium的新手(以前用WatiN做这个东西)我想用SendKeys输入一个日期到文本框中,避免用jquery-ui datepicker选择日期。

我尝试过很多东西,但是我一直有问题,这个日期不会间歇性地被拾取。偶尔会保留原始日期或部分日期。即改变日期而不是月份 - 这是因为点击日历,这是我试图避免的。

最后,我想出了一种似乎有效的技术:

    public void EnterDate(
        FirefoxDriver driver,
        string textBoxId,
        string date)
    {
        driver.FindElement(By.Id(textBoxId)).Clear();
        driver.FindElement(By.Id(textBoxId)).SendKeys(date.Substring(0, 9));
        driver.FindElement(By.Id(textBoxId)).SendKeys(date.Substring(9));
    }

似乎以两部分输入日期似乎可以避免日期选择器弄乱输入日期的问题。首先我输入所有字符,但最后一个字符,然后我自己输入年份的最后一位数字,这样做似乎可以在文本输入相关文本框时给日历时间更新。

我想知道我在这里遗漏了什么吗?经过大量的搜索,我在其他地方找不到问题,我不得不求助于这种技术。我们欢迎任何关于不同测试技术的建议,如果不存在替代方案,我希望这个问题可以帮助某人。

修改 我再次看了这个问题,无法弄清楚发生了什么。 即使使用我上面提出的解决方案,问题似乎也会间歇性地发生。 我认为问题是我的页面上有2个日期选择器。 问题总是出现在开始日期,这是第一个选择的日期,从未在第二个日期。

我使用Selenium从日期选择器中选择日期以避免问题。 我的代码就在这里,似乎没有问题。

public static void EnterDate(
    IWebDriver driver,
    string textBoxId,
    DateTime date)
{
// Deal with the date by choosing the month, year and day from the jquery datepicker 
// Click on the date text box
driver.FindElement(By.Id(textBoxId)).Click();

// Select the month
var _selectMonth = new SelectElement(driver.FindElement(By.ClassName("ui-datepicker-month")));
_selectMonth.SelectByIndex(date.Month - 1);

// Select the year
var _selectYear = new SelectElement(driver.FindElement(By.ClassName("ui-datepicker-year")));
_selectYear.SelectByValue(date.Year.ToString(CultureInfo.InvariantCulture));

// Select the day
driver.FindElement(By.LinkText(date.Day.ToString(CultureInfo.InvariantCulture))).Click();
}

跟:

// Enter start date
MyProject.Helper.Data.Entry.Form.EnterDate(this.c_driver, MyProject.Constant.StartDateId, _test.StartDate);

// Enter end date
MyProject.Helper.Data.Entry.Form.EnterDate(this.c_driver, MyProject.Constant.EndDateId, _test.EndDate);

我仍然对使用2个日期选择器时SendKeys似乎偶尔出现问题的原因感兴趣。

0 个答案:

没有答案