Quick Start
create a testing script
例 4.1. watir firewatir example
$ cat google.rb require 'rubygems' require 'watir' require 'firewatir' browser = Watir::Browser.new browser.goto 'http://google.com' browser.text_field(:name, 'q').set 'Ruby' browser.button(:name, 'btnG').click if browser.text.include? 'Ruby Programming Language' puts 'Text was found' else puts 'Text was not found' end
run test
$ irb google.rb google.rb(main):001:0> require 'rubygems' => true google.rb(main):002:0> require 'watir' => true google.rb(main):003:0> require 'firewatir' => true google.rb(main):004:0> google.rb(main):005:0* browser = Watir::Browser.new => #<FireWatir::Firefox:0x7f797dd2b880 url="http://www.google.com.hk/search?hl=zh-CN&source=hp&biw=1440&bih=660&q=Ruby&btnG=Google+%E6%90%9C%E7%B4%A2&aq=f&aqi=g10&aql=&oq=" title="Ruby - Google \346\220\234\347\264\242"> google.rb(main):006:0> browser.goto 'http://google.com' => #<FireWatir::Firefox:0x7f797dd2b880 url="http://www.google.com.hk/" title="Google"> google.rb(main):007:0> browser.text_field(:name, 'q').set 'Ruby' => 0 google.rb(main):008:0> browser.button(:name, 'btnG').click => 0 google.rb(main):009:0> if browser.text.include? 'Ruby Programming Language' google.rb(main):010:1> puts 'Text was found' google.rb(main):011:1> else google.rb(main):012:1* puts 'Text was not found' google.rb(main):013:1> end Text was found => nil
例 4.2. watir-webdriver example
require 'watir-webdriver' browser = Watir::Browser.new browser.goto 'http://www.google.com.hk' browser.text_field(:name => 'q').set 'netkiller' browser.button(:name => 'btnK').click if browser.text.include? 'Neo' puts 'Text was found' else puts 'Text was not found' end browser.close()
例 4.3. watir-webdriver remote
require 'watir-webdriver' browser = Watir::Browser.new :remote, :url => "http://192.168.2.44:4444/wd/hub", :desired_capabilities => Selenium::WebDriver::Remote::Capabilities.chrome browser.goto 'http://www.google.com.hk' browser.text_field(:name => 'q').set 'netkiller' browser.button(:name => 'btnK').click if browser.text.include? 'Neo' puts 'Text was found' else puts 'Text was not found' end browser.close()