WebDriver SwitchTo : Browser pop up not being detected - javascript

The type of pop up i'm trying to use is here
http://demos.telerik.com/aspnet-ajax/window/examples/radopen/defaultcs.aspx
I'm trying to just launch the pop up. And enter text in the pop up
I have this page object attempting to handle popups
var popUp = function() {
browser.getAllWindowHandles().then(function(handles)
{
this.originalWindow = handles[0];
this.newWindow = handles[1];
})
this.switchToPopUp = function() {
browser.switchTo().window(this.newWindow);
}
this.switchToOriginal = function() {
browser.switchTo().window(this.originalWindow);
}
}
module.exports = new popUp();
In my test I have these lines
openRadWindow.click();
popUp.switchToPopUp();
browser.element(by.id('ctl00_SearchTextBox').sendKeys("Automation Test List");
The error I'm seeing in the trace is
WebDriver.switchTo().window(undefined)

Thanks for leading me to the right answer. In my case there is an Iframe brought into the popup so I had to do
switchTo.frame('framename')

Related

Firefox - website takes me to new tab automatically and I cannot stop it

So I have a website where I can select links and click a button to open them all at the same time. When I do that Firefox takes me to one of the newly opened links automatically.
I wanted to stop this behavior, so I looked and looked, and eventually found this option:
browser.tabs.loadDivertedInBackground
Now, when I set this to true, newly opened tabs never automatically take me to them. So if I click an ad on a site that normally opens in a new tab and takes me to it, now it doesn't happen. I also tried this code:
<p><a href="#" onclick="window.open('http://google.com');
window.open('http://yahoo.com');">Click to open Google and Yahoo</a></p>
This code opens 2 links at the same time. I was thinking maybe opening multiple links at the same time somehow overrides Firefox. But no, the links opened and I was not automatically taken to any of the new tabs.
Also must be said that I'm having this problem in Firefox 75 and 74. But when I try it in Firefox 55.0.2, I don't have the problem. In Firefox 55.0.2 the "browser.tabs.loadDivertedInBackground" actually works even on the website where I have the problem (I can't share the site because it's behind login).
This appears to be the code responsible to open multiple links on the website I have an issue with:
$(document).on('click', '.statbtn', function () {
var me = $(this);
var isAnyRowSelected = false;
$('.row-checkbox').each(function () {
var t = $(this);
if (t.is(':checked')) {
isAnyRowSelected = true;
$('select[name="status[' + t.val() + ']"]').val(me.attr('id'));
}
});
if(isAnyRowSelected == false){
bootbox.alert("No Orders Selected");
}
});
$(document).on('click', '.openlink', function () {
var me = $(this);
var isAnyRowSelected = false;
$($('.row-checkbox').get()).each(function () {
var t = $(this);
if (t.is(':checked')) {
isAnyRowSelected = true;
console.log();
var win = window.open(t.data('link'), '_blank');
if (win) {
win.focus();
} else {
bootbox.alert('Please allow popups for this website');
}
}
});
So I tried everything I could think of. Many changes to the about:config, restarting my browser, unticking the "When you open a link in a new tab, switch to it immediately" option in Firefox. But nothing works. When I open links from this one site using this specific button, I always get automatically taken to one of the newly opened tabs.
Here is a similar-ish problem - https://www.reddit.com/r/firefox/comments/bnu6qq/opening_new_tab_problem/
Any ideas why this happens and how to fix it? I mean, a website shouldn't be able or allowed to override Firefoxe's native setting, right?
Okay, because I don't wanna be an ass, here is the solution.
$(document).on('click', '.statbtn', function () {
var me = $(this);
var isAnyRowSelected = false;
$('.row-checkbox').each(function () {
var t = $(this);
if (t.is(':checked')) {
isAnyRowSelected = true;
$('select[name="status[' + t.val() + ']"]').val(me.attr('id'));
}
});
if(isAnyRowSelected == false){
bootbox.alert("No Orders Selected");
}
});
$(document).on('click', '.openlink', function () {
var me = $(this);
var isAnyRowSelected = false;
$($('.row-checkbox').get().reverse()).each(function () {
var t = $(this);
if (t.is(':checked')) {
isAnyRowSelected = true;
console.log();
// var win = window.open(t.data('link'), '_blank');
setTimeout(() => window.open(t.data('link'), '_blank'),1000);
// if (win) {
// win.focus();
// } else {
// bootbox.alert('Please allow popups for this website');
// }
}
});
if(isAnyRowSelected == false){
bootbox.alert("No Orders Selected");
}
});
Basically, adding a "setTimeout" fixed it. For some reason Firefox needed the delay to process things correctly, I guess, I think. Before the delay, the actions would happen instantly, and I'll just guess that Firefox couldn't "catch up" to it in order to apply the exemption of not navigating to new tabs. But a timeout delay fixed it.
And for anyone that may run into this with a similar issue, it also required an edit in Firefox in "about:config" to set this to True.
browser.tabs.loadDivertedInBackground
That's all folks :)

IE11 - Popup window launched from script will not close using JavaScript

I am relatively new to JS so apologies for any basic errors I have made here.
I am attempting to insert some JS on our site that will detect if the user has Flash enabled & the site is able to launch a popup, if these fail the user will be directed to a support page to resolve these.
The code works without issue on Chrome and Firefox, the issue I am having is on IE the popup which launches as a test is not being closed by the script.
Am I missing something glaringly obvious?
function loadpopunder(){
var popupBlockerChecker = {
check: function(popup_window){
var _scope = this;
if (popup_window) {
if(/chrome/.test(navigator.userAgent.toLowerCase())){
setTimeout(function () {
_scope._is_popup_blocked(_scope, popup_window);
},250);
}else{
popup_window.onload = function () {
_scope._is_popup_blocked(_scope, popup_window);
};
}
}else{
_scope._displayError();
}
},
_is_popup_blocked: function(scope, popup_window){
if ((popup_window.outerHeight > 0)===true)
popup.close();
},
_displayError: function(){
popupFail=true;
}
};
var popup = window.open("http://www.google.com", '_blank', "width=10, height=10, left=1, top=1, scrollbars=no, resizable=no");
popupBlockerChecker.check(popup);
}
loadpopunder()

Pop Up Blocker in Chrome and IE

Below is the piece of code I am using to open a link in a new window say "abc".
If the user again clicks on the same link, it should close and reopen the link in the same window "abc".
window.openOrFocus = function(url, "abc") {
if (!window.popups) {
window.popups = {};}
if (window.popups["abc"]){
var v=window.open("", "abc");
v.close();}
window.popups["abc"] = window.open(url, "abc");
}
But Now, say I click on the link, it opens the URL in a new window named "abc".
Now I go and close the window "abc". and go back and again click on the link.
That time it shows up the pop up blocker.
I am confused as to why this pop up blocker is coming when the I go and manually close the window and try to reopen by clicking on the link.
Happens both in IE as well as Chrome
Probably because you're calling window.open with a blank URL or repeatedly in that case.
You don't need your window.open("", "abc") call; instead, just use the window reference you already have:
window.openOrFocus = function(url, windowName) {
if (!window.popups) {
window.popups = {};
}
if (window.popups[windowName]){
window.popups[windowName].close();
}
window.popups[windowName] = window.open(url, windowName);
};
I would also listen for the unload event so you can remove your reference:
window.openOrFocus = function(url, windowName) {
if (!window.popups) {
window.popups = {};
}
if (window.popups[windowName]){
window.popups[windowName].close();
}
window.popups[windowName] = window.open(url, windowName);
window.popups[windowName].onunload = function() {
delete window.popups[windowName];
};
};
Side note: This is a syntax error:
window.openOrFocus = function(url, "abc") {
// --------------------------------^
I've replaced it with windowName in the code above.

Twitter log in pop up window blocked

I have a twitter autentication on my angularjs web site. The problem I came up with is that when I press "Sign in using twitter" the pop up window with twitter signing in content should appear, but this pop up is blocked. Browsing google didn't give any useful results. Here some of my code:
HTML:
<a ng-show="!user.twitterUserLogged" ng-click="TwitterLogIn()" ... >
<i class="fa fa-twitter-square"></i></a>
controller:
$scope.TwitterLogIn = function() {
if(!$scope.user.twitterUserLogged){
TwitterTweetManager.getToken().then(function(token) {
$scope.user.TwitterToken = token;
var url = 'https://api.twitter.com/oauth/authenticate?oauth_token=',
params = 'location=0,status=0,width=800,height=600';
var twitter_window = window.open(url + $scope.user.TwitterToken.oauth_token,"twitter_window",params);
//if (twitter_window) {
var interval = window.setInterval(function() {
if (twitter_window.closed) {
window.clearInterval(interval);
$scope.TwitterLoginFinish();
}
}, 1000);
//}
});
}
};
Thank You!
Found some information about window.open() function. Is is true, that pop-up blockers will allow only onClick event? Because I have ng-click in my case. Could it be the core of a problem?
I found what the problem is. It was about asynchronous event on my ng-click. I was getting a token from server on my click event, in this case pop-up blockers will block your popping window. So now I'm getting a token before my event like this:
var TwitterToken = TwitterTweetManager.getToken().$object;
And my click event:
$scope.TwitterLogIn = function() {
if(!$scope.user.twitterUserLogged && TwitterToken){
$scope.user.TwitterToken = TwitterToken;
var url = 'https://api.twitter.com/oauth/authenticate?oauth_token=',
params = 'location=0,status=0,width=800,height=600';
var twitter_window = window.open(url + $scope.user.TwitterToken.oauth_token,"twitter_window",params);
var interval = window.setInterval(function() {
if (twitter_window.closed) {
window.clearInterval(interval);
$scope.TwitterLoginFinish();
}
}, 1000);
}
};

Avoid browser print dialog with javascript

is there any way to capture the browser's print event and cancel the appearance of the print dialogue?
Example:
User clicks in "File" -> "Print".
Before the print dialog appears, show a confirm() like 'It is possible that not all data is printed, continue anyway? <accept> <cancel>
User clicks <accept> -> Print dialog appears
User clicks <cancel> -> Print dialog doesn't appear
Right now I have this:
var beforePrint = function(ev) {
if (!messageShown) {
var result = confirm('Are you sure?');
if (!result) {
// TODO: Cancel
}
}
};
var afterPrint = function(ev) {
// TODO
};
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}
window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
You don't need to cancel it. Just call window.print() if they click on accept. If they use cntrl-p or file->print that's beyond your control anyway programmatically. BTW it should be noted that you actually can't do anything to prevent the user from manually trying to print. That includes intercepting their attempt to spawn the dialog and stop it from showing. The best you can do is apply a stylesheet to printing by default and then dynamically change that print stylesheet to enable the content's visibility when they use your code to print.

Categories

Resources