body.unload in Safari for Windows - safari-extension

so I have a Safari extension, and the following works well in Safari for Mac, but not in Safari for Windows.... (from my Global.html):
<body onload='initialize()' onunload='quit()'>
is there some kind of workaround to get this to work on Windows?
I am wanting to capture the Safari has been quit event....
I also tried using the close event:
safari.application.addEventListener("close", closed, true);
but I can't find a way to narrow this down to just the window close event. I get messages for each tab closure.
thanks in advance.

If you are using a library, which you should be, it should help with this.
For instance, I believe jquery's unload fixes it. Install jquery and use the following:
$(window).unload(function(){
// do something
});

Related

Capybara Selenium Navigate To URL Hangs With Popup Alert on Safari

At the end of my tests Capybara automatically navigates to "about:blank" in order to set up the next test. Sometimes the application I'm testing will throw a popup alert if the user leaves the page (which is expected). I have some code to handle this:
begin
page.driver.browser.navigate.to("about:blank")
page.driver.browser.switch_to.alert.accept
rescue Selenium::WebDriver::Error::NoAlertPresentError
# No alert was present. Don't need to do anything
end
This works fine on Firefox, Chrome, and IE. But for some reason on Safari the navigate command hangs, I assume because of the popup. Anyone know a workaround for this?
There is no simple workaround for this at this time in any version of Selenium language bindings. It is a known issue the Selenium team is not interested in resolving. Fundamentally, it is due to the architecture of Safari and consequently the architecture of the Safari Driver.
The JavaScript of the Safari Driver extension does not know about most of the alerts and popups and dialogs that appear as modal Cocoa layer windows.
It also cannot interact with them.
There is a way but it won't be easy and nobody's done it.
You would need to use Cocoa.
So you would want to use RubyCocoa in this case.
(or PyObjC if you used Python)
You would then possibly also want a sidecar app actually written in Objective-C.
The trick would be to use the AX (Accessibility API) and a separate process to observe if there is an alert as the front window and poke at its labels and buttons' text as visible to the AX APIs.
AX APIs are probably exposed in RubyCocoa via the ScriptingBridge.
However, you would need to add your 'app' to the Security preference pane's list of things allowed to control the computer.
With that, you could detect the window and handle it.
It could be fairly brittle across web sites, but if built well, you could handle expected conditions.
You could try to confirm like this which I believe should work across browsers
# click ok to confirm
page.evaluate_script('window.confirm = function() { return true; }')

Best way to display notifications in Opera v15 extension

I have a chrome extension that I adapted to the Opera. Fortunately, all the features of my chrome extension worked well in Opera 15 without many changes. However, my extension uses the webkitNotifications to notify the result of the context menu click. It seems that the web notifications doesn't work in Opera extensions.
var notification = webkitNotifications.createNotification('icon_32.png','My extension title','Notification text');
notification.show();
setTimeout(function(){notification.cancel();}, 5000);
In this case, what is the best way to notify the user when the context menu is clicked? Thanks.
webKitNotifications.createHTMLNotification has been deprecated. New suggested way to create notifications is in draft currently and not many browsers have implemented it.
In the meantime, I'd like to suggest to find another way to notify the user.

Selenium: simulating typeing on Chrome

Does anybody know how to simulate typing on Chromium? I want to simulate real typing and I successfully used combination of keydown+keypress+keyup on firefox. However, this approach does not work on Chrome. I tried key{down|press|up}native but that does not help, either. Using type command does not work, because it is not simulating the real typing, it is just setting the input element's value. I know there is a bug in Chromium where one is not able to dispatch key events successfully, but I wasn't sure does Selenium works on javascript level, or maybe on window level. Is this an obstacle I can't get over?
Thanks!
I don't know how you are using Selenium but if you are using it through Selenium RC API you can use normal type (which copies the string into the field). After that try using something like fireEvent("yourstringlcoator", "KeyUp"). This has worked for me in a situation when I wanted the keyUP event triggered (this was with jQuery datatables actually).

jQuery Mobile disables Mobile Safari's AutoFill

this is specifically about MOBILE SAFARI. let's not discuss other browsers.
alright, i've got a form that works with Safari's AutoFill on the mac, it also works in a Mobile Safari if i remove jQuery Mobile. however, as soon I have jQuery Mobile firing, the AutoFill button/feature in mobile Safari stops responding. the "AutoFill" button is greyed out.
can anyone explain this?
thanks
** UPDATE / ~ANSWER **
jQuery Mobile [v1.0a4.1] just up and sets:
this.setAttribute("autocomplete", "off");
on
var textInputs = allControls.filter( "input[type=text]" );
...which makes all the difference.
see: https://github.com/jquery/jquery-mobile/issues/785
I believe I read that this was updated in version 1.1, and this was fixed.
If its not, there's no reason why you shouldn't be able to reset it where you need it, either on a per page basis, or globably ...
PER PAGE:
$(document).delegate('yourpage','pageinit',function(){
$(‘input[type=text]‘).attr(‘autocomplete’,'on’);
});
GLOBAL:
$(document).bind('mobileinit',function(){
$(‘input[type=text]‘).attr(‘autocomplete’,'on’);
})
autocomplete on fix came from http://ranservices.com/2011/11/jquery-mobile-breaks-autofill-on-iphone/

cannot do playback in iMacro: element not found

Excuse me, I have tried to use iMacro in FireFox to do playback.
However, it seems that during the playback, it try to click a button before the page finish loading such that the playback stop and iMacro complains that the element (i.e. the button )cannot be found.
I have encountered similar problem in Selenium and have used "clickAndWait" command to solve such kind of problem in Selenium.
May I ask in iMacro, does it also have command such as the "clickAndWait" in selenium for us to prevent the problem?
Or should we use any other method to deal with it in iMacro?
THank you very much
You can use
WAIT SECONDS=x