1

Can someone tell me how to click with Watir on element that is triggered with javascript events.

For instance, on http://www.kissmetrics.com/, on the homepage, if I click on the 'Learn more' button on the example image, the will bring up a lightbox. Then I can click n the 'Get Started' button to move on the the registration page.

How can I simulate the thing in Watir? Ie. how can I click on first on 'Learn more', then on the 'Get started' button?

Thanks for any help.

esdfal
  • 11
  • 1
  • 2
  • Which 'get started' button did you want to click, there's a green one in the middle of the page, a black one on the right side further down, and a 'get started' link in the page footer. They all lead to the same place, but if you want to click on a specific one, we need to know which one it is, in order to provide you with example code. – Chuck van der Linden Mar 10 '11 at 18:25

3 Answers3

2

well actually there are javascript events fired when you click, but they may not be the ones the particular element is looking for. And sometimes an element has to first be put into a state where you can click on it via something like a mouseover.

so the trick is generally figure out what events need to be fired (see Zeljko's answer) and then use the .fireevent method to fire those events against the elements that need to see them, in the order that is required.

a lot of times you pretty much have to reverse engineer what the developers are doing by looking to see what elements in the html are looking for what events.

In this specific case, if your intent is to click the BLUE learn more button that seems to be on top of some kind of screen shot, then examining the HTML or The DOM shows that it has a convenient ID value which could be used. You can also see it is 'wired' to respond to the onclick event, and more importantly it is opening a new modal window (in this case a div that looks like a popup) onclick="new Modal('learn_more_content', {top: 110, modalClass: 'borderless'}).show() . so the following code ought to work to launch the modal

 browser.button(:id, "learn_more_btn").click

Then to click the green 'get started' button (of which there are several so we need a way to indicate which one)

browser.p(:class, "cta").button(:class, "green button has_arrow").click
Chuck van der Linden
  • 6,660
  • 2
  • 28
  • 43
0

I think the problem is that JavaScript event(s) are not fired when you click a link with Watir. This could help: How to find out which JavaScript events fired?

Community
  • 1
  • 1
Željko Filipin
  • 56,372
  • 28
  • 94
  • 125
0

I've tested this in irb and it works:

browser.span(:text, 'Learn more').click
browser.span(:text, 'Get Started').click

As long as it's in the DOM you can get to it.

kinofrost
  • 768
  • 6
  • 16