Telerik RadDatePicker 的自动控制

发布于 2024-12-08 23:09:11 字数 419 浏览 1 评论 0原文

我正在使用 WatIn 为使用 Telerik 控件的 Web 应用程序创建自动化测试:我的第一个挑战是驱动 Telerik 日期控件 - RadDatePicker。

仅仅在最可能的输入字段中输入文本是行不通的:在表单发布时,该值将重置为空白。所以我想我需要一组更复杂的自动化交互 - 例如,我发现 Telerik 站点上的此线程 讨论如何从 Telerik 组合框自动进行选择。

谁能提供我需要的神奇互动组合?

(我确信这个答案会对使用任何自动化测试工具的任何人有所帮助,因此我也用其他几个测试框架标记了这个问题:-))

I'm using WatIn to create an automated test for a Web App that uses Telerik controls: my first challenge is to drive a Telerik date control - a RadDatePicker.

Simply entering the text in the likeliest input field doesn't work: the value is reset to blank as the form is posted. So I imagine I need a more complex set of automated interactions -- for example, I found this thread on the Telerik site discussing how to automated a selection from a Telerik combo box.

Can anyone supply the magic combination of interactions I need?

(I'm sure that the answer would help anyone using any automated test tool, hence I've also flagged this question with a couple of other test frameworks too :-))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

请恋爱 2024-12-15 23:09:11

我正在使用 Selenium RC,几天前也遇到了类似的问题。我花了很长时间才找到正确的方法,所以我想我会分享对我有用的解决方案:(

注意:我无法使用 $find)

在 RadDatePicker 上设置日期的 javascript 是:

var appBrowser = this.browserbot.getCurrentWindow();
var datepicker = appBrowser.yourDatePickerId; //note: no quotes
var selectDate=new Date();
selectDate.setFullYear(yourYear,yourMonth,yourDay);
datepicker.SetDate(selectDate);
datepicker.DateInput.SetDate(selectDate);

然后使用selenium GetEval 在您的测试中调用 javascript 代码来设置日期:

selenium.GetEval("javascript here");

您绝对应该将其包装在一些参数化的帮助程序类中,每次您想要通过传递 id 来设置测试中的日期时,该类都会为您生成 javascript 代码控制和日期。

I'm using Selenium RC and had a similar problem few days ago. It took me ages to find the right way of doing it so I thought I will share the solution that worked for me:

