Watir-WebDriver: A detailed introduction

Update: 22 August 2011: Please see: watirwebdriver.com for a detailed guide to Watir-WebDriver

Update: 22 July 2011: I have updated quite a number of things that have changed since the earlier releases

Watir-WebDriver is a really great tool; Jari Bakken‘s done a really good job of it. There’s not a huge amount on the web about it, how to get it up and running and use it. I’m aiming to fix that here.

For those who don’t know what Watir-WebDriver is, it’s basically a nice Watir (ruby) implementation on WebDriver, so it gives you four browsers (three real, one headless) using one neat API, out of the box. The thing I like about it is you don’t need to use JRuby (like Celerity), which means it plays nice with Cucumber (although Cucumber does work under JRuby).

I’ve written about how Watir, WebDriver and Selenium all fit together before, so this post aims to be a lot more hands-on.

Getting Watir-WebDriver Running

There are essentially two components you need: the Watir-WebDriver ruby gem, and the remote WebDriver server. The remote WebDriver server is only needed if you want to run your tests in headless mode without a real browser (or want to use Opera). You obviously need ruby first but I won’t detail that here. You can find info about ruby versions etc. at watir.com. If you’re on Mac or Linux, I strongly suggest using RVM and bundler.

The Watir-WebDriver ruby gem

It’s a simple matter of opening a command prompt and typing:

  • gem install watir-webdriver (windows); or
  • sudo gem install watir-webdriver (osx or linux – better to use RVM and bundler)

The remote WebDriver Server

This is the slightly tricky part. This is so that WebDriver can run headless without a real browser, and isn’t needed for real browser support (bar Opera). The quickest easiest way to get up and running is to download this java jar file, open a command prompt where you have saved it, and run:

java -jar selenium-server-standalone-2.0b1.jar

You can also specify the startup and max memory allocated, which is handy when running large test suites.

java -Xms1024M -Xmx2048M -jar selenium-server-standalone-2.0a7.jar

You need to have this java server running whenever using WebDriver, but it’s easy enough to bootstrap it.

Update: Jari has pointed out you can run the server programmatically which is even better:

require 'selenium/server'
server = Selenium::Server.new("/path/to/jar", :background => true)
server.start
# run your tests
server.stop

First Impressions

Waiting in Watir-WebDriver doesn’t seem as straightforward as with Watir, probably due to the underlying drivers, but fortunately there’s an inbuilt waiting library to use (also available in Watir 1.6.7+):

  • Watir::Wait.until { ... }: where you can wait for a block to be true
  • object.when_present.set: where you can do something when it’s present
  • object.wait_until_present:; where you just wait until something is present
  • object.wait_while_present:; where you just wait until something disappears

Hello Watir-WebDriver

It seems pertinent to start with a google search example.

Hello Watir-WebDriver in three browsers

These three browsers seem to work very similarly, but obviously Internet Explorer will only run on Microsoft Windows.

require 'rubygems'
require 'watir-webdriver'
b = Watir::Browser.new :chrome
b.goto 'www.google.com'
b.text_field(:name => 'q').set 'Watir-WebDriver'
b.button(:name => 'btnG').click
b.div(:id => 'resultStats').wait_until_present
puts "Displaying page: '#{b.title}' with results: '#{b.div(:id => "resultStats").text}'"
b.close

The only difference for Firefox:

b = Watir::Browser.new :firefox

The only difference for IE:

b = Watir::Browser.new :ie

Hello Watir-WebDriver in Headless (HTML Unit)

I imagine this is the most anticipated feature for Watir users, as it means your tests run much faster, and you can still use ruby (unlike Celerity which uses JRuby). The script is fairly straightforward, you simply specify the hostname for your WebDriver server you started above. You need to specifically enable JavaScript by creating a capabilities profile.

require 'rubygems'
require 'watir-webdriver'
include Selenium
capabilities = WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => true)
b = Watir::Browser.new(:remote, :url => 'http://127.0.0.1:4444/wd/hub', :desired_capabilities => capabilities)
b = Watir::Browser.new :firefox
b.goto "www.google.com"
b.text_field(:name => "q").set "Watir-WebDriver"
b.button(:name => "btnG").click
b.div(:id => "resultStats").wait_until_present
puts "Displaying page: '#{b.title}' with results: '#{b.div(:id => "resultStats").text}'"
b.close

