# If else condition in ruby not working

**URL:** <https://www.ruby-forum.com/t/if-else-condition-in-ruby-not-working/189446>\
**Category:** Ruby\
**Created:** [June 1, 2010, 8:15am UTC](https://www.ruby-forum.com/t/if-else-condition-in-ruby-not-working/189446 "2010-06-01T08:15:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sweetzubair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@sweetzubair](https://www.ruby-forum.com/u/sweetzubair)\
**Post date:** [June 1, 2010, 8:15am UTC](https://www.ruby-forum.com/t/if-else-condition-in-ruby-not-working/189446/1 "2010-06-01T08:15:40Z")

</div>

i write simple code for button if button pressed than result is passed  
other wise failed.

but every time condition goes to the Failed.  
i try may things but every time i face this problem.

begin  
if(ie.button(:value,“Continue”).click)  
@result = ‘PASSED’  
@log = “Clicked Continue button”  
log.info “Clicked Continue button\n”  
else  
@result = ‘FAILED’  
@log = “Clicked Continue button not pressed”  
log.info “Clicked Continue button not pressed\n”  
end  
Readfrom\_file(@file1,“Continue button”,@log,@result)  
rescue  
end

---

<div class="post-metadata">

**Author:** ![sweetzubair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@sweetzubair](https://www.ruby-forum.com/u/sweetzubair)\
**Post date:** [June 1, 2010, 8:40am UTC](https://www.ruby-forum.com/t/if-else-condition-in-ruby-not-working/189446/2 "2010-06-01T08:40:30Z")

</div>

----- “King M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> if(ie.button(:value,"Continue").click)
> 
> ```
> 
> ## end
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Without knowing the failure you describe and what libraries you are  
using perhaps better organization of your code might be:

begin  
ie.button( :value, ‘Continue’ ).click  
@result = ‘PASSED’  
@log = ‘Clicked Continue button’  
Readfrom\_file(@file1,“Continue button”,@log,@result)  
rescue Exception =\> e  
@result = ‘Failed’  
@log = 'Clicked Continue button not pressed: ’ + e.message  
ensure  
log.info @log + “\n”  
end

---

<div class="post-metadata">

**Author:** ![sweetzubair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@sweetzubair](https://www.ruby-forum.com/u/sweetzubair)\
**Post date:** [June 1, 2010, 9:10am UTC](https://www.ruby-forum.com/t/if-else-condition-in-ruby-not-working/189446/3 "2010-06-01T09:10:19Z")

</div>

It’s Working but…  
if i give the

ie.button( :value, ‘’ ).click

without button name that time they click on button and search next page  
continue button and press many time.

---

<div class="post-metadata">

**Author:** ![sweetzubair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@sweetzubair](https://www.ruby-forum.com/u/sweetzubair)\
**Post date:** [June 1, 2010, 9:17am UTC](https://www.ruby-forum.com/t/if-else-condition-in-ruby-not-working/189446/4 "2010-06-01T09:17:23Z")

</div>

On Tue, Jun 1, 2010 at 1:15 AM, King M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> @result = 'PASSED'
> 
> ```
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Hi, King,

You need to give more context. I assume you are using watir and logger,  
I  
don’t know anything else, but regardless, I shouldn’t have to guess.

Also, I assume by “goes to the Failed” you mean the condition is false,  
as  
opposed to raises an error, for example.

Anyway Try making sure the browser is on the page you think it is on.  
Try looking at the html and making sure that there is only one button  
with a  
value of “Continue”.  
Try inspecting that button before you click it. Make sure it is there,  
make  
sure it is the button you want to click.  
Maybe use the debugger to get an interactive irb session into your code  
right at that moment, so you can play with that object, make sure it  
looks  
how you think it should look. (There is a nice Railscast gives an  
example of  
how to use ruby-debug  
[#54 Debugging with ruby-debug - RailsCasts](http://railscasts.com/episodes/54-debugging-with-ruby-debug))

Also, I don’t know what Readfrom\_file is, but you should check that it  
is  
named correctly, it’s context implies a function, but functions almost  
never  
begin with capital letters, and when they do, it’s not for whatever  
you’re  
trying to do with it.

---

<div class="post-metadata">

**Author:** ![sweetzubair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@sweetzubair](https://www.ruby-forum.com/u/sweetzubair)\
**Post date:** [June 1, 2010, 10:02am UTC](https://www.ruby-forum.com/t/if-else-condition-in-ruby-not-working/189446/5 "2010-06-01T10:02:59Z")

</div>

Method:  
def Readfrom\_file(filename,fieldata,log,result)  
@fielddata = fieldata  
@log = log  
@result = result  
@filename = filename

@filename.puts “

#{@fielddata} ”  
@filename.puts “ #{@log}”  
if(@result == ‘PASSED’)  
@filename.puts “#{@result} ”  
else  
@filename.puts “#{@result}”  
end  
end  
def closeFile(filename)  
@filename.close()  
end

main file:

require ‘watir’  
include Watir  
require ‘logger’  
require ‘D:\Scripts\report.rb’  
require ‘D:\Scripts\Create\_HTML.rb’  
@file1 = HTML\_HEADER()  
opts = {}  
File.open(“c:/test/Flow1.txt”) do |f|  
f.each\_line do |line|  
opts[$1] = $2 if line =~ /^(._)=(._)$/  
end  
end  
#Create the Timestamp on file  
fileName=Time.now.strftime(’%H-%M-%S-%d-%m-%Y’)  
OrignalFilename =“logger”  
fileName1=“C:\test\”+fileName+"\_"+OrignalFilename+".txt"  
@Customername =opts[“customername”]  
@Iteration = opts[“iteration”]  
log = Logger.new(fileName1)  
begin  
if(  
ie=Watir::IE.start(“http://"+@Customername+”.vendormate.net/"+@Iteration+"/login.do"))  
if(ie.contains\_text(“Login”))  
log.info “login page open successfully”  
else  
log.info “login page not found”  
break  
end  
end  
rescue Exception =\> e  
end  
ie.link(:text, “New User Registration \>\>”).click  
log.info “Click on New User Registration link”  
#Code for Finding file and Directory Path.  
path=“c:/test/”  
file = “Test.txt”  
begin  
if File.directory?(path) && File.exists?(path+""+file)  
log.info “File or Directory Found”  
else  
log.info “File or Directory Not Found\n”  
end  
rescue Exception =\> e  
end  
@FirstName = opts[“firstName”]  
@LastName = opts[“lastName”]  
@LegalName = opts[“legalName”]  
@VendorRepWorkPhone = opts[“vendorRepWorkPhone”]  
@Email = opts[“email”]  
@ConFirmEmail = opts[“confirmEmail”]

#Compair the username and password and set into the textbox  
begin  
ie.text\_field(:name,“firstName”).set(@FirstName)  
if(ie.text\_field(:name,“firstName”).value == @FirstName)  
@result = ‘PASSED’  
@log =“First Name is correct”  
log.info “First Name is correct”  
else  
@result = ‘FAILED’  
@log = “First Name not Match”  
log.info “First Name not Match”  
end  
Readfrom\_file(@file1,ie.text\_field(:name,“firstName”).name,@log,@result)  
rescue Exception =\> e  
end

begin  
ie.text\_field(:name,“lastName”).set(@LastName)  
if(ie.text\_field(:name,“lastName”).value == @LastName)  
@result = ‘PASSED’  
@log = “last Name is correct”  
log.info “last Name is correct”  
else  
@result = ‘FAILED’  
@log = “last Name not Match”  
log.info “last Name not Match”  
end  
Readfrom\_file(@file1,ie.text\_field(:name,“lastName”).name,@log,@result)  
rescue Exception =\> e  
end

begin  
ie.text\_field(:name,“legalName”).set(@LegalName)  
if(ie.text\_field(:name,“legalName”).value == @LegalName)  
@result = ‘PASSED’  
@log = “legal Name is correct”  
log.info “legal Name is correct”  
else  
@result = ‘FAILED’  
@log = “legal Name not Match”  
log.info “legal Name not Match”  
end  
Readfrom\_file(@file1,ie.text\_field(:name,“legalName”).name,@log,@result)  
rescue Exception =\> e  
end

begin  
ie.text\_field(:name,“vendorRepWorkPhone”).set(@VendorRepWorkPhone)  
if(ie.text\_field(:name,“vendorRepWorkPhone”).value ==  
@VendorRepWorkPhone)  
@result = ‘PASSED’  
@log = “vendor Rep Work Phone is correct”  
log.info “vendor Rep Work Phone is correct”  
else  
@result = ‘FAILED’  
@log = “vendor Rep Work Phone not Match”  
log.info “vendor Rep Work Phone not Match”  
end  
Readfrom\_file(@file1,ie.text\_field(:name,“vendorRepWorkPhone”).name,@log,@result)  
rescue Exception =\> e  
end

begin  
ie.text\_field(:name,“email”).set(@Email )  
if(ie.text\_field(:name,“email”).value == @Email)  
@result = ‘PASSED’  
@log = “Email is correct”  
log.info “Email is correct”  
else  
@result = ‘FAILED’  
@log = “Email not Match”  
log.info “Email not Match”  
end  
Readfrom\_file(@file1,ie.text\_field(:name,“email”).name,@log,@result)  
rescue Exception =\> e  
end

begin  
ie.text\_field(:name,“confirmEmail”).set(@ConFirmEmail )  
if(ie.text\_field(:name,“confirmEmail”).value == @ConFirmEmail )  
@result = ‘PASSED’  
@log = “confirm Email is correct”  
log.info “confirm Email is correct”  
else  
@result = ‘FAILED’  
@log = “confirm Email not Match”  
log.info “confirm Email not Match”  
end  
Readfrom\_file(@file1,ie.text\_field(:name,“confirmEmail”).name,@log,@result)  
rescue Exception =\> e  
end

page layout:  
firstname:  
lastname:  
LegalName:  
phone number:  
email id:  
conform email id:

continue button.button value is continue and name is \_eventId\_submit.

(next page opened)

reg number:  
conform reg number:

continue button.

C:\>ruby -v  
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

C:\>gem -v  
0.9.4

browser IE 7.0.5730.13
