Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
like image 216
Zabs Avatar asked Feb 24 '16 15:02

Zabs


3 Answers

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

expect(details.deviceModel.getText()).toBeNonEmptyString();
like image 172
alecxe Avatar answered Sep 28 '22 16:09

alecxe


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

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

=== other cases ====

 expect('hello world').not.toBe(''); //true 
 expect('').toBe(''); //true
like image 34
Surendra Jnawali Avatar answered Sep 28 '22 16:09

Surendra Jnawali


Without using jasmine-matchers.

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

See comment below for caveat(s)

like image 41
KCaradonna Avatar answered Sep 28 '22 16:09

KCaradonna