您的位置:软件测试 > 开源软件测试 > 开源功能测试工具 > Watir
Watir webdriver一些常用的方法
作者:网络转载 发布时间:[ 2014/1/2 16:07:02 ] 推荐标签:Watir 测试

把上面所有的都整理起来,是一个你可以作为参考的良好实例:

require "rubygems"
gem "test-unit"
require "test/unit"
require "watir-webdriver"
 
# check arguments for browser or headless specification
ARGV.each { |arg|
    if arg.downcase.include? 'chrome'
        $browser = 'chrome'
    elsif arg.downcase.include? 'firefox'
        $browser = 'firefox'
    elsif arg.downcase.include? 'ff'
        $browser = 'firefox'
    elsif arg.downcase.include? 'ie'
        $browser = 'ie'
    elsif arg.downcase.include? 'headless'
        $headless = true
    end}
 
module Test
  module Unit
    class TestSuite
      alias :old_run :run
      def run(result, &progress_block)
        old_run(result, &progress_block)
        File.open('errors.log', 'w'){|f|
          result.faults.each{|err|
            case err
              when Test::Unit::Error, Test::Unit::Failure
                f << err.test_name
                f << " "
              #not in log file
              when Test::Unit::Pending, Test::Unit::Notification, Test::Unit::Omission
              end
          }
        }
      end
    end
  end
end
 
class TestExample < Test::Unit::TestCase
  # setup is run before every test
  def setup
    $browser = 'chrome' if $browser.nil?
    $site = 'http://test.localhost' if $site.nil?
 
    if $headless
      require 'headless'
      $headless = Headless.new
      $headless.start
    end
    if $browser == 'chrome'
      $b = Watir::Browser.new :chrome
    elsif $browser == 'firefox'
      $b = Watir::Browser.new :ff
    elsif $browser == 'ie'
      $b = Watir::Browser.new :ie
    end
 
    $timeout_length = 30
 
    load_link($timeout_length){ $b.goto $site }
  end
 
  # teardown is run after every test
  def teardown
    # take screenshot of end of test, useful for failures/errors
    time = Time.new
    $b.driver.save_screenshot(File.dirname(__FILE__) + '/screenshots/' + @method_name + '_' + time.strftime('%Y%m%d_%H%M%S') + '.png');
    $b.close
    if $headless
        $headless.destroy
    end
  end
 
  def browse_to_new_project
    load_link($timeout_length){ $b.goto $site + "/designtourney/projects/new" }
  end
 
  def click_logo_design
    load_link($timeout_length){ $b.link(:class, 'logo-design').click }
  end
 
  def form_fill_first_page
    $b.text_field(:name, 'organization_name').set('Magic/More Magic')
    $b.text_field(:name, 'question_38').set('As mentioned above, we make magic and more magic.')
    $b.text_field(:name, 'question_39').set('People who like magic and more magic, as opposed to less magic.')
    $b.link(:id=> 'show-more').click
    $b.text_field(:name, 'question_41').set('Im putting stuff into question 41')
    $b.text_field(:name, 'question_45').set('Im putting stuff into question 45')
  end
 
  def first_page_asserts type = 'regular'
    assert_equal 'Magic/More Magic', $b.text_field(:name, 'organization_name').value
    assert_equal 'As mentioned above, we make magic and more magic.', $b.text_field(:name, 'question_38').value
    assert_equal 'People who like magic and more magic, as opposed to less magic.', $b.text_field(:name,'question_39').value
    assert_equal 'Im putting stuff into question 41', $b.text_field(:name,'question_41').value
  end
 
  def wait_for_ajax
    $b.div(:id, 'ajax-loader').wait_while_present
  end
 
  def load_link(waittime)
    begin
      Timeout::timeout(waittime)  do
      yield
    end
    rescue Timeout::Error => e
      puts "Page load timed out: #{e}"
      retry
    end
  end
 
  def test_save_for_later
    browse_to_new_project
 
    click_logo_design
 
    form_fill_first_page
    $b.link(:class, 'save').click
    wait_for_ajax
 
    assert_true $b.div(:id, 'fallr').visible?
 
    browse_to_new_project
 
    $b.div(:id, 'fallr').wait_until_present
    $b.wait_until{ $b.execute_script('return $('#fallr-wrapper').is(':animated')') == false }
    sleep 0.5
    $b.link(:id, 'fallr-button-yes').click
    $b.div(:id, 'fallr-overlay').wait_while_present
 
    # These assertions make sure the stuff for the first page is still all there
    first_page_asserts
  end
end

上一页1234下一页
软件测试工具 | 联系我们 | 投诉建议 | 诚聘英才 | 申请使用列表 | 网站地图
沪ICP备07036474 2003-2017 版权所有 上海泽众软件科技有限公司 Shanghai ZeZhong Software Co.,Ltd