0

I am new to watir and ruby. I have two questions for which I am unable to get the solution. I have googled and have still not got working solution. Any assistance will be of great help.

Question 1 : How to fire Javascript event.

The html is as mentioned below.

img onclick="javascript:previousScreen(1);" alt="Back" src="/WSWeb/images/someimage.gif"/

I have tried most of the solutions likes, fireevent etc. which was on Google. Please write me a piece of code which will work for the mentioned scenario.

Question 2 : How to click or validate if the element exists for html mentioned below.

Scenario 1 : This is the html text

input onclick="history.go(-1)" type="button" value="Back"/

Scenario 2 : <center> under this-> Text - Login Id. This is all what is present. This is a cell in a table which is in a frame. Please write me a code for both the scenarios.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
anagraj
  • 109
  • 1
  • 7

2 Answers2

3

Well, if you have two questions, post two questions. :)

If the page is public, post a link to it, that is the best way for us to help.

Regarding javascript events, see How to find out which JavaScript events fired?

You can click the button like this:

browser.button(:value => "Back").click

For the center element, you will have to provide more HTML. I am not sure what you need to do with it.

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

First of all, as to selecting elements that do not have an attribute, see the answers (all three) to this SO question asked a little while back: Accessing an element with no attributes in Watir

With regard to your specific examples/question here: If you refer to the watir wiki's listing of methods supported for selecting elements you will see that an image element (img tag) can be selected by the src. So for your first thing, just do this

browser.image(:src => '/WSWeb/images/someimage.gif').click

if for some reason the click method does not work, then you could try firing the onclick event directly

browser.image(:src => "/WSWeb/images/someimage.gif").fire_event('onclick')

For the second part, you will also notice that value can be used to select 'button' elements which includes input tags of type=button. thus to see if the button is there

browser.button(:value => 'Back').exists? #(will return true or false)

For the second scenario there, Watir does not currently support selecting by most formatting tags like center, so you would need to look at what element contained that text. You say it is a table cell (td) inside a frame (which I don't know how to identify since you provide no html) and if that is the case and I am understanding it right then

browser.frame(:how => 'what').cell(:text => 'Login Id').exists?

should work (presuming the text is actually 'Login Id' and not 'Login ID')

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