Mengxi Wang
Mengxi Wang

Reputation: 81

How to Use sendkeys when input type is number with Chrome

HTML

<ion-input [(ngModel)]="login.username" ngControl="username1" type="number" #username1="ngForm" id="userName" required>
</ion-input>

PROTRACTOR TEST CODE

let usern: ElementFinder = element.all(by.css('.text-input')).get(0);
usern.sendKeys('error');
expect(usern.getAttribute("value")).toEqual("error");
browser.sleep(500);
usern.clear();
browser.sleep(1000);
usern.sendKeys('12345');

The element is found but no text is entered into the field. If I change the element to type="text" the protractor command works.And the page view is 'e' and can't be clear.

Secondly if I send string like this: "we2124will", the actually send data is '2124' and the result from getAttribute("value") is 2124.

Thirdly even if I changed the sendKeys to number, the result is not full number string. For example:

Failures:

1) Login page should input username and password
 Message:
   Expected '125' to equal '12345'.
 Stack:
     Error: Failed expectation

There are some number missing.

Upvotes: 1

Views: 1813

Answers (3)

BRass
BRass

Reputation: 3838

Since you're using an <ion-input>, the actual HTML <input> tag will be nested within, and it won't have an id attribute. The effect is that the wrong element can get selected.

Try something like below to grab the nested input tag:

let username = element(by.id('userName')).all(by.tagName('input')).first();
username.sendKeys('fakeUser');

That worked for me.

Upvotes: 3

alecxe
alecxe

Reputation: 473833

As a workaround, you can introduce a reusable function that would perform a slow type by adding delays between send every key.

First of all, add a custom sleep() browser action, put this to onPrepare():

protractor.ActionSequence.prototype.sleep = function (delay) {
    var driver = this.driver_;
    this.schedule_("sleep", function () { driver.sleep(delay); });
    return this;
};

Then, create a reusable function:

function slowSendKeys(elm, text) {
    var actions = browser.actions();
    for (var i = 0, len = text.length; i < len; i++) {
        actions = actions.sendKeys(str[i]).sleep(300);
    }
    return actions.perform();
}

Usage:

var elm = $("ion-input#userName");
slowSendKeys(elm, "12345");

Upvotes: 1

findlayc
findlayc

Reputation: 146

What version of protractor are you using?

Not sure this is the issue but try grabbing the element by ng-model

var elem = element(by.model('login.username'));
elem.sendKeys('error');
expect(elem.getAttribute("value")).toEqual("error");
elem.clear();
elem.sendKeys('12345');
expect(elem.getAttribute("value")).toEqual("12345");

Upvotes: 0

Related Questions