If else condition is not working in ruby

I wrote simple code to verify text appears in the result page or not.

This is my code. I wrote this code in separate file and saved with ruby
extension. It executed successfully. But, False condition only executes.

I’m beginner to watir, can any one please tell me why it is happening.

require “watir”
b=Watir::Browser.new
b.goto(“http://google.com/”)
b.text_field(:name,“q”).set “pickaxe”
b.button(:name,“btnG”).click
if b.text.include?“Programming Ruby”
puts “Test passed”
else
puts “Test Failed”
end

hi anjaneyulu -

i have no experience whatsoever with watir, so hopefully someone who
does can help you with this question - in fact you might consider
reposting it as a question concerning watir, as i doubt that the problem
is in ruby itself (unless your ruby interpreter is somehow “broken”!)

try this code:

text = “chunky bacon”

if text.include?(“chunk”)
puts “yep”
else
puts “nope”
end

now try changing the argument to the include? method to something like
“xyz”, and i think you’ll see that the if/else condition is working in
ruby. i suspect - though again i don’t know anything about watir - that
the problem lies before you get to the if/else condition, and so the
condition is always returning false…

good luck!

  • j

anjaneyulu d. wrote in post #1003898:

This is my code. I wrote this code in separate file and saved with ruby
extension. It executed successfully. But, False condition only executes.

So you need to check the condition: what does

puts b.text.inspect

show?

You could update your code like this:

if b.text.include?“Programming Ruby”
puts “Test passed”
else
puts “Test Failed, got #{b.text.inspect}, does not contain ‘Programming Ruby’”
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs