CasperJS weird evaluate behavior - javascript

So, I have this code...
var config = require('./config.js');
var casper = require('casper').create(config.casper);
casper.on("page.error", function(msg, trace) {
this.echo("Page Error: " + msg, "ERROR");
});
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
});
function run () {
casper.start();
casper.thenOpen('https://twitter.com', function () {
if ( this.exists('form[action="https://twitter.com/sessions"].signin') ) {
this.echo('logging in...');
this.evaluate(function (username, password) {
document.querySelector('#signin-email').value = username;
document.querySelector('#signin-password').value = password;
document.querySelector('.flex-table-btn').click();
}, config.users.user.twitter.username, config.users.user.twitter.password);
} else {
this.echo('Alreaddy logged in, proceed...');
}
});
casper.waitForSelector('#tweet-box-mini-home-profile', function () {
if ( this.exists('#tweet-box-mini-home-profile') ) {
this.evaluate(function (text) {
document.querySelector('div#tweet-box-mini-home-profile.tweet-box.rich-editor.notie').innerText = text;
document.querySelector('button.btn.primary-btn.tweet-action.js-tweet-btn').click();
}, 'Test using automation');
this.wait(10000, function () {
this.echo ('Finished waiting, closing app now');
}); //wait for 10 seconds before closing
} else {
this.echo('Failed to logging in');
}
});
casper.run();
}
run();
The logging in part works just fine, I could logging in onto the dashboard.
But the second step, the tweeting step throw an error
Page Error: TypeError: 'null' is not an object (evaluating 'document.querySelector('div#tweet-box-mini-home-profile.tweet-box.rich-editor.notie').innerText = text')
and the tweet won't got posted. I've tried it manually on the browser and it works just fine.
var config = require('./config.js');
var casper = require('casper').create(config.casper);
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
});
casper.on("page.error", function(msg, trace) {
this.echo("Page Error: " + msg, "ERROR");
});
function run () {
casper.start();
casper.thenOpen('https://twitter.com', function () {
if ( this.exists('form[action="https://twitter.com/sessions"].signin') ) {
this.echo('logging in...');
this.evaluate(function (username, password) {
document.querySelector('#signin-email').value = username;
document.querySelector('#signin-password').value = password;
document.querySelector('.flex-table-btn').click();
}, config.users.dida.twitter.username, config.users.dida.twitter.password);
} else {
this.echo('Alreaddy logged in, proceed...');
}
});
casper.waitForSelector('#tweet-box-mini-home-profile', function () {
if ( this.exists('#tweet-box-mini-home-profile') ) {
this.evaluate(function (text) {
console.log('=========== Putting text ============');
document.querySelector('#tweet-box-mini-home-profile > div').innerHTML = text;
}, 'Test using automation');
} else {
this.echo('Failed to logging in');
}
});
casper.then(function () {
if ( this.exists('.js-tweet-btn') ) {
//recheck the tweet
this.echo(this.getHTML('#tweet-box-mini-home-profile'));
this.echo('============= Clicking Submit Button ==============');
this.click('.js-tweet-btn');
this.wait(10000, function () {
this.echo ('Finished waiting, closing app now');
}); //wait for 10 seconds before closing
} else {
this.echo('Submit button not found');
}
})
casper.run();
}
run();
this works, but it only tweet : "Compose new Tweet"
as if the text never change.
So my question is, did I do something wrong or this is some kind of bug? If so, is that a workaround? Thank you in advance.

First of all, use fillSelectors() or fillXPath() methods for filling forms.
And make sure that selector 'div#tweet-box-mini-home-profile.tweet-box.rich-editor.notie' is correct.

Related

jquery.js: Uncaught ReferenceError: connect is not defined

