LeanFT C# automation; Clicking on a wpf button control throwing exception

大城市里の小女人 提交于 2021-01-28 06:08:21

问题


Getting HP.LFT.SDK.GeneralReplayException: One or more specified arguments are not valid, while trying to click on a wpf button (using LeanFT with C# integrated in Visual Studio 2015 )

Given the code below:

// Identify the "LicensingButton" button
var LicensingButton = objAdminApplicationModel.wnd_Adminstration.Describe<IButton>(new ButtonDescription
    {
        Text = @"Licensing",
        ObjectName = @"Licensing"
    });
// Click the Licensing button.
LicensingButton.Click();

But I am getting below exception

Exception is HP.LFT.SDK.GeneralReplayException: One or more specified arguments are not valid.
   at HP.LFT.SDK.Core.ClassModel.TestObjectExecuterBase.HandleReplayError(Int32 errorCode, IDictionary`2 data)
   at HP.LFT.SDK.Core.Communication.CommunicationClient.HandleError(Action`2 onError, Int32 status, IDictionary`2 data)
   at HP.LFT.SDK.Core.Communication.CommunicationClient.Send(String messageType, IDictionary`2 data, Action`2 onError)
   at HP.LFT.SDK.Core.ClassModel.TestObjectExecuter.ExecuteMethod(String methodName, Object[] arguments)
   at HP.LFT.SDK.Core.ClassModel.TestObjectBase.ExecuteMethod(String methodName, Object[] arguments)
   at HP.LFT.SDK.ClickBehaviour.Click(MouseButton button)
   at HP.LFT.SDK.UiObjectBase.<>c__DisplayClassd.<Click>b__c()
   at HP.LFT.SDK.OperationExecutionWrapper.ExecuteWithEvents(ITestObject testObject, Object additionalInfo, Action innerAction, MethodBase methodInfo, Boolean reportOnlyOnError, Object[] arguments)
   at HP.LFT.SDK.OperationExecutionWrapper.ExecuteWithEvents[T1](Action innerAction, Action`1 originalMethod, T1 param1, Boolean reportOnlyOnError, ITestObject testObject, Object additionalInfo)
   at HP.LFT.SDK.UiObjectBase.Click(MouseButton button)
   at Admin4DM.Test.Licensing.Licensing_VerifyStaticTextDisplay() in C:\Source\Automation\Test\Licensing.cs:line 32

回答1:


The exception is indeed misleading. On a first look I thought the ButtonDescription is incorrectly constructed, meaning that one of Text or ObjectName property expects some other value.

But that's not the case.

The problem lies entirely in the click operation. As you can see when inspecting the Click method, it has two overloads:

  1. Expecting a MouseButton enum;

    When we call Click and we don't pass the enum or the object, MouseButton.Left enum value is used by default but you can also specify .Middle or .Right.

  2. Expecting a ClickArgs object, in the form of:

    new ClickArgs {
        Button = MouseButton.Left,
        Location = Position.Center
    }
    

    Location indicates where to click the button. (.BottomLeft, .BottomRight, .Center, .TopLeft and .TopRight).

    Actually if we use the MouseButton overload, it still uses the Position but clicks on Position.Center by default.

Now that the theory is over (phew), let's see what happens in practice.

The exception we see shows us that something obviously went wrong when clicking on that button, more specifically an exception was thrown while attempting to click on Position.Center with the MouseButton.Left. As .Center is calculated based on button's width and height property, probably there is some issue with the button that causes wrong calculations (unfortunatelly I can only assume this, I can't tell you for sure).

By the way, if this happens on any button of the AUT you're testing, most probably the devs are doing something wrong, as it's not common (for example, on the WPF I'm testing, I don't have the issue on any of the buttons).

What we can do is to attempt the followings:

  1. Try clicking with .Middle or .Right;
  2. Try clicking in other positions of the button using the Position enum;
  3. Try clicking in other positions of the button using HP.SDK.LFT.Mouse;

    // Use Mouse class to click on the button in a fine tuned location
    var loc = LicensingButton.AbsoluteLocation;
    var p = new System.Drawing.Point(loc.X + 5, loc.Y + 5);
    
    Mouse.Click(p, MouseButton.Left);
    
  4. Try identifying the object as Insight image-based identification;

  5. Try identifying the object using VRI;
  6. Try identifying some parent object (if possible) and execute the Click operation in such a way that it "hits" the button you want to actually click


来源:https://stackoverflow.com/questions/48269906/leanft-c-sharp-automation-clicking-on-a-wpf-button-control-throwing-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!