Adam R
2010-Mar-02 17:24 UTC
[rspec-users] RSpec and Watir, easy script structure question
Howdy! I''m relatively new to RSpec, but I''ve worked with Watir and Ruby for about 5 years now. My company is redesigning a web app from the ground up, so I''m taking the opportunity to leverage RSpec instead of Test/Unit. I''ve run into a problem with RSpec organization that I just can''t sort out on my own, so I''m hoping it''s straightforward enough that someone can help me out. Basically I have a single HTML page with an organized list of links. Each link points to a new page with a single verifiable item on it. The script collects all the links into an array, then iterates over the array, loading each link and verifying a single piece of text. That''s the easy part. Here''s the relevant code for what I believe to be the closest attempt right now: @links = Array.new @browser = Watir::Browser.new @browser.goto(page) @browser.link.each do |link| @links << [link.name, link.href] end @links.each do |name, link| describe name do it "should contain the word #{name}" do @browser.goto link @browser.div(:id, /example/).text.should include(name) end #it end #desc end #@links I''ve tried a combination of methods -- putting all of this code inside a describe block, etc, but I end up with the variables being unknown because they''re not inside the same describe or it block (even when set to $global), or the describe/it blocks being layered incorrectly. I''m sure there''s a simple solution to the format, but unfortunately I haven''t been able to Google or RDoc my way to an answer. I appreciate any advice you can offer! Thanks, Adam -- View this message in context: http://old.nabble.com/RSpec-and-Watir%2C-easy-script-structure-question-tp27758607p27758607.html Sent from the rspec-users mailing list archive at Nabble.com.
An update for this issue. This script works, but only if I explicitly define the array that I''m iterating over (][countryname, url]].each do, rather than @countries.each do). Can someone help me understand why my array here (@countries) is not recognized? I tried to instantiate it as a global variable ($countries) and it still was not recognized. If I explicitly describe an array in place of the variable, the script works. Is it an issue with my organization format, or something else? global_page_spec.rb $LOAD_PATH.unshift File.join(File.dirname(__FILE__), ''..'') if $0 == __FILE__ require ''helpers/example_helper'' describe "The country page for" do include ExHelper before(:all) do setup collect_global_countries end @countries.each do |name, link| describe name do it "should contain the word #{name} in the title" do @browser.goto link @browser.div(:id, /content-content/).text.should include(name) end #it end #desc end #countries after(:all) do teardown end #after end #spec example_helper.rb $LOAD_PATH.unshift File.join(File.dirname(__FILE__), ''..'') if $0 == __FILE__ require ''helpers/global_helper'' module ExHelper include GlobalHelper def setup @browser = Watir::Browser.new @browser.add_checker lambda {|b| b.text.should_not include(''The requested page could not be found.'')} end # setup def collect_global_countries @countries = Array.new @countries.should be_empty @browser.goto "http://www.#{$env}.com/global" @browser.table(:class, /global-list/).links.each do |link| @countries << [link.text, link.href] end #links @countries.should_not be_empty def teardown @browser.close end # teardown end #module -- View this message in context: http://old.nabble.com/RSpec-and-Watir%2C-easy-script-structure-question-tp27758607p27761159.html Sent from the rspec-users mailing list archive at Nabble.com.
On 2 Mar 2010, at 21:05, Adam R wrote:> > An update for this issue. This script works, but only if I > explicitly define > the array that I''m iterating over (][countryname, url]].each do, > rather than > @countries.each do). > > Can someone help me understand why my array here (@countries) is not > recognized? I tried to instantiate it as a global variable > ($countries) and > it still was not recognized. If I explicitly describe an array in > place of > the variable, the script works. > > Is it an issue with my organization format, or something else? > > global_page_spec.rb > $LOAD_PATH.unshift File.join(File.dirname(__FILE__), ''..'') if $0 == > __FILE__ > require ''helpers/example_helper'' > > describe "The country page for" do > include ExHelper > > before(:all) do > setup > collect_global_countries > end > > @countries.each do |name, link| > describe name do > it "should contain the word #{name} in the title" do > @browser.goto link > @browser.div(:id, /content-content/).text.should include(name) > end #it > end #desc > end #countries > > after(:all) do > teardown > end #after > end #spec > > example_helper.rb > $LOAD_PATH.unshift File.join(File.dirname(__FILE__), ''..'') if $0 == > __FILE__ > require ''helpers/global_helper'' > > module ExHelper > include GlobalHelper > > def setup > @browser = Watir::Browser.new > @browser.add_checker lambda {|b| b.text.should_not include(''The > requested page could not be found.'')} > end # setup > > def collect_global_countries > @countries = Array.new > @countries.should be_empty > > @browser.goto "http://www.#{$env}.com/global" > @browser.table(:class, /global-list/).links.each do |link| > @countries << [link.text, link.href] > end #links > > @countries.should_not be_empty > > def teardown > @browser.close > end # teardown > end #module > -- > View this message in context: http://old.nabble.com/RSpec-and-Watir%2C-easy-script-structure-question-tp27758607p27761159.html > Sent from the rspec-users mailing list archive at Nabble.com. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersBriefly, think of two passes over this file: the first ''parse'' pass, and the second ''execution'' pass. The #before and #it blocks run during the ''execution'' pass, but the code in the #describe blocks runs in the initial ''parse'' pass. Because @countries is not created until the before blocks run, there''s no way for the ''parse'' pass to know what value it has. That''s the basic essence of your issue. I''d recommend paring this back to a much simpler example, getting that working, then building up from there. I''m also not sure why you''re using the SUT to generate the tests, but that''s for another thread... cheers, Matt http://mattwynne.net +447974 430184
David Chelimsky
2010-Mar-03 00:11 UTC
[rspec-users] RSpec and Watir, easy script structure question
On Tue, Mar 2, 2010 at 11:24 AM, Adam R <reed.adam at gmail.com> wrote:> > Howdy! ?I''m relatively new to RSpec, but I''ve worked with Watir and Ruby for > about 5 years now. ?My company is redesigning a web app from the ground up, > so I''m taking the opportunity to leverage RSpec instead of Test/Unit. > > I''ve run into a problem with RSpec organization that I just can''t sort out > on my own, so I''m hoping it''s straightforward enough that someone can help > me out. > > Basically I have a single HTML page with an organized list of links. ?Each > link points to a new page with a single verifiable item on it. ?The script > collects all the links into an array, then iterates over the array, loading > each link and verifying a single piece of text. ?That''s the easy part. > > Here''s the relevant code for what I believe to be the closest attempt right > now: > > @links = Array.new > @browser = Watir::Browser.new > @browser.goto(page) > > @browser.link.each do |link| > ? ?@links << [link.name, link.href] > end > > ? ?@links.each do |name, link| > ? ? ? ? describe name do > ? ? ? ? ? ? ?it "should contain the word #{name}" do > ? ? ? ? ? ? ? ? ? ?@browser.goto link > ? ? ? ? ? ? ? ? ? ?@browser.div(:id, /example/).text.should include(name) > ? ? ? ? ? ? ?end #it > ? ? ? ? end #desc > ? ?end #@links > > I''ve tried a combination of methods -- putting all of this code inside a > describe block, etc, but I end up with the variables being unknown because > they''re not inside the same describe or it block (even when set to $global), > or the describe/it blocks being layered incorrectly. > > I''m sure there''s a simple solution to the format, but unfortunately I > haven''t been able to Google or RDoc my way to an answer. ?I appreciate any > advice you can offer!describe "something" do it "does something" do end end That''s the simplest format - everything should go in the example (it block), not directly inside the describe block. HTH, David> > Thanks, > Adam > -- > View this message in context: http://old.nabble.com/RSpec-and-Watir%2C-easy-script-structure-question-tp27758607p27758607.html > Sent from the rspec-users mailing list archive at Nabble.com. > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Adam Reed
2010-Mar-03 01:40 UTC
[rspec-users] RSpec and Watir, easy script structure question
Thanks David. I have the basic format working in other scripts, so that''s a good sign. Another user pointed me to this example, which is why I was exploring a different format:> ["Los Angeles", "Austin"].each do |location| > it "should show #{location}" do > @browser.div(:id, /example/).text.should include(location) > end > end >Is this not correct/usable? Thanks again, Adam On Tue, Mar 2, 2010 at 6:11 PM, David Chelimsky <dchelimsky at gmail.com>wrote:> On Tue, Mar 2, 2010 at 11:24 AM, Adam R <reed.adam at gmail.com> wrote: > > > > Howdy! I''m relatively new to RSpec, but I''ve worked with Watir and Ruby > for > > about 5 years now. My company is redesigning a web app from the ground > up, > > so I''m taking the opportunity to leverage RSpec instead of Test/Unit. > > > > I''ve run into a problem with RSpec organization that I just can''t sort > out > > on my own, so I''m hoping it''s straightforward enough that someone can > help > > me out. > > > > Basically I have a single HTML page with an organized list of links. > Each > > link points to a new page with a single verifiable item on it. The > script > > collects all the links into an array, then iterates over the array, > loading > > each link and verifying a single piece of text. That''s the easy part. > > > > Here''s the relevant code for what I believe to be the closest attempt > right > > now: > > > > @links = Array.new > > @browser = Watir::Browser.new > > @browser.goto(page) > > > > @browser.link.each do |link| > > @links << [link.name, link.href] > > end > > > > @links.each do |name, link| > > describe name do > > it "should contain the word #{name}" do > > @browser.goto link > > @browser.div(:id, /example/).text.should include(name) > > end #it > > end #desc > > end #@links > > > > I''ve tried a combination of methods -- putting all of this code inside a > > describe block, etc, but I end up with the variables being unknown > because > > they''re not inside the same describe or it block (even when set to > $global), > > or the describe/it blocks being layered incorrectly. > > > > I''m sure there''s a simple solution to the format, but unfortunately I > > haven''t been able to Google or RDoc my way to an answer. I appreciate > any > > advice you can offer! > > describe "something" do > it "does something" do > end > end > > That''s the simplest format - everything should go in the example (it > block), not directly inside the describe block. > > HTH, > David > > > > > Thanks, > > Adam > > -- > > View this message in context: > http://old.nabble.com/RSpec-and-Watir%2C-easy-script-structure-question-tp27758607p27758607.html > > Sent from the rspec-users mailing list archive at Nabble.com. > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100302/b04b3514/attachment.html>
David Chelimsky
2010-Mar-03 13:15 UTC
[rspec-users] RSpec and Watir, easy script structure question
On Tue, Mar 2, 2010 at 7:40 PM, Adam Reed <reed.adam at gmail.com> wrote:> On Tue, Mar 2, 2010 at 6:11 PM, David Chelimsky <dchelimsky at gmail.com> > wrote: >> >> On Tue, Mar 2, 2010 at 11:24 AM, Adam R <reed.adam at gmail.com> wrote: >> > >> > Howdy! ?I''m relatively new to RSpec, but I''ve worked with Watir and Ruby >> > for >> > about 5 years now. ?My company is redesigning a web app from the ground >> > up, >> > so I''m taking the opportunity to leverage RSpec instead of Test/Unit. >> > >> > I''ve run into a problem with RSpec organization that I just can''t sort >> > out >> > on my own, so I''m hoping it''s straightforward enough that someone can >> > help >> > me out. >> > >> > Basically I have a single HTML page with an organized list of links. >> > ?Each >> > link points to a new page with a single verifiable item on it. ?The >> > script >> > collects all the links into an array, then iterates over the array, >> > loading >> > each link and verifying a single piece of text. ?That''s the easy part. >> > >> > Here''s the relevant code for what I believe to be the closest attempt >> > right >> > now: >> > >> > @links = Array.new >> > @browser = Watir::Browser.new >> > @browser.goto(page) >> > >> > @browser.link.each do |link| >> > ? ?@links << [link.name, link.href] >> > end >> > >> > ? ?@links.each do |name, link| >> > ? ? ? ? describe name do >> > ? ? ? ? ? ? ?it "should contain the word #{name}" do >> > ? ? ? ? ? ? ? ? ? ?@browser.goto link >> > ? ? ? ? ? ? ? ? ? ?@browser.div(:id, /example/).text.should >> > include(name) >> > ? ? ? ? ? ? ?end #it >> > ? ? ? ? end #desc >> > ? ?end #@links >> > >> > I''ve tried a combination of methods -- putting all of this code inside a >> > describe block, etc, but I end up with the variables being unknown >> > because >> > they''re not inside the same describe or it block (even when set to >> > $global), >> > or the describe/it blocks being layered incorrectly. >> > >> > I''m sure there''s a simple solution to the format, but unfortunately I >> > haven''t been able to Google or RDoc my way to an answer. ?I appreciate >> > any >> > advice you can offer! >> >> describe "something" do >> ?it "does something" do >> ?end >> end >> >> That''s the simplest format - everything should go in the example (it >> block), not directly inside the describe block.> Thanks David.? I have the basic format working in other scripts, so that''s a > good sign.? Another user pointed me to this example, which is why I was > exploring a different format: > >> >> ["Los Angeles", "Austin"].each do |location| >> ? ? it "should show #{location}" do >> ? ? ? @browser.div(:id, /example/).text.should include(location) >> ? ? end >> end > > Is this not correct/usable?Correct is subjective. What you have here is somewhat harmless, but I''ve seen simple lists like that grow into nested lists with conditional logic where members of one list impact what is done with the values in the other. That makes it very difficult to understand failures and difficult to change as requirements change. HTH, David
Adam R
2010-Mar-03 16:08 UTC
[rspec-users] RSpec and Watir, easy script structure question
Thanks again for the parse/execute perspective Matt, that really sorted me out. Solution: Hey dummy, collect the data and create the array before starting the describe function. Moral of the story: I was trying to make collecting the data that I wanted to verify part of the test/spec, partially because I had it stuck in my head that ''everything'' had to be inside a describe block, and partially because I was treating RSpec like some new alien technology instead of a Ruby library. Code: @Array = [...] or Method to collect array data @array.each do |name, stuff| describe name do it "should put the lotion in the basket" do <code> end end end -- View this message in context: http://old.nabble.com/RSpec-and-Watir%2C-easy-script-structure-question-tp27758607p27770149.html Sent from the rspec-users mailing list archive at Nabble.com.