I'm running the following script with phantomjs:
var casper = require('casper').create();
var url = 'https://itunesconnect.apple.com/itc/static/login?view=1&path=%2FWebObjects%2FiTunesConnect.woa%3F'
casper.on("page.error", function(msg, trace) {
this.echo("Error: " + msg, "ERROR");
this.echo("file: " + trace[0].file, "WARNING");
this.echo("line: " + trace[0].line, "WARNING");
this.echo("function: " + trace[0]["function"], "WARNING");
errors.push(msg);
});
casper.start(url, function(){
casper.wait(7000, function(){
// casper.echo(casper.getHTML());
})
})
casper.run(function() {
if (errors.length > 0) {
this.echo(errors.length + ' Javascript errors found', "WARNING");
} else {
this.echo(errors.length + ' Javascript errors found', "INFO");
}
casper.exit();
});
Until a few days ago I could access the page which loads an iframe that contains 2 form fields, to allow user login.
Now I get the following error:
Error: Error: undefined is not a constructor (evaluating '$stateParams.path.startsWith('/')')
at setupDSiFrame (https://itunesconnect.apple.com/itc/static-resources/controllers/login_cntrl.js?cache=111920151100:99:46)
at https://itunesconnect.apple.com/itc/static-resources/controllers/login_cntrl.js?cache=111920151100:19:37
at $digest (https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100:13:11750)
at $apply (https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100:13:13237)
at f (https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100:12:56414)
at r (https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100:12:65848)
at onreadystatechange (https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100:12:66409)
file: https://itunesconnect.apple.com/itc/js/compiled/lib/vendor.js?cache=111920151100
line: 12
The page loads fine using slimerjs as the engine, but when using slimerjs the login form does not get filled in because the window is not in focus.
I believe this is an issue where casper is using an old version of WebKit and chokes on loading the page. How would I fix this?
Too late to solve the problem but maybe useful for future reference if somebody finds the question searching for a problem with startsWith in PhantomJS (as I did): startsWith method was added on the ECMAScript 6 specification, which is not supported by PhantomJS.
A good polyfill for this is mathiasbynens/String.prototype.startsWith
Related
I am trying blow code for creating screenshot using phantomJS 2.1.1 but getting blank screenshot. Can please tell me what i did wrong?
var page = require('webpage').create();
page.open('https://sigview.sigmoid.io/app/#/signIn',function({
setTimeout(function() {
page.render('sigview.png');
phantom.exit();
}, 1000);
});
I expected it will capture whole page of screenshot.
When I add:
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
Then I get:
CONSOLE: Google Analytics Initialized (from line # in "")
CONSOLE: jQuery.Deferred exception: undefined is not a function (evaluating 'Object.assign({},d,o)') t#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:8081643
o#https://sigview.sigmoid.io/app/scripts/scripts.e53141f8.js:1:8772726
invoke#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:464073
https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:458160
invoke#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:464073
https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:462631
getService#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:460540
injectionArgs#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:463648
instantiate#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:464197
https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:518515
createDetachedTour#https://sigview.sigmoid.io/app/scripts/scripts.e53141f8.js:1:11736460
https://sigview.sigmoid.io/app/scripts/scripts.e53141f8.js:1:3025948
invoke#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:464073
https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:462993
forEach#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:427302
createInjector#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:462942
doBootstrap#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:438797
bootstrap#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:439323
angularInit#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:438068
https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:722817
mightThrow#https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:237532
https://sigview.sigmoid.io/app/scripts/vendor.00e53c4a.js:1:238173 undefined (from line # in "")
I suspect the undefined errors make PhantomJs choke.
My lazy fix would be, not to use a deprecated library like PhantomJS and try https://github.com/GoogleChrome/puppeteer :)
I'm trying to detect when a console error occurs. I've looked here and have seen 2 suggestions
window.onerror
and
try catch
The following demonstrates an issue where this is not "caught" (by caught, as per the code below, I'd expect to see an alert and I do not).
window.onerror = function(error) {
alert("window error..." + error );
}
const arr = [];
try{
arr.push({
'firstValue':'Me 'And' You',
'otherValue':5
});
}catch(err){
alert("Error..." + err);
}
The part causing the issue is
'firstValue':'Me 'And' You',
The reason for this is how this line of code is generated and I suspect the real fix is to correct this (using MVC.NET Razor)
'firstValue': '#Model.MyStringWithNoFormattingOrChecking'
Where the value of MyStringWithNoFormattingOrChecking is Me 'And' You
However, my question is about why the onerror or try catch didn't work. Or what I could have done to have caught this using javascript (I don't actually show an alert, I log via Ajax)
Try putting the onerror code in its own script tag, which runs earlier than this syntax error.
This code does what you want
<head>
<script type="text/javascript">
window.onerror = function(error) {
alert("window error..." + error );
}
</script>
<script type="text/javascript">
const arr = [];
try{
arr.push({
'firstValue':'Me 'And' You',
'otherValue':5
});
}catch(err){
alert("Error..." + err);
}
</script>
</head>
[tested in Firefox, Chome and Edge]
I have a Phantomjs script that tries to open a url. phantomjs returns this error:
Unable to load resource (request ID:undefinedURL:http://foo.bar/tree/nav_value/27)
Error code: 203. Description: Error downloading http://foo.bar/tree/nav_value/27 - server replied: Not Found
But when I open the url http://foo.bar/tree/nav_value/27 with chrome browser, there's no problem and the page is loaded correctly!
This is the script:
// Read the Phantom webpage '#intro' element text using jQuery and "includeJs"
"use strict";
var page = require('webpage').create();
var system = require('system');
if (system.args.length != 2) {
console.log("please pass 2 argument")
}
var company_id = system.args[1]
console.log("c", company_id)
page.onConsoleMessage = function(msg) {
console.log("message", msg);
};
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (request ID:' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.onError = function(msg, trace) {
console.log("error", msg)
}
var nav_value;
page.open("http://foo.bar/tree/nav_value/27", 'post', 'username=navid&password=test', function(status) {
if (status === "success") {
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
page.evaluate(function() {
nav_value = parseInt($("#value").text());
});
phantom.exit(0);
});
} else {
phantom.exit(1);
}
});
EDIT:
Something odd happens. When I run this code with phantomjs on windows on another machine it works. But on Ubuntu it returns the error!
The url that phantomjs is trying to open is on the same server. (Ubuntu)
What is the problem?
Not sure this will help, but I have some ideas that helped me figure out problems with PhantomJS in the past.
First, as you say it works on another machine, you may want to test other versions of PhantomJS, by downloading the executable and specifying the path on your Python script. Version 1.9.8 helped me with bypassing some security restrictions in the past (I also left some settings in case it may interest).
driver = webdriver.PhantomJS(
executable_path='/path/to/the/downloaded/phantomjs19',
# you can specify args, such as:
service_args=[
'--ignore-ssl-errors=true',
'--ssl-protocol=any',
'--web-security=false',
],
# and also other capabilities:
desired_capabilities={
'phantomjs.page.settings.resourceTimeout': '5000',
'phantomjs.page.settings.userAgent': (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 "
"(KHTML, like Gecko) Chrome/15.0.87"
),
},
)
You may also try to see if upgrading Selenium helps.
pip install selenium --upgrade
Another idea that might help to understand what is happening is to try to print a screenshot and log the page source before the error happens. You can do it like:
# Set the window size to something appropriate for your tests.
driver.set_window_size(900, 800)
driver.save_screenshot('screen.png')
# Check if the page source matches your expectations.
with open('temp.html', 'w') as f:
f.write(driver.page_source)
Please, let me know if this helps!
I'm using protractor to test my angular application.
I want to show the current URL when a console.log is show in the browser.
I use this for now but I don't find the solution to show the current URL.
afterEach(function() {
browser.manage().logs().get('browser').then(function(browserLog){
var message = JSON.parse(browserLog[i].message).message.parameters[0].value;
expect(message.indexOf("localizationService") > -1).toBe(false, 'because\n There is a I18N error somewhere.\n Please see the error above in\n >>ERROR I18Ns : \n' + message + '\n' + browser.getCurrentUrl());
}
}
I just need to know what I have to use instead of browser.getCurrentUrl() which is returning a Promise
You can get the url by resolving the promise that getCurrentUrl() function returns and then expect your requirement. Here's how you can do it -
afterEach(function() {
browser.manage().logs().get('browser').then(function(browserLog){
var message = JSON.parse(browserLog[i].message).message.parameters[0].value;
browser.getCurrentUrl().then(function(url){
expect(message.indexOf("localizationService") > -1).toBe(false, 'because\n There is a I18N error somewhere.\n Please see the error above in\n >>ERROR I18Ns : \n' + message + '\n' + url);
});
}
}
Hope this helps.
I know this has probably been asked before, but I can't find where:
I know you can detect JS errors using extensions in stuff, but is there any way to detect ALL errors using JavaScript and display an alert whenever there is one?
In the browser define the window.onerror function. In node attached to the uncaughtException event with process.on().
This should ONLY be used if your need to trap all errors, such as in a spec runner or console.log/ debugging implementation. Otherwise, you will find yourself in a world of hurt trying to track down strange behaviour. Like several have suggested, in normal day to day code a try / catch block is the proper and best way to handle errors/exceptions.
For reference in the former case, see this (about window.error in browsers) and this (about uncaughtException in node). Examples:
Browser
window.onerror = function(error) {
// do something clever here
alert(error); // do NOT do this for real!
};
Node.js
process.on('uncaughtException', function(error) {
// do something clever here
alert(error); // do NOT do this for real!
});
For JS in Browser
<!DOCTYPE html>
<html lang="en">
<head>
<script>
var lastErr;
function errHand(e) {
lastErr = e;
switch (e.target.nodeName) {
case 'SCRIPT':
alert('script not found: ' + e.srcElement.src);
break;
case 'LINK':
alert('css not found: ' + e.srcElement.href);
}
return false;
}
window.onerror = function (msg, url, lineNo, columnNo, error) {
alert(msg + ' - ' + url + ' - ' + lineNo + ' - ' + columnNo);
return false;
}
</script>
<script src="http://22.com/k.js" onerror="errHand(event)"></script>
<link rel="stylesheet" href="http://22.com/k.css" onerror="errHand(event)" type="text/css" />
</head>
<body>
<script>
not_exist_function();
</script>
</body>
</html>
This works for attached js files trace errors just in same origin host environment
For Request error handeling like Ajax/WebSocket its better use their Bulit-In functions
Console functions override not work for reading auto generated browser error logs at this time with latest browser updates
For NodeJS
process.on('uncaughtException', function (err) {
console.error('uncaughtException:\n' + err.stack + '\n');
})
Use Both of them in the TOP of your codes