Handling javascript popup in watir - ruby

I am using autoit to handle javas script popup code as
autoit.WinWaitActive("[Class:#32770]")
result =autoit.ControlClick("[Class:#32770]","","Button1")
But when I click on the button to open the popup it waits for a longer time & if the user is performing operations on another window, it will no go further. Only when the user clicks on the current window does it work. Means user should be focused on IE browser at the time of javascript popup.

Most tools that work up at the OS UI level (as autoit does) require that the window to be worked on has focus in order to have things like clicks or keyboard input end up in the correct window.
You'll probably want to set the focus first, then try to click, if you are using autoit
There are other methods for dealing with JS popups, especially with more current versions (1.9.0 or above) of watir, which are more elegant. Refer to the Javascript Popups page in the Watir Wiki
Do be aware that most of the solutions you see presume that the browser will have focus. If you need to run scripts at the same time as doing other work and don't want what you are doing to interfere, I might recommend using a virtual machine to run the scripts

Related

xul dialog box wont have maximize and minimize buttons after updating firefox to version 32.0

used dialog tag in xul for a dialog box for an xul application.Earlier before firefox update the tool had maximize and minimize buttons but after update the buttons are no longer there in the tool.
MDN Docs: Window.openDialog, Window.open, <window>, <dialog>, <dialogheader>, Dialogs and Prompts
Without all of: The Firefox version from which you were upgrading, the version you upgraded to, the code you are using to open the window, and the XUL used to describe the window it is difficult to provide you with an answer which actually covers what you want to know.
MDN has the following to say on dialog windows: "Dialog windows are windows which have no minimize system command icon and no maximize/restore down system command icon on the titlebar nor in correspondent menu item in the command system menu. They are said to be dialog because their normal, usual purpose is to only notify info and to be dismissed, closed. On Mac systems, dialog windows have a different window border and they may get turned into a sheet."
That makes is clear that dialog windows do not normally have the controls you are asking about.
However, you can pass minimizable in the features parameter of the window.open() or window.openDialog() functions to turn on minimize control.
In general, if you want a dialog to have minimize and maximize buttons, you have to open it as a normal window with window.open(). You can limit the other toolbars that it has at the top by supplying appropriate parameters in the window.open() call. You can also make it modal, like some dialogs. You then create your own OK and Cancel buttons with appropriate code to accept the information in the dialog or cancel. Basically, if you want a maximized window for a dialog it should usually be doing quite a bit. In such case, you probably want more control over how your window looks than is available from a dialog window. Dialogs are, generally, subset of what windows can do with the ability to have a few buttons easily provided.

How to encapsulate Watir browser instance in a GUI using Ruby?

Basically, I'm programming a testing application for multiple sites using Watir. However, there are end-users (who can be assumed to be the typical user unfamiliar with the command line, using Windows). Now, I want to give them something to watch, say, what's going on visually (at the clients request, sigh).
I'll be making a GUI, that displays the form data being used to test (random info for forms, username, names, etc) the site and what's going on. I want a preview window, like the WebBrowser class in C#.NET.
Is there a concise way to restrict/encapsulate (idk what other phrase to use) a browser instance in a GUI (wxRuby, Shoes, etc.) w/ Watir (or Watir-WebDriver) in Ruby?
(Note: This is to be run on a Windows machine)
This is a bit of a tall order. Watir itself is just running from the command line, so you'd need to see if there is a way to do what you want with a command line window in the OS you are using. But then watir invokes an instance of the browser (which with Watir-webdriver can be a large number of different browsers), and each of those is their own beast, reacting with the OS and UI in their own way, and I've no idea who you might 'wrap' IE or Chrome or Firefox in the way you are describing..
It MAY be easier from that perspective to see if there would be a way to wrap the interface of a virtual machine perhaps? (maybe some way to do this with virtualbox or vmware?)
This seems like a pretty quirky request if you ask me, I'm having a hard time seeing the business value in what you are being asked to do.
If you're using watir-classic, you can hide your IE window with:
ie = Watir::Browser.new
ie.visible = false
That might be helpful if your requirement is to hide the browser window and only display a console window for logs/status messages.
If you're using Watir WebDriver/Selenium, I'm not sure if a similar method is available. In a quick search I didn't see anything.
The IWebBrowser2 "visible" property may be useful - not sure how hard it would be to patch this in to Watir WebDriver if it doesn't already support it:
http://msdn.microsoft.com/en-us/library/aa752082(v=vs.85).aspx

Is it possible to call a function in a different, but currently executing process?

I have a friend who's working at a company that offers pretty poor support for its developers (scoring a 1/12 on the Joel Test).
Their build process is locked down pretty tight, and depending on the size of project it could take 40+(x2) mouse clicks to deploy. So I thought, "Hey, why not automate it the clicks using the win32api?" (Specifically using Python). I've got him a real nice tool that works just fine except for one issue - the tool that they use has a navigation pane that may or may not be open.
You can open and close it with a button press, but I'm not sure how I could make sure it was either open or closed. It's irrelevant to the build process - the only problem is that it alters where the mouse needs to click on the screen depending on its open status. The application is written in .NET and it exposes a function call that applications are able to use to toggle the panel, so I've been looking around for ideas and so far I've got two of them:
Attach to the process via a debugger and execute the function call somehow.
Take a screenshot at the location of the panels titlebar (which I've got through the win32 API and doesn't appear to change regardless if the panel is hidden or not).
Is there an easier way to figure out the state of this panel? The developers are given an admin account on their machine in addition to their regular account, so I can entertain ideas that require admin access, though I don't think that should be necessary?
UPDATE:
It looks like there's a button that can close the pane. In UIAVerify something shows up as "text" "Navigation" "btnClose". It says its AutomationId is btnClose but it's a ControlType.Text
What technology is this panel built from? Is it standard GDI or WPF? If its GDI, it should have a HWND. You should be able to find this HWND through either a class name or window title. Once you have the HWND, you can get its width.
If its built with WPF, er, I have no idea, but Snoop does this kind of thing, so I know its possible.

