Reputation: 31
I am writing a test checking for the amount of gold I have. I am using typescrypt and protractor.
I have a method GetAmoutOfChips:
public static GetAmountOfChips(): PromiseLike<any> {
let chips: number = 1;
NavigateTo.myProfile();
browser.sleep(900);
let promise = MyProfile.Basic.chipsAmount.getText().then((chipAmount) => {
chips = parseInt(chipAmount);
console.log("+++++++++++1" + chips);
});
MyProfile.Basic.close.click();
console.log("+++++++++++2" + chips);
return promise;
}
I would like to use it in a test. So I am doing the following:
Actions.Basic.GetAmountOfChips().then((chipAmount: number) => {
chipsBalance = chipAmount;
console.log("+++++REAL" + chipsBalance);
});
console.log#1 returns the value i want to use. console.log#2 returns undentified. So I can not pass the variable to the test. What do i do to pass the variable from the method to the test?
Upvotes: 0
Views: 608
Reputation: 31600
You have to do everything inside the then. You can also return the value there to create another promise.
public static GetAmountOfChips(): PromiseLike<any> {
NavigateTo.myProfile();
browser.sleep(900);
let promise = MyProfile.Basic.chipsAmount.getText().then((chipAmount) => {
// this is executed asynchronously
MyProfile.Basic.close.click();
// return a value to create another promise
return parseInt(chipAmount);
});
return promise;
}
Promises can be quite confusing, you can find out more here.
In your test you must also do the checks in the then function.
Actions.Basic.GetAmountOfChips().then((chipAmount: number) => {
// test here not outside
expect(chipAmount).toBe(10);
});
Upvotes: 1