Use functions inside of page-object commands in Nightwatch - javascript

Ignore the placement of the selectUser function. I'm still trying to play around with it. How can I use functions inside of the page-object commands? Before that function was repeated 5 times inside each command function, but for cleanliness, that obviously needed to be changed, I just can't figure out how.
Page-object snippet:
var selectUser = function(userName, password) {
return this.waitForElementVisible('#usernameField')
.setValue('#usernameField', userName)
.setValue('#passwordField', password)
.click('#signOnButton')
.waitForElementVisible('#eventTaskManager');
};
module.exports = {
elements: {
usernameField: '#UserName',
passwordField: '#Password',
signOnButton: 'input[value="Sign On"]',
cancelButton: 'a[href$="/cancel"]',
errorMessage: '.icon-warning',
eventTaskManager: '.concierge'
},
commands: [{
signInAsUniregisteredUser: function() {
selectUser(unregisteredUserName, unregisteredUserPass);
},
signInAsRegisteredUser: function() {
selectUser(registeredUserName, prodRegisteredPass);
},
signInAsUnregisteredUser_Regression: function() {
selectUser(unregisteredUserName, unregisteredUserPass);
},
signInAsRegisteredUser_Regression: function() {
selectUser(registeredUserName, prodRegisteredPass);
},
signInAsRegisteredUser_Production: function() {
selectUser(prodRegisteredUser, prodRegisteredPass);
}
}]
};

First at all, for login feature, there are only 2 assertions which are login "ok" or login "fail" (unregistered,wrong credentials,missing username,...), so you only need this for page object.
var pageCommands = {
tryToLogin: function(userName, password) {
return this.waitForElementVisible('#usernameField')
.setValue('#usernameField', userName)
.setValue('#passwordField', password)
.click('#signOnButton');
},
assertLoginSuccesfully: function() {
return this.waitForElementVisible('#eventTaskManager'); // login pass
},
assertLoginUnSuccesfully: function() {
return this.waitForElementVisible('#errorMessage'); // login fail
}
};
module.exports = {
elements: {
usernameField: '#UserName',
passwordField: '#Password',
signOnButton: 'input[value="Sign On"]',
cancelButton: 'a[href$="/cancel"]',
errorMessage: '.icon-warning',
eventTaskManager: '.concierge'
},
commands: [pageCommands],
};
And in your test cases :
const loginPage = browser.page.login();
const dataForTest = require('./data.json');
const credentials = {
username : dataForTest.username ,
password : dataForTest.password
};
login.tryToLogin(credentials.username,credentials.password)
.assertLoginSuccesfully() // if data from dataForTest is correct
This practice will keep you stay away of hard values by storing every thing in data.json (or anything you want).

Related

DialogFlow and contexts with voximplant

I try to set Contexts in DialogFlow with Voximplant intergration described here:
https://cogint.ai/voximplant-dialogflow-connector-2019/#settingcontexts
require(Modules.AI);
const languageCode = "en-US";
const agentId = 247;
let agent,
call,
conversation,
endUserParticipant,
isConversationCreated = false,
isCallCreated = false,
isCallConnected = false,
isParticipantCreated = false;
VoxEngine.addEventListener(AppEvents.Started,
function (ev) {
agent = new CCAI.Agent(agentId);
agent.addEventListener(CCAI.Events.Agent.Started, () => {
conversation = new CCAI.Conversation({ agent: agent });
conversation.addEventListener(CCAI.Events.Conversation.Created, () => {
isConversationCreated = true;
createParticipant();
});
});
});
VoxEngine.addEventListener(AppEvents.CallAlerting,
function (ev) {
isCallCreated = true;
createParticipant();
call = ev.call;
call.answer();
call.addEventListener(CallEvents.Connected,
function () {
isCallConnected = true;
//Script whith phone number to contexts must be added here somehow. Probably in setupMedia function.
setupMedia();
});
call.addEventListener(CallEvents.Disconnected,
function () {
conversation.stop();
VoxEngine.terminate();
});
});
function createParticipant() {
if (!isConversationCreated || !isCallCreated) return;
endUserParticipant = conversation.addParticipant({
call: call,
options: { role: "END_USER" },
dialogflowSettings: {
lang: languageCode,
singleUtterance: true,
replyAudioConfig: { audioEncoding: "OUTPUT_AUDIO_ENCODING_OGG_OPUS" },
},
});
endUserParticipant.addEventListener(CCAI.Events.Participant.Created, () => {
isParticipantCreated = true;
setupMedia();
});
}
function setupMedia() {
if (!isParticipantCreated || !isCallConnected) return;
endUserParticipant.analyzeContent({
eventInput: { name: "WELCOME", languageCode: languageCode },
});
endUserParticipant.addEventListener(
//Script whith phone number to contexts must be added here somehow.
phoneContext = {
name: "phone",
lifespanCount: 99,
parameters: {
caller_id: call.callerid(),
called_number: call.number()
}
},
//endUserParticipant.setQueryParameters({contexts: [phoneContext]})
//Script whith phone number to contexts must be added here somehow.
CCAI.Events.Participant.PlaybackFinished,
() => {
//Added by and call works, but hang up
VoxEngine.setQueryParameters({contexts: [phoneContext]});
//Added by and call works, but hang up
VoxEngine.sendMediaBetween(call, endUserParticipant);
}
);
VoxEngine.sendMediaBetween(call, endUserParticipant);
}
The Voximplant number is forwarded to Dialogflow but after 20 seconds the voicebot become silent, but call is not terminated. I remove contexts part and the call and voicebot works as it is intended to.
What is wrong?
I ended up rewrite my code. I was able to pass on the caller_id / caller_number parameters to DialogFlow not as a Contexts by script. However, I added the two variables as Contexts in my Welcome intent.
function setupMedia() {
if (!isParticipantCreated || !isCallConnected) return;
endUserParticipant.analyzeContent({
eventInput: {
name: "WELCOME",
languageCode: languageCode,
parameters: {
//phone: call.callerid(),
caller_id: call.callerid(),
called_number: call.number()}
},
});
endUserParticipant.addEventListener(
CCAI.Events.Participant.PlaybackFinished,
() => {
VoxEngine.sendMediaBetween(call, endUserParticipant);
}
);
VoxEngine.sendMediaBetween(call, endUserParticipant);
}
I recommend using Voximplant's Modules.AI integration instead of Modules.CCAI as I used in the cogint.ai article you mentioned. Modules.CCAIis used automatically through Dialogflow's One-click integrations, but it is not as well supported beyond that from what I have seen.
They have instructions for that here and a walkthrough video here. Unfortunately the API is very different that what you have with the CCAI module, but you will find many more references and examples to that (like what I have on cogint.ai).
Modules.AI only works with Dialogflow ES.

Meteor.user() is undefined in client-side controller

In the client-side of a METEORJS application, i have a controller that display some users.
I have a problem with Meteor.user() function, the error is : Meteor.user(...) is undefined.
Here is my code :
this.AdminUsersController = RouteController.extend({
template: "Admin",
yieldTemplates: {
'AdminUsers': { to: 'AdminSubcontent'}
},
onBeforeAction: function() {
var permissions = Meteor.user().profile.permissions;
if (permissions && permissions.indexOf('Users') != -1)
this.next();
else this.redirect('/admin/dashboard');
},
action: function() {
if(this.isReady()) { this.render(); } else { this.render("Admin"); this.render("loading", { to: "AdminSubcontent" });}
/*ACTION_FUNCTION*/
},
isReady: function() {
var subs = [
Meteor.subscribe("users")
];
var ready = true;
_.each(subs, function(sub) {
if(!sub.ready())
ready = false;
});
return ready;
},
data: function() {
var data = {
params: this.params || {},
users: Users.find({labo_id: Meteor.user().profile.labo_id}, {sort: {createdAt:-1}})
};
return data;
},
onAfterAction: function() {
}});
It's in the data function.
I try to retrieve all users that are connected to the logged in user and got the same labo_id field...
I don't know why it give me that, because in the onBeforeAction function, i can access to Meteor.user(), and specially his profile...
Someone know what can i do to make it run ?
Thanks for your future answers :)
This is a timing issue. Meteor.user() does not necessarily return data, if it hasn't been loaded yet. Meteor.userId() however will return the _id of the user record (if they are logged in). If you can change your query to rely on that _id, then it can work.
Depending on which router you are using, you can add a resolve: entry to ensure that the route waits for the user record to be loaded before activating it, and then your query will work.

