3
votes

Need:

Using Cucumber 1.0.1 and Watir 1.9.2, I need to execute javascript code in order for a proprietary portal to do some navigation.

Issue:

I am able to execute JS code with the following:

def execute_js(js_code)
  @browser.goto("javascript:#{js_code};void(0)")
end

execute_js("doNavigate()")

By doing so, the navigation is done as expected but Watir doesn't re-take control of the browser.

What I look for:

I look for a solution for Watir to re-take control of the browser after the 'javascript goto'.

Tested alternative:

@browser.execute_script('alert("toto");')

gives me this:

  execScript
      OLE error code:80070005 in <Unknown>
        Access Denied.

      HRESULT error code:0x80020009
        An exception occurred. (WIN32OLERuntimeError)
  ./features/lib/portal.rb:110:in `tln_main_tab'

Local gems:

  • Ascii85 (0.9.0)
  • builder (3.0.0)
  • bundler (1.0.15)
  • capybara (1.0.0)
  • childprocess (0.1.9)
  • commonwatir (1.9.2)
  • cucumber (1.0.1)
  • diff-lcs (1.1.2)
  • ffi (1.0.9 x86-mingw32)
  • firewatir (1.9.2)
  • fuubar-cucumber (0.0.12)
  • gherkin (2.4.5 x86-mingw32)
  • hoe (2.10.0)
  • json (1.5.3)
  • json_pure (1.5.3)
  • mime-types (1.16)
  • nokogiri (1.5.0 x86-mingw32)
  • pdf-reader (0.9.0)
  • prawn (0.11.1)
  • rack (1.3.0)
  • rack-test (0.6.0)
  • rake (0.9.2)
  • rautomation (0.6.2)
  • rspec (2.6.0)
  • rspec-core (2.6.4)
  • rspec-expectations (2.6.0)
  • rspec-mocks (2.6.0)
  • ruby-progressbar (0.0.10)
  • rubygems-update (1.8.5)
  • rubyzip (0.9.4)
  • s4t-utils (1.0.4)
  • selenium-webdriver (0.2.2)
  • syntax (1.0.0)
  • term-ansicolor (1.0.5)
  • ttfunk (1.0.1)
  • user-choices (1.1.6.1)
  • viewcumber (0.1.2)
  • watir (1.9.2)
  • win32-api (1.4.8 x86-mingw32)
  • win32-process (0.6.5)
  • win32console (1.3.0 x86-mingw32) -windows-api (0.4.0)
  • windows-pr (1.2.0)
  • xml-simple (1.1.0)
  • xpath (0.1.4)
2
Why execute the JS code directly like that? there's no way a normal user would ever do that, why not just interact with the system as a user would?Chuck van der Linden
The proprietary portal does some heavy use of javascript to create a Top Level Navigation menu that is 99% JS and 1% HTML. Fact is that the menu links don't seem to be in the DOM... That's why I feel that my only way to navigate is by using its standard mechanism: use its own javascript functions.Arnaud Leymet
You can probably navigate it much the way a user would, by firing events that represent various user actions, mouseover, mousedown, etc. The key is in knowing what events are being monitored by what elements. Sometimes it's as easy as just the .click method on a div or cell (depending on how the control is created. In other cases there can be specific sequences that are required such as a mouseover first, which then puts the control into the state where it can accept a click. close observation when interacting manually usually gives you good clues, as does using browser developer tools.Chuck van der Linden

2 Answers

0
votes

Access Denied error message is usually connected to frames on a page. Take a look at Frames page at Watir wiki.

1
votes

I found a workaround:

def execute_js(js_code)
  begin
    Timeout::timeout(2) do
      @browser.goto("javascript:#{js_code};void(0)")
    end
  rescue Exception => e
    goto "#{@browser.url}#"  # <<< workaround is here
    return
  end
end

execute_js("doNavigate()")

It's not ideal, but it enables javascript execution, then updates the URL hash so that Watir knows that an actual action was done, so that Watir can go further.