Sean Morris
Sean Morris

Reputation: 135

Protractor - Checking if input field has text

I'm writing a simple protractor test that's intended to check if a certain input tag contains text after a button is clicked. I've tried a few things and I'm currently trying to use protractor.ExpectedConditions to check if it contains any text. Here's my code:

it("should click submit", function() {
    var EC = protractor.ExpectedConditions;
    var status = element(by.id('status'));
    $("#btnSubmitQuery").click();
    var condition = EC.textToBePresentInElement(status, 'C');
    browser.wait(condition, 8000, "Text is still not present");
    status.getText().then(function (text) {
        console.log(text);
    });
});

A REST call is made to a server once btnSubmitQuery is clicked but the problem is that I can never get any value for status. What happens when I run this code is that the browser just waits for 8 seconds then closes, even though I can see text in the element. Nothing gets logged to the console. Any ideas?

EDIT: The html element I'm checking looks like this:

<td><input id="status" type="text" class="form-control" placeholder="PaxStatus ..." value="{{paxInformation.status}}"ng-readonly="true"></td>

Upvotes: 5

Views: 5129

Answers (3)

Lelik_
Lelik_

Reputation: 7

I am using this one

var EC = protractor.ExpectedConditions;
var txt = browser.wait(EC.textToBePresentInElementValue(status, 'C'), 5000)

return browser.wait(txt, 5000).catch(() => { 
throw new Error('Text is still not presentt');});

Upvotes: 0

alecxe
alecxe

Reputation: 473833

Just to improve @maurycy's answer. There is a built-in Expected Condition for this particular case:

var EC = protractor.ExpectedConditions;
browser.wait(EC.textToBePresentInElementValue(status, 'C'), 5000)

Upvotes: 7

maurycy
maurycy

Reputation: 8465

Shouldn't it be more like that:

condition = function () {
  return status.getAttribute('value').then(function (value) {
    return value.length > 0
  })
};
browser.wait(condition, 8000, "Text is still not present").then(function () {
  status.getAttribute('value').then(function (text) {
    console.log(text);
  })
});

Upvotes: 6

Related Questions