Search code examples
rubytestingwatirwatir-webdriverirb

Ruby Script that runs list of irb watir commands


I regularly use the irb for testing out cucumber watir webdriver steps on a browser. This involves opening the irb and typing in a list of the same commands. The first few commands are always

  1. require 'watir-webdriver'
  2. browser = Watir::Browser.new(:chrome)
  3. b.goto 'www.foo.com'

the list of commands goes on like this. Every time I open the irb I have to go through all of these. Does anyone have any idea of a script which could be run to automate a list of commands such as the above? So that it would open the IRB and then go through the commands one by one saving me load of time.

Any help is greatly appreciated.


Solution

  • Probably the easiest thing to do is create a script.rb file in the root project directory (or wherever you're starting IRB), and write out the Ruby you normally run. Then when you start IRB, just load 'script.rb'

    However this won't work if you need access to your browser variable after setup. In that case it would make more sense to create a class.

    in ./test_browser.rb

    require 'watir-webdriver'
    require 'irb'
    
    class TestBrowser < Watir::Browser
      def self.build
        browser = new(:chrome)
        browser.goto 'www.foo.com'
        # other setup steps
        browser # return the browser at the end of the method
      end
    end
    
    IRB.start
    

    Then from the shell: $ ruby ./test_browser.rb

    This will start up IRB with your class loaded. Then all you need to do is call the .build method and store the browser in a variable

    browser = TestBrowser.build