How to encapsulate Watir browser instance in a GUI using Ruby? - 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

Related

Are browser tabs child windows?

I've been recently dabbling with writing applications in win32 (i run win10). Windows has this concept of parent windows and child windows each of which can be customized to do various things.
What does this structure like in a browser like chrome/firefox? I read somewhere that browsers don't have a concept of "windows" in the win32 sense. However, Spy++ shows that each tab of a browser window has a unique window handle (likely for interfacing with windows). But the "tab handle" does not have any other children.
If we consider a webpage that is broken into multiple sections, each with its own scrollbar, how does the browser know which scrollbar is active? I imagine that the browser would likely use the mouse pointer location to figure this out once a scroll message (in the win32 sense) is sent to it.
Fundamentally, what kind of code structure do browsers use?
A related, albeit distant question- how is the scroll without focus functionality implemented in windows? I believe the answer to this leads from the first but if its not, I'll make this a separate question.
Thanks!
PS. I only know C++.
PPS. Links to resources that can get me started are just as important and much appreciated.

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.

Handling javascript popup in watir

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

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/)

In Applescript, how do you debug the Menus and Menu Items of GUI scripting?

I often see all this crazy stuff with Apple scripting, involving telling menus and menu items, and UI elements and all that crazy soft of stuff to do things. I don't mind that it's kind of a crazy way to get things done - as long as it works - but my question is this: How do you debug that stuff? I mean, how do you know what your options are?
I have apple script editor and script debugger, but I'm not sure how to use them to see what the options are. I've tried searching with google but I haven't come up with anything.
I do some web development so I'm used to using Firebug to examine the DOM of a web page, I just assume that there should be something easy and similar to help with Applescript.
Thoughts?
I'm not sure if you're asking how to script the GUI or how to tell which GUI elements are available in an application. If the former, try starting with Graphic User Interface (GUI) Scripting.
If you want to find out what the GUI hierarchy is for an application, check out UI Browser, which will allow you to see the UI elements of an application and provide the information you need to target one of them.

Resources