如何在多个浏览器上运行rspec测试

时间:2018-11-15 10:06:30

标签: ruby multithreading selenium selenium-webdriver rspec

我想在rspec中同时在Firefox和chrome上运行我的脚本。想知道这可能吗?

当我们在Java中使用testNG时,我们可以使用testng.xml并行运行测试,但是现在我想知道在rspec中有没有类似的实现。

当我尝试使用多个线程时,两个浏览器都启动,但是脚本仅在firefox中执行。

我错过了什么吗?

我的Driver.rb文件看起来像

RSpec.configure do |config|
  config.before(:all) do


    ###############################################################################################################################################
    ####Running in Multiple browsers
    ###############################################################################################################################################
BROWSERS =[ 'chrome','firefox' ]


  threads = []
  BROWSERS.each do |browser_name|
    # send(browser_name, 'abc')
    puts "browser name in each loop ", browser_name
    threads << Thread.new do
      puts "browser name inside thread ", browser_name
      send browser_name
    end

  end
  threads.each {|thread| thread.join}

    def firefox()
      puts "begin firefox"
      path = File.dirname(__FILE__) + "/driver/geckodriver"
      begin
        @driver = Selenium::WebDriver.for :firefox
        puts "Starting tests, initializing " + @driver.inspect()
      rescue Exception=>e
        puts "Start Exception"
        puts e.message
        puts e.backtrace.inspect
        raise e
      end
      @base_url = @test_env['url']
      @driver.manage.window.maximize
      @accept_next_alert = true
      @driver.manage.timeouts.implicit_wait = DEFAULT_WAIT_TIME
    end


    def chrome
      puts "begin chrome"
      path = File.dirname(__FILE__) + "/../driver/chromedriver"
      prefs = {:download => {:prompt_for_download => false, :default_directory => "#{ENV['PATH']}", :url => @base_url} }
      @driver = Selenium::WebDriver.for :chrome, :prefs => prefs
      @base_url = @test_env['url']
      @accept_next_alert = true
    end

    begin
      puts "begin firefox"
      firefox
    rescue Exception=>e
      # Exception already displayed, exiting
      puts "Exception Found: #{e}"
      raise e
      @driver.quit
    end


  config.after(:all) do
    puts "Finishing tests, killing " + @driver.inspect()
    @driver.quit
  end

0 个答案:

没有答案