Adapting Modal Dialog Script to Firefox - watir

I'm adapting my regression tests to test a web app in firefox. The biggest stumbling block seems to be how to automate the modal dialogs in firefox.
In ie I use variations of the script below, but it doesn't work in Firefox. Is there an alternative that will work in both ie and firefox?
popup=Thread.new {
autoit=WIN32OLE.new('AutoItX3.Control')
ret=autoit.WinWait(title,"",60)
if (ret==1)
puts "There is popup."
autoit.WinActivate(title)
button.downcase!
if button.eql?("ok") || button.eql?("yes") || button.eql?("continue")
autoit.Send("{Enter}")
else
autoit.Send("{tab}")
autoit.Send("{Enter}")
end
elsif (ret==0)
puts "No popup, please check your code."
end
}
at_exit { Thread.kill(popup) }
end
button.click_no_wait
check_for_popups("Message from webpage", "OK")

Given you are talking about a javascript created dialog, I really have to ask, is there a lot of value in actually testing those?
It basically amounts to testing the functionality of the browser
If you are talking about the type of popups described here http://wiki.openqa.org/display/WTR/JavaScript+Pop+Ups then I think the first solution, of overriding the javascript may well be your best cross platform option.
The problem with modal dialogs like this is that they are basically a UI even that is happening out at the OS level, it's no longer inside the browser DOM, and thus you need tools that are specific to the OS (like stuff that depends on win32ole, such as autoit) in order to generate the necessary interaction with the native UI and click buttons, send keystrokes etc. Most of the solutions presented should I think work with FF on windows (with proper renaming of expected window titles etc) but would fail on a mac or *nix OS. That means you need a different solution for each OS, which is a pain.
It might simply be easier to verify you can find the proper stuff that would fire the event in the HTML of the page, so you know an event WOULD be fired, and then override things so it isn't. After all it's not really your job to validate that the browser pops up a local dialog when something like alert('This is an alert box') is invoked in javascript. Your concern is that in the HTML a given element is coded to fire off the event that is needed e.g. that there's something like this onClick = 'javascript:x = confirm('Do you really want to do this');" affiliated with the element

I am experiencing a similar problem in Firefox (and I do have to test in Firefox). I can see the code calling the Javascript but when I try to override as described above nothing happens. Is there any kind of a workaround for this? Anticipated updates to Watir? ;-)

Related

Watir Webdriver fire_event onmouseover no longer working

I've been using Watir to automate testing of my companies primary web app. The tests require a javascript dropdown menu fired by mouse over, and a link on that menu to be clicked.
$browser.link(:id=> 'element').fire_event "onemouseover"
Was working perfectly until we changed the look and feel of the app. The changes made to the look in feel didn't appear to change in any way how the HTML looks and the menus work. Everything else is working fine with Watir.
I've tried using a solution that seemed to work for others, by including:
class Watir::Element
def hover
assert_exists
driver.action.move_to(#element).perform
end
end
and using
browser.div(:id => "someId").hover
But when the menus appear they instantly go away before the driver has a chance to click the menu item.
browser.div(:id => "someId").hover
sleep 3
Will solve the problem. I have faced same issue and sleep solved it :)
I've managed to get around this one (provided the site uses jquery) by using execute_script to fire off some jquery to override the visibility on the 'hidden' menus. See this answer https://stackoverflow.com/a/8392467/409820 for more info
Needing to do that was one reason I and others requested the .hover method, so that is also something you could try.
(my scripts are still using that particular trick because a) it's working, and b) I'm working on other stuff right now and don't have a pressing need to fiddle with them.

Capybara Cucumber test fail to find xpath when browser is minimized

I'm running a scenario where form fields are automatically filled in with invalid values which trigger some javascripts to show warnings under each incorrectly filled field when I blur.
The test passes when the browser is in focus. It finds the xpath with the "expected warning" that I pass. But if I minimize or just click on another application, it fails to find the xpath.
I'm running Firefox 3.6 (going to update it soon) and the way I'm doing to find the xpath is by using "page.should have_xpath(xpath)"
Does anyone have any idea how what might solve this? It's really important for me to run it with the browser minimized.
Edit and alternative solution:
I guess the timing issue that occurs in events such as blur followed by finding a certain xpath in a minimized browser inherent to the driver itself. Therefore, I decided to run the tests in a Virtual Frame Buffer using xvfb in Linux and it seems to be working really well. I'm planning on applying this to be triggered by Hudson/Jenkins whenever a change is committed.
Could it be a timing issue? Perhaps if the browser isn't frontmost and maximised, the rendering is not happening quickly enough for the content be present when Capybara checks for it.
Also: How are you triggering the blur event?

how can i handle rad window popup by watir script?

How can i handle this type of Rad window pop up by using watir.
<span id="RadWindowTitlectl00_ContentPlaceHolder1_rwmWinManager_rwMessage" class="RadWTitleText" onselectstart="return false;" unselectable="on">Check Results</span>
This is for button html...
<input id="btnOk" class="LoginButton" type="button" onclick="javascript:CallBackToEdit('1');" value="Ok">
Please give guidance for this.
Need more info to help you.
If you're trying to access a newly generated popup window, do it something like this:
browser.window(:title => "annoying popup").use do
browser.button(:id => "close").click
end
Given that the HTML you have supplied just seems to be an ordinary div element, my first guess is that this is not truly any kind of popup, but just a div that is using Ajax/javascript and CSS to simulate the effect of a popup (by manipulating the coloring of objects, and perhaps the 'enabled' state as well so it appears to be modal)
To be sure we'd need to see more than just fragments of the HTML Or better yet a reference to an example of a page that implements this control. It would also be helpful to know WHICH set of 'Rad' controls (since Telerik has multiple versions (Ajax, MS-MVC, Silverlight, WPF and Winforms) available) and knowing which one might make it easier to find an example of the control on the demo pages at Telerik's site
based on what little you've provided I would think that simply
browser.button(:id, 'btnOK').click #ought to work
Note that since this thing could well be coming into existence via client side scripting, potentially a brief wait might be necessary to ensure that the object exists, before trying to click it.
If that does not work for you, then use developer tools to look at the button input element and make sure it is not in a frame.
Otherwise please give us either more HTML, specifics on which RAD control this is (so we can perhaps find an example among their demos) or both.

Pre-loading browser clipboard for testing pasting into fields with watir-webdriver

Our web application has some event code to "format" any text with pasted into a field so that any HTML styles do not break our data.
What would be a good way to pre-load the browser clipboard so that I can test pasting into the input field?
Is there any way to do it programmatic-ally or would I have the test script visit "a source page" and copy text before moving onto our application?
Any ideas or code snippets would be welcome.
Working with the clipboard will depend on your platform. E.g. on OS X, you can use pbcopy and Command-V:
open('|pbcopy', 'w') { |io| io << 'some text' }
browser.text_field(:name => 'q').send_keys([:command, 'v'])
I know there are equivalents on Linux (xclip?). Not sure about Windows.
I would consider using the .value= method to set the value. If it's been implemented the same as in watir, then it causes no events to be fired and sets the value directly, and then follow that up by sending an appropriate event (depending on what if any events are being monitored) such as onKeypress. I tried to figure out from the Watir-webdriver rdoc for textfield, if this distinction between .set and .value= has been maintained, but the way the doc describes them (at least there) makes them seem interchangable.. (Jarib can you clarify???)
Potentially you might need to first fire something like onFocus depending on the controls you are using. For example, as described in this SO case Setting a text field that has a JQuery mask on it for a jquery mask, they had to end up firing an unmask event to be able to even set the field.
This is a good case for utilizing the techniques described here How to find out which JavaScript events fired? and in the SO item linked in the comments for that question, to figure out just what events are fired when you manually paste something into a field. (note I would slueth it with both using the mouse, but also using something like tab to move between fields and set the focus, events common to those two methods are the ones most likely to be implemented by the controls.
I presume you have some kind of client side javascript that needs to check what was pasted into the field, and thus the reason for this test. If you are using standard HTML fields, with no javascript stuff, then I would consider this particular test case to be effectively the same as 'testing the browser' since supporting cut and paste in input fields is a standard browser function. In that case, you are sort of 'off the reservation' and I'd not bother with such a test case.

How to control over browser back button

I want a confirmation window on click of a browser back button. If I press yes then the previous page will get load ortherwise I will remain in the same page?
Any suggestion is highly appreciated.. But please be on track.. my question is straight forward
thx in advance..
Why do you want to do that?
You can prevent them from leaving the page by using Javascript, but if you can't use that, it's not possible to do anything about it.
Generally you should use the unload event in the body (in jQuery for instance, just add
jQuery(window).unload(function(evt){
if(!confirm('Do you really want to leave')){
evt.preventDefault();
}});
Prototype have something similar, and for pure Javascript, I guess that it still depends on the browser you're using, but window.unload = function(evt){return false;} might work.
Don't remember the correct syntax for it though.
but I do not know if you can limit that for only the back or if it will trigger for all the unloads (like clicking on a link, closing the browser etc.)
If you want to stop them because they might have unsaved data in a form, then that is ok. If you want to stop them from going back for another reason than that, I think you should rethink why.
Generally if using the back button can cause issues you already have bigger problems.
What you probably want to do is check that you do things like this:
Use POST for all requests that alter data
Use nonce's (unique ID's) to enure forms don't get submitted twice
I use noscript for this very reason. I insist on having control of my browser not the site that I am visiting. I only allow scripts for certain sites. For any site that disables my back button,I don't allow it to run scripts.

Resources