How to instantiate my browser? I can make it work but do not understand why

27 views
Skip to first unread message

Jeff Fagot

unread,
Jan 16, 2018, 4:47:30 PM1/16/18
to rspec
Hello,

I am having a hard time to understand how to instantiate my Browser.

I have found a way to make my script working but I do not why and what I really should be doing...

Here is my script that is failing:

##################    FILE_SPEC.RB    ####################

browser = Watir::Browser.new :firefox

RSpec.configure do |config|
  config.before(:each) { @b = browser }
  config.after(:suite) { browser.close unless browser.nil? }
end

context 'Testing reports of Type = 2' do
  describe 'Given I am logging into QC as a ValidUser' do
    @validUser = User.new(@b, 'validuser','mysecretpassword')
    @validUser.log_in_as('validuser','mysecretpassword')

    it 'The ValidUser is taken to the Dashboard page' do
      # @b = browser
      expect(@b.text).to include('Welcome back,  ValidUser  |  Log out')
    end
  end
end
########################################################
=>
#####       OUTPUT     ######

An error occurred while loading ./spec/report_spec.rb.
Failure/Error: @b.text_field(:id, 'UserName').set username

NoMethodError:
  undefined method `text_field' for nil:NilClass
# ./lib/user.rb:12:in `log_in_as'
# ./spec/report_spec.rb:66:in `block (2 levels) in <top (required)>'
# ./spec/report_spec.rb:64:in `block in <top (required)>'
# ./spec/report_spec.rb:62:in `<top (required)>'
No examples found.

Finished in 0.42119 seconds (files took 9.08 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples
########################################################

What I have tested:
 1) Now if I declare "@b = browser" under my 'context'  even using a config.before(:each) => Still it does not work 
 2) But when add @b = browser under the 'describe block => Then it runs fine

Could someone explain to me the logic here and what would be a good practice.

Thanks
JFF

Jon Rowe

unread,
Jan 16, 2018, 7:26:36 PM1/16/18
to rs...@googlegroups.com
It's a bit hard to determine what structure you're attempting to achieve here, but it's a scoping problem you have here.

Ruby's @ variables are either class or instance level depending on where you set them. 

Inside a describe or a context you are at class level, inside an it or before hook you are at instance level.

If you truly want only one watir browser instance I would probably assign it to a module rather than relying on a file level local variable, and access it that way e.g.

module Watir
  def self.browser
    @browser ||= ... #assign browser here
  end
end

Call Watir.browser to instaniate it, then call the same to access it wherever you want. You could assign this to a let for convience.

Cheers
Jon

Jeff Fagot

unread,
Jan 17, 2018, 12:32:23 PM1/17/18
to rspec

Thanks Jon for your time and answer.
Unfortunately, I have to say I am still confused. Moreover (maybe I am wrong but) I have never seen anyone doing as you are suggesting and I do not want a specific solution but rather a common/best practice solution.

So let me exposed you more to what I am trying to accomplish that would probably give you more insights about the "structure I am trying to accomplish" (knowing that there are many things I have to improve).

I am thinking of having a _spec file to test all my reports. I am aware that I will mostly have to refactor afterwards but for the time being I would have within that same _spec file 2 suites defined by their 'context' : one for the Type1 and another for the Type2.
Each 'context' block would have 'its own set of tests defined as 'describe' blocks.
Each 'context' could be run independently from one another and therefore have its own browser instance, while within a specific 'context', I would want to always refer to the same instance browser to play my test (I would log onto the same site with same user once per context).


###########  FILE_SPEC.RB ###########
context 'Testing reports of Type = 2' do

  describe 'Given I am logging into QC as a ValidUser' do
    .....
    it 'The ValidUser is taken to the Dashboard page' do
      expect...
    end

  end
  
  describe 'When I access report_type1 from search' do
    ...
    it 'It opens the report_type1' do
      expect...
    end
  end

  describe 'When I filter the report_type1' do
    ...
    it 'Displays the proper page of results' do
      expect...
    end
  end

end

context ''Testing reports of Type = 1' do
  ....very similar things as above
end
############################################

Any help would be much appreciated.
Thanks

Myron Marston

unread,
Jan 17, 2018, 1:25:15 PM1/17/18
to rs...@googlegroups.com

If you want all examples in a particular context to share an object, the easiest way is to use before(:context) to initialize it and store it in an instance variable, and then use that instance variable from your examples. Here’s an example:

context "my context" doi
  before(:context) { @browser = Watir::Browser.new :firefox }

  it "uses the browser" do
    @browser.do_something
  end

  it "uses the browser for something else" do
    @browser.do_something_else
  end
end

HTH,
Myron



--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+unsubscribe@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/d3279bff-ed2c-4983-b6a6-cf7c36df7c04%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Jeff Fagot

unread,
Jan 19, 2018, 2:30:10 PM1/19/18
to rspec
Thanks Myron for your tip, It is definitely the first time I see a before(:context).

Still I believe that my main issue has to do with what Jon Rowe is pointing out, the weirdest thing is that I originally was successful in writing a authentication_spec.rb file using nested describe and calling the same User class... It is just frustrating.

JFF
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.

To post to this group, send email to rs...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages