编码的UI播放 - 在特定文本框中输入文本时抛出错误(带有javaScript过滤按键) [英] Coded UI Playback - error thrown when entering text in particular textbox (w/ javaScript filtering keystrokes)

查看:59
本文介绍了编码的UI播放 - 在特定文本框中输入文本时抛出错误(带有javaScript过滤按键)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始编写一些编码ui测试,当我尝试在一个文本框中输入一个值时,我在回放期间遇到了一个问题,该文本框仅通过javaScript函数限制为数字(十进制)值。我已经将脚本识别为罪魁祸首,因为测试在禁用时成功运行。



根据函数的约束,我在测试中输入的值是合法的,所以我不明白为什么在测试运行器进入时会抛出异常它。有人可以提供任何见解吗? javascript函数似乎适用于我们的目的,所以为了使自动化测试工作而必须删除它似乎是不幸的。



以下是一些更多细节:



我的测试相当简单。我找到了2个文本框控件,并验证第一个缺少值时是否显示正确的验证消息。设置OrderIdTextBox.Text有效,但设置AmountTextBox.Text会抛出异常(当启用下面的js函数时):

  [TestMethod] 
public void Should_show_validation_error_if_orderId_is_missing()
{
_page.OrderIdTextBox.Text =;
if(_page.AmountTextBox.Exists)
{
_page.AmountTextBox.Text =10.00;
}
Mouse.Click(SubmitButton);
Assert.AreEqual(true,_page.OrderIdValidationSpan.Exists);
}

这是页面上的javascript过滤掉了不可接受的击键:

  $(document).on('keydown',。number,function(event){
//允许:退格,删除,制表,转义,输入和小数
if(event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || event.keyCode == 110 || event.keyCode == 190 ||
//允许:Ctrl + A
(event.keyCode == 65&& event.ctrlKey === true)||
//允许:主页,结尾,左,右
(event.keyCode> = 35&& event.keyCode< = 39)) {
//让它发生,不做任何事情
返回;
}
else {
//确保它是一个数字并停止按键
if(event.shiftKey ||(event.keyCode< 48 || event.keyCode> 57)&&(event.keyCode< 96 || event.keyCode> 105)){
event.preventDefault();
}
}
});

以下是我的测试在播放期间尝试设置AmountTextBox值时抛出的异常。

 测试方法(命名空间被剪切).Should_show_validation_error_if_orderId_is_missing抛出异常:
Microsoft.VisualStudio.TestTools.UITest.Extension.PlaybackFailureException:无法执行'控件上的文本SetProperty值为10.00'。其他详细信息:
TechnologyName:'Web'
ControlType:'编辑'
Id:'金额'
名称:'金额'

TagName:' INPUT'
---> System.Runtime.InteropServices.COMException:来自HRESULT的异常:0xF004F006


解决方案

尝试

  Playback.PlaybackSettings.SendKeysAsScanCode = true; 

这个类似的问题( https://stackoverflow.com/questions/16239673/exception-from-hresult-0xf004f006 )导致go4answers话题提出上述建议。



默认播放将键作为Unicode发送到任何应用程序。可能需要将键作为扫描码发送到某些应用程序。我们可以通过使用过载的Keyboard.Sendkeys函数来实现。 / p>

来自 http://blogs.msdn.com/b/vstsqualitytools/archive/2010/04/13/uitest-framework-in-visual-studio-2010 -part-2.aspx


I'm just starting to write some coded-ui tests, and I have run into a problem during playback when I try to enter a value into a textbox which is constrained to only numeric (decimal) values via a javaScript function. I've identified the script as the "culprit" because the test runs successfully when it is disabled.

The value I've entered in the test is legitimate per the constraints of the function, so I don't understand why the exception is being thrown when the test runner enters it. Can anyone offer any insight? The javascript function seems to work for our purposes, so it would seem unfortunate to have to remove it just to make the automated tests work.

Here are some more details:

My test is fairly straight-forward. I'm finding 2 textbox controls and verifying that the proper validation message appears when a value is missing from the first. Setting the OrderIdTextBox.Text works, but setting the AmountTextBox.Text throws the exception (when the js function below is enabled):

[TestMethod]
public void Should_show_validation_error_if_orderId_is_missing()
{
    _page.OrderIdTextBox.Text = "";
    if (_page.AmountTextBox.Exists)
    {
        _page.AmountTextBox.Text = "10.00";
    }
    Mouse.Click(SubmitButton);
    Assert.AreEqual(true, _page.OrderIdValidationSpan.Exists);
}

This is the javascript on the page which filters out unacceptable keystrokes:

$(document).on('keydown', ".number", function (event) {
    // Allow: backspace, delete, tab, escape, enter, and decimal
    if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || event.keyCode == 110 || event.keyCode == 190 ||
    // Allow: Ctrl+A
            (event.keyCode == 65 && event.ctrlKey === true) ||
    // Allow: home, end, left, right
            (event.keyCode >= 35 && event.keyCode <= 39)) {
        // let it happen, don't do anything
        return;
    }
    else {
        // Ensure that it is a number and stop the keypress
        if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
            event.preventDefault();
        }
    }
});

And here is the exception that's thrown when my test tries to set AmountTextBox value during playback.

    Test method  (namespace snipped).Should_show_validation_error_if_orderId_is_missing threw exception: 
    Microsoft.VisualStudio.TestTools.UITest.Extension.PlaybackFailureException: Cannot perform 'SetProperty of Text with value "10.00"' on the control. Additional Details: 
    TechnologyName:  'Web'
    ControlType:  'Edit'
    Id:  'Amount'
    Name:  'Amount'

TagName:  'INPUT'
 ---> System.Runtime.InteropServices.COMException: Exception from HRESULT: 0xF004F006

解决方案

Try

Playback.PlaybackSettings.SendKeysAsScanCode = true;

This similar question (https://stackoverflow.com/questions/16239673/exception-from-hresult-0xf004f006) lead to a go4answers topic which suggested the above.

"Playback by default sends keys to any application as Unicode. It might be necessary to send keys as scancode to certain applications. We can do this by using an over load of Keyboard.Sendkeys function."

From http://blogs.msdn.com/b/vstsqualitytools/archive/2010/04/13/uitest-framework-in-visual-studio-2010-part-2.aspx

这篇关于编码的UI播放 - 在特定文本框中输入文本时抛出错误(带有javaScript过滤按键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