本文介绍了LeanFT C#自动化;单击WPF按钮控件引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取 HP.LFT.SDK.GeneralReplayException:一个或多个指定的参数无效,同时尝试单击wpf按钮(将LeanFT与Visual Studio 2015中集成的C#结合使用)

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 )

给出以下代码:

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

但是我变得异常例外

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

推荐答案

该异常确实具有误导性.乍一看,我认为 ButtonDescription 的构造不正确,这意味着 Text ObjectName 属性之一需要其他值.

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.

但事实并非如此.

问题完全出在点击操作上.如您在检查 Click 方法时所看到的,它有两个重载:

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

  1. 期望一个 MouseButton 枚举;

当我们调用 Click 而不传递枚举或对象时,默认使用 MouseButton.Left 枚举值,但您也可以指定.Middle .Right .

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.

期望一个 ClickArgs 对象,形式为:

Expecting a ClickArgs object, in the form of:

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

位置指示单击按钮的位置.( .BottomLeft .BottomRight .Center .TopLeft .TopRight ).

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

实际上,如果我们使用 MouseButton 重载,它仍然会使用 Position ,但是默认情况下会单击 Position.Center .

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

现在,理论已经结束了,(phew),让我们看看在实践中会发生什么.

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

我们看到的异常向我们显示,单击该按钮时显然出现了问题,更具体地说,尝试使用 MouseButton.Left Position.Center 时引发了异常./code>.由于 .Center 是基于按钮的width和height属性计算的,因此按钮可能存在一些问题,导致错误的计算(不幸的是,我只能假设这一点,我不能肯定地告诉您).

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).

我们可以做的是尝试以下操作:

What we can do is to attempt the followings:

  1. 尝试点击 .Middle .Right ;
  2. 尝试使用 Position 枚举单击按钮的其他位置;
  3. 尝试使用 HP.SDK.LFT.Mouse ;按钮单击按钮的其他位置;

  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);

  • 尝试将对象标识为 Insight基于图像的识别;

    这篇关于LeanFT C#自动化;单击WPF按钮控件引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 08-22 23:52