简体   繁体   English

CodedUi:鼠标单击坐标

[英]CodedUi:Mouse click with coordinates

How to click on a specific portion of a button in all screen types? 如何在所有屏幕类型中单击按钮的特定部分? I have a button with dropdown values, I need to click on the downarrow image, I tried below code: 我有一个带下拉值的按钮,我需要点击向下箭头图像,我尝试下面的代码:

 Mouse.click(someBtn,new Point(250,45));

This works in my screen, this clicks somewhere else on desktops since axis changes. 这可以在我的屏幕上工作,这可以在桌面上的其他位置点击,因为轴会发生变化。 Suggest some workarounds or solutions. 建议一些解决方法或解决方案。

Try giving a position relative to the control that is being clicked, instead of an absolute point, use this property: 尝试给出相对于被点击的控件的位置,而不是绝对点,使用此属性:

 uitestcontrol.BoundingRectangle

like this: 像这样:

var btnPosition= someBtn.BoundingRectangle

and then select the position you want to click based on the controls current position. 然后根据控件当前位置选择要单击的位置。

for example: 例如:

Point relativePoint = new Point(btnPosition.X + 40, btnPosition.Y - 40);
Mouse.click(someBtn,relativePoint );

I had this problem and I found the same solution as Niels did in the comments above. 我有这个问题,我找到了与Niels在上述评论中所做的相同的解决方案。 If you do the same however, what you are actually doing is ignoring the UI elements and you are clicking on a known point on the screen. 但是,如果你这样做,你实际上在做的是忽略UI元素,然后点击屏幕上的已知点。 Your window must not move for this to work. 您的窗口不得移动以使其工作。 So the solution Niels and myself have used can be done in 1 line of code (no need for the Bounding rectangle or someBtn) like this... 所以Niels和我自己使用的解决方案可以在一行代码中完成(不需要Bounding矩形或someBtn),就像这样......

Mouse.Click(new Point(575, 920)); Mouse.Click(new Point(575,920));

The risk is that your window moves but as the Coded UI has an unclickable point for some reason I can't see any other way. 风险是你的窗口移动,但由于Coded UI由于某种原因有一个不可点的点,我看不到任何其他方式。 This line is in a method in my UIMap.cs (having moved the method there by right clicking on the method in the UIMap.uitest and choosing "Move Code to UIMap.cs"). 这行是在我的UIMap.cs中的一个方法(通过右键单击UIMap.uitest中的方法并选择“将代码移动到UIMap.cs”来移动方法)。 So I agree with Niels but if you do that, the rest of the code in that answer is not being used! 所以我同意Niels但是如果你这样做,那个答案中的其余代码就没有被使用了!

If you don't know about Moving Code to UIMap.cs then read about it, basically if you change system generated code without moving it then your code will be overwritten when you build next, leaving you wondering where your code went and why your test stopped working. 如果您不知道将代码移动到UIMap.cs然后阅读它,基本上如果您更改系统生成的代码而不移动它,那么下次构建时您的代码将被覆盖,让您想知道代码的去向以及测试的原因停止工作。 Don't ask me how I know this! 不要问我怎么知道这个!

thanks for all the lead on fixing the similar issue which i faced & struck out for two days ... My Search was How to Click on cell with hidden properties of a Grid in Windows application through CodedUI Automation 感谢所有领导修复我遇到的类似问题两天......我的搜索是如何通过CodedUI Automation在Windows应用程序中点击具有网格隐藏属性的单元格

My Sol goes as below ,.. credits @ MSDN link dude 我的索尔如下,..点@ MSDN链接老兄

public void InstantiateControls()
    {

        #region  UserSearch
        frSearchUsers = new ControlWithContainer<WinGroup>(this.SourceControl, By.ControlName("frSearchUsers"));
        txtFindUser = new ControlWithContainer<WinEdit>(this.SourceControl, By.ControlName("txtFindUser"));
        vgrdUsers = new ControlWithContainer<WinWindow>(frSearchUsers.Control.SourceControl, By.ControlName("vgrdUsers"));
        vgrdUserscUSTOM = new ControlWithContainer<WinCustom>(vgrdUsers.Control.SourceControl, By.ControlName("vgrdUsers"));

        #endregion
    }

    public void clickCell()
    {
        var cellGrid = vgrdUserscUSTOM.Control.SourceControl;
        UITestControl cellGridCell = new UITestControl(cellGrid);
        cellGridCell.SearchProperties["ControlType"] = "Cell";
        cellGridCell.SearchProperties["InnerText"] = "Dawson,Jade";            
        if (cellGridCell.TryFind())
        {
            cellGridCell.SetFocus();
            cellGridCell.Find();             

            UITestControlCollection uic = cellGridCell.FindMatchingControls();

            foreach (UITestControl ui in uic)
            {
                if (ui.BoundingRectangle.Width > 0)
                {
                    Mouse.Click(ui);
                    break;
                }

            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM