I'm trying to use PhantomJS 2.1.1 to update my profile picture on facebook. I'm able to log in, and navigate to the page where I should upload the picture, but page.uploadFile is just not working.
The last screenshot (page.render('end.png')) shows facebook alert "Please choose file to upload" confirming that page.uploadFile didn't work.
Any idea on how I can achieve this?
upload_page2.png
end.png
var args = require('system').args;
var page = require('webpage').create();
page.open('https://m.facebook.com', function(status){
if(status === "success"){
setTimeout(function(){
page.evaluate(function (args) {
document.getElementsByName("email")[0].value = args[1];
document.getElementsByName("pass")[0].value = args[2];
document.getElementById("loginbutton").click();
}, args);
}, 2000);
setTimeout(function(){
page.render('logged.png');
page.open('https://m.facebook.com/photos/upload/?profile_pic&upload_source=profile_pic_upload&profile_pic_source=tagged_photos_page', function(status){
});
}, 4000);
setTimeout(function(){
page.render('upload_page1.png');
page.uploadFile('input[name=file1]', 'http://proprofs-cdn.s3.amazonaws.com/images/games/user_images/misc/4288844624.png');
}, 6000);
setTimeout(function(){
page.render('upload_page2.png');
page.evaluate(function (args) {
var a = document.querySelectorAll('[type="submit"]');
a[0].click();
}, args);
}, 10000);
} else {
console.log("Error opening url \"" + page.reason_url + "\": " + page.reason);
}
setTimeout(function(){
page.render('end.png');
phantom.exit();
}, 15000);
});
Related
I also encounter a problem with a project, login with facebook works on absolutely any browser, even those on mobile, but in the integrated browser in the facebook application it doesn't work, it just doesn't connect me, it sends me back to login ... can you help me with a piece of advice please? Thank you.
I used this script:
<script>
(function(){
var body = $('body');
var socialLoginErrorElm = $('#loginError');
var loginModal = $('#loginModal');
body.on('social-login:error', function(e, error) {
socialLoginErrorElm.removeClass('hide').html('<div class="alert alert-danger">' + error + '</div>');
loginModal.removeClass("logging-in");
});
window.loginWithFb = function(){
FB.login(function(response) {
if (response.authResponse) {
if(response.authResponse.grantedScopes.split(',').indexOf('email') < 0) {
//If email permission not granted
body.trigger('social-login:error', (__('fbNoEmailError')));
return;
}
FB.api('/me', {fields: 'id,name,email'}, function(response) {
console.log('Logged in as ' + response.name + '.');
//Dual check email - needed to if check if the user has a verified email ID
if(!response.email) {
body.trigger('social-login:error', (__('fbNoEmailError')));
return;
}
body.trigger('loggedIn:fb');
});
} else {
body.trigger('social-login:error', (__('fbPermissionError')));
}
}, {
scope: 'email',
auth_type: 'rerequest',
'return_scopes': true
});
}
var body = $('body');
body.on('click', '[data-action="loginWithFB"]', function(e){
loginWithFb();
e.preventDefault();
});
body.on('loggedIn', function(){
loginModal.modal('hide');
});
body.on('loggedIn:fb', function(){
if(!User.isLoggedIn()) {
$.get(BASE_PATH + '/login/fb').success(function(response){
User.setData(response.user);
}).fail(function(jqXHR, textStatus, errorThrown){
body.trigger('social-login:error', jqXHR.responseText);
}).always(function(){
loginModal.removeClass("logging-in");
});
}
});
body.on('prompt-login', function(e, message){
loginModal.find('.login-prompt-message').html(message);
loginModal.modal('show');
});
})();
function showNewPointsAlert(addedPoints) {
var alertOptions = {
title: "+"+ addedPoints +" " + __('points'),
text: __('earnedNewPointsMessage'),
imageUrl: "{{LeaderboardHelpers::getPointsIcon()}}",
confirmButtonText: __('earnedNewPointsOkayBtnText'),
allowEscapeKey: true,
allowOutsideClick: true,
customClass: 'new-points-alert'
}
#if(!empty($mainBtnColor))
alertOptions.confirmButtonColor = '{{{$mainBtnColor}}}';
#endif
swal(alertOptions);
}
$('body').on('user-activity-recorded', function() {
$.get('{{route('getMyPoints')}}').success(function(response) {
if(response && response.points) {
var oldPoints = parseInt(User.data.points);
var newPoints = parseInt(response.points);
User.data.points = newPoints;
User.setData(User.data);
if(oldPoints != newPoints) {
var animateClass = 'animated bounceIn';
$('#headerUserMenu').removeClass(animateClass).addClass(animateClass);
var addedPoints = parseInt(newPoints) - parseInt(oldPoints);
#if(MyConfig::isTrue('leaderboard.showNewPointsAlert'))
showNewPointsAlert(addedPoints);
#endif
}
}
}).fail(function() {
});
});
</script>
I have written a basic after hook in cucumberjs for some reason , it is not working as expected.It is supposed to attached screenshot and write browser console log , when scenario fails. But it attaches the screen shot after the feature in html report and prints the browser console log at after in between the second scenarioenter image description here.Any clue what's wrong??
this.After(function(scenario, callback) {
if (scenario.isFailed()) {
global.browser.takeScreenshot().then(function(base64png) {
var decodedImage = new Buffer(base64png,'base64').toString('binary');
scenario.attach(decodedImage, 'image/png');
});
global.browser.manage().logs().get('browser').then(function (browserlog){
browserlog.forEach(function (log) {
if (log.level.value > 900) {
console.error(log.message.substring(log.message.indexOf('Error'),log.message.indexOf('\n')))
}
})
});
callback();
} else {
callback();
}
});
According to the cucumberjs github page https://github.com/cucumber/cucumber-js#attachments
Images and other binary data can be attached using a stream.Readable. In that case, passing a callback to attach() becomes mandatory:
You could split the single after hook into two separate hooks:
this.After(function(scenario, next) {
browser.takeScreenshot().then(function(png) {
var decodedImage = new Buffer(png, 'base64').toString('binary');
scenario.attach(decodedImage, 'image/png', next);
}, function(err) {
next(err);
});
});
this.After(function(scenario, next) {
global.browser.manage().logs().get('browser').then(function (browserlog){
browserlog.forEach(function (log) {
if (log.level.value > 900) {
console.error(log.message.substring(log.message.indexOf('Error'),log.message.indexOf('\n')))
}
});
});
});
Is the first time that I'am using this forum.
My problem is that I want to close a fancybox to open another fancybox. And for that I am using in my code the parent.$.fancybox.close() to close the Fancybox Iframe Window. When the user click in the Add to cart button. The fancybox close and my program in js STOP running. Example:
$(x).click(function(){
parent.$.fancybox.close();
idCombination = $('#idCombination').val();
$.ajax({
type: "POST",
url: baseUri + '?rand=' + new Date().getTime(),
data: 'controller=cart&add=1&ajax=true&qty=' + $('#quantity_wanted').val() + '&id_product=' + id_product + '&token=' + static_token + ( (parseInt(idCombination) && idCombination != null) ? '&ipa=' + parseInt(idCombination): ''),
beforeSend: function() {
$('#loading_img').show();
},
success: function(q) {
$.ajax({
type: "POST",
url: path + "ajaxcart/pop_up.php",data:'referer='+document.referrer,
beforeSend: function() {
$(".shop").show();
$('#loading_img').show();
},
success: function(res) {
$(".shop").show();
$("#shopping").show();
$('#loading_img').hide();
$('#shopping').html(res);
}
});
}
});
$("html, body").animate({scrollTop: 300}, "slow");
return false;
});
I'm using Backbone, Require and Underscore Bundled with Phonegap or Cordova.
I tried using the childbrowser plugin but it wont work. I followed the instructions here.
http://blog.digitalbackcountry.com/2012/03/installing-the-childbrowser-plugin-for-ios-with-phonegapcordova-1-5/
define([
'jquery',
'backbone',
'underscore',
'base64',
'mobile',
'const',
'child',
'text!template/login/login.tpl.html'
],function($, Backbone, _, base64, Mobile, Const, ChildBrowser, template){
var EncodeAuth = function(user,pass)
{
var _tok = user + ':' + pass;
var _hash = Base64.encode(_tok);
return "Basic "+ _hash;
}
var LoginView = Backbone.View.extend({
events:{
"click .login-btn" : "Login",
"click .connection-btn" : "OpenSite"
},
initialize: function(){
},
Login: function(){
Const.USERNAME = $("#username").val();
Const.PASSWORD = $("#password").val();
if(!Const.USERNAME || !Const.PASSWORD)
{
navigator.notification.alert("Invalid Username/Password!");
$("input").val("");
}else{
var auth = EncodeAuth(Const.USERNAME,Const.PASSWORD);
var sendAuthorization = function (xhr) {
xhr.setRequestHeader('Authorization', auth)
};
this.model.save(this.model, {
beforeSend : sendAuthorization,
success: function(model,result){
if(result.ErrorMessage === null)
{
alert(JSON.stringify(result.Message));
$("input").val("");
}
else
{
alert(JSON.stringify(result.ErrorMessage));
$("input").val("");
}
},
error: function(model,result){
alert("Remote server returned an error. Not Found");
$("input").val("");
}
});
}
},
OpenSite: function(){
window.plugins.ChildBrowser.showWebPage("http://www.google.com");
}
});
return LoginView;
});
Any ideas?
I want to get first name, last name, email when user login with facebook. My app is in Cordova. I am using cordova plugin for facebook authentication this one "https://github.com/Wizcorp/phonegap-facebook-plugin". I can login with Facebook but how can I get name, email from it.
Thanks in advance.
add facebook cordova plugin using below command See Details
cordova plugin add https://github.com/Wizcorp/phonegap-facebook-plugin --variable APP_ID="YOURAPPID" --variable APP_NAME="YOURAPPNAME"
html code
<div onclick="fblogin();"><img src="img/facebook.png" /></div>
Javascript code
<script>
function fblogin(){
facebookConnectPlugin.login( ["public_profile","email"],
function (response) {
if(response.authResponse.userID!=''){
facebookConnectPlugin.api(response.authResponse.userID+"/?fields=id,email,first_name", ["public_profile"],
function (response) {
console.log('SUCCESS:'+response);
alert('first_name : '+response.first_name+',email:'+response.email+',id:'+response.id);
},
function (response) {
console.log('ERROR:'+response);
});
}
},
function (response) {
console.log('ERROR:'+response);
});
}
</script>
Try this Login Function with below Snippet.
var fbLogin = function() {
facebookConnectPlugin.login(["public_profile", "email", "user_birthday", "user_location"],
function(response) {
console.log("Login Response :" + JSON.stringify(response));
//alert("Login Response :" + JSON.stringify(response))
this.authId = response.authResponse.userID;
if (response.status == "connected") {
facebookConnectPlugin.api("/" + response.authResponse.userID, ["public_profile", "email", "user_birthday", "user_location"],
function(result) {
this.email = result.email;
this.firstname = result.first_name;
this.lastname = result.last_name;
this.birthdate = result.birthday;
this.city = result.location.name;
},
function(error) {
alert("Failed: " + error);
});
}
},
function(response) {
alert("Other Response : " + JSON.stringify(response))
});
}