Ruby Watir: cannot launch browser in a thread in Linux -


i'm trying run code in red hat linux, , won't launch browser. way can work if launch browser outside of thread, makes no sense me. here mean:

require 'watir-webdriver' $alphabet = ["a", "b", "c"] $alphabet.each |z|     puts "pshaw"     thread.new{         thread.current["testputs"] =  "ohai " + z.to_s         thread.current["mybrowser"] = watir::browser.new :ff         puts thread.current["testputs"]     }      $browser = watir::browser.new :ff end 

the output is:

pshaw (launches browser) ohai (launches browser) pshaw (launches browser) ohai b (launches browser) pshaw (launches browser) ohai c (launches browser) 

however, if remove browser launch outside of thread, so:

require 'watir-webdriver' $alphabet = ["a", "b", "c"]      $alphabet.each |z| puts "pshaw" thread.new{     thread.current["testputs"] =  "ohai " + z.to_s     thread.current["mybrowser"] = watir::browser.new :ff     puts thread.current["testputs"]     } end 

the output is:

pshaw pshaw pshaw 

what going on here? how fix can launch browser inside thread?

edit add:

the solution justin ko provided worked on psedocode above, it's not helping actual code:

require 'watir-webdriver' require_relative 'credentials' require_relative 'reportgenerator' require_relative 'installpagelayouts' require_relative 'packagehandler' dir[(dir.pwd.to_s + "/bmx*")].each {|file| require_relative file } #this includes files in directory names starting bmx   module runner def self.runtestcases(orgtype, *casenumbers)     $testcasearray = array.new      casenumbers.each |thiscasenum|         $testcasearray << thiscasenum     end      $alltestcaseresults = array.new     $alphabet = ["a", "b", "c"]     @count = 0     @multiorg = 0     @peorg = 0     @eeorg = 0     @threads = array.new     $testcasearray.each |thiscase|         $alphabet[@count] = thread.new {                 puts "working one"             thread.current["tbrowser"] = watir::browser.new :ff             puts "working two"             if ((thiscase.declareorg().downcase == "multicurrency") || (thiscase.declareorg().downcase == "mc"))                 currentorg = $multicurrencyorgarray[@multiorg]                 @multiorg += 1             elsif ((thiscase.declareorg().downcase == "enterprise") || (thiscase.declareorg().downcase == "ee"))                 currentorg = $eeorgarray[@eeorg]                 @eeorg += 1                      else #default single currency pe                 currentorg = $peorgarray[@peorg]                 @peorg += 1             end             setuporg(currentorg, thiscase.testcaseid, currentorg.layoutdirectory)             runningtest = thiscase.actualtest()             if runningtest.crashed != "crashed" #changed read attr_reader isntead of deleted casestatus method testcase.rb                 cleanuporg(thiscase.testcaseid, currentorg.layoutdirectory)             end                         @threads << thread.current         }         @count += 1          end       @threads.each |thisthread|                    thisthread.join     end     writereport($alltestcaseresults) end    def self.setuporg(thisorg, caseid, layoutpath)     begin         thisorg.login         pkg = packagehandler.new         basicinstalled = "false"         counter = 0         until ((basicinstalled == "true") || (counter == 5))             pkg.basicinstaller()             if thread.current["tbrowser"].text.include? "you have attempted access page"                 thisorg.login             else                     basicinstalled = "true"             end             counter +=1         end         if !((caseid.include? "bmxb") || (caseid.include? "bmxb"))             moduleinstalled = "false"             counter2 = 0             until ((moduleinstalled == "true") || (counter == 5))                 pkg.packageinstaller(caseid)                 if thread.current["tbrowser"].text.include? "you have attempted access page"                     thisorg.login                 else                         moduleinstalled = "true"                 end                 counter2 +=1             end          end         installpagelayouts(layoutpath)     rescue         $alltestcaseresults << testcaseresult.new(caseid, caseid, 1, "setup failed!" + "<p>#{$!}</p><p>#{$@}</p>").hashemup         writereport($alltestcaseresults)     end end  def self.cleanuporg(caseid, layoutpath)     begin         uninstallpagelayouts(layoutpath)         pkg = packagehandler.new         pkg.packageuninstaller(caseid)         thread.current["tbrowser"].close     rescue         $alltestcaseresults << testcaseresult.new(caseid, caseid, 1, "cleanup failed!" + "<p>#{$!}</p><p>#{$@}</p>").hashemup         writereport($alltestcaseresults)     end end    end 

the output it's generating is:

working 1 working 1 working 1 

it's not opening browser or doing of subsequent code.

it looks code having problem mentioned in thread class documentation:

if don't call thr.join before main thread terminates, other threads including thr killed.

basically main thread finishing pretty instantaneously. however, threads, create browsers, take lot longer that. result threads terminated before browser opens.

by adding long sleep @ end, can see browsers can opened code:

require 'watir-webdriver' $chunkythread = ["a", "b", "c"] $chunkythread.each |z|   puts "pshaw"   thread.new{     thread.current["testwords"] =  "ohai " + z.to_s     thread.current["mybrowser"] = watir::browser.new :ff     puts thread.current["testwords"]    } end sleep(300) 

however, more reliability, should join threads @ end:

require 'watir-webdriver' threads = [] $chunkythread = ["a", "b", "c"] $chunkythread.each |z|   puts "pshaw"   threads << thread.new{     thread.current["testwords"] =  "ohai " + z.to_s     thread.current["mybrowser"] = watir::browser.new :ff     puts thread.current["testwords"]    } end threads.each { |thr| thr.join } 

for actual code example, putting @threads << thread.current not work. join evaluating @threads empty. try doing following:

$testcasearray.each |thiscase|     @threads << thread.new {             puts "working one"         thread.current["tbrowser"] = watir::browser.new :ff          # other thread stuff     }     $alphabet[@count] = @threads.last     @count += 1      end   @threads.each |thisthread|                thisthread.join end 

note not sure why want store threads in $alphabet. put in $alphabet[@count] = @threads.last, removed if not in use.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -