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

40.1k Views Asked by At

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'
3

There are 3 best solutions below

1
On BEST ANSWER

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

expect(details.deviceModel.getText()).toBeNonEmptyString();
2
On

Without using jasmine-matchers.

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

See comment below for caveat(s)

3
On

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

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

=== other cases ====

 expect('hello world').not.toBe(''); //true 
 expect('').toBe(''); //true