Multithreading WATIR Script

468 views
Skip to first unread message

watirboy

unread,
Jan 25, 2011, 1:11:13 PM1/25/11
to Watir General
Hey Guys,

I read the solution here:

http://groups.google.com/group/watir-general/browse_thread/thread/86488f3bcf641f4/c0dbd194a23cca8b?lnk=gst&q=multithreading#c0dbd194a23cca8b

and for some odd reason it is not working at all.

Here is my situation. The user has a portal that they can enter data.
Once this data is entered, our back end system generates a unique ID
for the PDF Image associated with this data. The issue at hand is that
sometimes this unique ID is generated for two separate sets of data.

Here is what I have so far...

{code}

require 'rubygems'
require 'watir'
require 'yaml'
require 'thread'
include Watir

def create_data
$HIDE_IE=false
browser = Watir::Browser.new
browser.speed = :zippy
config = YAML::parse(File.open(File.dirname(__FILE__) + '/../../
config.yml')).transform
user = config['employee']['user']
pass = config['employee']['pass']
site = config['employee']['site']

browser.goto site
browser.text_field(:name, 'txtuser').set(user)
browser.text_field(:name, 'txtpwd').set(pass)
browser.button(:name, 'btnlogin').click

browser.text_field(:name, 'txttitle').set("JUNK")
browser.text_field(:name, 'txtcomment').set("JUNK")
browser.button(:name, 'btnsubmit').click

browser.button(:name, 'btnhistory').click

linkloc = browser.link(:text, /New Data/).link_string_creator

pdffile = linkloc[0].split('/').last
puts 'new Data PDF is: ' + pdffile

browser.close
end

threads = []
2.times do
threads << Thread.new {create_data}
end
threads.each {|x| x.join}

{/code}

When I run this, only one instance of IE comes up. What am I doing
wrong?

Any help would be greatly appreciated... :)

Dave McNulla

unread,
Jan 25, 2011, 3:22:33 PM1/25/11
to Watir General
When I have trouble with tailoring something that is new to me, I step
backward to the original and make sure that worked before I made
changes. In that thread, the suggestion is to run the unit test. Maybe
you can try that to see if you get success there. If the original
doesn't work, then you may have another issue to deal with.

BTW, browsers have changed a little since 2008. Maybe that has
something to do with it. My IE creates a separate process for each
thread. Are you getting multiple threads? Which version of IE are you
using?

Good luck,

Dave

On Jan 25, 1:11 pm, watirboy <enrique.j.ma...@gmail.com> wrote:
> Hey Guys,
>
> I read the solution here:
>
> http://groups.google.com/group/watir-general/browse_thread/thread/864...

watirboy

unread,
Jan 25, 2011, 4:02:59 PM1/25/11
to Watir General
Hey Dave,

Thanks for the reply. The original script works great (pretty much a
copy and past from an existing WATIR Unit Test). I am using IE 8 on a
Windows XP Machine. It doesnt seem like both threads are coming up
(just the first one...)

MJP

unread,
Jan 25, 2011, 4:12:47 PM1/25/11
to Watir General
Hi Watirboy,
I've been using 'system' call to get multiple Watir/IE sessions to
work with unique ids. Example:

threads = []
numUsers.times do |i|
threads << Thread.new do
#Need quotes to pass spaces on command line.
params = 34.chr<<params<<34.chr
result = system("ruby #{testScript} #{params}") #execute the
script and wait for it to finish.
end
threads.each {|t| t.join}

# Note that in the testScript, 'new_process' is used to get a new IE
browser. Example:
$ie = Watir::IE.new_process

Regards,
MJP

On Jan 25, 3:22 pm, Dave McNulla <mcnu...@gmail.com> wrote:

Željko Filipin

unread,
Jan 26, 2011, 3:56:25 AM1/26/11
to watir-...@googlegroups.com
On Tue, Jan 25, 2011 at 7:11 PM, watirboy <enrique...@gmail.com> wrote:
> Here is my situation. The user has a portal that they can enter data.
> Once this data is entered, our back end system generates a unique ID
> for the PDF Image associated with this data. The issue at hand is that
> sometimes this unique ID is generated for two separate sets of data.

I do not understand the problem. What are you trying to do?

Željko
--
watir.com - community manager
watirpodcast.com - host
testingpodcast.com - audio podcasts on software testing. all of them

watirboy

unread,
Jan 26, 2011, 11:59:11 AM1/26/11
to Watir General
Hey Zeljko,

I am trying to throw up two IE processes utilizing Ruby's threading
mechanism so I can run concurrent tests. I was trying to use the same
test method for both threads, but when I run it only one instance of
IE comes up instead of both threads.

The test is to see if a generated unique id can collide with our
system (example, I write a note and save it, the unique ID would be
0001.pdf. if i have two people trying to save at the same time, will
both get 0001.pdf or will we get two different ids, 0001.pdf and
0002.pdf.)

Thanks :)

