2

I'm trying to test a page containing an element that, when the page is first opened, has its style= attribute set to display: none;. In Firefox, when the test interacts with the page (two text fields, for now), the button is enabled, and can be clicked programmatically. In IE8, however, the button cannot be clicked if the page has been interacted with by the test, even if I try to manually click it. This is what the code looks like:

<a tabindex="21" onclick="if(!allow_submit() || nextStepLocked()){return false;};; new Ajax.Request(saveInfo); return false;" id="nextStepButton" href="#" class="nextButton" style="display: none;">
    <img src="/btn_next_step.png" alt="Btn_next_step">
</a>
<img style="" src="/btn_next_step.png?" id="hiddenNextStepButton" class="nextButton" alt="Btn_next_step">

There's a similar block on the page that leads to this one, and I was able to click it just fine with the code browser.image(:alt => "Btn_next_step").click. I also noticed that when the image switches from a non-clickable to a clickable state, it moves up a few pixels; if I trigger the clickable state in code, this same movement occurs, but the button does not become clickable.

So far, I have tried using AutoIt to mimic the actions normally taken on the page, and I have also tried many different ways of interacting with the image, such as referencing different indices, using multiple identification methods at once, and so on. I even tried messing around in irb, but to no avail.

Update:

I checked the input fields, and they do indeed respond to onchange events, but only to capitalize the first letter of the text you enter. Since the button seems to work if I do just about anything on the page, I looked through the entire source and found the following at the end of the page:

<script>
    document.onkeydown=function(e){
        accession_hotkeys(e);
    };
</script>

I'm not sure how to properly fire this event, though. Doing it on any specific element doesn't seem to have an effect.

Roderick
  • 2,383
  • 3
  • 20
  • 33
  • Would be helpful if you could also show us the HTML on the input fields, (see my answer below) as I suspect there's probably some stuff set to happen when specific events take place with respect to those fields. – Chuck van der Linden Mar 02 '11 at 18:31

4 Answers4

1

This would be a comment, but I don't have the rep yet. If you can click it without WATiR then you can click it with WATiR using a top-level hardware click, using the code here. If not, please disregard this answer.

The code is useful anyway, it does an actual mouse click at the highest level, using the mouse pointer. If you cannot interact with it in WATiR, and you cannot interact with it without then I'd say you're better off with Dave's answer than mine.

Community
  • 1
  • 1
kinofrost
  • 768
  • 6
  • 16
  • I ran into some issues with your code (uninitialized constant WindowsInput::Win32API (NameError)), which I fixed by using a slightly modified version I found here: [link](http://wiki.openqa.org/display/WTR/Right+Click+an+Element). The only caveat is that I have to maximize the window after it opens, but I think I can do that programatically with some of AutoIt's features. – Roderick Mar 02 '11 at 14:53
  • Thanks for the feedback, I'll update the original code to match. I'm glad they've put it on the openqa wiki, I originally got hold of it quite some time ago. Glad to be of help! – kinofrost Mar 02 '11 at 17:22
  • 1
    IMHO resorting to autoit is a brute force workaround.. this should be possible via watir without help from autoit as long as all the controls are in the browser and not some kind of popup alert or something of that sort. I don't suppose this page is someplace public you can share with us? – Chuck van der Linden Mar 03 '11 at 00:55
  • +1 for this not being an elegant solution, and your answer below. If it can be solved without hardware clicking through the Win32API solution then all the better. – kinofrost Mar 03 '11 at 09:36
  • @chuck No, it's not public. Also, after creating a test using this, I find that it works about 70% of the time. – Roderick Mar 07 '11 at 20:35
1

Rather than trying to figure out what javascript you have to invoke, or settings you have to manipulate, I'd suggest trying to see what is needed to have that happen 'naturally' ala a human interacting with the site. Have a good look at the HTML for your input fields to see if they are wired to fire off javascript scripts when some event fires (I'd suspect 'onchange')

Firewatir and Watir are not derived from a common code base (more like sorta merged after the fact), and it could well be that firewatir is setting off those events when the field is set, but watir is not. The other possibility is that the firewatir code could be just enough slower that as it goes to click the button, the client side code was able to alter the state of the button, while that has yet to happen on the IE side.

What might be required is for you to fire a specific event against the input element after you have .set the value the way you want it. Again, I suspect it will be 'onchange' but cannot be sure without seeing your html.

The other thing you may need to do is be sure the code is waiting long enough for any client side scripting to make changes, so I'd suggest perhaps trying a one second sleep prior to trying to click the button.

Normally I troubleshoot stuff like this using an IRB session as it lets me do things like set values, fire events, at my own pace, and I can examine the state of the page after I've done each thing. For example fire an event at an element and see how the page 'responds' in turn. (which is just dammed hard to do at 'script speed'). Also if things work in IRB but not when the exact same code is executed as a script, the problem is almost always that the scripts are doing things 'too fast' and you need to insert a few delays to allow the client side code to do it's thing in between specific script steps.

Chuck van der Linden
  • 6,660
  • 2
  • 28
  • 43
  • Whatever it is, I think it has to actually be done before any .set. If I do the .set and stop the script, I'm completely unable to click the button, even manually. – Roderick Mar 07 '11 at 14:52
0

My guess is that you have to find which JavaScript event to fire: How to find out which JavaScript events fired?

Community
  • 1
  • 1
Željko Filipin
  • 56,372
  • 28
  • 94
  • 125
  • I already tried that. The main issue I had was that I couldn't pin down what event was firing, because whatever it is, it activates as I enable logging on the offending element. If I refresh, I have to re-enable logging on that element again. – Roderick Mar 02 '11 at 15:44
0

Another shot in the dark, since it appears to be looking for any kind of 'keydown' event on the entire document.. I wonder if

browser.fireEvent(onkeydown)

would work?

Chuck van der Linden
  • 6,660
  • 2
  • 28
  • 43