3

I wish to achieve the following:

var textPromise1 = element(by.id('id1')).getText();
var textPromise2 = element(by.id('id2')).getText();

if(textPromise1.SOMELOGIC == textPromise2.SOMELOGIC )
console.log('These are equal')

I know the promise is resolved using .then(). However, I want to know if there exists some way with which above can be achieved!

Thanks,

Sakshi Singla
  • 2,462
  • 1
  • 18
  • 34

1 Answers1

3

You need to resolve the promises for both the texts. The better way here is to use expect and let the expect resolve the promise for you. This is how I solved this kind of problem for myself-

    textPromise1.then(function(data){
    //here you can call an external function that will apply some logic to your data string
    var string1= data.someLogicFunction1();//or perform whatever operations are required on data
    expect(someLogicFunction2(textPromise2)).toBe(string1);

})
Rahul Vig
  • 716
  • 1
  • 7
  • 24
  • Hi Rahul, Thanks for the response. I am aware of the above approach already(as mentioned in the post). expect will yield a presence in my result file. However, all I want to achieve here is an IF-ELSE condition. Is there some other better way of comparison? – Sakshi Singla Feb 18 '15 at 03:48
  • What you will need to do is that resolve the promise and store that value in a global variable. `textPromise1.then(function(data){ global.var1=data })` Then use global.var1 as and when required. – Rahul Vig Feb 18 '15 at 06:09
  • 1
    This does not happen! I tried. The var1 does not travel out of the .then() block until n unless we return it! And IF we return it, then it will also become a promise which then again needs to be resolved using .then and thus renders the same problem again! – Sakshi Singla Feb 18 '15 at 06:51
  • It will. I am currently using this kind of implementation. Make sure you use `global.var` even while using it for comparison. – Rahul Vig Feb 18 '15 at 07:16
  • 1
    I declared globally var flag = false; Then inside a .then block I changed its value using global.flag = true and got it printed outside the .then scope. It gives the value as 'undefined' – Sakshi Singla Feb 18 '15 at 07:26
  • Actually, there is no need to declare the variable flag globally. Just assign the variable with value `global.flag = true ` and then use it anywhere using 'global.flag'. Let me know if this works. – Rahul Vig Feb 18 '15 at 07:54
  • 1
    This did not work either :( Same behavior. Shows value to be 'undefined' – Sakshi Singla Feb 18 '15 at 08:58
  • Can you print the value inside `.then` using `console.log(global.var)` and check? I think you are not getting the value there itself. That would be last shot at it. – Rahul Vig Feb 18 '15 at 09:06
  • 1
    Yes. I am getting the correct value printed inside the .then block. This is wat I did: Did not declare global.flag anywhere. Assigned value to global.flag = true inside the .then block. Then accessed the value inside/outside of the .then() – Sakshi Singla Feb 18 '15 at 09:14
  • I don't know what is happening there. The only way out I think is to nest the `.then` and propagate the values. I have not tried that but worth a try. – Rahul Vig Feb 18 '15 at 09:31
  • @SakshiSingla - the `undefined` you are obtaining is certainly due to the fact that you are asking the promise value before it has been resolved. – Marco Faustinelli Feb 09 '16 at 16:47