How do I query mysql database by using promises in protractor?
I would like to execute different queries multiple times during test execution but my executeSelectQuery function executes in the beginning of the test and not in the end. How can I overcome this by using promises?
I also need to access the object inside executeSelectQuery and I am not being able to. Function returns undefined.
Below code is how my executeSelectQuery function looks like:
function executeSelectQuery(sql){
connectDatabase.connection.query(sql, function(err, data) {
var result;
if (err) {
throw new Error('[ FAIL ] - Unsuccessful query execution!!!');
}
else{
result = data[0].user;
log.info('[ SUCCESS ] - Query executed successfully: ' + sql + ' with the following result ==> ' + result);
return result;
}
});
}
This is how my test looks like:
it('Should navigate to webpage..', function(){
log.info("Test is being executed..");
login.goTo(parameters.url);
login.login(parameters.username, parameters.password);
helper.executeSelectQuery(query_select);
});
Just make your executeSelectQuery()
function return a promise that resolves with the data.
executeSelectQuery = (sql) => {
return new Promise((resolve, reject) => {
connectDatabase.connection.query(sql, (err, data) => {
if(err) {
reject(err);
}
resolve(data);
});
});
}
Then resolve it in your tests
executeSelectQuery().then((data) => {
console.log(data);
})
Keep query functions call in separate 'it' block in the last .
describe('Overall Test Suite',function() {
var expectedResult; //global variable for all 'it' blocks inside suite.
it('Should navigate to webpage..', function(){
log.info("Test is being executed..");
login.goTo(parameters.url);
login.login(parameters.username, parameters.password);
});
it('Executing Queries', function(){
expect(helper.executeSelectQuery(query_select)).toBeEqual(expectedResult);//let jasmine resolve the promise implicitly
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.