Protractor - what best way to check a string IS NOT empty in e2e testing
Asked Answered
I

3

20

What is the best way to ensure a value is found (e.g not an empty string) using e2e testing, my example simply matches the text itself, I want to count the string length & ensure it isn't 0.

describe 'Device Details', ->
device = ionic.Platform.device()
details =
'deviceManufacturer': $('#deviceManufacturer'),
'deviceModel': $('#deviceModel')

it 'Device Manufacturer must not be empty', ->
  expect(details.deviceModel.getText()).toEqual '10'
Inexpert answered 24/2, 2016 at 15:16 Comment(0)
E
21

There are different ways to do that but I prefer toBeNonEmptyString() from the jasmine-matchers package - simple and readable:

expect(details.deviceModel.getText()).toBeNonEmptyString();
Enucleate answered 24/2, 2016 at 15:17 Comment(1)
installing a package while nice, may be over optimisation. I find @Surendra Janwali's answer equally succinct in readability: expect().not.toBe(''); but not require a packageMayweed
A
23

try not.toBe('') to check not empty

expect(details.deviceModel.getText()).not.toBe(''); 

=== other cases ====

 expect('hello world').not.toBe(''); //true 
 expect('').toBe(''); //true
Adonai answered 10/3, 2017 at 22:25 Comment(3)
This is the best answer here. Readable to purpose. Also handles undefined cases well.Mayweed
Bad answer. Many things are not equal to an empty string. Numbers, booleans, instances of virtually anything. If you want to use this for whatever reason, it has to be preceded by a type check.Omer
That's too strong a statement, given the title implies it's known to be string, and the question body's example method can only return a string. Under those assumptions, this is the simplest and easiest answer. It's an important caveat in the general (and my) case though, so I submitted an edit to make the limitation explicit and add the necessary additional check to assert on the type.Knighthood
E
21

There are different ways to do that but I prefer toBeNonEmptyString() from the jasmine-matchers package - simple and readable:

expect(details.deviceModel.getText()).toBeNonEmptyString();
Enucleate answered 24/2, 2016 at 15:17 Comment(1)
installing a package while nice, may be over optimisation. I find @Surendra Janwali's answer equally succinct in readability: expect().not.toBe(''); but not require a packageMayweed
P
18

Without using jasmine-matchers.

   details.deviceModel.getText().then(function(text) {
      expect(text.length).not.toEqual(0)
    });

See comment below for caveat(s)

Phloem answered 24/2, 2016 at 17:0 Comment(2)
thanks i had some issues with the jasmine-matchers so this way is very useful alsoInexpert
It may not be an issue but note that this test will pass for everything except primitives, empty strings, and empty arrays. an object where .length is undefined for example would pass this test as that is not equal to zero. Hope this helps, again, it may not be an issue for your particular case.Mateusz

© 2022 - 2024 — McMap. All rights reserved.