Protractor: Matching Strings to be case insensitive

Created on 17 Jul 2014  路  4Comments  路  Source: angular/protractor

Hi @juliemr

In my application I have status of an event.
I have created an object for those status while writing test scripts like this:

var satus = {
open : 'Open',
inProgress : 'In-Progress'
closed : 'Closed'
};

I have that status displayed in many pages of the application and at different pages it is written in Upper Case letters while not in others.

Can we expect Strings as insensitive?

Thanks,
Mohit

question

Most helpful comment

You could convert the strings to lower case. To do that, you'll have to wait until the promise is resolved:

elem.getText().then(function(text) {
  expect(text.toLowerCase()).toEqual('closed');
});

Closing since this is a question, not a bug report or feature request.

All 4 comments

You could convert the strings to lower case. To do that, you'll have to wait until the promise is resolved:

elem.getText().then(function(text) {
  expect(text.toLowerCase()).toEqual('closed');
});

Closing since this is a question, not a bug report or feature request.

ok

for
element.all(by.css('span[ng-if="vm.selectedMerchant"]')).first().getText().toLowerCase()

ERROR: TypeError: element.all(...).first(...).getText(...).toLowerCase is not a function

but

for

element.all(by.css('span[ng-if="vm.selectedMerchant"]')).first().getText()

there is output string.. What is wrong?

If I'm reading your question correctly, caneraydinbey, the answer to your question relates to promises.

The element.all function is an asynchronous function known as a promise, as are the first and getText functions, chaining off of the element promise. Because of this, you're not actually getting a string back. In contrast, the lowerCase is a synchronous function, one which is on the String prototype. The expect function can take a promise and resolve it, using the result, which is why it seems like getText returns a string.

If you wanted to actually lower case the results of your getText, you'd need to chain another promise to the end of it, a then promise most likely, which will take the text from the resolution of the getText promise. Then, you can lowercase the text, and return it for the expect statement.

Here's an example, where I set it to a variable for ease of use:

var textElementText = element.all(by.css('span[ng-if="vm.selectedMerchant"]')).first().getText().then(function(text){ return text.lowerCase() })

expect(textElementText).toContain('lowercase')

Of course, this assumes that textElementText actually contains the string 'lowercase', otherwise the test fails.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidkarlsen picture davidkarlsen  路  3Comments

amedvedjev picture amedvedjev  路  3Comments

mvolkmann picture mvolkmann  路  3Comments

jmcollin78 picture jmcollin78  路  3Comments

koshkarov picture koshkarov  路  3Comments