[Wtr-general] Access is denied

Chris .Rasch crasch at gmail.com
Thu May 3 18:15:00 EDT 2007


When running the attached script, while executing line 238, I would
get the following error:

1) Error:

test_300_buywizard(MT_mtaco):

WIN32OLERuntimeError: failed to get IEnum Interface

    HRESULT error code:0x80070005

      Access is denied.

    marketocracy_login2.rb:61:in `each'

    marketocracy_login2.rb:61:in `visible_menus'

    marketocracy_login2.rb:46:in `visible_child_by_text'

    marketocracy_login2.rb:37:in `nav_menu'

    marketocracy_login2.rb:239:in `test_300_buywizard'

(Line numbering is a bit off since I changed the script after the
error message was generated).

Note that I called the same function earlier in the test, but don't
get the error then.  The error appears to arise from line 61 in the
load_nav_menu method:

@marketocracy_divs.each do |d|

The first time it's called, the @marketocracy_divs variable is
uninitialized (set to nil).  However, when the bug arises, the
@marketocracy_divs variable still retains the values it had from a
previous call.  I suspected I got the "Access is denied" error message
because I was trying to access DIV objects that had been created from
an earlier page.   So I changed line 59 from this:


	load_nav_menu unless @marketocracy_divs # grab it on the first try

to this:
	load_nav_menu # unless @marketocracy_divs # grab it on the first try

This time it ran without error.

Since I was apparently able to work around the problem, this is posted
mostly in case someone else has a similar problem.  However, is my
analysis of the cause of the problem correct?  Any comments in general
on the causes of "Access is denied."  error messages?


About my system:

Watir 1.5  running under cygwin (2.510.2.2) on
Windows XP Professional (Version 2002, Service Pack 2).

Windows XP is in turn running as a virtual OS under Parallels (Build
3170 RC3) running on OS X Tiger 10.4.9 (2.16 GHz Intel Core 2 Duo)

cygwin+cygwin ruby (via cygwin setup.exe)+rubygems-0.9.2
(via tarball)+watir-1.5.1.1164 (via gem)
-------------- next part --------------
require 'rubygems'
require 'watir'
require 'ruby-debug'

require 'test/unit'
require 'test/unit/ui/console/testrunner'

Debugger.start
class MarketocracyNavMenuException < Watir::Exception::WatirException; end

module Watir

  # I am patching Watir with some custom code written
  # specifically for the marketocracy navigation menu.
  class IE

    # Once you've opened one of the top 4 menus
    # use this method to drill down to a specific
    # menu item by providing a variable length list
    # of Strings representing the menu items
    #
    # Raises MarketocracyNavMenuException  
    #
    # Typical usage: 
    #
    #   ie.image(:name, "Menu4i").fire_event("onmouseover")
    #   ie.nav_menu("Cash", "Benchmarks", "Alpha Contour")
    def nav_menu(*path)
      destination = path.pop # pop removes the last element from an array and returns it 
      path.each do |step| # each step in the path do the following
        child = visible_child_by_text(step)
        child.fireEvent("onmouseover")
        # Wait a few milliseconds for JavaScript to execute
        sleep 0.2
      end

      visible_child_by_text(destination).click
      wait
      load_nav_menu # need to reload divs on each page
    end
    
    private

    def visible_child_by_text(text)
      result = nil
      visible_menus.each do |menu|
        menu.children.each do |child|
          if child.innerText == text
            result = child
          end
        end
      end
      raise MarketocracyNavMenuException, "Menu item with #{text} was not visible!" unless result
      result
    end
    
    def visible_menus
      # execute the load_nav_menu method, unless the @marketocracy_divs variable      # has already been set
      load_nav_menu # unless @marketocracy_divs # grab it on the first try
      visible = []
      @marketocracy_divs.each do |d|
        # append each visible marketocracy div to the visible array
        # Where are the style and invoke methods defined? 
        visible << d if d.style.invoke('visibility') =~ /visible/
      end
      # return the array of visible divs
      visible
    end
    
    def load_nav_menu
      # Document is defined in watir.rb.  It returns an @ie.document object,
      # @ie is created by WIN32OLE.new('InternetExplorer.Application') 
      @marketocracy_divs = document.getElementsByTagName("DIV")
    end
  end
end

class MT_mtaco < Test::Unit::TestCase
	include Watir
	@@tb_adminPassword = "m"
	@@logins = "m100.csv"
	@@beginTime = Time.now
	@@homepageTime = 0
	@@overviewTime  = 0
	@@timeSpreadsheet = File.new(Time.now.strftime("%d-%b-%y") + ".csv", "a")  #Note this creates a new file every day...
	@@maxAllowedLoginTime = 30 
	@@executionEnvironment = "Test"
	
	def close_all_windows
		loop do
			begin
				Watir::IE.attach(:title, //).close
			rescue Watir::Exception::NoMatchingWindowFoundException
				break
			rescue
				retry
			end
		end
	end
	
	def setup
		begin
			close_all_windows
			$ie = Watir::IE.new
			test_site='http://www.marketocracy.com'
			$ie.goto(test_site)
			@@homepageTime = Time.now - @@beginTime
		end
	end
    
# 	def test_100_homepage
# 		teststring = "research"
# 		begin
# 			assert($ie.contains_text("#{teststring}"))
# 				puts("PASSED. Found test string: #{teststring}")
# 		rescue => e
# 				puts("FAILED. Could not find test string: #{teststring}.  " + e.message + "\n" + e.backtrace.join("\n"))
# 		end
# 	end
# 	
	def login(username, password)
		
		@@homepageTime = Time.now - @@beginTime
			
			
		error_count = 0
		error_message = ""
        # debugger
		$ie.text_field(:name, "username").set("#{username}")
		$ie.text_field(:name, "password").set("#{@@tb_adminPassword}")
		$ie.button(:name, "Submit").click
		
		refresh_count = 0
		while $ie.contains_text("PLEASE CLICK HERE TO REFRESH") && refresh_count < 20
			sleep 3
			$ie.link(:text, /PLEASE CLICK HERE TO REFRESH/ ).click
			refresh_count += 1
		end
		
		@@overviewTime = Time.now - @@beginTime
		
	
		if refresh_count >= 20
			error_count += 1
			puts error_message +=  "#{error_count}. More than 20 refreshes.  "
		end
		
		if !$ie.contains_text("Funds Managed by")
			error_count +=1
			error_count +=1
			puts error_message += "#{error_count}. Not on overview page.  "
		end
		
		if @@overviewTime > 60 
			error_count +=1
			puts error_message += "#{error_count}. Login time > max allowed login time.  "
		end
		puts "@@homepageTime: #{@@homepageTime}"
		puts "@@overviewTime: #{@@overviewTime}"


		@@timeSpreadsheet.puts "#{@@executionEnvironment}, #{@@homepageTime}, #{@@overviewTime}, #{refresh_count}, #{error_message}" 
		return error_count, error_message

	end

#     def test_menu
# 
#         ie = Watir::IE.start('http://www.marketocracy.com')
#         ie.text_field(:name, "username").set("trasch")
#         ie.text_field(:name, "password").set("m")
#         ie.button(:name, "Submit").click
# 
#         # get my funds loaded asap
#         refresh_link = ie.link(:text, /PLEASE CLICK HERE TO REFRESH/)
#         refresh_link.click if refresh_link.exists?
# 
#         ie.image(:name, "Menu4i").fire_event("onmouseover")
#         ie.nav_menu("Cash", "Benchmarks", "Alpha Contour")
# 
#         ie.image(:name, "Menu1i").fire_event("onmouseover")
#         ie.nav_menu("About Us", "Overview")
# 
#     end

		
	
# 	def test_200_singlelogin
# 		
# 		login = "trasch"
# 		
# 		error_count, error_message = login("#{login}", "#{@@tb_adminPassword}")
# 
# 		if error_count > 0 
# 			flunk
# 		end
#     end

#     def test_250_makeatrade
# 
#         login = "trasch"
# 
# 		error_count, error_message = login("#{login}", "#{@@tb_adminPassword}")
#        
#         # debugger 
#         $ie.image(:name, "Menu4i").fire_event("onmouseover")
#         $ie.nav_menu("Make a Trade")
#         $ie.text_field(:index, 2).set("MSFT")
#         $ie.button(:value, "Quote").click
#         $ie.text_field(:index, 4).set("1")
#         $ie.button(:value, "Buy").click
#         if not ($ie.contains_text(/MSFT/) and  $ie.contains_text(/Last Filled/))
#             error_count +=1 
#             error_message += "\nPage did not contain expected text"
#         end
# 
# 
#         if error_count > 0
#             puts error_message
#             flunk
#         end
#     end


    def test_300_buywizard

        login = "trasch"

		error_count, error_message = login("#{login}", "#{@@tb_adminPassword}")
       
#         debugger 
        $ie.image(:name, "Menu4i").fire_event("onmouseover")
        $ie.nav_menu("T2", "Buy Wizard")
        $ie.text_field(:index, 2).set("MSFT")
        $ie.text_field(:index, 3).set("1")
        $ie.button(:value, "Buy").click
        $ie.image(:name, "Menu4i").fire_event("onmouseover")
        $ie.nav_menu("Make a Trade")
        if not ($ie.contains_text(/MSFT/) and  $ie.contains_text(/Last Filled/))
            error_count +=1 
            error_message += "\nPage did not contain expected text"
        end


        if error_count > 0
            puts error_message
            flunk
        end
    end

#     def test_250_sell_wizard
# 
#         login = "trasch"
# 
# 		error_count, error_message = login("#{login}", "#{@@tb_adminPassword}")
#        
# #         debugger 
#         $ie.image(:name, "Menu4i").fire_event("onmouseover")
#         $ie.nav_menu("T2", "Sell Wizard")
# #         debugger
#         $ie.text_field(:index, 2).set("MSFT")
#         $ie.text_field(:index, 3).set("1")
#         $ie.button(:value, "Buy").click
#         $ie.image(:name, "Menu4i").fire_event("onmouseover")
#         $ie.nav_menu("Make a Trade", "Open Orders")
#         if not ($ie.contains_text(/MSFT/) and  $ie.contains_text(/Last Filled/))
#             error_count +=1 
#             error_message += "\nPage did not contain expected text"
#         end
# 
# 
#         if error_count > 0
#             puts error_message
#             flunk
#         end
#     end

	
	# def test_300_averagelogin

	#	File.open("#{@@logins}", "r") do |file|
	#		file.each do |login|
	#			error_count, error_message = login("#{login}", "#{@@tb_adminPassword}")
	#		end
	#	end
	# end
end






More information about the Wtr-general mailing list