Wasi
Wasi

Reputation: 751

How to verify character count of text field?

I want to check the number of characters I can insert in a text field, and was thinking of using 'for loop' but it would not help as Selenium tries to insert more than required character the field will not accept but test goes on without any failure, so is there a way to get character count of the text field?

Upvotes: 0

Views: 11219

Answers (2)

tony2tones
tony2tones

Reputation: 1670

Using Protractor I captured the actual text in the field and then did a forloop to count each letter.

element(by.css('elementPATH')).getAttribute('value').then(function(words){
            //forloop to count each word
            var x = 0
            for(var i = 0; i < words.length; i++) {
                x = x + 1;
            };
            //check condition
            expect(x).toBe(200);
            return true;
        });

Let me know if this helps.

Upvotes: 0

Petr Janeček
Petr Janeček

Reputation: 38444

Would this work?

final String myLongString = "Something horrrrribly looooong";
final int longStringLength = myLongString.length();

// assuming driver is a healthy WebDriver instance
WebElement elem = driver.findElement(By.id("myInput"));
elem.sendKeys(myLongString);
// it's possible that you'll first need to lose focus on elem before the next line
int realLength = elem.getValue().length();
assertEquals(longStringLength, realLength);

Upvotes: 1

Related Questions