From: Kev Jackson Date: 2006-03-29T12:05:52+09:00 Subject: Can anyone see the obvious msitake I've made? Hi, I'm hacking up some scripts and I have a lot of repetitive code ie: #corporate name @@ie.text_field(:id, @test_data['pg_401']['corp_nm_fld']).value=@test_data['pg_401']['corp_nm'] @@ie.text_field(:id, @test_data['pg_401']['corp_nm_kn_fld']).value=@test_data['pg_401']['corp_nm_kn'] #rep name @@ie.text_field(:id, @test_data['pg_401']['rep_j_pos_fld']).value=@test_data['pg_401']['rep_j_pos'] @@ie.text_field(:id, @test_data['pg_401']['rep_nm_fld']).value=@test_data['pg_401']['rep_nm'] @@ie.text_field(:id, @test_data['pg_401']['rep_nm_kn_fld']).value=@test_data['pg_401']['rep_nm_kn'] #telephone @@ie.text_field(:id, @test_data['pg_401']['tel_no_1_fld']).value=@test_data['pg_401']['tel_no_1'] @@ie.text_field(:id, @test_data['pg_401']['tel_no_2_fld']).value=@test_data['pg_401']['tel_no_2'] ... It looks like a perfect place to use a macro to write out the call to ie.text_field at run-time (especially because of the duplicated variables) I've written a method_missing method in my base class and tried to implement something like @@ie.text_field(:id, @test_data['pg_401']['tel_no_1_fld']).value=@test_data['pg_401']['tel_no_1'] => text :id, :pg_401, :tel_no_1 This would dramatically reduce the amount of typing (and it'll reduce the training time for the qc team who ultimately have to write these scripts), and may allow me to refactor further so that the entire test script could be constructed from a yml file on the fly - which would save a *lot* of hassle. Now I've changed @@ie to $ie as it makes more sense (watir and I only want to be driving one ie window at once, so make it a global) in my method_missing definition I have when /text/=~m.id2name #works $ie.text_field(:id, eval("$test_data['#{args[1].to_s}']['#{args[2]}_fld']").to_s).set(eval("$test_data['#{args[1].to_s}']['#{args[2]}']")) #doesn't work args[0] = id $ie.text_field("#{args[0].to_sym}", eval("$test_data['#{args[1].to_s}']['#{args[2]}_fld']")).set(eval("$test_data['#{args[1].to_s}']['#{args[2]}']")) What I want to do is to create a call to $ie.text_field with the correct params - as you can see if I specify the symbol :id, it works perfectly (not sure eval is the correct thing to use but it works), however if I try to get the symbol from the args[] and then to_sym before executing, I always get testScenario(NewApplicationTest): Watir::Exception::UnknownObjectException: Unable to locate object, using id and form:address1 However I've just tried (as I was typing this email) $ie.text_field(eval("args[0].to_sym"), eval("$test_data['#{args[1].to_s}']['#{args[2]}_fld']")).set(eval("$test_data['#{args[1].to_s}']['#{args[2]}']")) And it works! :( Can anyone explain why #{args[0].to_sym} is not actually evaluating to the :id symbol, but eval(args[0].to_sym) does? Especially as the error produced from watir is exactly the same as if I'd have typed: $ie.text_field(:id, 'a_field_that_doesn't_exist') Also if there is a better way to do this without using three eval calls per method that I want to add (seems a little overkill to me) Thanks Kev