简体   繁体   中英

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

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. 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"). So I agree with Niels but if you do that, the rest of the code in that answer is not being used!

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

My Sol goes as below ,.. credits @ MSDN link dude

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

            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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