[英]Protractor querying SQL Server database (MSSQL)
I am trying to execute a simple query on SQL Server but when I run it with protractor it just runs quickly and doesnt return (log) anything.我正在尝试在 SQL Server 上执行一个简单的查询,但是当我使用量角器运行它时,它运行得很快并且不返回(记录)任何内容。 I would appreciate any hints, working examples or pointers to what I am doing wrong and how to exec a SQL query on SQL Server with protractor.
我将不胜感激任何提示、工作示例或指向我做错了什么以及如何使用量角器在 SQL Server 上执行 SQL 查询的指针。
var sql = require('mssql');
describe('test db connection', function () {
it('tests db connection', function () {
ConnectDB()
})
function ConnectDB() {
var config = {
user: 'user',
password: 'password',
server: 'xyz.database.windows.net',
database: 'dbdev',
options: {
encrypt: true
}
}
var connection = new sql.Connection(config)
connection.connect(function (err) {
console.log(err)
})
var request = new sql.Request(connection);
request.query('select * from Config where [Key] like \'HidePreop%\'', function (err, recordeset) {
var res = recordeset;
console.log(res)
});
Protractor test - it
blocks will only wait for webDriverJS commands in Protractor control flow to finish and any other async activity you have to manually make the it
block wait using done
.量角器测试-
it
块仅会等待在量角器控制流到结束,你必须任何其它异步活动webDriverJS命令手动进行it
使用块等待done
。
In this case -在这种情况下 -
describe('test db connection', function () {
it('tests db connection', function (done) {
// Any method that returns a promise. Similary if your method returns a callback you can handle accordingly
ConnectDB().then(function _onSuccess(){
done();
}).catch(function _onFailure(err){
done.fail(err);
})
})
});
And I would modify your funcion - ConnectDB()
to return a promise based on the resolution of the callback provide by the mssql npm package .我会修改您的功能 -
ConnectDB()
以根据mssql npm 包提供的回调的分辨率返回一个承诺。 Refer here on how to convert a callback to Promises.请参阅此处了解如何将回调转换为 Promise。 its an awesome tutorial.
这是一个很棒的教程。
function ConnectDB() {
return new Promise(function (fulfill, reject) {
var config = {
user: 'user',
.............
};
var connection = new sql.Connection(config);
connection.connect(function (err) {
reject(err);
});
var request = new sql.Request(connection);
request.query('select * from Config where [Key] like \'HidePreop%\'', function (err, recordeset) {
if (err) reject(err);
else fulfill(recordeset);
});
});
}
I access the MSSQL database using this configuration in it's own file and then import it into the test file so I can use it a bit like a fake page object.我在自己的文件中使用此配置访问 MSSQL 数据库,然后将其导入到测试文件中,这样我就可以像使用假页面对象一样使用它。 I would highly recommend storing any database information in an external file outside of the repository.
我强烈建议将任何数据库信息存储在存储库之外的外部文件中。 I am using a .env file from the npm library dotenv which can be installed using
npm install --save-dev dotenv
我正在使用 npm 库 dotenv 中的 .env 文件,该文件可以使用
npm install --save-dev dotenv
//db.js
const sql = require('mssql');
require('dotenv').config();
var config = {
user: process.env.MSSQL_USERNAME,
password: process.env.MSSQL_PASSWORD,
server: process.env.MSSQL_SERVER,
database: 'YOURDB',
options: {
encrypt: true
}
};
module.exports = {
/** Define sql queries here */
deleteEmployeeByLastName(lastName) {
let my_query = `DELETE FROM dbo.Employee WHERE LastName='${lastName}'`;
sql.connect(config).then(function () {
new sql.Request()
.query(my_query).then(function (recordset) {}).catch(function (err) {
console.log(err);
});
});
}
}
The test file should look something like this测试文件应该是这样的
//test.js
var db = require('db.js');
describe('Employee Management', function () {
it('Deleting an employee', function (done) {
db.deleteEmployeeByLastName('Pmurt');
//REST OF CODE HERE
//...
//...
done();
})
});
None of the above worked for me and then I found this link - https://www.tutorialsteacher.com/nodejs/access-sql-server-in-nodejs I applied what they suggested Also, I took the information from the link, placed this in a function, placed the function into a class...以上都不适合我,然后我找到了这个链接 - https://www.tutorialsteacher.com/nodejs/access-sql-server-in-nodejs我应用了他们的建议另外,我从链接中获取了信息,把它放在一个函数中,把这个函数放在一个类中......
export class SqlDo {
static serverDo() {
//the info from the link //来自链接的信息
and added more after the并在之后添加更多
var server = app.listen(5000, function () {
console.log('Server is running..');
section.部分。 I added
我加了
browser.waitForAngularEnabled(false);
browser.get('http://localhost:5000');
and this adds the output to a screenshot which is collected by the screenshot reporter.这会将输出添加到屏幕截图报告器收集的屏幕截图中。 Then I created a spec and in the 'it' I call the class.function
然后我创建了一个规范,在“它”中我调用了 class.function
import { SqlDo } from './server';
describe('', () => {
it('test connection and output', () => {
SqlDo.serverDo();
});
});
All worked with the database output rendered and in a screenshot and the screenshot captured in the html report.所有这些都与呈现的数据库输出和屏幕截图以及在 html 报告中捕获的屏幕截图一起使用。
I hope this helps anyone still struggling.我希望这可以帮助任何仍在苦苦挣扎的人。
Posting a working example for mssql@4.0.4为 mssql@4.0.4 发布一个工作示例
var sql = require('mssql');
describe('test db connection', function () {
it('tests db connection', function (done) {
ConnectDB().then(function _onSuccess(_returned){
console.log(_returned.recordset[0].FirstPSPOrderId)
done();
}).catch(function _onFailure(err){
done.fail(err);
})
})
}); });
function ConnectDB() {
return new Promise(function (fulfill, reject) {
var config = {
user: 'xxx',
password: 'xxx',
server: 'xxx',
port: 'xxx',
database: 'xxxxxx',
options: {
encrypt: true
}
};
var connection = new sql.ConnectionPool(config);
connection.connect((err) => {
if (err) reject(err);
//});
let query = "select [FirstPSPOrderId] from XYZ"
connection.request()
.query(query, (err, recordeset) => {
console.dir('Record set: ' + recordeset)
console.dir('Err: ' + err)
if (err) reject(err);
else fulfill(recordeset);
});
});
});
} }
an updated answer based on async/await:基于 async/await 的更新答案:
package.json:包.json:
"devDependencies": {
"@types/mssql": "4.0.11",
"msnodesqlv8": "0.6.11",
"mssql": "4.3.0",
}
test:测试:
import { ConnectionPool } from 'mssql/msnodesqlv8';
describe('any test', () => {
it('should prepare db', async () => {
const connectionString = 'Server=#server#;Database=#db#;Trusted_Connection=Yes;Driver=msnodesqlv8'
const connectionPool: ConnectionPool = new ConnectionPool(connectionString);
await connectionPool.connect();
const result = await connectionPool.request().query('select 'Hello World' as resultColumn');
connectionPool.close();
expect(result.recordset[0].resultColumn).toEqual('Hello World');
}
}
Substitute query with a sql statement that prepares data.用准备数据的 sql 语句替换查询。 eg Delete From Users Where... Then use result.affectedRows to see if delete worked
例如从用户那里删除...然后使用 result.affectedRows 来查看删除是否有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.