On Jan 26, 3:56 am, Željko Filipin <zeljko.fili...@wa-research.ch>
wrote:

MJP

unread,
Jan 26, 2011, 3:00:26 PM1/26/11
to Watir General
Hi Watirboy,
It sounds like you want to sync up the processes to create contention.
It takes some time to launch IE browsers. I've been sleeping 3 seconds
between each launch. So launching ten browsers would take 30 seconds
before all the browsers exist. (Your mileage may vary.) In this
example - I would add 30 seconds to Time.now and pass this as a
parameter to the Testscript(s).

Example:
# Used when creating threads.
delayBetweenUsers = 3
# Number of seconds used as a pause so all clients can synch up to the
same moment.
secToSync = numUsers * delayBetweenUsers
...

# In the testScript sleep until that time to get the scripts in sync.
while Time.now.to_i < waitUntil.to_i
Thread.pass
sleep 1
end
...

Regards,
MJP

Željko Filipin

unread,
Jan 27, 2011, 4:12:47 AM1/27/11
to watir-...@googlegroups.com
On Wed, Jan 26, 2011 at 5:59 PM, watirboy <enrique...@gmail.com> wrote:
> The test is to see if a generated unique id can collide with our
> system (example, I write a note and save it, the unique ID would be
> 0001.pdf. if i have two people trying to save at the same time, will
> both get 0001.pdf or will we get two different ids, 0001.pdf and
> 0002.pdf.)

I do not think you need threads at all.

Are you trying to do something in the *exactly* same time?

1) If not, then just open two browsers and do what you need:

 browser1 = Watir::Browser.new
 browser2 = Watir::Browser.new

# do something with browser1
# do something with browser2

2) If yes, are you aware of the fact that you can never be sure that something happened at the *exactly* same time?

Željko

MJP

unread,
Jan 27, 2011, 3:51:50 PM1/27/11
to Watir General
Hi Zeljko,
Very good points. Maybe the OP can respond more specifically to your
questions and his requirements.
On your second point -

' 2) If yes, are you aware of the fact that you can never be sure
that
something happened at the *exactly* same time?'

- I agree. With this type of scenario we are usually testing that the
system's queue (or connection pool, or whatever...) is functioning as
expected. We want to sync the requests up close enough to force the
system's queue through it's paces, and possibly force error handling.
So instead of trying to get two request sent at *exactly* the same
time, I would try for a higher number of requests sent *about* the
same time.
Regards,
MJP

On Jan 27, 4:12 am, Željko Filipin <zeljko.fili...@wa-research.ch>
wrote:

watirboy

unread,
Jan 28, 2011, 9:29:25 AM1/28/11
to Watir General
Hey Zeljko,

Thanks for the new view of how I can do this. As MJP mentioned, I am
trying to stress test concurrency issues on our system.

I will look into this solution and get back to you guys. Thank you!
Reply all
Reply to author
Forward
0 new messages