"object invoked has disconnected from its clients" error message is displayed while working with popup windows - watir

I am trying to work with popup windows. I have gone through the site "http://wiki.openqa.org/display/WTR/JavaScript+Pop+Ups". I have modified the "ie-class.rb" by adding the code mentioned in the solution #7 of the site.
Also I have tried the example mentioned below :
require 'watir/ie'
require 'win32ole'
iewin = Watir::IE.new
iewin.goto("http://www.w3schools.com/js/tryit_view.asp?filename=tryjs_confirm")
iewin=Watir::IE.attach(:url, /w3schools/)
iewin.bring_to_front
iewin.maximize
iewin.button(:value, "Show a confirm box").click_no_wait
txt = iewin.clickprompt("OK") # waits for popup and click ok
puts txt #prints the popup text
txt1=iewin.clickprompt("OK")
puts txt1
iewin.close
On executing the following code, popups are handled but produces the following error :
C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-1.7.1/lib/watir/ie-class.rb:319:in `method_missing': unknown property or method: `name' (NoMethodError)
HRESULT error code:0x80010108
The object invoked has disconnected from its clients.
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-1.7.1/lib/watir/ie-class.rb:319:in `exists?'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/watir-1.7.1/lib/watir/ie-class.rb:406:in `close'
from popup.rb:50:in `<main>'
Press a button!
You pressed OK!
But when I remove the command "iewin.close", error message is not displayed.
"iewin.close" command closes the browser and then there is nothing for watir to perform then why is the error message displayed.
Please suggest.

It appears that you are using ruby 1.9.2 with Watir. The watir installation page recommends using Ruby 1.8.7. That should fix the problem.
Good luck,
Dave

Notice that the output you have is not in order. You are seeing the error from closing the browser, BEFORE the output from your Puts.
Not sure if this is a treading issue or what, but in any event it looks from the output like the window is attempting to close before stuff that is dependent on it has finished. Perhaps try inserting a brief sleep for a second or five right before you try to close the window and see if you are still having issues.

Dug into this a bit. It appears that Ruby 1.9 is throwing a "NoMethodError" instead of a "WIN32OLERuntimeError". The Watir IE code reads as such
def exists?
begin
!!(#ie.name =~ /Internet Explorer/)
rescue WIN32OLERuntimeError
false
end
end
To fix this a second rescue can be added to the call
def exists?
begin
!!(#ie.name =~ /Internet Explorer/)
rescue WIN32OLERuntimeError
false
rescue NoMethodError
false
end
end
I don't know the side effects of adding such a call, but it does suppress the close warning.

Related

Running 'System Events' from Excel For Mac Via VBA Results in Invalid Procedure 5 Error

I am trying to run a very simple apple script from VBA. The VBA code is:
NavigationJunk = AppleScriptTask("Budget Report Apple Scripts/NavigationMoveWindowsAppleScript.scpt", "NavigationMoveWindowLeftHandler", "")
The script reads:
on NavigationMoveWindowLeftHandler(Junk)
tell application "System Events" to key code 123 using {option down, control down}
end NavigationMoveWindowLeftHandler
where the complete path for the .scpt is: "/Users/Andrew/Library/Application Scripts/com.microsoft.Excel/Budget Report Apple Scripts/NavigationMoveWindowsAppleScript.scpt" (this is the correct path as documented here: Apple Script Task Throwing an Error 5
This code simply performs the key stroke cntrl+option+left arrow. That key stroke is a shortcut for the app Magnet to move the window to be half screen left.
This worked prior to updating my system to MacOS Ventura. I attempted to restart, uninstall Excel, reset the permissions in System Settings/Privacy/Automation (as documented here: Reset MacOS Privacy Settings) and reinstall Excel. When I tried to run the VBA code I was prompted to grant permission to system events and still received an invalid procedure error. I verified that when I call the handler from within the Apple Script Editor the system performs the keystrokes with no issues. I assume this is some sort of permission thing...How do I fix this?
Of note: this code called prior to the AppleScriptTask above runs just fine:
AppleScriptResult = Split(AppleScriptTask("Budget Report Apple Scripts/NavigationMoveWindowsAppleScript.scpt", "NavigationGetScreenResolution", ""), " ")
This is curious because the .scpt is the same file and calls a different handler:
on NavigationGetScreenResolution(Junk)
return do shell script "system_profiler SPDisplaysDataType | grep Resolution"
end NavigationGetScreenResolution
and runs just fine. Any help here would be greatly appreciated

Detect that automated Chrome page has been closed

I have written code to automate entries into a survey using Excel VBA with selenium controlling chrome.
If the user closes the driven chrome window then currently I get a vba error when the next element can't be found.
I want to write some code to detect the chrome window is no longer present and then close down the vba code smoothly.
My idea was to try to find the tab title and execute the closure code if it did not exist - meaning I got the chrome not available error. The code below seems to work but there is a 13 second wait whilst it looks for the page title. Shortening any of the 4 Timeouts available in selenium basic does not seem to change this wait.
My questions are:
Is there a better way to check if the driven chrome window has closed?
Is there a way to shorten the period of time before the error is thrown?
On Error Resume Next
tabTitle = ch.Title
If Not tabTitle Then
Debug.Print "Closed"
End If
On Error GoTo 0
Many thanks
The only way I know of is to catch UnreachableBrowserException. I am not sure what Selenium version you are using, but in Java, you would surround your code in a try/catch
try {
// do your Excel thing here
} catch (UnreachableBrowserException e) {
// execute your alternate plan
}
The problem is that this could happen at any time, anywhere. You can simply put this everywhere on the code. If this is part of a fully-automated solution, why not simply run your script in headless mode. This will prevent the root cause of the problem which is a user unexpectedly closing the browser.

Make Greasemonkey throw error on undefined variable

I've recently switched from chrome to firefox for greasemonkey development. I have some problems with debugging.
The following func
I meant to write:
self = this;
but instead I wrote
this=self;
Self wasn't defined, so the script didn't run, however, nor did I get any javascript error. Why and can I get them somehow? I get some errors.
Unfortunately, Greasemonkey - and javascript in general, along with at least a few other non-compiled languages - do not give very good debugging errors... I can't count the number of times it tells me there is a missing ) at the end of an argument list that doesn't exist, and it turns out my error is elsewhere...
When a script of mine won't run I add an alert after any value assignments and if they don't alert or alert the wrong data I have found the problem... for your example above I would debug by:
this=self;
alert(self);
Although I would probably see the problem when I went to add the alert, but if I didn't notice - the alert either wouldn't pop up or it would have the wrong value... and if it didn't pop up, the error console would likely say self not defined.

watir 3 modal window access

I tried watir wiki example to access modal window. But was getting Argument error. I am trying with watir 3 version.
puts b.modal_dialog(:title, "showModalDialog Method Sample Target Page").exists?
puts b.modal_dialog(:title, "showModalDialog Method Sample Target Page").title
b.modal_dialog(:title, "showModalDialog Method Sample Target Page").close
b = Watir::Browser.new()
b.goto("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog.htm")
b.button(:value,"Push To Create").click_no_wait
Error message
C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-classic-3.0.0/lib/watir-classic/contai
ner.rb:60:in `modal_dialog': wrong number of arguments (2 for 0) (ArgumentError) from C:/webtest/popup.rb:29:in `<main>'
Can some one help how to access modal window using watir 3
Thanks
The error you're receiving is saying that you're supplying more arguments than are actually taken by the method.
I've never had call to use the actual modal methods within Watir, but after quick experimentation with the page you supplied I was able to interact with the modal using the following:
b.goto("http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/showModalDialog.htm")
b.button(:id => "dCommand").click_no_wait
puts b.modal_dialog.text
In the above example I had it print out the text of the modal, but presumably any regular Watir command could be substituted.
It would appear that the requirement for an argument indicating which modal to interact with has been removed in Watir 3.0 or earlier (presumbaly because you can only interact with one?). I moved to Watir-webdriver as I primarily test in Firefox, so I'm not sure when this might have changed, but it does appear to be the case that it does not accept any arguments in 3.0.

Excel Object SaveAs, error happens when Existing File is Open

Excel_Obj = CREATE OleObject
Excel_Obj.ConnectToNewObject( 'excel.application' )
Excel_Obj.Workbooks.Add
Excel_Obj.Application.ActiveWorkbook.WorkSheets.Add
Excel_Sheet = Excel_Obj.Application.ActiveWorkbook.WorkSheets[1]
//EXAMPLE
Excel_Sheet.Cells[1,1] = 45
Excel_Obj.Application.ActiveWorkbook.SaveAs(ls_file,56) //csv
//where ls_file = the Opened File
error happened after / during saveas.
try catch throw "error calling external object..in click..line.. saveas.."
--
i want to state to the user that the excel file is open therefore cannot be overwritten properly. I used a try catch and throwed a proper message but before the messagebox for the catch event happens, the PB execution error R0035 happens. any solutions or proper way to know if the excel file is open.
You might be able to check if the file is open first, have a look at this answer:
how to check if file is opened in excel using OLE (leaves excel process open)
I'd try a PowerScript FileOpen () call with a LockReadWrite! parameter to see if it can be opened, followed immediately by a FileClose () if it was successful. (I think this is a PowerScript-specific variation on the DXL solution Colin linked to.)
Good luck,
Terry
Have you tried approaches similar to these?
Using Win32 API:
http://www.rgagnon.com/pbdetails/pb-0030.html
Using PB function fileopen()with the (default) exclusive rights set:
http://www.tek-tips.com/viewthread.cfm?qid=1610670
In other words, see if the file can be opened exclusively before connecting to Excel or making the CSV?
You will have to turn off the option to break into the debugger for that exception to see the exception handling work in the IDE. Look for the Help topic "Exception Settings dialog box" for details. Once you see it's working I recommend you set it back to break into the debugger, since you'd normally want to see what threw the error.
You cannot use ole when document is open( even you set lock write) by the user and not by apllication.
my approach, I have been using many times till now:
Check excel is open or not, use use can use api or wsh script in the internet to check app opened. If opened do not run save as and tell user to close excel and not run it for while for while.
if you user run excel and your program still running active workbook and worksheet application will be switch to excel that opened by user ( imagine it wrong written data).
Change your code as below
if Excel_Obj.ConnectToNewObject( 'excel.application' ) <> 0 then
messagebox("warning", "could connect to excel", stopsign!)
RETURN
end if
just for knowledge another technique is DDE call, but not common today and most complicated.
Happy coding From pb developer.

Resources