I'm writing a test using Selenium and JavaScript. I'm new to both, and also new to functional programming and promises. I'm trying to create a function that needs to do 3 things:
- Click on an input
- Clear the input
- SendKeys to input
My current function does not work:
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;
}
The function would then be called as for example:
clearAndSendKeys(driver, webdriver.By.id('date_field'), '14.09.2015').then(function(){
//Do stuff
});
I expected the variable returnValue
to contain the promise from sendKeys
. However the function clearAndSendKeys
returns the undefined variable before sendKeys is ran. I assume this is because returnValue
was never defined as a promise, and so the program does not know that it needs to wait for sendKeys
.
How can I make my function clearAndSendKeys
return the promise from sendKeys
? I'd rather avoid having to add a callback to the clearAndSendKeys
function.
Edit: Removed .then({return data})
from the code as this was a typo.