How to click on onbeforeunload prompt in Watir test? - watir

I have a webpage that has an onbeforeunload script that prompts the user when they take an action that would navigate away from the current page.
How do I interact with this popup using Watir? My current approach looks like this:
$ie.link(:text, 'Dashboard').click_no_wait
hwnd = $ie.enabled_popup(10)
assert(hwnd, 'The expected \'leave this page?\' popup was not shown')
win = WinClicker.new
win.makeWindowActive(hwnd)
win.clickWindowsButton_hwnd(hwnd, "OK")
The problem is that if I use "click no wait" the popup is not created, and the test times out. If I use "click" then the popup is created, but the test hangs after it opens.
Any suggestions?

Do you want to assert dialog message?
I try your code, but could not find solution.
This is to try to catch dialog then when popup,get msg while 5sec. polling per 1sec.
$ie.link(:text, 'Dashboard').click_no_wait
#autoit = WIN32OLE.new('AutoItX3.Control')
5.times do
if #autoit.WinWait('Windows Internet Explorer','',1)==1 then
#autoit.WinActivate('Windows Internet Explorer',"")
#dialog_text = #autoit.WinGetText('Windows Internet Explorer',"")
dialog_pop = "YES"
break
else
sleep(1)
dialog_pop = "NO"
end
end

This should do the trick closing this alert:
browser.alert.ok
See the Watir docs here.

Related

How to Know an Alert Pop-Up Has Occurred

I am automating a website. I am using SeleniumBasic in Excel VBA.
I have a simple Pop-Up, or Alert, with an OK button.
I cannot access the Pop_Up by using Inspect so I have no HTML to show.
When the Pop-Up occurs, I respond with Driver.SwitchToAlert.Accept. It goes away, as desired.
I don't know how to check programmatically if the Pop-Up is present.
If I assume that the Pop-Up may have occurred and use this code on a just-in-case basis, i.e. use the code without the Pop-Up existing, the program stops without any error message.
Is there a way to check if the Pop-Up window exists before I respond?
Well, I found the answer:
I got this code from someone who got it from someone else.
Private Function AlertIsPresent() As Boolean
'Returns true if an alert is present, false otherwise
Dim T As String
On Error Resume Next
T = Driver.Title 'This raises error 26 if an alert(pop-up) is displayed
AlertIsPresent = (26 = Err.Number)
End Function
If function returns 'true', use the Driver.SwitchToAlert.Accept to close it.

AutoIt v3: _IENavigate + _IELoadWait Not Working as Expected?

This code is supposed to open a new browser set at "www.website.com," submit a username and password, wait for the page to load after being submitted, navigate to a new page, and inject a javascript code into the address bar.
My current results from this code are opening a new browser set at "www.website.com," submit a username and password. The submit works, then from here, the code breaks and instead of navigating to the next page (page2) it just hangs.
Even when I add an ignore command to the _IEFormSubmit($oForm) I can't get my page to navigate to the next page.
#include <IE.au3>
#RequireAdmin
Local $oIE = _IECreate("http://www.website.com")
;_IELinkClickByText($oIE, "Sign In") ;Optional
Local $oForm = _IEFormGetObjByName($oIE, "regular-user-login-form")
Local $oText = _IEFormElementGetObjByName($oForm, "log")
_IEFormElementSetValue($oText, "username")
Local $oText = _IEFormElementGetObjByName($oForm, "pwd")
_IEFormElementSetValue($oText, "password")
_IEFormSubmit($oForm)
_IENavigate($oIE, "http://www.website.com/page2/")
Send("{F4}javascript:check_in();{ENTER}")
Please for the love of god, what am I doing wrong.
Edit: I've also changed the _IEFormSubmit($oForm) to another javascript submit and I can still log in without any problems, but once I reach that next page I can't use _IENavigate, so the problem has to lie there.
I have automated so many different pages and sometimes the script gets stuck on _IEFormSubmit for no reason. That's an AutoIt bug.
Her's a quick fix for that
_IEFormSubmit($oForm, 0)
_IELoadWait($oIE, 1000)
Your code should be:
#include <IE.au3>
#RequireAdmin
Local $oIE = _IECreate("http://www.website.com")
;_IELinkClickByText($oIE, "Sign In") ;Optional
Local $oForm = _IEFormGetObjByName($oIE, "regular-user-login-form")
Local $oText = _IEFormElementGetObjByName($oForm, "log")
_IEFormElementSetValue($oText, "username")
Local $oText = _IEFormElementGetObjByName($oForm, "pwd")
_IEFormElementSetValue($oText, "password")
_IEFormSubmit($oForm, 0)
_IELoadWait($oIE, 1000)
_IENavigate($oIE, "http://www.website.com/page2/")
Send("{F4}javascript:check_in();{ENTER}")
Onetime, I tried to report two bugs to autoit forum, but the guys who are there are pretty frustrated fellas. So, now when I find one, I just solve it my way.
Off topic:
When dealing with objects you should know of another bug
If $oInput.outertext = "Continue" then ...
The above code will sometime fail even if the outertext matches.
This is solved using, use
If $oInput.outertext == "Continue" then ...