Rendering user profile page in MeteorJS

I'm trying to create a page for users so that when they are logged in they can see the content they've uploaded onto the site. I can't seem to get the page to render. I've searched all over, but can't seem to find anything. Any ideas of where I'm going wrong?
from my publication.js file
Meteor.publish('myTickets', function() {
var currentUserId = this.userId;
return Tickets.find({
createdBy: currentUserId
})
});
from the router.js
Router.route('/users/:_id', {
name: 'userPage',
waitOn: function() {
return Meteor.subscribe('myTickets');
},
data: function() {
return Tickets.find({
userId: this.userId
})
}
});
userpage.js
Template.userPage.helpers({
tickets: function() {
var currentUserId = Meteor.userId();
return Tickets.find({
createdBy: currentUserId
}, {
sort: {
createdAt: -1
}
});
}
});
I asked you in a comment what you exactly wanted to do (you have a route with a parameter, but you are never using it), but anyway, I think I got your problem;
You are using this.userId to get your current user ID in your router, but your should use Meteor.userId() instead; this.userId can only be used in publish methods.

React/reflux how to do proper async calls

I recently started to learn ReactJS, but I'm getting confused for async calls.
Lets say I have a Login page with user/pass fields and login button. Component looks like:
var Login = React.createClass({
getInitialState: function() {
return {
isLoggedIn: AuthStore.isLoggedIn()
};
},
onLoginChange: function(loginState) {
this.setState({
isLoggedIn: loginState
});
},
componentWillMount: function() {
this.subscribe = AuthStore.listen(this.onLoginChange);
},
componentWillUnmount: function() {
this.subscribe();
},
login: function(event) {
event.preventDefault();
var username = React.findDOMNode(this.refs.email).value;
var password = React.findDOMNode(this.refs.password).value;
AuthService.login(username, password).error(function(error) {
console.log(error);
});
},
render: function() {
return (
<form role="form">
<input type="text" ref="email" className="form-control" id="username" placeholder="Username" />
<input type="password" className="form-control" id="password" ref="password" placeholder="Password" />
<button type="submit" className="btn btn-default" onClick={this.login}>Submit</button>
</form>
);
}
});
AuthService looks like:
module.exports = {
login: function(email, password) {
return JQuery.post('/api/auth/local/', {
email: email,
password: password
}).success(this.sync.bind(this));
},
sync: function(obj) {
this.syncUser(obj.token);
},
syncUser: function(jwt) {
return JQuery.ajax({
url: '/api/users/me',
type: "GET",
headers: {
Authorization: 'Bearer ' + jwt
},
dataType: "json"
}).success(function(data) {
AuthActions.syncUserData(data, jwt);
});
}
};
Actions:
var AuthActions = Reflux.createActions([
'loginSuccess',
'logoutSuccess',
'syncUserData'
]);
module.exports = AuthActions;
And store:
var AuthStore = Reflux.createStore({
listenables: [AuthActions],
init: function() {
this.user = null;
this.jwt = null;
},
onSyncUserData: function(user, jwt) {
console.log(user, jwt);
this.user = user;
this.jwt = jwt;
localStorage.setItem(TOKEN_KEY, jwt);
this.trigger(user);
},
isLoggedIn: function() {
return !!this.user;
},
getUser: function() {
return this.user;
},
getToken: function() {
return this.jwt;
}
});
So when I click the login button the flow is the following:
Component -> AuthService -> AuthActions -> AuthStore
I'm directly calling AuthService with AuthService.login.
My question is I'm I doing it right?
Should I use action preEmit and do:
var ProductAPI = require('./ProductAPI')
var ProductActions = Reflux.createActions({
'load',
'loadComplete',
'loadError'
})
ProductActions.load.preEmit = function () {
ProductAPI.load()
.then(ProductActions.loadComplete)
.catch(ProductActions.loadError)
}
The problem is the preEmit is that it makes the callback to component more complex. I would like to learn the right way and find where to place the backend calls with ReactJS/Reflux stack.
I am using Reflux as well and I use a different approach for async calls.
In vanilla Flux, the async calls are put in the actions.
But in Reflux, the async code works best in stores (at least in my humble opinion):
So, in your case in particular, I would create an Action called 'login' which will be triggered by the component and handled by a store which will start the login process. Once the handshaking ends, the store will set a new state in the component that lets it know the user is logged in. In the meantime (while this.state.currentUser == null, for example) the component may display a loading indicator.
For Reflux you should really take a look at https://github.com/spoike/refluxjs#asynchronous-actions.
The short version of what is described over there is:
Do not use the PreEmit hook
Do use asynchronous actions
var MyActions = Reflux.createActions({
"doThis" : { asyncResult: true },
"doThat" : { asyncResult: true }
});
This will not only create the 'makeRequest' action, but also the 'doThis.completed', 'doThat.completed', 'doThis.failed' and 'doThat.failed' actions.
(Optionally, but preferred) use promises to call the actions
MyActions.doThis.triggerPromise(myParam)
.then(function() {
// do something
...
// call the 'completed' child
MyActions.doThis.completed()
}.bind(this))
.catch(function(error) {
// call failed action child
MyActions.doThis.failed(error);
});
We recently rewrote all our actions and 'preEmit' hooks to this pattern and do like the results and resulting code.
I also found async with reflux kinda confusing. With raw flux from facebook, i would do something like this:
var ItemActions = {
createItem: function (data) {
$.post("/projects/" + data.project_id + "/items.json", { item: { title: data.title, project_id: data.project_id } }).done(function (itemResData) {
AppDispatcher.handleViewAction({
actionType: ItemConstants.ITEM_CREATE,
item: itemResData
});
}).fail(function (jqXHR) {
AppDispatcher.handleViewAction({
actionType: ItemConstants.ITEM_CREATE_FAIL,
errors: jqXHR.responseJSON.errors
});
});
}
};
So the action does the ajax request, and invokes the dispatcher when done. I wasn't big on the preEmit pattern either, so i would just use the handler on the store instead:
var Actions = Reflux.createActions([
"fetchData"
]);
var Store = Reflux.createStore({
listenables: [Actions],
init() {
this.listenTo(Actions.fetchData, this.fetchData);
},
fetchData() {
$.get("http://api.com/thedata.json")
.done((data) => {
// do stuff
});
}
});
I'm not big on doing it from the store, but given how reflux abstracts the actions away, and will consistently fire the listenTo callback, i'm fine with it. A bit easier to reason how i also set call back data into the store. Still keeps it unidirectional.

