Coded UI Playback - error thrown when entering tex

2019-05-28 19:52发布

问题:

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

回答1:

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



回答2:

You could also try:

if (_page.AmountTextBox.Exists)
    {
        Mouse.Click(_page.AmountTextBox);
        Keyboard.Sendkeys("10.00");
    }