Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 19967

How to enter a single backspace with Protractor?

I would like to use .sendKeys() to enter text in an input field, and then backspaces one character. I've tried using the following code with .sendKeys(protractor.Key.BACK_SPACE) but it seems like it clears the entire field.

My test:

describe('backspace', function() {
    it('types some stuff and backspaces one character', function() {
        element(by.model('invoice.customerName')).sendKeys('Ali Khoda');

        element(by.model('invoice.customerName')).sendKeys(protractor.Key.BACK_SPACE);

        expect(element(by.model('invoice.customerName')).getText()).toBe('Ali Khod');

    });
});

The error: Expected '' to be 'Ali Khod'.

Is there a way to backspace only a single character?

UPDATE:

I've also tried the following since .sendKeys() returns a promise by default.

describe('backspace', function() {
    it('types some stuff and backspaces one character', function() {
        element(by.model('invoice.customerName')).sendKeys('Ali Khoda').then(function() {

            element(by.model('invoice.customerName')).sendKeys(protractor.Key.BACK_SPACE).then(function() {

                expect(element(by.model('invoice.customerName')).getText()).toBe('Ali Khod');

            });

        });

    });
});

The test fails just the same.

Upvotes: 6

Views: 9833

Answers (1)

Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 19967

The workaround I found was the following:

expect(element(by.model('invoice.customerName')).getAttribute('value')).toBe('Ali Khod');

Instead of using .getText(), I used .getAttribute('value').

Upvotes: 1

Related Questions