I run this code on a website with a database, this interacts with the ambiance.js , jquery.js (3.2.1) and socket.js but I believe this requires some other dependency that I omitted.
var SOCKET = null;
var user = null;
$(document).ready(function() {
connect();
});
function request(msg)
{
var m=msg;
if(m.type == 'aMessage')
{
console.log(m.msg);
}
function connect()
{
if(!SOCKET)
{
var hash = getCookie('hash');
if (hash == '')
{
$.ambiance({message: 'Please login!'});
}
else
{
$.ambiance({message: 'Connecting to server..', type: 'success'});
}
SOCKET = io(':4095');
SOCKET.on('connect', function(msg)
{
if(hash != '')
{
$.ambiance({message: 'Connected', type: 'success'});
}
SOCKET.emit('hash', {
hash: hash
});
});
SOCKET.on('connect_error', function(msg){
$.ambiance({message: 'Connection lost', type: 'error'});
});
SOCKET.on('request', function(msg){
request(msg);
});
}
}
}
Actually, you created the connect() function inside the request function (which wasn't called yet):
Just move it outside and it should work.

Elasticsearch.js - wait for ping to complete, async call

I am playing with elasticsearch.js from the browser. I would like to ping elasticsearch, wait for the request to complete and then return the result of the connection. But right now it is happening asynchronously, and returning undefined even when the connection is ok. I have code like this:
var connectionOK = false;
function createElasticsearchClient(hostAddress) {
var client = new $.es.Client({
hosts: hostAddress
});
return client;
}
function checkElasticsearchConnection(client) {
$.when(pingElasticsearch(client)).done(function () {
return connectionOK;
});
}
function pingElasticsearch(client) {
console.log("ELASTICSEARCH: Trying to ping es");
client.ping({
requestTimeout: 30000,
// undocumented params are appended to the query string
hello: "elasticsearch"
}, function (error) {
if (error) {
console.error('ELASTICSEARCH: Cluster is down!');
connectionOK = false;
console.log("INSIDE: " + connectionOK);
} else {
console.log('ELASTICSEARCH: OK');
connectionOK = true;
console.log("INSIDE: " + connectionOK);
}
});
}
and how it is used:
var esClient = createElasticsearchClient("exampleserver.com:9200");
var esCanConnect = (checkElasticsearchConnection(esClient));
You're mixing asynchronous functions with synchronous functions. You could go with this approach instead:
function createElasticsearchClient(hostAddress, callback) {
var client = new $.es.Client({
hosts: hostAddress
});
return callback(client);
}
function pingElasticsearch(client, callback) {
console.log("ELASTICSEARCH: Trying to ping es");
client.ping({
requestTimeout: 30000,
// undocumented params are appended to the query string
hello: "elasticsearch"
}, function (error) {
if (error) {
return callback('ELASTICSEARCH: Cluster is down!');
} else {
return callback(null);
}
});
}
And then run
createElasticsearchClient("exampleserver.com:9200", function(esClient) {
pingElasticsearch(esClient, function(err) {
if (err) console.log(err);
else {
//Everything is ok
console.log('All good');
}
});
});

wait for parse.com delete to finish before rendering screen

My code below will delete my row but it will display the error message.
Any ideas?
delete: function(id) {
// Check login
if (!Parse.User.current()) {
this.navigate('#/login', { trigger: true });
} else {
var query = new Parse.Query(Blog);
query.get(id, {
success: function(id) {
id.destroy({});
alert('Delete Successful');
},
error: function(blog, error) {
console.log(error);
alert('Error in delete query');
}
});
}
}
});
My script for parse is..
<script src="//www.parsecdn.com/js/parse-1.2.19.min.js"></script>

CasperjsJS How to setup test suite with dependent test cases

