how to test if a string DON'T match using protractor
Asked Answered
F

3

18

I'm migrating my karma-ng-scenario tests suite to protractor. I would like to do something like

// karma-ng-scenario
expect(element('legend').text()).not().toBe("LOGIN_CONNECT");

in the protractor way. But it seems there isn't a not() function.

I'm using angular-translate to bind the LONGIN_CONNECT string into multiple languages and I want to test if the string is translated.

More globally, is there a a way test if something is different ? ... don't have a class, don't exists on the page, is not selected, ...

Fijian answered 6/2, 2014 at 9:11 Comment(0)
P
25

It is definitely worth looking at the API docs. I have these open pretty much all the time. There are lots of Web Driver functions you can use like isEnabled(), isDisplayed(), isSelected() etc. Protractor uses Jasmine syntax so you can use '.toBe(false)' to assert things are false. To check for classes, you can do something like:

expect(myElement.getAttribute('class')).toContain('my-class-name');

To compare strings and assert that they do NOT match you could use .not. Jasmine docs say:

Every matcher's criteria can be inverted by prepending .not:

expect(x).not.toEqual(y); compares objects or primitives x and y and passes if they are not equivalent

Pyro answered 6/2, 2014 at 9:51 Comment(1)
my fault ! I'm pretty new in jasmine, karma, protractor and there is a lot of things to learn ... I confused the not jasmine function and the not() karma function ! It works fine with expect(web.findElement(by.tagName('legend')).getText()).not.toMatch("LOGIN_CONNECT");Fijian
W
5

You can use something like:

expect(model.getText()).not.toContain('abcdef');

There is a .not property nowadays.

Weihs answered 30/4, 2018 at 19:35 Comment(1)
This is the most elegant solution in my version of jasmine/protractorKyliekylila
T
4

I'm using the following to check for NOT matching:

expect(element('legend').text() === "LOGIN_CONNECT").toBe(false);
Tedious answered 21/3, 2016 at 8:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.