Validation of the data itself entered in the field (character limit)

Asked

Viewed 262 times

-1

Suppose I have a field that has a limit of 15 characters.

In my test i add 16 characters.

How do I validate that they were entered only 15 characters in the field?

browser.findElement(By.xpath("//input[@class='form-control ng-untouched ng-pristine ng-valid ui-inputtext ui-corner-all ui-state-default ui-widget']")).sendKeys("1236767676786983");

WebElement campoID = browser.findElement(By.xpath("//input[@class='form-control ng-untouched ng-pristine ng-valid ui-inputtext ui-corner-all ui-state-default ui-widget']"));

String texto = campoID.getText();
assertEquals("123676767678698", texto);

1 answer

1


Hello,

Normally we use gettext() for most elements, but for "input" the text is kept in the page structure differently, it is under the attribute "value".

Then you should use the "getattribute" method by passing the "value" attribute to capture the input text.

Thus:

String texto = campoID.getAttribute("value");

To validate the amount of characters the best is to assert the amount of characters not in the text:

Thus remaining:

assertEquals(15, texto.length())

To be more readable you can add a message indicating the validation you are doing.

assertEquals("O tamanho do texto esperado é ", 15, texto.length())
//A resposta será: "O tamanho do texto esperado é (15) was (16)" 
//Caso o resultado seja falso.
  • Imart, thanks for the feedback! I tried, but the test returns error! Could you help me? browser.findElement(By.xpath("//input[@class='form-control ng-untouched ng-pristine ng-valid ui-inputtext ui-corner-all ui-state-default ui-widget']")).sendKeys("1236767676786983");
WebElement campoID = browser.findElement(By.xpath("//input[@class='form-control ng-untouched ng-pristine ng-valid ui-inputtext ui-corner-all ui-state-default ui-widget']"));
String texto = campoID.getText();
assertEquals(15, texto.length()); Erro: Expected :15
Actual :0 However I filled the field with character!

  • Could someone help me?

  • Stephen, I edited the answer with what you need to run this test. Give a ok if it worked!

  • Imart, I appreciate the feedback! However, keep giving Expected :2 Actual :0 .... What can it be? browser.findElement(By.xpath("//input[@class='form-control ng-untouched ng-pristine ng-Valid ui-inputtext ui-corner-all ui-state-default ui-widget']"). Sendkeys("1236767676786983"); Webelement fieldID = browser.findElement(By.xpath("//input[@class='form-control ng-untouched ng-pristine ng-Valid ui-inputtext ui-corner-all ui-state-default ui-widget']")); String text = fieldID.getattribute("value"); assertEquals(15, text.length());

  • Could you assist me

  • Dude, check if the xPath you are using is correct, if yes it tries to click off the element, it can be on the label of the element itself but tries to click off the input. This should be right, if not, look in the html where is the value you typed and sent here. uses the function the browser nuisance.

  • Imart, I will check! I’m traveling. As soon as I return already valid! Thanks already!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.