1
votes

I am using webdriver with FF38, but leaving the browser open window after my script is done. I find that dialogs no longer open in that window, if I continue after the testing.

The script is meant to automate forms input rather than doing it by hand, but the website does use dialog boxes to express choices -- (for example, deleting data that the script has just entered, so that I can rerun the script without overwriting information)

Is there a way to disconnect the webdriver dialog handling after I'm done?

I'm feeling a little foolish, but my searches haven't born fruit, so I may be using the wrong words in my search, given my newness to ruby and webdriver.

Example would be this:

require "watir-webdriver"

l_Browser = Watir::Browser.new :firefox
l_Browser.goto "http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert"

# Click the button that opens the dialog
l_Browser.div(:class => "container").div(:class => "iframecontainer"). \
    div(:class => "iframewrapper").iframe(:id => "iframeResult"). \
    button(:onclick => "myFunction()").click

The result is that a popup will appear, but no popups will appear further attempts to click on the button once the script is done.

This includes even if no popup is triggered during the script (ie:, last line commented out)... Once the script is finished running, no popups appear in a watir webdriver opened window. (They will open if I click on the button while the script is running, but not after)

Based on the answer below, I am using:

begin
    b = Watir::Browser.new :firefox
    File.open('d:\\MARK.TXT', 'w') {|f| f.write(YAML.dump(b)) }
    # Load MessageBox and wait here
    b = YAML.load(File.read('d:\\MARK.TXT'))
ensure
    if !b.nil?
        b.close()
    end
end

... but it currently allows for errors that can be ignored... I just don't know how wise it is to ignore them in the long run:

    D:/Ruby193/lib/ruby/gems/1.9.1/gems/childprocess-0.5.6/lib/childprocess/windows/handle.rb:50:in `exit_code': The handle is invalid. (6) (ChildProcess::Error)
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/childprocess-0.5.6/lib/childprocess/windows/process.rb:41:in `exited?'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/childprocess-0.5.6/lib/childprocess/abstract_process.rb:147:in `poll_for_exit'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.46.2/lib/selenium/webdriver/firefox/binary.rb:59:in `quit'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.46.2/lib/selenium/webdriver/firefox/launcher.rb:62:in `quit'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.46.2/lib/selenium/webdriver/firefox/bridge.rb:75:in `quit'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.46.2/lib/selenium/webdriver/common/driver.rb:165:in `quit'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.7.0/lib/watir-webdriver/browser.rb:136:in `close'
        from D:/Users/risendevil/Documents/Aptana Studio 3 Workspace/Ruby Test/Default.rb:19:in `<main>'

Versions: Firefox 38.0.5 selenium (0.2.11) selenium-webdriver (2.46.2, 2.45.0) watir-webdriver (0.7.0)

2
Can you restate the problem in terms of what your script does -- code snippets help -- and what your desired outcomes are? Specifically, can describe why you need to keep the browser after executing a script and what you hope to do next?Sam
I tried to replicate what I think you are trying to do here, but I could get JS dialogues as much as I wanted gist.github.com/samnissen/76bc20505ba93257261eSam
I don't include Browser.close because I have no problems with alerts, as long as the script is running, but once the script has stopped running, I'd like the Browser window to remain open.Mark Schultz
I believe I have answered the question correctly -- please see my answer belowSam

2 Answers

0
votes

I learned something new answering your question: Turning an object into text is called serialization. Turning text into an object is called deserialization.

And here's a gist of you want to do, specifically.

The important part is

my_object = SomeObject.new
my_object.some_method # => returns your expected result
File.open('path/to/some.file', 'w') {|f| f.write(YAML.dump(my_object)) }
# Do whatever you want
my_object_reloaded = YAML.load(File.read('path/to/some.file'))
my_object_reloaded.some_method # => returns your expected result

You could even do this directly to your browser:

b = Watir::Browser.new
b.goto 'http://google.com' # => goes to Google
File.open('path/to/some.file', 'w') {|f| f.write(YAML.dump(b)) }
b = nil
# Do whatever you want, wait as long as you want.
# (Disclaimer: There are probably major limitations to 'as long as you want'.)
b = YAML.load(File.read('path/to/some.file'))
b.goto 'http://yahoo.com' # => goes to Yahoo
0
votes
require "watir-webdriver"

l_Browser = Watir::Browser.new :firefox
l_Browser.goto "http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert"

l_Browser.iframe(:id => 'iframeResult').button(:xpath => "//button[text()='Try it']").when_present.click # click on "Try it" button
l_Browser.alert.close # closing popup