Username not changing properly using MongoDB update function?

Below is a snippet of my code where I begin by searching the collection of notes to see if any of them contain the username that I am changing my current session's username to. If the username has not yet been used, it may be changed to that so I change the current session's username and then I update every note to be under this new username then display a changesuccess.jade file. However, when I run the code everything appears to run fine exept the username for each note doesn't change. I feel like it's due to the find() method on the 5th line. Any help would be greatly appreciated!
router.post('/changeusername',function(req, res) {
var newUsername = req.body.username;
var user = req.session.username;
var userFound = notesCollection.find( { owner: newUsername } )
var results = function(infoInput) {
res.render("changedUser.jade", {title: "Username Change",
info: infoInput});
}
var checkChange = function(err) {
if (err) {
results("Username change failed!");
} else {
results("Username changed!");
}
}
console.log(userFound);
if (!userFound.length) {
notesCollection.update({ owner: user },
{ owner: newUsername},
{ multi: true },
checkChange);
} else {
res.render("changedUser.jade", {title: "Username Change",
info: "Username change failed!"});
}
});
If i understand your problem correctly, you are trying to update a collection in mongodb and it is not getting updated.
So the problem is with the way you are calling mongoose#update.
Since you want to update 'owner', you should use mongodb#$set
Mongoose supports the same $set operator in the conditions too.
So just a little correction for your case:
var conditions = { owner: user }
, update = { $set: { owner: newUsername }}
, options = { multi: true };
notesCollection.update(conditions, update, options, callback);
function callback (err, numAffected) {
// numAffected is the number of updated documents
})
So try this now.

Categories

Resources