selenium: is the first window handle always the main window? - selenium

Is the first window guaranteed to be the main window? Or is the order random and inconsistent?
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
I can't find any information about the order of the window handles, just a way to iterate thorugh them all. I am assuming that the first in the list of window handles is going to be the main window.

According to the current WebDriver API Specification:
6.3 Iterating Over Windows
getWindowHandles
An array containing a window handle for every open
window in this session. This array of returned strings must contain a
handle for every window associated with the browser session and no
others. For each returned window handle the javascript expression
"window.top.closed" (or equivalent) must evaluate to false at the time
the command is being executed.
The ordering of the array elements is not defined, but may be
determined by iterating over each top level browser window and
returning the tabs within that window before iterating over the tabs
of the next top level browser window.
In short, no - there is no guaranteed order.

As alecxe pointed out the handles can be in any order.
I solve the issue of knowing which handle is the main window by saving the handle immediately after I point Selenium to the first page to load (at this point the list of handles contains only one handle, which is the main window), and I save this value for future use. Then when I scan the list of handles later, I compare with the value I saved and know that handles that don't match it are not the initial window.
Doing this is useful in "tear down" or "clean up" code between tests in a test suite if you have some tests that may open other windows. Using the method I describe here, your tear down code can be generic and not worry about whether any specific test is actually opening additional windows.

Related

Do not require attention for ShowDialog

I had a question about Dialogs in VB.NET. I am working on a point of sale program, and at one point during a sale, I have a few windows that pop up. For example, a user will go into a sale that is window A. In window A, they have the option of entering products, etc., and if they choose a 'repair' product, it opens window B, allowing them to choose options. In window B, there is a button that pops up window C that allows them to attach products TO the repair. My issue is with window B opening window C.
Because I open window B as a Dialog (in order to check if DialogResult.OK is true), any window I open with B is non-touchable, as B is a Dialog and requires attention before going to any other windows/forms.
My question is - is there any way to still use a dialog, but allow for manipulating other open forms while the dialog is up, and if not, what would be the best way to check if the user selected OK, or cancelled out of the window?
The only solution I can think of right now would be to open window C as a dialogue as well (it's actually a UserControl, and I'm still trying to find where in the code it's actually getting openned/called), or to create a variable that is passed in to the form, and then passed back out when it's closed, that basically sets a flag to either continue or cancel...
Any advice/ideas??
If I were to explain this using code, this answer would be very long, so instead I'm going to give you a high level overview.
.Show() vs .ShowDialog()
The link below will take you off to Microsofts website to explain the technical differences between these two. However in laymans terms, .ShowDialog() will create the form where it is the only window allowed to have focus in the application. Forms that are called in this instance are hierarchical, in that if you open them in order of 1,2,4,3 then they must be closed in the 3,4,2,1 order. Forms that are opened with just .Show() can be focused at any time.
How to: Display Modal and Modeless Windows Forms
Form.FormBorderStyle property
This property controls how the OS will display the window. The different options under this selection changes the way the window behaves. Depending on the options that are chosen you can make a window that only has a close button on it, or it may not even have a title bar at all. Setting this option to None will take away all controls of the form and only leave you with the Me.ClientArea to work with. When you want a completely custom GUI, this is how you do it but you have to implement your own controls for everything, closing the form, size handles, the ability to move the form on the screen, etc...
Form.FormBorderStyle Property
Passing data between forms
When someone asks how to pass data back and forth between forms, they are usually talking about modeless forms that were created using .Show(). The most common thing I see on SO is to use the tag property of an object (a form is an object that has this property too) to pass data back and forth. While I won't say this is a bad practice, I will recommend creating public properties on your forms. These can be set from a separate form and you can perform additional actions when setting the values (be careful though, this way of doing things isn't thread safe). If you are using a Modeless form as though it were a Modal form, then you can simply override the .Dispose property to return a value or you can create a method named DialogResult that will return the value you need. The caveat to using a DialogResult or similar method is that if the form has been disposed then you can't access the value you wanted to return.
You can use myNewForm.Show(Me) for the Window you want to be shown as a dialog. This will show myNewForm as a child of the current form, but lets you interact with the current form.

pyGTK how to redisplay a top level window (with child widgets) after window destroy signal is sent