(NOTE: I couldn't use $find)

The javascript to set the date on the RadDatePicker is:

var appBrowser = this.browserbot.getCurrentWindow();
var datepicker = appBrowser.yourDatePickerId; //note: no quotes
var selectDate=new Date();
selectDate.setFullYear(yourYear,yourMonth,yourDay);
datepicker.SetDate(selectDate);
datepicker.DateInput.SetDate(selectDate);

and then use selenium GetEval in your test to call javascript code to set the date:

selenium.GetEval("javascript here");

You should definitely wrap it around some parametrised helper class that will generate the javascript code for you each time you want to set the date in the test by passing id of the control and date.

饮惑 2024-12-15 23:09:11

我终于有了一个可行的解决方案。关键是使用 javascript 调用 telerik 控件的客户端 API。所有复杂的 Telerik 控件(例如 RadInput)都需要相同的技术。

我使用了 WatiN 网站上推荐的技术来编写自己的控件 http://watinandmore.blogspot.com/2009/12/wrapping-complex-logic-in-control.html 得出这个:

public class TelerikDatePicker : Control<TextField>
{
    public DateTime? Value
    {
        get
        {
            var jScript = string.Format(@"$find(""{0}"").get_selectedDate();", Element.Id);
            var selectedDateStr = Eval(jScript);
            return TranslateJavascriptDateStringIntoDateTime(selectedDateStr);
        }

        set
        {
            var jScript = string.Format(@"$find(""{0}"").set_selectedDate(new Date(""{1:MMMM dd,yyyy}""));", Element.Id, value);
            Eval(jScript);
        }
    }

    public void SetValue(DateTime? value)
    {
        if (value.HasValue)
            Value = value.Value;
        else
            Clear();
    }


    public void Clear()
    {
        var jScript = string.Format(@"$find(""{0}"").clear();", Element.Id);
        Eval(jScript);
    }


    private string Eval(string script)
    {
        return Element.DomContainer.Eval(script);
    }

    public bool IsEnabled()
    {
        var jScript = string.Format(@"$find(""{0}"").get_enabled();", Element.Id);
        return Eval(jScript) == "true";
    }

    private DateTime? TranslateJavascriptDateStringIntoDateTime(string jsDateStr /*E.g. Mon Mar 12 00:00:00 UTC+0100 2012*/)
    {
        if (String.IsNullOrEmpty(jsDateStr) || jsDateStr == "null") return null;

        var abbreviatedMonth = jsDateStr.Substring(4, 3);
        var dayOfTheMonth = jsDateStr.Substring(8, 2).TrimEnd(' ');
        var year = jsDateStr.Substring(jsDateStr.Length-4, 4);

        const string format = "d MMM yyyy";
        var dateStr = dayOfTheMonth + " " + abbreviatedMonth + " " + year;

        return DateTime.ParseExact(dateStr, format, CultureInfo.InvariantCulture);
    }
}

I finally have a solution that works. The key is to use javascript to call the client-side API for the telerik control. The same technique is required for all complex Telerik controls, e.g. RadInput.

I used the technique recommended on the WatiN website for writing your own control http://watinandmore.blogspot.com/2009/12/wrapping-complex-logic-in-control.html to come up with this:

public class TelerikDatePicker : Control<TextField>
{
    public DateTime? Value
    {
        get
        {
            var jScript = string.Format(@"$find(""{0}"").get_selectedDate();", Element.Id);
            var selectedDateStr = Eval(jScript);
            return TranslateJavascriptDateStringIntoDateTime(selectedDateStr);
        }

        set
        {
            var jScript = string.Format(@"$find(""{0}"").set_selectedDate(new Date(""{1:MMMM dd,yyyy}""));", Element.Id, value);
            Eval(jScript);
        }
    }

    public void SetValue(DateTime? value)
    {
        if (value.HasValue)
            Value = value.Value;
        else
            Clear();
    }


    public void Clear()
    {
        var jScript = string.Format(@"$find(""{0}"").clear();", Element.Id);
        Eval(jScript);
    }


    private string Eval(string script)
    {
        return Element.DomContainer.Eval(script);
    }

    public bool IsEnabled()
    {
        var jScript = string.Format(@"$find(""{0}"").get_enabled();", Element.Id);
        return Eval(jScript) == "true";
    }

    private DateTime? TranslateJavascriptDateStringIntoDateTime(string jsDateStr /*E.g. Mon Mar 12 00:00:00 UTC+0100 2012*/)
    {
        if (String.IsNullOrEmpty(jsDateStr) || jsDateStr == "null") return null;

        var abbreviatedMonth = jsDateStr.Substring(4, 3);
        var dayOfTheMonth = jsDateStr.Substring(8, 2).TrimEnd(' ');
        var year = jsDateStr.Substring(jsDateStr.Length-4, 4);

        const string format = "d MMM yyyy";
        var dateStr = dayOfTheMonth + " " + abbreviatedMonth + " " + year;

        return DateTime.ParseExact(dateStr, format, CultureInfo.InvariantCulture);
    }
}
〆凄凉。 2024-12-15 23:09:11

我对 RadDatePicker 也有同样的问题,在输入值后会发回空。

我设法通过以下步骤在输入字段中自动输入日期:

  1. 选择与控件关联的 dateInput 文本框。
  2. 对其执行 dateInput.MouseClick()
  3. 字段中输入日期 Manager.Desktop.KeyBoard.TypeText("1/1/1990")
  4. 执行 .MouseClick() 在页面上的任何其他元素上,例如某些 div,

我发现需要 #4 来触发事件以使控件模糊,从而“保存”其值。

然后您可以提交,并且该值应该随之而来。

I had the same issue with a RadDatePicker that would post back empty after typing in values.

I managed to automate typing in the date into the input field with these steps:

  1. Select the dateInput text box associated with the control.
  2. Do a dateInput.MouseClick() on it
  3. Type the date into the field Manager.Desktop.KeyBoard.TypeText("1/1/1990")
  4. Do a .MouseClick() on any other element on the page, e.g. some div

I found that #4 was needed to fire the events to make the control blur therefore "saving" its value.

Then you can submit and the value should go along with it.

蓝天 2024-12-15 23:09:11

解决了 SeleniumIDE 的问题 - 在使用“sendKeys”或“type”方法设置值后,必须使用“fireEvent”方法,“ControlID”的值为“blur”。奇怪的是,使用 WebDriver 进行自动化时不会出现此问题。

Solved that problem for SeleniumIDE - you have to use "fireEvent" method with value "blur" for "ControlID" after you've set value using "sendKeys" or "type" methods. Strange that this problem does not occur when automating with WebDriver.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文