{"action":"created","issue":{"url":"https://api.github.com/repos/watir/watir-rspec/issues/15","repository_url":"https://api.github.com/repos/watir/watir-rspec","labels_url":"https://api.github.com/repos/watir/watir-rspec/issues/15/labels{/name}","comments_url":"https://api.github.com/repos/watir/watir-rspec/issues/15/comments","events_url":"https://api.github.com/repos/watir/watir-rspec/issues/15/events","html_url":"https://github.com/watir/watir-rspec/issues/15","id":175555239,"number":15,"title":"Instantiate browser in before :suite?","user":{"login":"Portalboy","id":6773480,"avatar_url":"https://avatars.githubusercontent.com/u/6773480?v=3","gravatar_id":"","url":"https://api.github.com/users/Portalboy","html_url":"https://github.com/Portalboy","followers_url":"https://api.github.com/users/Portalboy/followers","following_url":"https://api.github.com/users/Portalboy/following{/other_user}","gists_url":"https://api.github.com/users/Portalboy/gists{/gist_id}","starred_url":"https://api.github.com/users/Portalboy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Portalboy/subscriptions","organizations_url":"https://api.github.com/users/Portalboy/orgs","repos_url":"https://api.github.com/users/Portalboy/repos","events_url":"https://api.github.com/users/Portalboy/events{/privacy}","received_events_url":"https://api.github.com/users/Portalboy/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-09-07T16:59:29Z","updated_at":"2016-09-12T16:05:01Z","closed_at":"2016-09-12T15:52:29Z","body":"In my program I am using Watir to spool up web browsers and test various pages for a website. The easiest way to do this (which I have been using) is to go with watir-rspec's default behavior of opening a new browser instance for every test package, then close it afterwards.\r\n\r\nI really want to make my program more efficient, so that instead of opening and closing windows for every unit test, it will 'borrow' a browser that was already open from the previous test, redirect it to the page it tests on, and test. \r\nThe problem with this is that although when I instantiate my browser object in before :all it works just fine, in before :suite it can't be accessed. If I puts the object name from before :all it comes back nil, and likewise within each spec file it comes back nil.\r\n\r\nBasically, I'm instantiating the object in before :suite and destroying it in after :suite so that it can persist through all specs, then only close at the end. Is there anything I am missing about scope here? Is there a better way to do this altogether?\r\nFurthermore, is there a way to do this while still maintaining the command shortening functionality \r\n\r\n(I.E. button(id: \"foo\") or send_keys :return)?\r\n\r\nMy spec_helper.rb\r\n\r\n```ruby\r\nrequire_relative '../login'\r\nrequire_relative '../logInObject'\r\n\r\nrequire 'watir'\r\n\r\n\r\n\r\n\r\n# Configuration for watir-rspec\r\nrequire \"watir/rspec\"\r\n\r\nRSpec.configure do |config|\r\n  # Use Watir::RSpec::HtmlFormatter to get links to the screenshots, html and\r\n  # all other files created during the failing examples.\r\n  config.add_formatter(:progress) if config.formatters.empty?\r\n  config.add_formatter(Watir::RSpec::HtmlFormatter)\r\n\r\n  # Open up the browser for each example.\r\n#=begin\r\n  config.before :all do\r\n  \r\n#  puts \"----------before all!\"\r\n#    @browser = LoginDrone.new :internet_explorer if !@browser\r\n#\t@browser.doLogin\r\n#\tputs \"Before all browser: #{@browser}\"\r\n#\tsleep(3)\r\n  end\r\n  \r\n  config.before :suite do\r\n#  puts \"----------before suite!\"\r\n#  puts \"Before suite browser: #{@browser}\"\r\n\t$user = Credentials.new #if !$user\r\n\t@browser = LoginDrone.new :internet_explorer if !@browser\r\n\t@browser.doLogin\r\n\tputs @browser\r\n  end\r\n  \r\n#  config.before :all do\r\n#\tgoto \"office.rcsdev.com/dasqa\"\r\n#  end\r\n#=end\r\n=begin\r\n  #This bit is of my own making, it chooses not to open a new browser if one exists.\r\n  config.before :all do\r\n    if @browser.nil?\r\n\t\t@browser = LoginDrone.new :internet_explorer\r\n\t\tdoLogin\r\n\tend\r\n  end\r\n=end\r\n#=begin\r\n  # Close that browser after each example.\r\n  config.after :suite do\r\n  puts \"----------after suite!\" if @browser\r\n  puts \"after suite browser: #{@browser}\"\r\n    @browser.close if @browser\r\n  end\r\n#=end\r\n\r\n\r\n\r\n\r\n\r\n  # Include RSpec::Helper into each of your example group for making it possible to\r\n  # write in your examples instead of:\r\n  #   @browser.goto \"localhost\"\r\n  #   @browser.text_field(name: \"first_name\").set \"Bob\"\r\n  #\r\n  # like this:\r\n  #   goto \"localhost\"\r\n  #   text_field(name: \"first_name\").set \"Bob\"\r\n  #\r\n  # This needs that you've used @browser as an instance variable name in\r\n  # before :all block.\r\n  config.include Watir::RSpec::Helper\r\n\r\n  # Include RSpec::Matchers into each of your example group for making it possible to\r\n  # use #within with some of RSpec matchers for easier asynchronous testing:\r\n  #   expect(@browser.text_field(name: \"first_name\")).to exist.within(2)\r\n  #   expect(@browser.text_field(name: \"first_name\")).to be_present.within(2)\r\n  #   expect(@browser.text_field(name: \"first_name\")).to be_visible.within(2)\r\n  #\r\n  # You can also use #during to test if something stays the same during the specified period:\r\n  #   expect(@browser.text_field(name: \"first_name\")).to exist.during(2)\r\n  config.include Watir::RSpec::Matchers\r\nend\r\n\r\n```\r\n\r\nLoginDrone is an object that inherits from Watir::Browser with some added goodies, like the doLogin method which goes to my test site and gets a credential input.\r\n\r\nP.S. I *-DON'T-* want to use global variables if at all possible, because A: they are inefficient and B: I want to eventually add multi-threading support."},"comment":{"url":"https://api.github.com/repos/watir/watir-rspec/issues/comments/246397789","html_url":"https://github.com/watir/watir-rspec/issues/15#issuecomment-246397789","issue_url":"https://api.github.com/repos/watir/watir-rspec/issues/15","id":246397789,"user":{"login":"jarmo","id":174160,"avatar_url":"https://avatars.githubusercontent.com/u/174160?v=3","gravatar_id":"","url":"https://api.github.com/users/jarmo","html_url":"https://github.com/jarmo","followers_url":"https://api.github.com/users/jarmo/followers","following_url":"https://api.github.com/users/jarmo/following{/other_user}","gists_url":"https://api.github.com/users/jarmo/gists{/gist_id}","starred_url":"https://api.github.com/users/jarmo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jarmo/subscriptions","organizations_url":"https://api.github.com/users/jarmo/orgs","repos_url":"https://api.github.com/users/jarmo/repos","events_url":"https://api.github.com/users/jarmo/events{/privacy}","received_events_url":"https://api.github.com/users/jarmo/received_events","type":"User","site_admin":false},"created_at":"2016-09-12T16:05:01Z","updated_at":"2016-09-12T16:05:01Z","body":"As I mentioned in my previous reply then global variables are bad when misused (all these examples in your referred link are true). However, when used in places where it actually makes sense, they do not pose any threat. In this example, you have one browser instance, which is a singleton and can be stored in a global variable. No need to worry about namespace conflicts (no-one else should create another global `$browser` variable in your specs because there is no need and it does not make sense), about memory problems (because you still only have one variable which should not be garbage-collected) and so on.\r\n\r\nThe only problem you're referring to is multi-threading issue, but even for that it seems to me that you can use a global variable for storing different instances (unless you're going to use fresh browser instance for each spec). Or, even more likely, you're going to run a new Ruby process for each \"thread\", because Ruby in its essence is single threaded language (due to usage of GIL), which means that you can split up your specs and still re-use different global variables within each process - using instance variables would mean that you need to re-create browsers between each example group.\r\n\r\nDo not be afraid of using global variables - just don't misuse them!"}}
{"actor":{"display_login":"jarmo"}}