Forum: Ruby Watir unable to locate element - please help

Af359c1370bdf5545ba92b25f78b069a?d=identicon&s=25 andrej nikovic (rubynewby)
on 2013-08-30 03:04
Hi,

I have a watir test script that is failing with the error:
Unable to locate element, using {:class"someclass", :text=>"2"}

The weird thing is that when I look at the page source I see that indeed
there is no class "someclass" in the source BUT the radio button in
question still gets selected.

If the radio button still gets selected then why is this error coming
up?
I'm new to ruby and to watir so this has me quite confused. I don't know
how it can even select the radio button if it's throwing up this error
but it certainly is getting selected.

The particular line of code that's failing is:
cell(:class => "someclass", :text => "2").click

Any help at all here would be appreciated.
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-08-30 09:49
Show me the page you're using and the code around that line as well.
It's possible that the radio button is being selected by a different
line of code.

If there's no class: "someclass" element on the page, then why is that
in your code?
Af359c1370bdf5545ba92b25f78b069a?d=identicon&s=25 andrej nikovic (rubynewby)
on 2013-09-01 23:31
Hey there thanks for your reply.

The problem I'm having here is that I'm in a situation where I have to
maintain someone elses Watir test scripts that I had no input in
writing. Add to that the fact that I've never used Watir before and am
learning on the job and you can see the difficulty of my situation.

So that's why I cannot answer why the "cell(:class => "someclass", :text
=>"2")" line is there when there is not "someclass" element on the page.
However I looked through the test script and in that same method I saw
where it is selecting the radio button, in line:
"case_allocating_page.select_complexity_rating("2")"
So thank you for that.

This code is in a test helper script that all other tests use, and a
fair number of them are failing with that one error message of being
unable to find that element. I'll show you this method below and if you
could give me any help then it would be greatly appreciated.

def accept_app(opts = {:ac_user => "ac1", :dao_user => "dao2"})
    relogin_as(opts[:ac_user]) unless opts[:same_user] ||
displayed_username == opts[:ac_user]
    goto_url(@new_app_url)
    application_page = expect_page ApplicationPage
    application_page.click_task("Allocate Application")
    case_allocate_page = expect_page CaseAllocatePage
    page_text.should_not contain("Access Denied!")
    case_allocate_page.select_complexity_rating("2")
    case_allocate_page.click_refresh

    domain = get_domain || "EDAMNP"
    if opts[:same_user]
      debug displayed_username
      try_until(3) {
case_allocate_page.select_user("#{domain}\\#{displayed_username}") }
    else
      to_dao_user = (domain =~ /np/i) ? opts[:dao_user] :
map_user(opts[:dao_user])
      debug "XX => #{domain} #{to_dao_user}"
      begin
        try_until(3) {
case_allocate_page.select_user("#{domain}\\#{to_dao_user}") }
      rescue => e2
        cell(:class => "dxpPageNumber", :text => "2").click
        try_until(3) {
case_allocate_page.select_user("#{domain}\\#{to_dao_user}") }
      end
    end
    case_allocate_page.enter_comments("allocated}")
    case_allocate_page.click_submit
  end


Apologies for the poor indentation.
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-09-02 09:54
Ok, I'll assume that this is the line which is failing:

cell(:class => "dxpPageNumber", :text => "2").click

If you look at where that line is, it's under this:

rescue => e2

The fact that it's gone inside a rescue clause suggests that the real
problem lies within this line:

case_allocate_page.select_user("#{domain}\\#{to_dao_user}")

So If I were you I'd dig deeper into what "select_user" is doing there.
I can't see the code for that method, presumably it's inside the
CaseAllocatePage class. You can add debugging "puts" lines inside the
method to track what's going on.

Also, it's generally bad practice rescuing all standard errors like that
(rescue without an error class). It's better to be specific about the
kind of error you're expecting, which also helps when reading the code:

rescue ThisKindOfError => e2

If you can, it would also help to follow the steps manually, inspecting
the DOM elements and using the exact criteria that Watir is using,
step-by-step.

There isn't much more I can suggest without seeing the HTML and
behaviour of the page you're running this on, and/or the inside of your
CaseAllocatePage class.
Af359c1370bdf5545ba92b25f78b069a?d=identicon&s=25 andrej nikovic (rubynewby)
on 2013-09-02 23:18
Thanks for your post.

the select_user method in case_allocate_page looks like this:

  def select_user(user)
    click_radio_option("rdoDao", user)
  end

I'm puzzled as to why it's failing there but I will use your idea of
putting puts statements in there as a debugging tool because I'm running
these tests through the command prompt as opposed to a testing tool.

Anyways I'll use the advice you gave in your last post and give you an
update on how I go.

Thanks again.
Af359c1370bdf5545ba92b25f78b069a?d=identicon&s=25 andrej nikovic (rubynewby)
on 2013-09-03 04:19
Hey so I put some puts statements in and you were right, the problem is
on line:
case_allocate_page.select_user("#{domain}\\#{to_dao_user}")

I've inserted puts statements in the following places:

begin
puts "A"
  try_until(3)
{case_allocate_page.select_user("#{domain}\\#{to_dao_user}")}
puts "B"
rescue => e2
puts "C"
  cell(:class => "dxpPageNumber", :text => "2").click

I've also inserted a puts in the select user method in the case allocate
page as follows:

def select_user(user)
puts "D"
  click_radio_option("rdoDao", user)
end

A and C are printed to the console and predictably B is not, neither is
D but I would have expected that one to be printed. This tells me that
the tests never reach the select_user method in case_allocate_page but I
don't know why.

Anyways you have helped me isolate where the problem is and for that I'm
grateful.
Any further help is a bonus and if I solve this problem I will let you
know where I was going wrong.

Thanks.
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-09-03 09:46
That's very odd if it isn't getting as far as puts "D".

You could try disabling the "rescue" clause and see what error is being
raised. That might give you a bit more insight into the root of the
problem.
Af359c1370bdf5545ba92b25f78b069a?d=identicon&s=25 andrej nikovic (rubynewby)
on 2013-09-03 23:00
I took away the try until(3){} statement around the call to select user
and then it worked fine :S which is weird because there was no where
that
was giving me any indication that the try until(3) was any sort of
problem.

My guess is maybe it's issues between different versions of watir and
ruby (I'm running watir 2.0.2 and ruby 1.8.7) but I don't know really,
it's just weird.

Anyways Thank you very much for your help! It is very much appreciated.
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-09-03 23:41
In that case you need to test all of the other areas where "try" appears
as well.
Af359c1370bdf5545ba92b25f78b069a?d=identicon&s=25 andrej nikovic (rubynewby)
on 2013-09-05 02:31
Running the full test suite I can see that many tests fail because of
the try_until methods that are in the scripts.

I'm using ruby 1.8.7 and watir 2.0.2 to test IE8.
I'm wondering if there's a require statement that's missing or if there
is some problem with my watir version that is not letting it recognize
try_until as a valid method.

Any thoughts?
Obviously I've been googling this a lot but I just can't seem to find
something that's really useful.
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-09-05 08:15
try_until isn't a standard part of Ruby, or of any gem I know about. I
suspect that it's a method which has been written as a part of your test
code.

Write an isolated test without any rescues which uses try_until, read
the error message, and find out what's going on. I think that either
there's some wrong code inside the method, or the method doesn't exist.
Af359c1370bdf5545ba92b25f78b069a?d=identicon&s=25 andrej nikovic (rubynewby)
on 2013-09-05 22:28
Yea you're right that explains why google hasn't been coming up with any
documentation around try_until.

I can only assume that I haven't been given the full test script suite
because doing a search for try_until in all the test scripts and helpers
comes up empty.

I would have found this out sooner if I wasn't so new to ruby but either
way you helped me a LOT so thank you for your time and help!

Thanks again.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.