2013-03-03 129 views
6

我寫了幾個類來管理我想要如何處理多個網站,並使用類似的方法(即登錄,刷新)。每個類都打開自己的WATIR瀏覽器實例。Watir的紅寶石線程

class Site1 
    def initialize 
     @ie = Watir::Browser.new 
    end 
    def login 
     @ie.goto "www.blah.com" 
    end 
end 

的代碼在主樣本有沒有線程是如下

require 'watir' 
require_relative 'site1' 

agents = [] 
agents << Site1.new 

agents.each{ |agent| 
    agent.login 
} 

這工作得很好,但直到當前已完成登錄犯規移動到下一個代理。我想整合多線程來處理這個問題,但似乎無法讓它工作。

require 'watir' 
require_relative 'site1' 

agents = []; threads = [] 
agents << Site1.new 


agents.each{ |agent| 
    threads << Thread.new(agent){ agent.login } 
} 

threads.each { |t| t.join } 

這給了我錯誤:未知屬性或方法:navigate。 HRESULT錯誤代碼:0x8001010e。該應用程序稱爲一個接口,被編組爲另一個線程。

有誰知道如何解決這個問題,或者如何實現類似的功能?

+1

它似乎是watir-classic或其中一個庫使用的錯誤。在Firefox中使用watir-wedriver時,問題不會發生。 – 2013-03-04 17:25:24

+0

感謝賈斯汀,它看起來像你是對的。 watir-webdriver運行良好,所以我猜它只是一個watir-classic的bug。 – cubesnyc 2013-03-04 21:37:44

回答

0

對此不太確定,但這裏是使用線程的擺動。

require 'thread' 
    threads = []    # Setting an array to store threaded commands 
    c_thread = Thread.new do # Start a new thread 
    login      # Call our command in the thread 
    end 
    threads << c_thread