want to auto click cancel in prompt in firefox

OK so i currently play a game that has a random "bot-check" pop up in
the tab in the pop up it has a "OK" and "cancel" button and I'm trying
to find or make a grease monkey script to auto click "cancel" when it
pop's up and since its random it needs to be a always on type script
the prompt always shows up in the same x and y co-ordinates and button 1 is always OK and button 2 is always cancel hitting escape works just the same as hitting cancel
ive tried google and i cant find anything
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js
$(".popup").bind('show',function(){
$(this).hide();
});
may work, depends on the popup type.
if it doesn't work, try this:
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js
$(".popup").bind('show',function(){
$(".popup .cancel).trigger("click");
});
[if JQuery wasn't successfully installed in your script previously, you'll need to uninstall your script and reinstall it]
edit: for imitation of pressing escape, use
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js
var esc = $.Event("keydown", {
keyCode: 27
});
$(".popup").bind('show',function(){
$('body').trigger(esc);
});

Closing a web page after submit

I want to close a document after someone has updated it.
I tried the following javascript in the submit button:
document.forms[0].submit();
window.close()
But the web-query-save agent doesn't fire then. But if I comment out the 'close', the WQS agent executes as expected but then I have the dreaded "Form Processed" page. I supposed I could but a nice page up but I'd like to just close the page. Any thoughts?
thanks
clem
At the end of your WebQuerySave Agent you can print a message to output some javascript.
Print |<script type="text\javascript">|
Print |window.close();|
Print |</script>|
I know this is 9 years old, but this solution didn't work for me.
Instead, I end my webquerysave agent with a Print statement back to the same document:
Print "[" + webDbPath + "/0/" + currdoc.Universalid + "?EditDocument&saveSuccess=true]"
I can then check for the saveSuccess in the URL on the onload and if found, close the window with: window.open('','_self').close();

Chrome extension icon change issue

I'm developing a Chrome extension and have a strange problem with icon change on-the-fly.
In my popup.html I have a button by clicking on which I want to change the icon of the extension in the browser. E.g. from colored one to black and white (when application is inactive).
So the function which is responsible for this:
function toggleActivated(){
localStorage.isActive = toBool(localStorage.isActive) ? false : true;
$('#activate-disactivate span').text(toBool(localStorage.isActive) == false ? 'Включить' : 'Выключить');
chrome.browserAction.setIcon({path: toBool(localStorage.isActive) ? '48.png' : '48_bw.png'});
//window.close();
}
But the problem is that I want to close popup after the icon changed. If I use the window.close() at the end - then the icon is not changed, but if it is commented out - then the icon is changed fine.
Why is there a conflict between chrome.browserAction.setIcon() and window.close()?
It sounds like chrome.browserAction.setIcon is getting fired asynchronously but the popup is getting closed before it finishes. You could try adding a 500ms setTimeout before closing.
You should also file a bug report at new.crbug.com.
I know this is old, but I was having the same problem and abraham was right when he said that chrome.browserAction.setIcon was being called asynchronously. But I don't think that setting a timeout is the best answer.
If you check the setIcon documentation you can see that this method takes a function callback as parameter. That's where you should call window.close(). Like this:
chrome.browserAction.setIcon({ path: icon_path }, function() {
window.close();
});
UPDATE:
As niraj.nijju pointed in the comment below, you can pass a tabId param to the setIcon function to limit the scope of the change.

Resources