如何验证文本字段的字符数?

时间:2012-06-06 11:11:12

标签: selenium junit automation testng

我想检查一下我可以在文本字段中插入的字符数,并且正在考虑使用'for循环',但是当Selenium试图插入超过字段不接受的所需字符时它无济于事但是测试结果在没有任何失败的情况下,有没有办法获得文本字段的字符数?

2 个答案:

答案 0 :(得分:1)

这会有用吗?

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);

答案 1 :(得分:0)

使用量角器我在字段中捕获了实际文本,然后做了一个forloop来计算每个字母。

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;
        });

如果有帮助,请告诉我。