繁体   English   中英

Watir / Rspec浏览器循环

[英]Watir/Rspec Browser Loop

我需要一些使用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
  • 去谷歌
  • 加载Chrome
  • 去谷歌
  • 搜索Chrome

似乎当我包含Rspec describe循环不起作用,因为我有意。

终于想通了:)

以下是任何想要进行多次浏览器测试的人的代码,而不为每个浏览器制作不同的规范。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM