Question on watir

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)

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.

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

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

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.

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.

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

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.

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

OK

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

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

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 ?

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’

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

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

Ok thank you.

RAJ

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?

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

hi,

In the below line

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

what is the use of ‘first’?

RAJ