I have an application which displays a window containing widgets every minute. If I destroy this window by closing it in the window manager (linux), the destroy signal is sent. Then the next minute rolls around, at which time self.window.show_all() is executed and the window pops up empty.
So I did some investigating. I executed print self.window.get_children() just before the show_all command, which returned an empty list. So I executed self.vbox.reparent(self.window) and I get this response:
calendar.py:237: GtkWarning: IA__gtk_widget_reparent: assertion `widget->parent != NULL' failed
self.vbox.reparent(self.window)
These two responses seem to contradict one another. On the one hand, the empty list returned by print self.window.get_children() seems to imply that the window does not have any children. On the other, the output returned by self.vbox.reparent(self.window) seems to imply that self.vbox still has a parent, which would be self.window (as defined previously)
I've tried using a popup window: self.window = gtk.Window(gtk.WINDOW_POPUP) instead, but I would like to be able to close the window through the window manager, so that I don't have to add an additional button just to close (hide) the window. The popup window doesn't seem to provide this functionality in my window manager (awesome).
So if you want to redisplay a top level window with its children after it is destroyed, how can this be done?
Have you tried making another copy of
the window class and showing that one
instead? Note: This will reset anything that was set in the window. i.e. if a person did something to make label1 say 'Hi!' instead of 'Hello!', it is going to be reset again to 'Hello!', since you are recreating the window.
You can also set Gtk.Window.hide_on_delete as the handler for the delete-event signal; then closing the window will hide it instead of destroying it.

VB2010: Viewing Object Structure for Learning/Visualization Purposes

I was wondering if there is a way to take an object in Visual Basic 2010 (Express, FWIW) and browse through its structure to visualize how the data inside is laid out.
For example, I have an object called "model" that is populated by a function that is a black box to me. Model is set by a "read" function that loads a DXF file from disk. The read function isn't very well-documented.
What I've discovered is that model.Entities ends up containing a list of different objects, all with different properties. I'd like to be able to simply browse this list of objects and view their associated properties and values at run-time, similar to how you can use Intellisense to view a list simply by typing "blah." and waiting for the pop-up to appear.
A tree view that you can pop open and closed would be excellent. Obviously this has to work during run-time rather than in the editor because the file hasn't been loaded if the program isn't running.
Is this something that's possible in Visual Basic 2010? Is it a built-in feature I can't find?
Thanks!
If a function returns an object, then that object has a class definition somewhere. Right-click the reference in VS and select "View in Object Browser" and you'll see the class layout with all properties and methods. You don't need to do this at run-time, either.
If you want to dig even deeper then you should check out Reflector.
EDIT
After reading your comments more, I usually do one of three things when I'm trying to do this:
Use the Autos and Locals window
Use the Immediate Window
Use "break-point and hover"
Use the Autos and Locals window
Set a breakpoint and check out the Autos and Locals windows. If you don't see them they're under the main menu at Debug, Windows. This allows you to walk a tree-view of your variables. Sometimes there can be a lot of stuff in here which I why I generally use one of the other two methods below.
Use the Immediate Window
The Immediate Window (IW) allows you to type in expressions and print out values. Its not a tree-view like you want but it allows you to hunt and peck at least. If you imagine the following short and simple code and you put a breakpoint on the second line:
Dim Names As New List(Of String)({"Alice", "Bob", "Chuck"})
Console.WriteLine(Names)
In the IW you could type:
?Names
And it would output:
Count = 3
(0): "Alice"
(1): "Bob"
(2): "Chuck"
The question mark symbol means "print". You can type almost any valid expression for print:
?Names(0)
"Alice"
?Names(0).Substring(0,1)
"A"
?Names(0).Contains("ice")
True
And as you're doing all of this you're getting IntelliSense about whatever is going on.
Use "break-point and hover"
I don't think this has a name beyond IntelliSense but once you've hit a breakpoint you can hover over any variable and inspect its current values. You'll get the occasionally warning that inspection will cause some processing but since you're debugging only this should be fine. Sometimes when I'm debugging a collection I'll create a variable specific to one item in the collection just to make this technique easier. I'll get rid of it once I'm done debugging but it really helps this process.
Is there another

NSWindow does not respond to keystroke command-s

It may be very simple, but I cannot find it:
I have three windows in three separate NIBs in my application. One is opened when a new document is opened, the other two can be opened from the program's window menu.
The problem is: two windows (in them the one that is opened at the beginning) accepts the normal keystroke as for example command-s for save, and the other one does not and gives a warning sound instead. I cannot figure out the difference between the two windows or their controllers. I know it will have to do with the responder chain, but I am left clueless.
Any ideas?
Check to make sure that the window's delegate is set to the window controller, and that the window controller implements -saveDocument: (or whatever action the Save item is connected to).
Windows don't respond to key combinations. Menu items do. In response to being pressed (whether using the mouse, using a key combination, or using Accessibility), the menu item sends its action message down the responder chain.
You get a beep when nothing in the responder chain responds to the action message.
Assuming that this is an NSDocument-based application and you've started Apple's doc-based-app template, the menu item's action is saveDocument:, and the NSDocument object is the object that responds to that message. When your document windows are active, their documents are in the responder chain, so the menu item that sends that action message is enabled. When your third window is active, the document is not in the responder chain; nothing else responds to that message, so the menu item is disabled.
This problem isn't specific to Saveā€”it affects all action messages that should go through to the document object. One important other example is Print: The user will probably mean to print the document, not the third window.
You've probably made this third window a kind of window that exists as a peer to the other windows. Besides this responder-chain problem you're having, the user will also probably not realize that they have left the document; they expect to still be able to do document things. Consider making it a utility panel instead.
If you really do have a good reason to make this window whatever kind of window it is, you'll need to keep the last-active document object in the responder chain when this third window becomes main, while at the same time handling the case where the window becomes main because a document window (possibly the last one) has closed.
Well, it turns out that I implemented the third window in a way where I created it with its controller using initWithNibFile, ran a procedure in the controller and then sent it a [window close] command because I did not want it to appear on the screen yet. That somehow took it out of the document-associated window, no idea why. No I migrated that specific called procedure into the document controller itself, treat the window like the second window and voila, it works again.

VB.Net Start-up window changes based on App.config setting, how is it changing?

I have this VB.Net 1.1 project that I have to make some changes to. There is a flag in the App.config file. If it is false, the page just loads a splash screen and runs the program normally. If it is true it first opens a login window.
VB.Net is not something I've ever worked with before. I can't for the life of me figure out where the logic for the picking the startup object is. In the property pages, Main.vb is always set as the startup object, but that's not even the window that loads up when the flag is false, it always comes after the splash screen.
I've search all through the code for any reference to creating a new instance of the login window to display it but can't find it. I've searched for where it checks what the flag is set to, but anything I've found for that is not in reference to loading the login window.
Any ideas?
What is the name of the login window class? You could either do a search in the entire solution to find all occurences of that name (e.g. by pressing Ctrl+Shift+F) or place the cursor on the class name and press Shift+F12. The latter will find all references of the login window class. In the search result window now look for new MyLoginWindow to see where it is instantiated.
From that location on you can use the same method again to find the callees and possibly the place in the code where the config flag is checked (Or you could place a breakpoint, debug and walk up the call stack to see where you are coming from - that could be easier).