Forum: RSpec RSpec and Watir, easy script structure question

Posted by Adam R (Guest)
on 2010-03-02 19:35
(Received via mailing list)
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.
Posted by Adam R (Guest)
on 2010-03-02 22:16
(Received via mailing list)
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.
Posted by Matt Wynne (mattwynne)
on 2010-03-03 00:19
(Received via mailing list)
On 2 Mar 2010, at 21:05, Adam R wrote:

> it still was not recognized.  If I explicitly describe an array in  
> describe "The country page for" do
>      @browser.goto link
> example_helper.rb
> requested page could not be found.')}
>
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Briefly, 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
Posted by David Chelimsky (Guest)
on 2010-03-03 01:14
(Received via mailing list)
On Tue, Mar 2, 2010 at 11:24 AM, Adam R <reed.adam@gmail.com> wrote:
> link points to a new page with a single verifiable item on it.  The script
> @browser.link.each do |link|
>    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
Posted by Adam Reed (Guest)
on 2010-03-03 02:42
(Received via mailing list)
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
Posted by David Chelimsky (Guest)
on 2010-03-03 14:29
(Received via mailing list)
On Tue, Mar 2, 2010 at 7:40 PM, Adam Reed <reed.adam@gmail.com> wrote:
>> >
>> > collects all the links into an array, then iterates over the array,
>> >
>> >              end #it
>> > I'm sure there's a simple solution to the format, but unfortunately I
>> block), not directly inside the describe block.
>
> 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
Posted by Adam R (Guest)
on 2010-03-03 17:17
(Received via mailing list)
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.