How to know where the cursor is in a WpfEdit field with CodedUI automation?
Asked Answered
B

2

6

On the CodedUI WpfEdit class there is a way to get the selected text, but I cannot find a way to get the cursor position when nothing is selected (i.e. the index of the caret in the text). Is there anything available for that in the CodedUI framework?

My goal is to assert the position of the cursor in the text contained by the control.

Berner answered 25/3, 2015 at 16:58 Comment(0)
S
0

There isn't a codedui method for that but try the following: add the reference:

 using System.Windows.Forms

in the code where you need to get the moue coordinated type:

Point p = new Point(Cursor.Position.X, Cursor.Position.Y);

remember that this is not a relative position to the control but the position of the mouse on the screen.

calculating the position of the point relative to control shouldn't be much problem.

Sixpence answered 29/3, 2015 at 16:11 Comment(1)
Sorry my post wasn't very clear, I edited it: I actually meant the position of the caret in the text content of the field – Berner
C
0

I'm not sure there is a way, and I would imagine that there is a different requirement than actually finding the cursor position.

If you are trying to insert some text, you can always copy the text out to the test method, insert the text, and write it back.

Or, if you need to not do that, you could always use the Keyboard.SendKeys method to send a home command and then any number of right arrow commands you need to place the cursor where you'd like it.

Can you elaborate further as to what exactly you need with the cursor position?

Cathrinecathryn answered 6/6, 2016 at 2:18 Comment(4)
Edited. Note that we did something similar actually as a workaround. You just send a key to insert a character in the field, then assert that the content of the field is what you would expect if the cursor had been at the position you want e.g. say you know the text is "ab" and you want to assert that the cursor is between the a and the b you send c and assert that the text is "acb" – Berner
I see your edit, but is that really a requirement to assert where the cursor is in the text field? ie, does the application do something that requires that position be something specific? I'm trying to figure out what the purpose is for this assertion. Text boxes work how they work and it's not really necessary to test for the cursor unless your app is doing something or has a requirement about the position. – Cathrinecathryn
Yes believe me I would do something else with my working time if it was not a requirement 😌. Think an application with a complex UI where time is critical and thus can entirely be driven with a keyboard and where every action needs to happen in the minimal number of key presses. – Berner
Dang. The workaround you are using is the only way I could think. If you do find out another way, please post it back here!! :) – Cathrinecathryn

© 2022 - 2024 β€” McMap. All rights reserved.