# Question on watir

**URL:** <https://www.ruby-forum.com/t/question-on-watir/224437>\
**Category:** Ruby\
**Created:** [November 12, 2012, 5:45am UTC](https://www.ruby-forum.com/t/question-on-watir/224437 "2012-11-12T05:45:39Z")\
**Posts on this page:** 20\
**Page:** 10

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 4, 2013, 1:20pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/181 "2013-04-04T13:20:25Z")

</div>

how about:

b.text\_field(:id,from spreadsheet).set(from spreadsheet)

or

b.text\_field(:id,from hash).set(from hash)

or

b.text\_field(:id,from array).set(from array)

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 4, 2013, 1:25pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/182 "2013-04-04T13:25:08Z")

</div>

On gmail’s login page, the id is always the same.

If you’re using a site which doesn’t keep the same ID, you’ll need to  
find something else which is immutable (even a parent, child, or  
adjacent element) and use that as the identifier.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 4, 2013, 2:32pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/183 "2013-04-04T14:32:20Z")

</div>

How do you, doing it manually, identify where each value goes?

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 4, 2013, 3:05pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/184 "2013-04-04T15:05:31Z")

</div>

Look at this code

[:text\_fields, :select\_lists, :checkboxes].each do |type|  
$browser.send(type).each do |i|  
if type.to\_s[0…-2].eql?(‘text\_field’)  
puts ‘enter your name’  
p=gets.chomp  
puts “begin”  
puts “$browser.text\_field(:id,’#{i.id}’).set(#{p})”  
puts “rescue =\>e”  
puts “end”  
end  
end  
end

I hope now you can understand what I am doing here.

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 4, 2013, 3:14pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/185 "2013-04-04T15:14:48Z")

</div>

Well, this:  
if type.to\_s[0…-2].eql?(‘text\_field’)

negates this:  
[:text\_fields, :select\_lists, :checkboxes].each do |type|

This whole section appears to accomplish nothing other than outputting  
text:  
puts “begin”  
puts “$browser.text\_field(:id,’#{i.id}’).set(#{p})”  
puts “rescue =\>e”  
puts “end”

And you didn’t answer my question. How do you know where a given value  
goes when you look at the page?

What is your ultimate goal? You seem to be taking a rather convoluted  
path there.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 4, 2013, 2:18pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/186 "2013-04-04T14:18:01Z")

</div>

But my problem is little differs. Here ids are fixed, but I can’t create  
a sheet which is coherent to the field in the text box, so I couldn’t  
put it in a loop,

for an example 10 textboxes are there but I may match first textbox has  
to be filled with the detail of first column in the spread sheet, but I  
can’t achieve this, because Order varies.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 4, 2013, 3:39pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/187 "2013-04-04T15:39:47Z")

</div>

My aim is to produce that four lines code for all fields.

How do you know where a given value  
goes when you look at the page?  
I don’t know that’s what I am stopping as soon as my code approch field  
and waiting for input from me, and I will see what text box is this and  
i will enter that value through key board and that value will be  
gathered into a variable “p” and then that will be assigned into the  
code.

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 4, 2013, 4:08pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/188 "2013-04-04T16:08:18Z")

</div>

Raj pal wrote in post #1104394:

> My aim is to produce that four lines code for all fields.

That sounds like a very repetitive way of going about this. In general,  
code should not be repeated.

If the only thing that changes is the ID, then you should focus your  
efforts on finding something which doesn’t change, or establishing a  
pattern of behaviour so you can write a method which handles this rather  
than writing out an individual line of code for each case.

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 5, 2013, 8:44am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/189 "2013-04-05T08:44:48Z")

</div>

sheet.each do |record|  
if (record[‘DataSet’] == dataSet)  
return record  
end  
end

hi,  
I am trying to find the data with hash, can you re-write the  
statement without any loop using any of the predefined method in ruby?

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 4, 2013, 4:26pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/190 "2013-04-04T16:26:47Z")

</div>

OK

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 5, 2013, 10:00am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/191 "2013-04-05T10:00:12Z")

</div>

I don’t know what your sheet looks like, what data you need to return,  
or what gem you’re using to access the sheet.  
In general a hash could contain all the relevant data without having to  
pick and choose, possibly in a nested hash.

Since I don’t know what your data looks like I’ll give you a generic  
example.

h = {}

sheet.each do |row|

#Create a new nested hash with the key from the first column  
h[row[0].value ] ||= {}

#Reference it so we don’t have to keep writing that line  
ref = h[row[0].value ]

#Fill in other details  
ref[‘password’] = row[1].value  
ref[‘data1’] = row[2].value  
ref[‘data2’] = row[3].value

end

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 5, 2013, 10:10am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/192 "2013-04-05T10:10:10Z")

</div>

i think you did not understand my question here.

For an example,

sheet

name age class  
raj 21 first  
gopal 22 second

now,

Now sheet is the array but the element in the array is hash.

sheet.each do |element|  
if element[‘name’]=‘gopal’  
return element  
end  
end

so my aim is to get the record which consist of the name gopal. Now I am  
using loop for this as i have written, my question is, can able to  
access that element without loop?do you know any predefined function for  
that?

Another question, we can use the key to find the element in the hash,  
but do we find the key using value? one of the way is,

raja.each do |key,value|  
if value==‘hi’  
return key  
end  
end  
Is there any other way ? Is there any predefined function?

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 5, 2013, 11:40am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/193 "2013-04-05T11:40:23Z")

</div>

Yes thank you, this is what I expected.

Another question,

I am using ADODBconnector for exatracting the data from spread sheet,  
here I am getting an error.

14:56:56] [ERROR] : Open  
OLE error code:80004005 in Microsoft JET Database Engine  
The connection for viewing your linked Microsoft Excel worksheet  
was lost.  
HRESULT error code:0x80020009

it shows the error in the below line.

def query(sql)  
recordset = WIN32OLE.new(‘ADODB.Recordset’)  
----------\> recordset.Open(sql, connection)#this line it indicates.  
@data = recordset.GetRows.transpose  
@fields = []  
recordset.Fields.each do |field|  
@fields \<\< field.Name  
end  
end

Do you have any idea ?

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 5, 2013, 10:31am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/194 "2013-04-05T10:31:46Z")

</div>

There are many ways to do this. You should go through some tutorials on  
using Arrays and Hashes.

In an Array containing Hashes:  
sheet.select { |row| row[‘name’]==‘gopal’ }.first

In a Hash:  
raja.index ‘hi’

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 5, 2013, 12:13pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/195 "2013-04-05T12:13:24Z")

</div>

hi,

I have opened the connection like below

$dbConnector = ADODBConnector.new(dataFile)  
$dbConnector.openConnection()

How do I ensure that connection is active?

I working in multilple project and I maintain all project in same  
eclipse it’s working in other project, but not here.

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 5, 2013, 12:20pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/196 "2013-04-05T12:20:41Z")

</div>

Again, this is veering away from Ruby into other territory.  
If you have another working project then check the differences. Often  
it’s a missed character in your connection string.

If you want to see the options available with the object ADODBConnector,  
then try this:  
$dbConnector.ole\_methods

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 5, 2013, 12:25pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/197 "2013-04-05T12:25:21Z")

</div>

Ok thank you.

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 5, 2013, 11:45am UTC](https://www.ruby-forum.com/t/question-on-watir/224437/198 "2013-04-05T11:45:29Z")

</div>

I didn’t know the Jet engine was still in use, thought that had died  
out.

I’d guess that your connection or sql string has an error in. This is  
really more of a StackOverflow type of question.

One other possibility is that you might still have a connection active  
from a previous attempt. Have you tried turning it off and on again?

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 5, 2013, 1:09pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/199 "2013-04-05T13:09:52Z")

</div>

hi I used your code,

my\_hash = {  
User1=\> ‘Pass1’,  
User2=\> ‘Pass2’  
}

my\_hash.each do |username, password|  
$browser.text\_field(:id,‘Email’).set(username.to\_s)  
$browser.text\_field(:id,‘Passwd’).set(password)  
#Other stuff here…  
end

First of all it throws the error in User1:pass1 and then I changed into  
User1=\>‘Pass1’ Now it’s throw an error  
C:/work1/LnTIDITAutomation/Main/hi.rb:48: uninitialized constant User1  
(NameError)

and then I changed this into  
my\_hash = {  
User1=\> ‘Pass1’  
User2=\> ‘Pass2’  
}

Now it is applying. But I want to know whether it’s working in higher  
version of ruby, because you might have applied the code

RAJ

---

<div class="post-metadata">

**Author:** ![rganapathy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ee7513/32.png) [@rganapathy](https://www.ruby-forum.com/u/rganapathy)\
**Post date:** [April 5, 2013, 12:57pm UTC](https://www.ruby-forum.com/t/question-on-watir/224437/200 "2013-04-05T12:57:31Z")

</div>

hi,

In the below line

sheet.select { |row| row[‘name’]==‘gopal’ }.first

what is the use of ‘first’?

RAJ

[Previous page](https://www.ruby-forum.com/t/question-on-watir/224437.md?page=9)

[Next page](https://www.ruby-forum.com/t/question-on-watir/224437.md?page=11)