Sample Timings

As an experiment, I looped the Google Search script above 100 times to measure and compare the execution times.

  • Internet Explorer 8 on Windows 7: 400 seconds
  • Firefox 3.6 on Mac OSX: 277 seconds
  • Headless HTMLUnit on Mac OSX: 269 seconds

Very surprisingly, the headless run wasn’t much quicker at all. This may be due to running the test on Google over the Internet and not a local application.

Caveat Emptor

There are a number of differences between Watir-WebDriver and Watir. The main ones that are important to me are:

Zero based indexing as opposed to 1 based

For example,

In Watir/FireWatir: this finds the first table on a page:

puts b.table(:index => 1).text

But in Watir-WebDriver, it finds the second table on a page.

Attaching to windows
Attaching to new windows (for example pop-ups) has been fairly straightforward in Watir with its attach method, which is missing in Watir-WebDriver. It looks like you can iterate through a collection of windows in Watir-WebDriver using browser.windows, but I haven’t tried this out yet.

Summary

Watir-WebDriver is a very solid testing tool that uses a great browser automation engine (WebDriver) with a clean ruby API (Watir). I believe it will be a popular choice amongst testing teams, particually those using it for ATDD through Cucumber and running it as headless on a continuous integration server.

Further Reading

45 thoughts on “Watir-WebDriver: A detailed introduction

  1. Nice post, although some things i’d point/question:
    1) Your post is too strongly emphasizing on the fact of remote webdriver capability and presents it like the Java Remote server is needed even if you’re using a regular browser – it’s not. At least this impression could be made to novice users.
    2) What do you mean exactly by the difference of waiting between Watir-WebDriver and Watir? Any concrete examples you could point out? These Watir::Wait methods are also available in Watir starting from 1.6.7 by the way.
    3) You should also mention somewhere that Watir-WebDriver is a work-in-progress and might have some problems-bugs, especially at the Internet Explorer side.

    By the way, the “snow effect” is nice, but not if it’s happening on the white background. Then it is “flickering” the black text.

    • Thanks for your feedback Jarmo.
      1) Post updated to emphasize this is only needed for headless browser.
      2) Post updated to show wait methods are in Watir 1.6.7 onwards. Every time I run my example code in Firefox WebDriver on Mac, it fails, whereas it passes in Watir/FireWatir.

      require "rubygems"
      require "watir-webdriver"
      b = Watir::Browser.new :firefox
      b.goto "www.google.com"
      b.text_field(:name => "q").set "Watir-WebDriver"
      b.button(:name => "btnG").click
      puts "Displaying page: '#{b.title}' with results: '#{b.div(:id => "resultStats").text}'"
      b.close
      

      Error:

      /Library/Ruby/Gems/1.8/gems/watir-webdriver-0.1.7/lib/watir-webdriver/elements/element.rb:218:in `assert_exists': unable to locate element, using {:id=>"resultStats", :tag_name=>"div"} (Watir::Exception::UnknownObjectException)
      from /Library/Ruby/Gems/1.8/gems/watir-webdriver-0.1.7/lib/watir-webdriver/elements/element.rb:57:in `text'

      3. Updated first line of post to reflect work in progress. My view is that everything is a work in progress and at some point you need to be able to tell people to start using it.
      4. Thanks for the feedback on the snow, I have disabled it.

  2. I was very hopeful that this would be the solution I was looking for, but my code…

    browser.text.include?(“string”)

    …that works fine with Watir, fails with an “undefined method” error using Watir-webdriver.

    That’s disappointing.

      • Looks like text.include?(“string”) this works in firefox and htmlunit, but not for chrome browser.

        Any idea ?

        • What version of Chrome are you using?
          I think it’s a bug in Chrome 14, try updating to the latest ChromeDriver and/or downgrading Chrome.

  3. Slight correction:

    The line that causes the error looks like this:

    browser.text_field(:name, “j_username”).include?(“string”)

  4. Abe,

    I think your example will not work on Watir either. The text_field method returns a TextField object. The include? method is on string. If you change the code to:

    browser.text_field(:name, “j_username”).value.include?(“string”)

    it should work on both gems. The value method returns a string with the contents of the TextField. You can safely call include? on that string.

  5. Cheezy,

    Thanks for the tip! And now that you mention it I recall having problems with this code a while back, but not having the time then to really delve into why it wasn’t always working. Awesome!

    I’ll try that out as soon as I get to work. It will be fantastic if I can switch over to watir-webdriver permanently!

  6. Well, my triumph was short-lived. With that road block removed I proceeded forward about 2 feet before hitting the next one.

    Unfortunately I’m too much of a n00b programmer to know at this point whether the problem is with my code, my system environment, or watir-webdriver. The error messages I’m getting are very obscure (to me)

    For now I’ll stick to Watir on Firefox, I guess.

  7. HI!
    Breaking brain 3rd day, trying to download file( a .zip archive) by url.
    Please help!!
    How can I do this by htmlunit:
    server = Selenium::Server.new(“merlion/selenium-server-standalone-2.0b2.jar”, :background => true)
    server.start
    capabilities = Selenium::WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => true)
    browser = Watir::Browser.new(:remote, :url => “http://127.0.0.1:4444/wd/hub”, :desired_capabilities\
    => capabilities)

    catalog_url = “https://iz.merlion.ru/?action=Y28A38CB&action1=Y7650503&type=xml&noh=1″
    browser.goto(catalog_url)

    So, how can I save the archive in specified path?
    Much thanks!

  8. I’m trying out the watir-webdriver sample script and running into an issue with missing libs – watir-webdriver/extensions/wait:

    C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': no such file to load — watir-webdriver/extensions/wait (LoadError)
    from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
    from watirWebDriver_01.rb:3:in `’

    How does watir-webdriver/extensions/wait get installed? Is it a gem?
    Thanks in advance,
    Liz

  9. I’m using 1.87. With what version of Ruby was it added?

    C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.4/lib/watir-webdriver/elements/element.rb:292:in `method_missing': undefined method `wait_util_present’ for # (NoMethodError)
    from watirWebDriver_01.rb:10:in `’

    • It looks like you have a typo in your code: it should be “wait_until_present” not “wait_util_present”

  10. watir-webdriver is working with ie and chrome but failing with firefox. The script I’m using is the example listed above:
    require “rubygems”
    require “watir-webdriver”
    #require “watir-webdriver/extensions/wait”

    #b = Watir::Browser.new :chrome
    #b = Watir::Browser.new :ie
    b = Watir::Browser.new :firefox
    b.goto “www.google.com”
    b.text_field(:name => “q”).set “We the People”
    b.button(:name => “btnG”).click
    b.div(:id => “resultStats”).wait_until_present
    puts “Displaying page: ‘#{b.title}’ with results: ‘#{b.div(:id => “resultStats”).text}'”
    b.close

    When I attempt to run the script with :ie selected as the browser I get the following error message. I’m using Firefox 5.0/Ruby 1.9.2 on Windows XP. Am I missing something?

    C:/Ruby192/lib/ruby/1.9.1/win32/registry.rb:172:in `find': unknown encoding name – CP0 (ArgumentError)
    from C:/Ruby192/lib/ruby/1.9.1/win32/registry.rb:172:in `initialize’
    from C:/Ruby192/lib/ruby/1.9.1/win32/registry.rb:231:in `exception’
    from C:/Ruby192/lib/ruby/1.9.1/win32/registry.rb:231:in `raise’
    from C:/Ruby192/lib/ruby/1.9.1/win32/registry.rb:231:in `check’
    from C:/Ruby192/lib/ruby/1.9.1/win32/registry.rb:277:in `EnumKey’
    from C:/Ruby192/lib/ruby/1.9.1/win32/registry.rb:551:in `each_key’
    from C:/Ruby192/lib/ruby/1.9.1/win32/registry.rb:566:in `keys’
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-0.2.2/lib/selenium/webdriver/firefox/binary.rb:140:in `block in windows_registry_path’
    from C:/Ruby192/lib/ruby/1.9.1/win32/registry.rb:389:in `open’
    from C:/Ruby192/lib/ruby/1.9.1/win32/registry.rb:496:in `open’
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-0.2.2/lib/selenium/webdriver/firefox/binary.rb:139:in `windows_registry_path’
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-0.2.2/lib/selenium/webdriver/firefox/binary.rb:125:in `windows_path’
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-0.2.2/lib/selenium/webdriver/firefox/binary.rb:106:in `path’
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-0.2.2/lib/selenium/webdriver/firefox/binary.rb:49:in `execute’
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-0.2.2/lib/selenium/webdriver/firefox/binary.rb:30:in `start_with’
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-0.2.2/lib/selenium/webdriver/firefox/launcher.rb:68:in `start_silent_and_wait’
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-0.2.2/lib/selenium/webdriver/firefox/launcher.rb:35:in `block in launch’
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-0.2.2/lib/selenium/webdriver/firefox/socket_lock.rb:20:in `locked’
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-0.2.2/lib/selenium/webdriver/firefox/launcher.rb:32:in `launch’
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-0.2.2/lib/selenium/webdriver/firefox/bridge.rb:19:in `initialize’
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-0.2.2/lib/selenium/webdriver/common/driver.rb:27:in `new’
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-0.2.2/lib/selenium/webdriver/common/driver.rb:27:in `for’
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-0.2.2/lib/selenium/webdriver.rb:74:in `for’
    from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.2.5/lib/watir-webdriver/browser.rb:35:in `initialize’
    from ruby192_test05.rb:7:in `new’
    from ruby192_test05.rb:7:in `’

    Has anyone else encountered this issue? Found a solution?
    Thx,
    Liz

    • You need the latest watir-webdriver gem to use firefox 5.

      gem update watir-webdriver

      Or

      bundle update (if you use bundler)

  11. Hi,

    I try to enabled the javascript when using htmlunit for the testing.
    capabilities = WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => true)

    But get the following error:

    NameError: uninitialized constant General::WebDriver

    Any idea about this ?

    Thanks!

  12. Hello!

    Can’t seem to get ‘Hello Watir-WebDriver’ example working. Simply cut and pasted the example and changed ‘chrome’ to ‘ie’. Googling has been unfruitful. The message I receive is: C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load — watir-webdriver/e
    xtensions/wait (LoadError)
    from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require’
    from hello.rb:3

    My environment is:
    OS: Windows 7 Professional
    Browser: IE9
    ruby 1.8.7 (2011-02-18 patchlevel 334) [i386-mingw32]
    Gems:
    *** LOCAL GEMS ***

    abstract (1.0.0)
    actionmailer (3.0.9)
    actionpack (3.0.9)
    activemodel (3.0.9)
    activerecord (3.0.9)
    activeresource (3.0.9)
    activesupport (3.0.9)
    arel (2.0.10)
    builder (3.0.0, 2.1.2)
    bundler (1.0.15)
    childprocess (0.2.0)
    commonwatir (1.9.2)
    cucumber (1.0.2)
    database_cleaner (0.6.7)
    diff-lcs (1.1.2)
    erubis (2.6.6)
    factory_girl (1.3.3)
    ffi (1.0.9 x86-mingw32)
    firewatir (1.9.2)
    gherkin (2.4.5 x86-mingw32)
    hoe (2.10.0)
    i18n (0.5.0)
    json (1.5.3)
    json_pure (1.5.3)
    mail (2.2.19)
    mime-types (1.16)
    nokogiri (1.5.0 x86-mingw32)
    pickle (0.4.8)
    polyglot (0.3.1)
    rack (1.2.3)
    rack-mount (0.6.14)
    rack-test (0.5.7)
    rails (3.0.9)
    railties (3.0.9)
    rake (0.9.2)
    rautomation (0.6.2)
    rdiscount (1.6.8)
    rdoc (3.8)
    rspec (2.6.0)
    rspec-core (2.6.4)
    rspec-expectations (2.6.0)
    rspec-mocks (2.6.0)
    rubygems-update (1.8.5)
    rubyzip (0.9.4)
    s4t-utils (1.0.4)
    selenium-client (1.2.18)
    selenium-webdriver (2.1.0)
    term-ansicolor (1.0.5)
    thor (0.14.6)
    treetop (1.4.9)
    tzinfo (0.3.29)
    user-choices (1.1.6.1)
    watir (1.9.2)
    watir-webdriver (0.2.6)
    win32-api (1.4.8 x86-mingw32)
    win32-process (0.6.5)
    windows-api (0.4.0)
    windows-pr (1.2.0)
    xml-simple (1.1.0)

    • Fails with the same error for :firefox too.

      I’m running Firefox 5.0, everything else is the same as above.

      • Strangely, this:
        require ‘rubygems’
        require ‘watir’
        include Watir
        b = IE.new
        b.goto(‘www.google.com’)
        b.text_field(:name => “q”).set “Watir-WebDriver”
        b.button(:name => “btnG”).click
        b.div(:id => “resultStats”).wait_until_present
        puts “Displaying page: ‘#{b.title}’ with results: ‘#{b.div(:id => “resultStats”).text}'”
        b.close

        Works fine! Output:
        U:\test>ruby hello2.rb
        Displaying page: ‘watir-webdriver – Google Search’ with results: ‘About 69,800 results (0.05 seconds)’

        Any ideas? Much appreciated. Thanks!

      • Thanks for the quick reply, but, no dice. :( Not sure what I’m doing wrong. Environment is the same as previously stated, removed line you suggested.

        require ‘rubygems’
        require ‘watir-webdriver’
        b = Watir::Browser.new :ie
        b.goto ‘www.google.com’
        b.text_field(:name => ‘q’).set ‘Watir-WebDriver’
        b.button(:name => ‘btnG’).click
        b.div(:id => ‘resultStats’).wait_until_present
        puts “Displaying page: ‘#{b.title}’ with results: ‘#{b.div(:id => “resultStats”).text}'”
        b.close

        C:/Ruby187/lib/ruby/1.8/net/protocol.rb:135:in `sysread': An existing connection was forcibly closed by the remote host. (Errno::ECO
        NNRESET)
        from C:/Ruby187/lib/ruby/1.8/net/protocol.rb:135:in `rbuf_fill’
        from C:/Ruby187/lib/ruby/1.8/timeout.rb:67:in `timeout’
        from C:/Ruby187/lib/ruby/1.8/timeout.rb:101:in `timeout’
        from C:/Ruby187/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill’
        from C:/Ruby187/lib/ruby/1.8/net/protocol.rb:116:in `readuntil’
        from C:/Ruby187/lib/ruby/1.8/net/protocol.rb:126:in `readline’
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:2028:in `read_status_line’
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:2017:in `read_new’
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:1051:in `request’
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:1037:in `request’
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:543:in `start’
        from C:/Ruby187/lib/ruby/1.8/net/http.rb:1035:in `request’
        from C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.1.0/lib/selenium/webdriver/remote/http/default.rb:73:in `respons
        e_for’
        from C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.1.0/lib/selenium/webdriver/remote/http/default.rb:41:in `request

        from C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.1.0/lib/selenium/webdriver/remote/http/common.rb:34:in `call’
        from C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.1.0/lib/selenium/webdriver/remote/bridge.rb:472:in `raw_execute’

        from C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.1.0/lib/selenium/webdriver/remote/bridge.rb:88:in `create_sessio
        n’
        from C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.1.0/lib/selenium/webdriver/remote/bridge.rb:68:in `initialize’
        from C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.1.0/lib/selenium/webdriver/ie/bridge.rb:39:in `initialize’
        from C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.1.0/lib/selenium/webdriver/common/driver.rb:31:in `new’
        from C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.1.0/lib/selenium/webdriver/common/driver.rb:31:in `for’
        from C:/Ruby187/lib/ruby/gems/1.8/gems/selenium-webdriver-2.1.0/lib/selenium/webdriver.rb:76:in `for’
        from C:/Ruby187/lib/ruby/gems/1.8/gems/watir-webdriver-0.2.6/lib/watir-webdriver/browser.rb:35:in `initialize’
        from hello.rb:3:in `new’
        from hello.rb:3

  13. copied the headless testing example as is and getting this error:
    timed out after 30 seconds, waiting for {:tag_name=>”div”, :id=>”resultStats”} to become present (Watir::Wait::TimeoutError)

  14. Alister,
    Is there a dev forum or google group for watir-webdriver? I’ve seen several issues with IE9 and now Firefox and feel like stack overflow isn’t the right place for those questions, which may often spawn lengthy discussions.

  15. is there some examples on how to interacting with frames using watir-webdriver? can’t find anything along the line of show_the_frames on a page.

    Thanks.

    • You should be able to do something like this:

      b.frames # a collection of all frames on a page
      b.frame(:id => "content_ifr").send_keys "hello world"
      
  16. Hi
    Do you have a code example on how to show all the frames on a page and also selecting a frame? Am using firefox with watir-webdriver 2.6.

    Thanks.

  17. for what it’s worth, here are a few other things I’ve had to change (some of which are noted above). It would be great if we could collect these into a Wiki page…

    removed:
    Watir::Element#click_no_wait
    Watir::IE.speed=
    Watir::IE.enabled_popup

    changed:
    Watir::Element#to_s (now shows objectId instead of nicely formatted info about the element, probably should use .text instead)
    Exception message text (for example, ‘selectPartner is disabled’ has become /object is disabled.*selectPartner/)
    send_keys special chars similar to :enter instead of ‘{ENTER}’ (some browsers seem to prefer :return)

    added:
    Watir::Element#double_click (FileFields seem to require double-click in IE9)
    Selenium::WebDriver::Error::UnhandledError (may have to account for this when manipulating Elements)

  18. Hello i liked your site, but i am trying to do som automate testing, my code says:
    require ‘watir’
    b = Watir::Browser.new
    b.goto ‘www.google.com’
    b.text_field(:name => ‘q’).set ‘Watir-WebDriver’
    b.button(:name => ‘btnG’).click
    b.div(:id => ‘resultStats’).wait_until_present
    puts “Displaying page: ‘#{b.title}’ with results: ‘#{b.div(:id => “resultStats”).text}'”
    b.close

    ***Runs Ok
    ***When I want to specify the browser with this code, send me a lot of errors:

    require ‘watir-webdriver’
    b = Watir::Browser.new :firefox
    b.goto ‘www.google.com’
    b.text_field(:name => ‘q’).set ‘Watir-WebDriver’
    b.button(:name => ‘btnG’).click
    b.div(:id => ‘resultStats’).wait_until_present
    puts “Displaying page: ‘#{b.title}’ with results: ‘#{b.div(:id => “resultStats”).text}'”
    b.close
    *** I receive this errors
    cannot load such file — ffi_c (LoadError)
    C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’
    C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require’

    ***when i add the Selenium require:
    require ‘watir-webdriver’
    require ‘Selenium’

    b = Watir::Browser.new :firefox

    b.goto ‘www.google.com’
    b.text_field(:name => ‘q’).set ‘Watir-WebDriver’
    b.button(:name => ‘btnG’).click
    b.div(:id => ‘resultStats’).wait_until_present
    puts “Displaying page: ‘#{b.title}’ with results: ‘#{b.div(:id => “resultStats”).text}'”
    b.close

    ***and got this errors:
    C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:55:in `require': iconv will
    be deprecated in the future, use String#encode instead.
    cannot load such file — Selenium (LoadError)
    C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:55:in `require’
    C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb:55:in `require’
    D:/New folder/Projects/MBAM-TC/features/First_Visit.rb:4:in `’
    C:/Ruby193/lib/ruby/gems/1.9.1/gems/cucumber-1.1.3/lib/cucumber/rb_support/rb_la
    nguage.rb:129:in `load’

    *** i have install all the gems, and i am working on windows 7, with ruby v 1.9.3p0

  19. Pingback: Cucumber and Watir - Force Process

Comments are closed.