On Tue, Mar 31, 2009 at 11:33:42PM -0700, Vikas Tulashyam wrote:
> 
> Hi Anna,
> Is there any method by which I can convert the String values to Hash.
> 
> Thanks
> Vikas

Well, the fastest (but also dirtiest) way is instance_eval:

irb(main):001:0> s = "{:name=>'q'}"
=> "{:name=>'q'}"
irb(main):002:0> s.class
=> String
irb(main):003:0> o = instance_eval(s)
=> {:name=>"q"}
irb(main):004:0> o.class
=> Hash

This would work, but I wouldn't recommend it since it interprets
everything in <Locator> as Ruby code.

You could also add more structure to the locator and build the hash
yourself:

<Locator>
<Key>name</Key>
<Value>q</Value>
<Locator>

def get_object_locator(object_id)
  object_file = Document.new File.new('c:\a.xml')
  object_file.elements.each("//TestObject[Identifier='#{object_id}']") {
    locator = element.elements['Locator']
    key = locator.elements['Key'].text
    value = locator.elements['Value'].text
    return { key.to_sym, value }
  }
end

That's not the most elegant of code, but you get the idea.


HTH,
Anna

> 
> On Apr 1, 11:06 am, Anna Gabutero <a...@lavabit.com> wrote:
> > On Tue, Mar 31, 2009 at 09:07:29PM -0700, Vikas Tulashyam wrote:
> >
> > > Hi firends,
> > > Please help me.
> >
> > > Thanks
> > > Vikas
> >
> > > On Mar 31, 6:24 pm, Vikas Tulashyam <vtulash...@gmail.com> wrote:
> > > > Hi,
> > > > Actually, I am trying to read the sLocator value form a xml file and
> > > > thats the main problem if I put the value directly into a variable
> > > > then it's working fine like-->
> >
> > > > a= {:name=>'q'}
> > > > return $sBrowser.text_field(a)
> >
> > > > Code written above works fine but when I m doing this-
> >
> > > >         sLoc = getObjectLocator("Text_Search")
> > > >         return $sBrowser.text_field(sLoc)
> >
> > > >         def getObjectLocator(sObjectId)
> > > >         begin
> > > >           $sObjectFile = Document.new File.new("c:\\a.xml")
> >
> > > >           $sObjectFile.elements.each("//TestObject[Identifier='#
> > > > {sObjectId}']") { |element|
> > > >             sLocator = element.elements['Locator'].text
> > > >             sObjectType = element.elements['ObjType'].text
> > > >             return sLocator
> > > >           }
> >
> > > > sLoc returns the correct value, I tried this by printing the sLoc
> > > > value but It is nt working
> >
> > It is returning a different value.  Your assignment is equivalent to:
> >
> >   sLoc = "{:name=>'q'}"
> >
> > which is different from:
> >
> >   a = {:name=>'q'}
> >
> > The first is a string, the second is a hash, and these object types are
> > not interchangeable.
> >
> > - Anna
> > 
> 
> ____________________________________________________________________________________
> Use the link below to report this message as spam.
> https://lavabit.com/apps/teacher?sig=533695&key=3598775855
> ____________________________________________________________________________________


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Watir General" group.
To post to this group, send email to watir-general@googlegroups.com
Before posting, please read the following guidelines: 
http://wiki.openqa.org/display/WTR/Support
To unsubscribe from this group, send email to 
watir-general-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/watir-general
-~----------~----~----~----~------~----~------~--~---

Reply via email to