How to end an endless stream of alert messages in PhantomJS - javascript

How can I close endless alert in PhantomJS?
a website has endless alert
I used
page.onAlert = function(msg) {
console.log('ALERT: ' + msg);
};
to check whether the website has alert
but this method you will never stop because that's an endless alert

You don't have to print all alerts that the page you're visiting sends your way. Simply remove the handler to silence them:
page.onAlert = function(msg) {
console.log('ALERT: ' + msg);
page.onAlert = function(){};
};
This prints only the first alert. You can make this more sophisticated by adding counting alerts or something like that.

Try this for every reloaded page that would have an alert later.
driver.execute_script("window.confirm = function(){return true;}");
See more reference here.

Related

Why does printing this variable show a prompt, instead of an error message?

Please why does console.log(theMessage) show a popup prompt, instead of: "You typed" + textTypedIntoPrompt.
Or even some error message related to it?
New to programming and just trying to understand why this program works the way that does.
Thanks.
var textTypedIntoPrompt = prompt ("Type some text");
var theMessage = "You typed" + textTypedIntoPrompt;
console.log(theMessage);
it's not the console.log which open the popup prompt, it's the prompt function
the log is then done in the console as shown in the snippets below
var textTypedIntoPrompt = prompt("Type some text");
var theMessage = "You typed : " + textTypedIntoPrompt;
console.log(theMessage);
here's a detailled explanation of what happens
var textTypedIntoPrompt =
// create a variable named textTypedIntoPrompt
prompt("Type some text");
// open the popup prompt
// the popup prompt freeze the execution until the prompt was confirmed or dismissed
// once the prompt is confirmed/dismissed the function returns
// either what the user wrote (if confirmed) or null (if dismissed)
// when you're here textTypedIntoPrompt contains the input
// and your browser already forgot it came from a prompt
var theMessage =
// then you create a variable named theMessage
"You typed : " + textTypedIntoPrompt;
// and you assign to it the string 'you typed : ' followed by the value of textTypedIntoPrompt
// (which is the return value of prompt)
console.log(theMessage);
// finally you print this string into the console
// use ctrl+shift+k to open the console in most browsers
and finally a snippet proving console.log doesn't open a prompt
console.log("didn't open a prompt :)")
while this one proves prompt does open a prompt (as it's name is telling us)
prompt("I opened a prompt", "you can also set a default value")
// the return value is not saved into a variable so it is lost

Detect a console.warn message

I have a need to find out when a warning (not error) message is sent to the browser's console.log . From research, I know that I can detect error messages with JavaScript code similar to this:
window.onerror = function(errorMsg, url, lineNumber){
// any action you want goes here
// errorMsg is the error message itself.
// url should be the file presenting the error, though i have
// found that it only presents to me the address of the site.
// lineNumber is the line number the error occoured on.
// here is an example of what you could do with it:
alert("Error in " + url + " at " + lineNumber + ":\n" + errorMsg);
}
But that only shows error messages. It will not show any warning messages.
Is there a similar way to detect a specific warning message in the console.log? Or, alternately, is there some JS code that will allow me to search the console.log for a specific string?
Note that I can see the warning message in the browser's debugging (console logging) window. But I want to 'intercept' the warning message and perform some action if that warning is found.
Added
The intent is to find out when a specific warning message occurs; the message being similar to "Loading failed for the <script> with source "https://www.example.com/js/somescript.js" .
But the question of how to intercept a warning message (not error; the code I included does that) that is output by the browser (not my code).
Added
So, here's what I need. Assume this code
console.warn("Here is a warning!");
What code would you write to see if that message was written to the console as a warning?
The warnings you are talking about are generated by the browser, not console.warn. This is why you cannot override it. Most likely, you need to manually add listeners for each event you need. For example, if you want to handle a script loading error use the onerror event:
<script src="https://www.example.com/js/somescript1.js" onerror="console.log('Cannot load script from ' + this.src)"></script>
<script src="https://www.example.com/js/somescript2.js" onerror="console.log('Cannot load script from ' + this.src)"></script>
How about overriding the console.log()?
let tmpConsoleLog = console.log;
console.log = function(msg){
// Intercept code goes here using msg variable
alert(msg);
// then perform the normal logging
tmpConsoleLog(msg);
}
console.log("Something");
It's very late, but here's an answer that actually directly answers your question.
var originalConsoleWarn = console.warn;
console.warn = function(message) {
doSomethingWithWarn(message);
originalConsoleWarn(message);
};
function doSomethingWithWarn(message){
console.log("DOING SOMETHING WITH: " + message);
}
console.warn("hi");

CasperJS running not showing output to console

I'm running Casperjs from the cmd, and while everything seems to work ok (the script runs as expected),
It won't show the echo command in the cmd without me pressing a key (return).
Here's the code:
var casper = require('casper').create();
casper.start('http://casperjs.org/');
casper.then(function() {
this.echo('First Page: ' + this.getTitle());
});
casper.thenOpen('http://phantomjs.org', function() {
this.echo('Second Page: ' + this.getTitle());
});
casper.run();
And here's an image of the problem (won't show anything without me pressing return):
After I hit return twice:
Thanks!
Well, I found a work around so to say...
I created a batch file and ran it and it worked.
I've created something like this:
echo Testing something...
casperjs test1.js

Why does phantom.exit() have a 2 second delay?

I noticed that for a simple script like:
var url = "http://stackoverflow.com";
var page = require('webpage').create();
page.onConsoleMessage = function(msg) {
console.log('Page title is ' + msg);
};
page.onLoadFinished = function(status) {
console.log('Status: ' + status);
};
page.open(url, function(status) {
page.evaluate(function() {
console.log(document.title);
});
phantom.exit();
});
calling phantom.exit() will not exit immediately, rather it will wait 2 seconds before doing so. I'm using version 2.1.1.
Do you know where this delay comes from and how I can make phantom exit immediately? Thank you!
Look like "open issue":
https://github.com/ariya/phantomjs/issues/14033
I did try your code with the last version and I get the same behaviour.
You can use phantomjs#1.9.X as "jmullo" commented in the issue thread.
Like other browser, phantomjs is just headless. so it's exit() method will do many things, and it may cost couple of seconds.

Phantomjs check if javascript exists and is working

I am quite new to Phantomjs and am starting to getting to know how to use it. However all of the semi advanced tutorials does not cover the thing i want to use Phantomjs for.
Now my question is how would i check if a Javascript is active on the site and if it is working correcly (i.e not throwing erros in the console).
I hope someone is able to point me in the right direction or know how to do this.
you can interact with the open page using the webpage.evaluate method:
var page = require('webpage').create();
page.open('http://m.bing.com', function(status) {
var title = page.evaluate(function(s) {
//what you do here is done in the context of the page
//this console.log will appear in the virtual page console
console.log("test")
//here they are just returning the page title (tag passed as argument)
return document.querySelector(s).innerText;
//you are not required to return anything
}, 'title');
console.log(title);
phantom.exit(); //closes phantom, free memory!
});
in order to see the virtual page console, you have to add a onConsoleMessage callback:
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
EDIT: by default javascript is executed.

Categories