Show a popup menu in another application's window

How can a Delphi XE application show a popup menu inside another application's window? The idea is for a helper-type app, running in the background. On a registered hotkey the application needs to display a popup menu near the text caret or mouse cursor.
Applications that do that are common, here's a menu created by AutoHotkey and displayed in a text editor:
I guess what I'm asking is: how can I display a popup menu at an arbitrary screen location, without it being attached to a Delphi control?
Create a TPopupMenu with the appropriate menu items. When you need to show it simply call Popup passing the top left position in screen coordinates.
PopupMenu1.Popup(X, Y);
#DavidHeffernan answered your question, but you might not have asked the right question.
Let's take the example you gave: the user is running some arbirary application, and you want to be able to detect a hotkey, display a menu, and then take some action based on the menu item chosen (and maybe even the user's context, such as the word under the cursor). This is more complicated than simply displaying a menu at arbitrary screen coordinates.
My recommendation is to use AutoHotKey instead of trying to replicate this in some other programming language. In case you're not aware of this, it is possible for your code to run AutoHotKey scripts. IIRC, you can compile AHK scripts, so you wouldn't need to install AHK, just the compiled scripts. AHK may not be the most elegant of solutions, but it has depth and maturity.
If this is not possible, then I suggest you research Windows Hooks and DLL Injection. Unless you can find some preexisting code or framework, this will entail quite a bit of work.
The reason for this complexity? To augment another program smoothly (without running into problems with focus, etc.) you want to have your code run as part of that other program. The mechanics of this can be done via DLL injection. However, that's only the first step. Once your code is running in the right context, then your code has to inter-operate with the "host" program. This can be tricky (it helps if you have deep experience with Windows messaging and the Windows API). If you want this to work smoothly with any arbitrary program, it gets even harder.

Can I pass the -f parameter to iexplore.exe when using Watir?

I need to run several instances of a Ruby script that's using the Watir gem, and they all need to be able to run in IE fullscreen mode.
CLARIFICATION: They need to be able to open IE in a mode that allows multiple instances of IE to operate in full screen mode, not necessarily trigger the fullscreen. Opening up IE normally allows only a single instance to open fullscreen (not maximized).
I can make this happen manually, by specifying the '-f' parameter on iexplore.exe. Is there a way to make this happen when creating the browser object from Watir?
If I can't make it happen when this as some sort of runtime parameter, I'd be ok with changing the base Watir call that opens IE - if I could and could find it.
require 'watir'
browser = Watir::Browser.new <--- adding '-f' somehow here?
Try this, it worked for me on IE 9.
browser = Watir::Browser.new
browser.goto 'http://www.google.com'
browser.getIE.parent.FullScreen = true
Edit: I found a registry setting that may do what you are trying.
[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main]
Fullscreen = "yes" (Default = "no")
Did you try
browser.maximize
It only works on IE, but it is an option that's been documented for a while (see the watir cheat-sheet)
Of course you need perhaps to consider WHY the browser needs to run full screen, or what happens if you move the scripts to a system where the default screen size is different. You might be better off to set the browser to a specific size, which can be done with a simple javascript resizeTo function, you can even 'goto' it as a URL
browser.goto('javascript:resizeTo(800,600)')
There is also a 'moveTo' that can be used the same way. so you can position the window at a known point.
If you need the browser in a particular place and size, something else to consider would be creating a page with the proper javascript to set things they way you want and then make that your default homepage so it runs as soon as the browser opens. If your google for 'javascript maximize browser' or 'javascript resize browser' you will likely find example code for such a page.
=-=-=-= Edit (based on clarification that 'fullscreen' is what is wanted, not merely maximized)
Lastly you could look at simply simulating a 'F11' keypress as that is the fullscreen toggle for most browsers. If you are using watir-webdriver this can be done via the sendkeys method
browser.send_keys :f11
However that is a toggle, and in a script would depend on things being in the right state to start with. Something that got out of sync might end up turning 'off' the fullscreen.
So you might investigate also the idea of a specific page on the local system that would spawn a new fullscreen window, and having the code attach to the new window. (or using the switching window code in watir-webdriver) although this sort of 'popup a new window in fullscreen mode' is something you might expect to be blocked or deprecated (see below) in the future if not already on some browsers.
Warning: being able to throw the browser into fullscreen mode from the HTML is something that is somewhat frowned upon because it is considered a security vulnerability. This is because someone could craft a specific image to make it LOOK like a url bar and other controls were present, and the user at a legitimate site, when creating a phishing site. Such sites are currently one of the larger issues the web community faces right now (contents of my spamfilter are about 20% phishing and rising rapidly) So while there may have been methods in the past to do that, increasingly they get 'shut down' by newer more secure versions of browsers. This might tend to make the sendkeys option your best bet going forward in terms of something that should work cross browser. (nearly all that I know of use F11 for the fullscreen toggle)
-k puts IE into a full screen kiosk mode that is more than F11. There are no toolbars, menus, icons, etc. You have to hit Alt+F4 to close it and you can Alt+Tab to other open tasks.
Feels hackish, but I figured out I can do something like (but I'd love a better way):
require 'watir'
IO.popen('C:\\Program Files\\Internet Explorer\\iexplore.exe -f "newwindow"')
browser = Watir::IE.attach(:title,/newwindow/)

Resources