量角器+ CucumberJS MySQL查询

时间:2014-10-21 12:39:11

标签: asynchronous cucumber protractor

目前我的自动化框架使用来自cucumberJS的量角器。我们使用chai作为断言库的承诺,我最近遇到了对数据库进行直接mysql查询的需求。

如何构建步骤定义以便能够获取查询,并在同一步骤中使用查询结果?我当前的困难是正在运行量角器的异步方式,导致我在需要查询结果发生的步骤之后执行查询,以及传递作为查询结果创建的JSON对象的范围。

  this.loginWithMysqlUser = function(uname) {
var mysql = require('mysql');
var connection = mysql.createConnection({
  host : 'localhost',
  user : '*******',
  password : '*******',
  database : '*******'
});

  connection.connect();

  connection.query('SELECT * FROM prot_users WHERE username = ?', [uname], function(err, rows) {
    if(err) throw err;

    mysqlUser = {
      username: rows[0].username,
      password: rows[0].password
    };

  });

  connection.end();

    loginpage.login(mysqlUser);

  };

此功能驻留在登录页面声明中。

2 个答案:

答案 0 :(得分:1)

通常你的黄瓜测试脚本会喜欢:

Feature: As an admin I would like to check if a customer has an     
         account
Scenario: Check that customer name is registered in DB
Given that I am logged in as admin
And I wish to check that customer (foo) is registered
Then I expect following details from DB query:
    | username | password | database |
    | foo      | bar      | mysql    |

带有步骤定义:

Given(/^that I am logged in as admin$/, function(callback){
   ..
   logic goes here
   ..
});
And(/^I wish to check that customer (foo) is registered$/, 
function(username, callback){
   // create connection with db as described
   // with supplied username
   // Use a promise to create mySQL connection
   // and queries DB based on username as described
   // on successful resolution set DBResult to results
   // for username, password and database
   // on error set DBResult to undefined
});
Then(/^I expect following details from DB query$/, function(data,   
callback)
{
   var rows = data.raw;
   // extract values of input table cells into DBExpect using
   // rows[0].username
   // rows[0].password
   // rows[0].database
   // Check equality of DBResult and DBExpect objects
   ..
   expect.isFulfilled(DBResult).toEqual(DBExpect).notify(callback);
});

答案 1 :(得分:0)

我最终包含了使用connection.query函数中的数据所需的登录和函数的所有逻辑。

似乎工作正常,并且可以从该查询函数中调用量角器。