我正在使用Selenium和JavaScript编写测试。我对这两者都是新手,也是功能编程和承诺的新手。我试图创建一个需要做三件事情的函数:如何从最后的承诺中返回一个函数中的承诺“then”
- 点击输入
- 清除输入
- 的SendKeys输入
我目前的功能不工作:
var clearAndSendKeys = function(driver, elementIdentifier, sendKeys) {
var returnValue;
driver.findElement(elementIdentifier).then(function(inputField){
inputField.click().then(function() {
inputField.clear().then(function() {
returnValue = inputField.sendKeys(sendKeys);
});
});
});
return returnValue;
}
然后该函数将被称为例如:
clearAndSendKeys(driver, webdriver.By.id('date_field'), '14.09.2015').then(function(){
//Do stuff
});
我期望变量returnValue
包含来自sendKeys
的承诺。但是,在运行sendKeys之前,函数clearAndSendKeys
会返回未定义的变量。我认为这是因为returnValue
从未被定义为承诺,所以程序不知道它需要等待sendKeys
。
如何让我的功能clearAndSendKeys
从sendKeys
返回承诺?我宁愿避免为clearAndSendKeys
函数添加回调。
编辑:从代码中删除.then({return data})
,因为这是一个错字。
你需要的returnValue分配给承诺,第一条线应该是'VAR的returnValue = driver.findElement(...', – Saar
http://jsfiddle.net/arunpjohny/s2b0v8nu/1/ - 使用回调 –
嗨阿伦,为什么他应该使用回调时试图使用诺言? – Saar