protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Retrieving Label values to verify text later

Open mohit-excelindia opened this issue 12 years ago • 20 comments

Hi All,

I have some problem in verifying the text and values of labels and text fields. I know value of a label can be retrieved using following

prot.findElement(By.id('element')).getText().then(function(text){ console.log(text) });

but if I tries to access the value retrieved outside the promise, then I am not able to do this. Is there a way I can access the value of the text outside the promise function and use the retrieved value to verify or assert with some other values??

Thanks

mohit-excelindia avatar Dec 17 '13 17:12 mohit-excelindia

My understanding is that because code outside of the then are run before the promise resolves, you can't access the text before the promise resolves. Another option would be to chain on a then like so:

var myText;
prot.findElement(By.id('element')).getText().then(function(text){
    myText = text
}).then(function(){
    //do something with myText
})

console.log(myText) //will be undefined

wlingke avatar Dec 17 '13 17:12 wlingke

Does this not seems like some limitation as if I have very large test to perform and the text I retrieves as my first statement and then verifies it at the very end and it will be more complex if I have verify quite a number of text, then there will be very huge chain.

mohit-excelindia avatar Dec 18 '13 04:12 mohit-excelindia

If you're getting a lot of text, you can get all the text, then do all the manipulation of the data afterwards. Like this:

var myText1, myText2
prot.findElement(By.id('element1')).getText().then(function(text){
    myText1 = text
})
prot.findElement(By.id('element2')).getText().then(function(text){
    myText2 = text
}).then(function(){
   //do stuff with myText1 and myText2
})

The chains are unfortunately generated by the fact that getText returns a promise. There's no way around that as far as I know.

wlingke avatar Dec 18 '13 04:12 wlingke

In the last then function myText1 will not work as its scope was limited to first findElement only. And exactly that was was my point if we have to verify the details of the fields in a form, then there will be too much chaining which will as per me add more to complexity.

mohit-excelindia avatar Dec 18 '13 04:12 mohit-excelindia

The last then should be able to access the text set from the first findElement via myText1. There must be a bug elsewhere in your code. I would recommend reducing the complexity of your test and just trying out the structure above.

wlingke avatar Dec 18 '13 04:12 wlingke

I will try by accessing myText1 in chain after second findElement, but I guess it will not work. I will post after trying it out.

mohit-excelindia avatar Dec 18 '13 04:12 mohit-excelindia

It should work as long as you declare myText1 outside of the first then. In other words, dont do this:

//Don't do this

prot.findElement(By.id('element1')).getText().then(function(text){
    var myText1 = text  //dont declare here
})

wlingke avatar Dec 18 '13 05:12 wlingke

Here is what I did

var text1, text2;
prot.driver.findElement(protractor.By.className('element1')).getText().then(function(text){ text1 = text; })

prot.driver.findElement(protractor.By.css('element2')).getText().then(function(text){
text2 = text;
}).then(function(){
console.log(text1);
console.log(text2);
});

but I got the following stacktrace: Failures:

  1. Exception loading: C:\Users\Mohit\AppData\Roaming\npm\node_modules\protract or\example\test.js Error Message: SyntaxError: Unexpected end of input Stacktrace: SyntaxError: Unexpected end of input at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module.load (module.js:312:12) at Module.require (module.js:364:17) at require (module.js:380:17) at Object.exports.executeSpecs (C:\Users\Mohit\AppData\Roaming\npm\node_modu les\protractor\node_modules\minijasminenode\lib\index.js:112:7) at C:\Users\Mohit\AppData\Roaming\npm\node_modules\protractor\lib\runner.js: 258:14 at C:\Users\Mohit\AppData\Roaming\npm\node_modules\protractor\node_modules\s elenium-webdriver\lib\goog\base.js:1178:15 at webdriver.promise.ControlFlow.runInNewFrame (C:\Users\Mohit\AppData\Roam ing\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\pr omise.js:1438:20)

Is there a problem with chaining?

mohit-excelindia avatar Dec 18 '13 17:12 mohit-excelindia

your second protractor.By.css('element2') should be protractor.By.className('element2')

wlingke avatar Dec 18 '13 17:12 wlingke

How does it make a difference if I am finding an element with css-selector or className. It should work fine either way.

mohit-excelindia avatar Dec 18 '13 18:12 mohit-excelindia

you ommitted the period in the css selector. Use protractor.By.css('.element2')

wlingke avatar Dec 18 '13 18:12 wlingke

Actually I have used it correctly in the code I have used. Just I have replaced the elements with dummy text here and thus it is not looking good. Here is the actual code

var text1, text2;

prot.driver.findElement(protractor.By.className('btn-info')).getText().then(function(text){
text1 = text;
});

prot.driver.findElement(protractor.By.css('p.pull-right')).getText().then(function(text){
text2 = text;
}).then(function(){
console.log(text1);
console.log(text2);
});

This code produces the above said error.

mohit-excelindia avatar Dec 18 '13 18:12 mohit-excelindia

Ah sorry, I didn't look closely at the error. It says you have a syntax error somewhere in your code. It doesn't look like its due to the then chaining. did you jshint your code?

wlingke avatar Dec 18 '13 18:12 wlingke

Thanks. There was an error and code mentioned by you executed in the desired manner.

So this is what I have concluded from the above exercise: Element retrieved in any of the promise can be accessed in the last promise of the it.

mohit-excelindia avatar Dec 18 '13 18:12 mohit-excelindia

Due to promise chaining, you can access retrieve a value by any later then (not necessarily just the last). You should check out the documentation on control flow. Please close this issue.

wlingke avatar Dec 18 '13 18:12 wlingke

Just a quick last question. Does later 'then' function needs to be in same 'it' or we can access in different 'it' also?

mohit-excelindia avatar Dec 18 '13 18:12 mohit-excelindia

Declare myText1 outside of the it block and you can access it in another it block. However, I strongly recommend against doing this because it makes your tests dependent on each other and this is very bad practice.

wlingke avatar Dec 18 '13 19:12 wlingke

Thanks @wlingke for the solution! Yup, this is a limitation of promises. I think it'd be great to add an example like this to the control-flow documentation (adding to my todos).

juliemr avatar Dec 18 '13 20:12 juliemr

Issue resolved, thus closing it.

mohit-excelindia avatar Dec 19 '13 17:12 mohit-excelindia

Hi All,

I am using protractor trying to capture a label value from html page shows null . Please advice. Thanks

error :Expected null to equal 'xyz'.

Code : element(by.binding('ctrl.formData.branchAddressLine1')).getAttribute('value').then(function (value) { expect(value).toEqual('xyz'); });

reddynr avatar Nov 17 '16 20:11 reddynr