Coded UI thinks control is in a different position - c#

I haven't been able to find someone else who has this same issue and it is extremely frustrating. Basically, for whatever reason, Coded UI is seeing a control in a different area then where it actually is. Like this:
Bounding Rectangle for Label with "IDLE" in it is up and to the left of the control
As you can see in the picture for some reason the Bounding Rectangle is up and to the left of the selected Label with the word "IDLE" in it. This is the first time I have seen this issue and after doing like 50 googles searches I decided to come here.
First off I am using Coded UI with a Windows Forms Application (I don't know if that makes a difference), it seems to do the same thing with all Windows Forms Applications, it has worked fine for all my WPF applications.
Second, no matter how many times I record my click on a button in my application or something of the sort, running the recorded method will always click off the button (due to it thinking the button is positioned up and to the left of the control). Up and to the left seems to be the only consistent thing, how much up and to the left varies based off of where the application is on the screen.
If anyone has any ideas or something I can try please help, I have no idea what is going on. If you need anymore information feel free to ask.
Thanks
Edit for Code sample:
public class Controller : WinWindow
{
#region Constructors
public Controller()
{
SearchProperties[PropertyNames.Name] = "My Sample C#";
SearchProperties.Add(new PropertyExpression(PropertyNames.ClassName, "WindowsForms10.Window", PropertyExpressionOperator.Contains));
WindowTitles.Add("My Sample C#");
enableWindow = new EnableWindow(this);
}
#endregion
public WinEdit CommunicationSetup
{
get
{
if(communicationSetup == null)
{
communicationSetup = new WinEdit(this);
communicationSetup.SearchProperties[PropertyNames.ControlName] = "Communication";
communicationSetup.WindowTitles.Add("My Sample C#");
}
return communicationSetup;
}
}
private WinEdit communicationSetup;
}
Extra Edit:
To Click on the thing I've used this:
Mouse.Click(new Point(obj.BoundingRectangle.Location.X + 20, obj.BoundingRectangle.Location.Y + 10));
and This:
Mouse.Click(obj);
Both click up and to the left of the control, as expected since the BoundingRectangle show there as seen in the picture
Edit: To replace "right" with "left" because I'm clearly an idiot and don't know my directions lol
Edit: Here is some more pictures showing the DrawHighlight() for the button Enable on my application, sorry I have to take a ton of information out for company policy reasons which might make it a little difficult to see what everything is going on but I have to
I also moved the application around so you can see how much of a difference DrawHighlight() has when the application is moved
Application dragged further down and to the right

what is stored in the obj variable that you try to pass to Click() method?
The problem might occur because you have not enough unique properties, who may definitely describe the control. Try to add additional SearchProperties. The most valuable would be a Control ID or Automation ID. If you cannot obtain this property on the wanted control, ask your development team to add it.
Try also to debug the method. Investigate of is returned in BoundingRectangle property of the control. Try out DrawHighlight() method. It will mark the position of the control with a blue rectangle. Another option would be to use TryGetClickablePoint() method. Maybe your control's size is bigger than you can visually obtain. The last but not least: examine your control from UIMap Editor and its whole hierarchy. You might find that the control you are looking for was badly recorded.
If nothing helps, then provide some more information and results of your investigation.
Good luck with that one.
Alex

I figured a consistent way to solve my problem, thanks to #Loathing for his comments on my question. Here is how I solved it:
var ret=new Rectangle();
GetWindowRect(obj.WindowHandle, ref ret);
Mouse.Hover(new Point(ret.X, ret.Y));
I still am not sure why this works so if someone sees this and understands feel free to comment and explain, I'm a little fuzzy on what goes on with GetWindowRect.

I recently came across some interesting results when trying to debug cross browser tests between IE and chrome that similarly mimicked what you've reported. It turns out that CodedUI and IE seems to be sensitive to the display scaling. I was using a multi monitor setup with different resolutions and changing each's scaling, using mostly 125%. The control's top and left coordinates were always off in IE and I could never figure out why. After doing some math and dividing by 125%, sure enough the number matched the coordinates it was supposed to be reporting. If I tried to change the monitor scaling without restarting windows, the issue got worse and I would also miss clicks. I imagine your solution to GetWindowRect appears be DPI sensitive as well, and reporting the correct coordinates with scaling taken into account.
Link to microsoft page that loosely discusses this:
Understanding Screen Scaling Issues

I get this all the time and the only way I managed to resolve it is restarting windows (go figure yet another MS solutions with Restart of the entire OS). Anyhow, in my case, it could be working fine, but later I would RD into my desktop from a laptop and everything would go nuts with this issue. I had to restart when working directly with PC and restart again when working from RD session.

I have had this issue fairly frequently as well. In my case whenever my code tried to download a file, the browser would popup the little download bar at the bottom. From then on, all my location rectangles would be off by the height of that bar.
My guess is that it is a bug in Microsoft's CodedUI that is unable to reset itself when the window it is testing changes sizes in certain cases. I have found a lot of bugs in CodedUI, I really hope that some day they get it more stable.
The fix for my issues so far has been to close the Internet Explorer window I was using to run the tests and restart it. CodedUI can't even get this right though, so I go out and kill all iexplorer.exe processes then re-start the browser.

Related

VisualStudio.TestTools.UITesting.Mouse Click cursor misplaced

I'm trying to execute the UI test cases from my project (this is testing a web site), and they are calling several times:
Mouse.Click(UITestControl, Point);
I also tried calling:
Mouse.Click(UITestControl);
Everything is working fine in a couple of other devs machines, but in mine it seems to be some offset in the coordinates, the cursor is not moving to the right position of the controls is supposed to click on, has anybody else dealt with this kind of issue? Is there some configuration that can be used to make the click points more generic?

How do I profile ShowDialog?

I took over a Keyboard hook project and have been sprucing it up a bit. It used to be just 4 forms that had all the logic and maths in those 4 files. So MANY hours later I just about got the program back together after decoupling everything. The latest bit is by using the Model-View-Presenter design concept on the main window. All my tests work great on my machine, and naturally the program performs like it should on the target computers.
The target computers are simple x86 Intel Atom processors with 2gb of ram. They have buttons on them that correlate to F13-F18 on a keyboard (that is where my keyboard hook program comes into play). My development PC is a semi-stout 6-core AMD x64 machine with 8gb of ram. One thing I wanted to do was test out a way of setting those Function buttons to a particular task. All my unit tests pass for this portion. When I click on a new task type it works as desired.
"Cool", I say, "Lets see it in action" so i hit run in Visual Studio. I my program that F9-F12 are the keys to hook. Now the assignment window only opens up when you press one of the hooked keys when the main window is open. If that main window is not open then the task is performed. So I open my main window and hit F9 and the Assignment window shows but i can't click on anything for about 4 seconds. Then it "catches" up and does what ever I clicked on. I thought maybe it was my debugger so i went to the bin folder and ran the application directly and same deal.
I thought maybe it was a permissions thing so I ran as admin and same problem. I wondered how bad it would be on the target computers. I copied my bin folder to a flash drive, copied the content into Program Files and ran it. It worked great (well to a certain degree) But the problem was not present. I can select any task I want immediatly after the dialog is visible.
One strange thing is though that my keyboard hook stops working after 11 times of trying to reassign it...but that is a separate question. So i thought I remember someone saying, use a profiler. Well my visual studio had one so I turned it on but i can't make heads or tails of the data returned. I'd post code, but I don't know what to post. I even went as far as using System.Console.Write("."); around the area I thought was the problem but that wasn't the problem
protected override void theHook_RegisteredKeyPressed(System.Windows.Forms.Keys key)
{
if (TakeOver)
{
if (!Busy)
{
busy = true;
LibraryTrace.Start("NewKeyAssignment", key);
using (var window = new AssignmentTypePanel())
{
presenter = new TaskPresenter(window, theList[key].Task);
window.Presenter = presenter;
//execution will not continue past here until window is closed
window.ShowDialog();
}
theList[key] = new TaskKey(key, presenter.Task);
LibraryTrace.End("NewKeyAssignment", theList[key].Task.ToString());
busy = false;
}
}
else
{
base.theHook_RegisteredKeyPressed(key);
}
}
So i'm stuck. I can't figure out what to type, what to ask. Any tips on how i can find the root of this problem?
I had a similar problem with an application i developed using winforms- the 'server-side' operation was lightning-quick, but still the form showing the results took ~5 seconds to show.
Upon investigation, it turns out that winforms has some known performance issues.
There are some suggestions for optimization (like here or here and a few others i've found at the time but can't seem to locate right now).
Actually, none of them helped in my case.
I ended up realizing that the real bottleneck was creating and displaying a new form; what I ended up doing was a bit of a hack-
I placed ALL of my controls on the main form, with some of them hidden, and simulated a change of forms by hiding some controls and displaying others.
It's quite ugly, and won't scale up, but worked very well in my case.

remote desktop application with wrongly displayed buttons

I have a winform within a remote desktop application that is built dynamically with a container panel for a toolbar with large (regular) buttons. The application is writen in C# and .NET 4.0. The application is viewed as it should only if I do not maximize the window (meaning restored view is fine).
Now, after RDP login - within most client computers - the application looks fine (even when we maximize and restoring). Within low resolution client computers (800x600), the buttons on the toolbar are not displayed in the right position (such as in the 1024x768 clients).
I used double buffering and it didn't help... the problem may or may not be depended on the screen resolution (it can be memory issue or something). It is not a problem of localization layout.
The following image shows the Fine and Bad states: http://imgur.com/gO4UJ,v7O9K
What can I do to resolve this issue?
OK, so in case anybody gets to this kind of case, this is the thing that made the issue:
In the form, there was an empty repaint override. The programmer that created the form was overriding the repaint with an empty method (don't know why, I guess I'll ask my team for the business logic).
How did I reproduce it in the development environment? Following Albin Sunnanbo's note, I ran the RDP application in minimized version during initialization, and I noticed that it occurs with large resolution clients also in this use case... So, I know repaint occurs while performing a window resize - and this made me suspicious about the method...
==> On a side note, I guess this is why inheritance is so bad, and going on to WPF is better. It took me 3 days to understand why this issue occurred.

How to get a value of a RadioButton from a WinForm application that I don't have an API for?

I have a small calculation system that will be installed in multiple PCs. Those PCs have a program installed in them and this program is always open (but minimized).
I need to get the value of a 'RadioButton' in this program but I can't access it!
I've tried to get an API for this application but the company developed it refused to co-operate. I've got a crazy idea, you can refer to my question regarding this idea Click It and the last thing is to get the cache for the button but I think this is going to work with WebApplications only!
Any help would be Great! =) .. And btw I decided to build my application as web application but then I changed my mind to build it as a WinForms because of the screenshot. But if I got a better and a more clean solution I'd go back to WebApp
Screen grabbing is not a good option. You should be able to use FindWindow based methods to get hold of the radio button if the app uses windowed controls. If not then UI Automation may very well be able to get the value of the radio button. Whatever it's usually possible to read UI state out of another application with methods like this, and much easier than screen scraping.

"Red Cross" problem on MenuStrip and ToolStrip

I have a piece of software that has worked fine on many machines, althoughon one machine there is a problem that appears to occur occasionaly, the MenuStrip and the ToolStrip both appear as a blank white background with a red cross over it, as a custom control would if you created a null object. This doesn't happen whilst I am debugging and I don't know how to make the problem re-occur, but it does happen. I was wondering if anyone knew what could be the problem?
Would it be the version of the .NET framework?
Thanks
This is a common occurrence when there's a GDI+ problem ("The Red X of Death"). Are you doing any custom drawing in OnPaint? Or perhaps there's a graphic resource or a glyph which is corrupt or being improperly disposed of.
Edit: I've re-read your question. You seem to have this only on one of the machines. I've googled around a bit for this, and I stumbled upon this old thread. The post at the bottom suggests that there might be an issue with Virtual Memory turned off:
We did manage to solve this - we were
seeing the problem on a device running
XP embedded. The XPe image developer
had turned off Virtual Memory and as
soon as we turned it on the problem
went away. I believe it is just a
symptom of the system running out of
memory to display the graphics (maybe
particularly if you use a lot of
double buffering)
Hope that helps.
Sounds like a symptom of an Out Of Memory Exception to me.
Edit:
Which can sometimes lead onto a System.InvalidOperationException: BufferedGraphicsContext
Are you trying to update the GUI controls from a thread other than the GUI thread? Combine the cross thread operation with an exception handler that swallowed everything and we had the behavior you describe (on a grid control, not a menustrip bar) on an app I was maintaining.
Definitively sounds like a cross-thread problem.
Make sure you use Invoke when accessing controls and/or firing events from a thread that is not the main UI thread.
Seeing this happen on just one computer of more than a 1000 that have our prouducts.
On that one computer I am seeing a .NET 3.5 program occassionally show the red X on its datagrid. And another far simpler .NET 2.0 program got the red X on its menuStrip. I only have source code for the simpler program but I can say that there isn't any user code at all which affects that component. No cross-thread stuff because nothing updates it. It's contents are set at development time with one item added to it at program load. The Red X failure was well after program load.
I was very surprised to see the problem across two different frameworks and on one program that has no data bindings. I am very wishfully hoping that the computer has its virtual memory turned off.
If it isn't that then any guidance on system parts that are shared across .NET 2.0 and .NET 3.5 would be appreciated.
Update: The user with the problem retired the computer and replaced it (which solved the problem)

Categories

Resources