Selenium,mocha,mysql。 Mysql操作顺序

时间:2015-01-20 14:36:37

标签: javascript mysql node.js selenium-webdriver mocha

我正在使用带有mocha和node-mysql的selenium进行测试。但进入麻烦。看起来我的代码的一部分在测试操作之前执行mysql查询:

mysqlfunc.js

var mysql = require('mysql');
    var connection =     mysql.createConnection('here is my connection url');

    exports.countNums = function countNumbers(phone, user, callback){
	connection.query('Here is SELECT', function(err, rows, fields) {
      if (err) throw err;

  
      callback(rows[0]['COUNT(*)']); 
  
    });
    connection.end();
    };

并且test.js:

var wd = require('selenium-webdriver');
var assert = require('chai').assert;
var phonelist = require('./mysqlfunc.js')

var SELENIUM_HOST = 'http://localhost:4444/wd/hub';
var URL = 'myurl.com';

var client = new wd.Builder()
   .usingServer(SELENIUM_HOST)
   .withCapabilities({ browserName: 'firefox' })
   .build();


  
client.manage().timeouts().implicitlyWait(10000);


suite('Blacklists');

test('adding to blacklist', function(done){


	
		client.get(URL);
	client.findElement({ name: 'username' }).sendKeys('autolistmanageuser');
	client.findElement({ name: 'password' }).sendKeys('autolistmanageuser');
    client.findElement({ id: 'login-button' }).click();
	client.findElement({ linkText: 'Black list' }).click();
	client.sleep(2000) 
	client.findElement({ linkText: 'Addnum' }).click();
	   	
	
	client.findElement({ xpath: '/html/body/div[4]/div/div/div[2]/div/form/fieldset/div[2]/div[1]/div/div/div/input' }).sendKeys('79265914970');
	client.findElement({ xpath: '/html/body/div[4]/div/div/div[2]/div/form/fieldset/div[2]/div[3]/div/div/label[1]/input[@value="1"]' }).click();
	
	
	client.findElement({ xpath: '/html/body/div[4]/div/div/div[2]/div/form/div[2]/button' }).click()
	
//HERE IS THE PROBLEM!!! Looks like countNums executing before all the selenium code above then result is 1
phonelist.countNums('79265914970','autolistmanageuser', function(result) {
		
		assert.equal(0, result, 'Number was not deleted');
	
		done();
	},done);
});

phonelist.countNums中的问题,它在selenium操作之前执行时返回错误的值 selenium代码使用网页删除电话号码 phonelist.countNums - 从已删除号码的Mysql事件中选择count(*),所以我在等待0

1 个答案:

答案 0 :(得分:0)

Scince一切都是异步的,我的phonelist.countNums没有进入webdriver的flowcontrol并且与webriver并行启动。 所以解决方案就是加入webdriver命令:



client.isElementPresent({ linkText: 'pages 2600' }).then(function(result) {
        
		phonelist.countNums('79265914970','autolistmanageuser', function(date) {
		console.log('external ' +date);
		assert.equal(0, date, 'number is still present');
		});
		done();
	},done);




这对我很有用,我希望这是正确的答案。祝你好运!