Reputation: 1188
When I search in StackOverflow I got multiple solutions to get the value from the input element.
I tried below of the methods
method 1:
return serialNumberTextField.getAttribute('ng-model')
.then((text)=>{return serialNumberTextField.evaluate(text);})
.then((evalText) =>{ console.log("serial number in eval method :"+evalText);
return evalText;})
method 2:
return serialNumberTextField.getAttribute('value')
.then( (text) =>{
console.log("serial number in method :"+text);
return text;})
When print in the two methods it print the exact value eg:- method1: serial number in eval method :assetDF1534 method2: serial number in method :assetDF1534
I used the return value in two places
first place: 'assetName' variable is catch from the above methods to get the serial number.
assetNameTextField.clear().sendKeys(assetName);
second place:
element(by.xpath("//a[contains(text(),'"+ assetName+"')]")).click();
first place, it worked fine and enter the value 'assetDF1534' in the assetname text field but when it comes to creating web elements using the asset name failed because it is giving 'assetName' as promise instead of value. when I print the 'assetName' in those methods displayed as promise as below:
asset name to enter in filter is :ManagedPromise::2239 {[[PromiseStatus]]: "fulfilled"} asset name is :ManagedPromise::2239 {[[PromiseStatus]]: "fulfilled"}
Sendkeys handled promise as it is a protractor method but not in another case. How to get the value instead of a promise?
Upvotes: 0
Views: 177
Reputation: 2858
You need to resolve the promise instead of returning it. This should work.
function getSerialNumber() {
return new Promise((resolve, reject) => {
serialNumberTextField.getAttribute('value').then((text) => {
resolve(text);
})
.catch((err) => {
reject(err);
});
});
}
Upvotes: 1
Reputation: 1188
Found the solution as below:
function definition
this.getSerialNumber = function()
{
return serialNumberTextField.getAttribute('value')
.then( (text) =>{ return text; })
};
function call
this.editAsset = async function(data)
{
this.getSerialNumber()
.then( (serialNumber) =>{
data.serialNumber = serialNumber;
});
}
the value return from the getSerialNumber()
is assigned to the value using .then()
. We can resolve the promise using .then()
Upvotes: 0