How would I create a test suite where I have a test case that is dependent on a previous test case to start?
Example:
Test 1 Login with user X
Test 2 My account page (Comes after user logged in)
Test 3 "Follow up with other tests with the same user, but on other pages"
I've been through documentation of CasperJS writing_modules & other examples but I cannot find or understand how to set it up for my specific requirements.
Or if it's even possible to do so.
Currently I have it setup as follows:
File 1 Suite
// Suite.js
var Suite;
Suite = require("./Login_User.js");
Suite.login_User("randomname","randompassword");
casper.then(function() { ;
this.test.pass("Hit the first then");
Suite = require("./MyAccount.js");
Suite.Enterprise();;
});
casper.run(function() {
return this.test.done();
});
File 2 Login Functionality
// Login_User.js (Test 2)
casper.test.begin("login page", function(test) {
casper.options.waitTimeout = 5000;
casper.on('remote.message', function(msg) {
this.echo('rem msg: ' + msg);
});
casper.on("page.error", function(msg) {
this.echo("Page Error: " + msg, "ERROR");
});
var url = "https://website.com";
casper.start(url, function() {
console.log("page loaded");
});
exports.login_User = function(username, password) {
casper.test.comment(username + "\n" + password);
casper.then(function() {
this.evaluate(function(username, password) {
$("cmpLogin_UserName").val(username);
$("cmpLogin_Password").val(password);
$("cmpLogin_LoginButton").click();
}, {
"username": username,
"password": password
})
casper.waitForUrl("https://website.com/myAccount.aspx", function() {
console.log("Logged in");
casper.capture("Loggedin.png");
});
});
casper.then(function(){
this.test.assertTitle("My Account", "Title is Login");
});
casper.run(function() {
test.done();
});
}
});
File 3 My Account Content
// MyAccount (Test 3)
casper.test.begin("Account page", function(test) {
casper.options.waitTimeout = 5000;
casper.on('remote.message', function(msg) {
this.echo('rem msg: ' + msg);
});
casper.on("page.error", function(msg) {
this.echo("Page Error: " + msg, "ERROR");
});
var url = "https://website.com/myAccount.aspx?";
casper.start(url, function() {
console.log("page loaded");
casper.capture("MyAccountEnt.png");
});
// It does not reach this section!!!
exports.Enterprise = function() {
casper.then(function() {
this.test.assertTitle("My Account", "My Account page");
this.test.pass("test test test");
});
casper.run(function() {
test.done();
});
}
});
Command line output
http://i.imgur.com/ZHsZ5hX.png?1

Accounts.sendVerificationEmail Issue in Meteor

I Need to send VerificationEmail using Meteor.I did the code but It didn't send VerificationEmail and also got error on server side.Error is : Can't find user.I didn't have any idea about this.so Please see the below code & suggest me how to do.
JS Code:
if (Meteor.isClient)
{
Template.main.events
({
'submit #register-form' : function (e,t)
{
console.log("You pressed the button Register ");
e.preventDefault();
var username = t.find('#username').value
, name = t.find('#name').value
, email = t.find('#email1').value
, password = t.find('#pwd1').value;
console.log("password="+password);
var isValidPassword = function(val, field)
{
if (val.length >= 6) {
return true;
} else {
Session.set('displayMessage', 'Error & Too short.')
return false;
}
}
if (isValidPassword(password))
{
console.log(" *** isValidPassword *** ");
Accounts.createUser({email: email, password : password,username : username }, function(err)
{
if (err)
{
console.log(err);
}
else
{
console.log("Register Successfully");
Meteor.call('sendEmail',
'*****#gmail.com',
'****.com',
'Hello from Meteor!',
'This is a test of Email.send.');
}
});
}
else
{
console.log("*** Error ***");
}
}
});
}
if (Meteor.isServer)
{
Meteor.startup(function ()
{
// code to run on server at startup
});
//Meteor methods
Meteor.methods
({
sendEmail: function (to, from, subject, text)
{
Accounts.config({sendVerificationEmail: true, forbidClientAccountCreation: false});
process.env.MAIL_URL = 'smtp://****#gmail.com:*pwd*#smtp.gmail.com:587';
this.unblock();
Accounts.sendVerificationEmail(to);
}
});
}
Did you send the email to an email address? When you use to in Accounts.sendVerificationEmail(to);
it must be the _id of the user you want to send the confirmation email to, not their email address.

Categories

Resources