How to show the current URL in protractor debug - javascript

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.

Related

Generated screenshot for login page is blank

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 :)

Phantomjs not loading the correct url inside onResourceRequested

I have a simple script based off Phantomjs's Network Monitoring example that tries to log all the requests and the response headers associated with a particular website:
"use strict";
var page = require('webpage').create(),
system = require('system'),
address;
if (system.args.length === 1) {
console.log('Usage: getRequests.js <some URL>');
phantom.exit(1);
} else {
address = system.args[1];
console.log('The url entered is ' + address + '\n')
page.onResourceReceived = function(response) {
console.log('Received Request: ' + JSON.stringify(response.headers, undefined, 4) + '\n');
};
page.onResourceRequested = function (req) {
console.log('Request URL ' + JSON.stringify(req.url, undefined, 4) + "\n");
}
page.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};
page.open(address);
}
However, when I try to run it, using ./phantomjs getRequests.js www.google.com (or any other webpage), it gives the error:
The url entered is www.google.com
Request URL "file:///home/myusername/scripts/www.google.com"
Unable to load resource (#1URL:file:///home/myusername/scripts/www.google.com)
Error code: 203. Description: Error opening /home/myusername/scripts/www.google.com: No such file or directory
Received Request: []
Basically, the url that I am entering on the terminal is being prepended with the path of the script file. The input that I am getting inside the address variable is perfectly fine i.e. google.com
Could anyone please help with this issue? I cannot understand why phantomjs maybe doing this. I am running Ubuntu 14.04 on VirtualBox. And the script was running fine before when I was just trying to output the requests for a particular page.
Thanks.
You'll probably laugh, but the correct way to use the script is
./phantomjs getRequests.js http://www.google.com

CasperJS/PhantomJS chokes on loading a page

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

Apache Cordova -- Download file from local web directory to Android/iOS directories?

having a bit of trouble with my download function for Cordova. I have a function here:
document.addEventListener("deviceready", function () {
var fileUrl = e.target.getAttribute('data-soundurl');
console.log(fileUrl);
//returns as "../www/card-sounds/sound2.m4a"
var fileOutputPath = e.target.getAttribute('data-quote') + ".m4a";
console.log(fileOutputPath);
//returns as "Sound Title.m4a"
var fileTransfer = new FileTransfer();
var output = "file:///android_asset/www/res/db/"+fileOutputPath;
fileTransfer.download(
fileUrl,
output,
function(entry) {console.log("download complete: " + entry.toURL());},
function(error) {console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false,
{headers:{"Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="}}
);
console.log(output);
//returns as "file:///android_asset_www_res/db/Sound Title.m4a"
}, true);
And my log output just shows:
download error source null
download error target null
upload error codenull
I can't seem to get any sort of .root part of the FileSystem to actually give me a defined string so I'm just trying to get it to download to Android for now. Am I doing something fundamentally wrong? I am so confused here.
Try this :
var output = "/res/db/"+fileOutputPath;
For your output use
var output = cordova.file.externalRootDirectory + fileOutputPath;
cordova.file.externalRootDirectory should give you something like "file:///storage/emulated/0/"

Catch script error - type,line and file

window.onerror = function(type, file, line){
if(type) {
console.log(type);
}
if(file) {
console.log(file);
}
if(line) {
console.log(line);
}
}
this code returns "Script error" when there is an error at some of the .js files. I need the type, file and line of the error. How can I get it?
When window throws error this script works perfect but it is not the same when there is error in the .js file.
I know that these things I can find on the console but imagine that I don't have one and i cannot install.
window.onerror = ErrorLog;
function ErrorLog (msg, url, line) {
console.log("error: " + msg + "\n" + "file: " + url + "\n" + "line: " + line);
return true; // avoid to display an error message in the browser
}
The post of Cryptic "Script Error." reported in Javascript in Chrome and Firefox should answer your "Script Error." problem. Namely it is probably caused by "Same origin policy".
Though I am still looking for why webkit will give me "undefined" file name and "0" line number for uncaught exception.
Here's what I use to capture errors. I have it request an image whose url points to a server side script.
function logJSErrors(errObj) {
if (!errObj || !errObj.lineNumber || errObj.lineNumber == 0) {
return; // can't use it any way.
}
if (window.location && window.location.toString().indexOf('rfhelper32.js') >= 0) {
return; // ignore the stupid Norton/Firefox conflict
}
var img = new Image();
img.src = "/jserror?m=" + encodeURIComponent(errObj.message) +
"&location=" + encodeURIComponent(window.location) +
"&ln=" + encodeURIComponent(errObj.lineNumber) +
"&url=" + encodeURIComponent(errObj.fileName) +
"&browser=" + encodeURIComponent(errObj.browserInfo);
}
window.onerror = function (msg, url, line) {
logJSErrors({ message : msg,
lineNumber : line,
fileName : url,
browserInfo : window.navigator.userAgent
});
// if a jquery ajax call was running, be sure to make the spinning icons go away
if (jQuery) {
try {
jQuery.event.trigger("ajaxStop");
} catch(e) {/* do nothing */
}
}
};

Categories

Resources