【问题标题】:Watir/Rspec Browser LoopWatir/Rspec 浏览器循环
【发布时间】:2014-11-06 01:45:49
【问题描述】:

我需要一些帮助,在 Watir/Rspec 的循环中使用多个浏览器。

  • 我的目标是:
    1. 转到 Google.ca
    2. 快速搜索内容
    3. 关闭浏览器。
    4. 使用不同的浏览器循环步骤 1-3。

我可以使用 Watir 让它工作,但不知道如何让它与 Rspec 一起工作。

Watir(工作代码):

require 'watir-webdriver'
require 'rspec'

  browsers = [:ff, :chrome]
  browsers.map do |x|
  $browser = Watir::Browser.new x
  $browser.goto('http://www.google.ca')
  $browser.text_field(:id, 'gbqfq').set 'Juventus'
  $browser.send_keys :enter
  $browser.close


end #End loop

Rspec(不工作):

require 'watir-webdriver'
require 'rspec'

  browsers = [:ff, :chrome]
  browsers.map do |x|
  $browser = Watir::Browser.new x
  $browser.goto('http://www.google.ca')

  describe 'loop' do
    it 'does something' do
      $browser.text_field(:id, 'gbqfq').set 'Juventus'
      $browser.send_keys :enter
      $browser.close
    end
  end #End describe
end #End loop

上面的代码就是这样:

  • 加载 Firefox
  • 转到 Google
  • 加载 Chrome
  • 转到 Google
  • Chrome 上的搜索

似乎当我包含 Rspec describe 时,循环无法按我的预期工作。

【问题讨论】:

    标签: ruby rspec watir watir-webdriver


    【解决方案1】:

    终于明白了:)

    这里是任何想要在不为每个浏览器制定不同规范的情况下进行多个浏览器测试的人的代码。

    require 'watir-webdriver'
    require 'rspec'
    
          browsers = [:ff, :chrome]
          browsers.map do |x|
    
            describe 'Browser' do
    
            before(:all) do
              @browser = Watir::Browser.new x
            end
    
            it 'goes to Google.ca' do
              @browser.goto('http://www.google.ca')
            end
    
            it 'searches' do
              @browser.text_field(:id, 'gbqfq').when_present(3).set 'Juventus'
              @browser.send_keys :enter
              sleep 0.5 #roughly takes 0.5s for the images to load. 
            end
    
            it 'closes browser' do
              @browser.close
            end
          end #end describe
        end #end loop
    

    我认为要使其正常工作,您需要在 describe 之后初始化浏览器,而在我在 describe 之前初始化浏览器之前

    【讨论】:

      猜你喜欢
      • 2011-12-08
      • 1970-01-01
      • 2012-11-23
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      • 2018-02-23
      相关资源
      最近更新 更多