[wtr-general] Re: Retaining variables between tests in a thread-safe manner
If you use only IE, try replacing following Watir::IE.new with Watir::IE.new_process Michael - Original Message From: Justin Ko To: Watir General Sent: Thursday, February 5, 2009 4:11:24 AM Subject: [wtr-general] Retaining variables between tests in a thread-safe manner We are creating a smoke test for our web based application using the Test/Unit module in Ruby (v1.8.6) with Watir (v1.6.2). The smoke test would consist of a series of tests such as: Test 1: Open an IE browser and goto to the application's home page Test 2: Sign into the application Test 3: Test some feature that is accessible to the signed in user Test 4: Test some other feature that is accessible to the signed in user We would like to: 1) Use the same browser (Watir::IE object) throughout the entire series of tests and 2) Be able to run the smoke test multiple times concurrently. In trying to do this, we are running into problems with variable scope. When we assigned the Watir::IE object to an instance variable (@), the variable appeared to be destroyed during the teardown of each test. This prevents us from re-using the same browser across multiple tests. When we assigned the Watir::IE object to a class (@@) or global ($) variable, we could use the same browser throughout the series of tests. However, when using multiple threads to run the smoke test multiple times concurrently, the threads collided due to everyone using the same browser. The below code illustrates our problem. If you run the code as it is, two browsers will be opened and all of the .goto() commands end up being performed on the last created browser (ie both threads are sharing one browser). If you change the '@@browser' to '@browser', the test_open_browser test completes as expected (ie two browsers open and each navigates to a page). However, the browser is not retained from test to test, resulting in the test_navigate_somewhere_else test giving the following error: test_navigate_somewhere_else(TC_MyTest): NoMethodError: undefined method `goto' for nil:NilClass caller.rb:16:in `test_navigate_somewhere_else' Question: Are there any ideas on how we can retain a variable (the browser) between tests, yet still be thread-safe? Thanks, Justin Ko Code: require 'watir' require 'watir/ie' require 'test/unit' require 'test/unit/ui/console/testrunner' class TC_MyTest < Test::Unit::TestCase def test_open_browser pages = ['www.google.ca', 'www.bestbuy.ca', 'www.futureshop.ca'] @@browser = Watir::IE.new sleep(2+rand(10)) @@browser.goto( pages[rand(pages.length)] ) end def test_navigate_somewhere_else pages = ['www.henrys.com', 'www.gmail.com', 'maps.google.ca'] @@browser.goto( pages[rand(pages.length)] ) end end class TS_MyTests def self.suite suite = Test::Unit::TestSuite.new suite << TC_MyTest.suite return suite end end threads = [] 2.times do |x| threads << Thread.new(x) { |myPage| Test::Unit::UI::Console::TestRunner.run(TS_MyTests) } end threads.each { |aThread| aThread.join } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Watir General" group. To post to this group, send email to watir-general@googlegroups.com Before posting, please read the following guidelines: http://wiki.openqa.org/display/WTR/Support To unsubscribe from this group, send email to watir-general-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/watir-general -~--~~~~--~~--~--~---
[wtr-general] Re: Retaining variables between tests in a thread-safe manner
You could use a Singleton or pass your browser object around as a parameter. Aidy 2009/2/5 Justin Ko : > > We are creating a smoke test for our web based application using the > Test/Unit module in Ruby (v1.8.6) with Watir (v1.6.2). > > The smoke test would consist of a series of tests such as: > > Test 1: Open an IE browser and goto to the application's home page > Test 2: Sign into the application > Test 3: Test some feature that is accessible to the signed in user > Test 4: Test some other feature that is accessible to the signed in > user > > We would like to: > 1) Use the same browser (Watir::IE object) throughout the entire > series of tests and > 2) Be able to run the smoke test multiple times concurrently. > > In trying to do this, we are running into problems with variable > scope. > > When we assigned the Watir::IE object to an instance variable (@), the > variable appeared to be destroyed during the teardown of each test. > This prevents us from re-using the same browser across multiple tests. > > When we assigned the Watir::IE object to a class (@@) or global ($) > variable, we could use the same browser throughout the series of > tests. However, when using multiple threads to run the smoke test > multiple times concurrently, the threads collided due to everyone > using the same browser. > > The below code illustrates our problem. > > If you run the code as it is, two browsers will be opened and all of > the .goto() commands end up being performed on the last created > browser (ie both threads are sharing one browser). If you change the > '@@browser' to '@browser', the test_open_browser test completes as > expected (ie two browsers open and each navigates to a page). > However, the browser is not retained from test to test, resulting in > the test_navigate_somewhere_else test giving the following error: > > test_navigate_somewhere_else(TC_MyTest): > NoMethodError: undefined method `goto' for nil:NilClass >caller.rb:16:in `test_navigate_somewhere_else' > > > Question: Are there any ideas on how we can retain a variable (the > browser) between tests, yet still be thread-safe? > > Thanks, > Justin Ko > > > Code: > > require 'watir' > require 'watir/ie' > require 'test/unit' > require 'test/unit/ui/console/testrunner' > > class TC_MyTest < Test::Unit::TestCase > def test_open_browser >pages = ['www.google.ca', 'www.bestbuy.ca', 'www.futureshop.ca'] >@@browser = Watir::IE.new >sleep(2+rand(10)) >@@browser.goto( pages[rand(pages.length)] ) > end > > def test_navigate_somewhere_else >pages = ['www.henrys.com', 'www.gmail.com', 'maps.google.ca'] >@@browser.goto( pages[rand(pages.length)] ) > end > end > > class TS_MyTests > def self.suite >suite = Test::Unit::TestSuite.new >suite << TC_MyTest.suite >return suite > end > end > > threads = [] > > 2.times do |x| > threads << Thread.new(x) { |myPage| >Test::Unit::UI::Console::TestRunner.run(TS_MyTests) > } > end > threads.each { |aThread| aThread.join } > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Watir General" group. To post to this group, send email to watir-general@googlegroups.com Before posting, please read the following guidelines: http://wiki.openqa.org/display/WTR/Support To unsubscribe from this group, send email to watir-general-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/watir-general -~--~~~~--~~--~--~---