Coded UI tests not correctly finding my control - c#

I'm busy running a proof of concept on what should be a very basic coded UI test.
My application is Winforms, I have a form that allows you to log in to the application.
Here 2 controls exist called _textUsername and _textPassword respectively.
To simplify the whole thing, I want playback to be able to double click the username text field (_textUsername).
However, during playback the _textPassword is selected.
I've tried to adjust the search criteria to include the control name , but then it fails to find the control at all and fails.
My question is simple: I have 2 controls on my form : _textUsername and _textPassword, UI coded tests seems to always find the _textPassword, how can I get it to find the other text box instead?

Try manually coding the controls. You can use the UI Test Builder to find the search properties. inspect.exe is also useful. Sometimes the properties aren't what you expect.
// Controls
WinWindow logonWindow = new WinWindow();
WinEdit _textPassword = new WinEdit(logonWindow);
WinEdit _textUsername = new WinEdit(logonWindow);
// Add search properties and configurations
logonWindow.SearchProperties[WinWindow.PropertyNames.Name] = "Main Window Name";
logonWindow.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
_textPassword.SearchProperties[WinEdit.PropertyNames.Name] = "Password";
_textPassword.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
_textUsername.SearchProperties[WinEdit.PropertyNames.Name] = "Username";
_textUsername.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
// Identify each control
logonWindow.DrawHighlight();
_textPassword.DrawHighlight();
_textUsername.DrawHighlight();

This turned out to be wrong versions in DevExpress between the client application and the test runner code.

Related

How can I add a button to the layout? - C# [duplicate]

This question already has answers here:
How to add buttons dynamically to my form?
(8 answers)
Closed 2 years ago.
I cannot find any methods to add the button to the layout.
I am trying to add the child (button) to the layout, but I can't find any methods to do so.
Source Code:
using System;
using System.Windows.Forms;
namespace WinForms
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Button button = new Button {Height = 100, Width = 100, Text = "Test"};
}
}
}
You open your project in Visual Studio 2019 (not Visual Studio Code, not JetBrains Rider) - free versions for which exist if your context qualifies for the license. If you don't qualify for a free license, you[r workplace] can easily afford a license of some form
You double click Form1 in the solution explorer and then you see something that looks like what the form will look like when you run the program, and you open the controls tool panel and drag a button out of it and drop it onto the form...
But if you want to get into hand-writing the volumes of boring repetitive code to build a UI then you add controls to the Controls collection of other controls, viz:
Form1 f = new Form1();
Button button = new Button {Height = 100, Width = 100, Text = "Test"};
f.Controls.Add(button);
Application.Run(f);
Every Control has a Controls collection to which other Controls can be added (not just things you think of as "things that have child controls, like Panel or GroupBox" - some controls are collections of other controls, like a NumericUpDown is a textbox and a couple of buttons)
For an example of how much code you'll need to write, lay out a reasonable looking UI in the design view and then open the Form1.Designer.cs - you'll see why we do it with the aid of a design tool! :)
Wouldn't it be faster to learn if I didn't use the Designer tool?
IMO, no. That's like saying "wouldn't it be faster to learn if I hand code an SVG in notepad rather than using Inkscape/Gimp to draw the image visually.. or create a PNG by typing the bytes out in a hex editor"
Getting so close to the raw low level means you end up "not being able to see the wood for the trees" and it hinders your learning. For a lengthy discourse on abstractions and why we use them/how they apply to every daily process including learning and operating in life, see the comment trail
You need to add the button to your Form, not to the Main method!
The issue with your code is that it's in the wrong place - actually, it only runs after the form is closed, because Application.Run will run your form in a message loop, allowing UI events to fire.
You can either use the Visual Studio WinForms Designer (if using Visual Studio), or manually add the code after the InitializeComponent() method - so, either right in the constructor, or in any of the Form startup events, such as Load or Shown.
It's likewise very important to add the Button (or any dynamically instantiated control, for that matter) to the Controls collection of the Form - otherwise, your Button won't be displayed:
Button button = new Button {Height = 100, Width = 100, Text = "Test"};
this.Controls.Add(button);
There are certainly use cases for dynamic generation of controls; however, it's very unusual to build out your controls manually before the form even runs - in most cases of dynamic control generation, the form is up and running - the dynamic generation is in response to some user action. I recommend using the designer for general UI layout.

How to properly implement drag and drop for text with Gtk in Mono-C#?

I'd like to implement an application where users can add text to it using drag and drop. Somehow I can find no sufficient example on the web helping me in this task. There are plenty of examples for Windows, but I am planning to implement this using Gtk and Mono on Linux.
I created a widget - a label in my case - and added the following code to the main window:
Gtk.Drag.DestSet (label1, DestDefaults.Drop, new TargetEntry[] {
new TargetEntry("text/plain", TargetFlags.Widget, 0)
}, Gdk.DragAction.Copy);
label1.DragMotion += new Gtk.DragMotionHandler(OnLabelDragMotion);
The method OnLabelDragMotion() gets called if a drag something onto the control. This part works fine. But now how do I start from here? How do I recognize if the user actually dropped something onto the control?

Multiple forms one windows C# possible or only panels/usercontrol?

So here's my Question, I'm new to C#(teaching my self at that) Here's the thing, I'm working on a basic sim game, nothing to complex but I've got the design and basic functions done.
However In order to implement it, I'm currently using multiple Forms(Visual Studio 2013)
I have my "main" form which has the "action" buttons to it
So when i want to go to a user Profile page I have
Btn_profileview Click(object sender, EventArgs e){
Form profile = new Form();
profile.Show();
}
The User would then implement the changes(for instance change name) which is written to a text file, for use in other areas of the program.
However It opens a new windows, I've tried modal and nonmodal windows and while the benefit of Modal so they have to actual close the window solves the issue, i'd rather have it just overwrite the preexisting Form, and then on close go back to the "main" screen without actually using multiple windows.
Now I was told UserControl and/or Panel would solve the issue, but it would cause a complete redesign moving from the multiple forms to the multiple panel screens and figuring out how to get those to work(Visible and Invisible), i'm assuming it wouldn't be extremely difficult something along the lines of Panel"name".show(); and panel"name".close();
But would it be possible to actually add a line of code to the pre-existing code(so as not to cause a complete reesign) or are Panels and UserControl the only real way to implement within 1 continuous windows?
paqogomez is right: There are many ways to do it.
Here is one that combines a lot of the pros:
You create an invisible Tab on your window with as many pages as you need. Place a Panel on each tab and create all your controls on of them. This does not mean that you have to do it all over - you can move and drop the controls you already have without much hassle. Of course you need to turn off docking and maybe anchors, but other than that this is a simple process.
If you have controls on the 2nd form with the same name, these names should be changed to something unique though. I hope all Controls have proper names already, but especially Labels get neglected, at least here.. (With a little luck you can even use cut and paste to get Controls from another form to panel2!)
The big pro of this trick is that you can do all work in the designer of the same form. The Tab control serves only as a container where you keep your panels without adding to the UI and without giving the user control of what is shown.
Next you create one Panel variable in your main form:
Panel currentPanel;
On Load you assign the first 'real' Panel to it like this:
currentPanel = panel1;
this.Controls.Add(currentPanel);
Later, each time you want to switch, you re-assign the panels you need like this:
this.Controls.Remove(currentPanel);
currentPanel = panel2; // or whichever panel you want to show..
this.Controls.Add(currentPanel );
If your real panels are docked to fill the tabpage, as they should, the currentPanel will fill the form. You still have access to each panel and to each control by their names at any time but you see no overhead of tabs and your form never changes, except for the full content.

Dock Windows Forms (tabbed chat interface)

Edit for those who say to use tab control
I would love to use a tab control; yet i have no idea how to go about linking the tab control up from the main form. I would assume that I would have to do something like this:
Create Form with a blank TabControl on it, no pages created.
Create a CustomuserControl (Add -> user Control), with my controls on it.
When a new chat comes in, create a tab control Item, Tab Control Page, add the Custom Control to the Tab Control Page. Add the tab control handle to the hash table, so that when new messages come in, they can be referenced in the proper control.
But, i am so not sure how to do this. For example, I know that I can create custom events inside of the User Control, so that, for example, if each control has a 'bold' button, i can each page that has that control on it, to actually USE the button.
Yet i also need to register message callbacks, so that I can use a MessageGrabber to send data to it, and tha'ts not assigned inside of the UserControl, that's assigned programatically when a new window comes in; but since I have no controls to reference, i can't assign.
KISS Philosophy
Wouldn't it be easier to just create the form, like i do now, and then just dock that form within a window or something? So that, in essence, it's still creating the form, but it's also a separate window?
Original Question
Okay, so i'm stumped (which isn't that big of a surprise when it comes to complex C# logic lol)! What i'm trying to do is the following:
Goal: Setup tabbed chatting for new chat application.
Completed: Open new window whenever a chat message is received, or a user requests a new chat from the roster. This is working perfectly, and opens only a window when the user doesn't already have the chat open. Nice and happy there.
Problem: I dont want windows. Well, i do want A window, but, i do not want tons of separate windows. For example, our Customer Service team may have about 10 active IM windows going at one time, i do not want them to have to have 10 windows tiled there lol. I'd rather they have a single Private IM window, and all 10 tabs docked within the window.
Logic: This is my logic here, which may be flawed, i do apologize:
OnMessage: Open new chat window if one doesn't already exist; if one exists, open it as a tab within the current chat window.
SendMessage: ^^ ditto ^^
Code Examples:
if (!Util.ChatForms.ContainsKey(msg.From.Bare))
{
RosterNode rn = rosterControl1.GetRosterItem(msg.From);
string nick = msg.From.Bare;
if (rn != null)
nick = rn.Text;
frmChat f = new frmChat(msg.From, xmpp, nick);
f.Show();
f.IncomingMessage(msg);
return;
}
Note on above: The Util. function just keeps tracks of what windows are opened inside of a hashtable, that way, when messages come in, they route to the proper window. That is added with the:
Util.ChatForms.Add(m_Jid.Bare.ToLower(), this);
Command in the frmChat() form.
Library in Use: agsxmpp from: http://www.ag-software.de/agsxmpp-sdk/download/
Problem:
How can i convert this code to open inside of tabs, instead of windows? Can someone please give me some ideas, and help with that. I just can't seem to wrap my head around that concept.
Use TabControl

How can I get the reference to currently active modal form?

I am writing a small class for driving integration testing of a win form application. The test driver class has access to the main Form and looks up the control that needs to be used by name, and uses it to drive the test. To find the control I am traversing the Control.Controls tree. However, I get stuck when I want to get to controls in a dialog window (a custom form shown as a dialog). How can I get hold of it?
You can get a reference to the currently active form by using the static Form.ActiveForm property.
Edit: If no Form has the focus, Form.ActiveForm will return null.
One way to get around this is to use the Application.OpenForms collection and retrieve the last item, witch will be the active Form when it is displayed using ShowDialog:
// using Linq:
var lastOpenedForm = Application.OpenForms.Cast<Form>().Last()
// or (without Linq):
var lastOpenedForm = Application.OpenForms[Application.OpenForms.Count - 1]
I'm not sure if you can access controls on a pre-built dialog box; they seem all packaged together. You may have more luck building a dialog box of your own that does what you want it to do. Then you can access the .Controls inside of it.
Correct me if i'm wrong, though, it sounds as if you are possibly attempting to access the controls on the dialog form when it's not quite possible to.
What I mean is, ShowDialog will "hold up" the thread that the form was created on and will not return control to the application (or, your test class) until ShowDialog has finished processing, in which case your user code would continue on its path.
Try accessing or manipulating the controls from a separate thread (in this case, refactor the test driver class to spawn a separate thread for each new form that must be displayed and tested).

Categories