Reputation: 499
The form has three fields 1. First Name 2. Last Name 3. Phone
I want max 20 characters length validation in name fields.
Upvotes: 0
Views: 5798
Reputation: 1
There are multiple ways to validate minimum and maximum character length of some text field. Here is a code snippet to get the email field minimum and maximum character length and to validate it, hope i answered your question :)
First get the HTML attribute minlength and maxlength for your required field.
public class LoginPage{
@FindBy(name = "email") WebElement txtEmail;
public int[] getEmailMinMaxCharLength() {
int[] charLength = new int[2];
charLength[0] = Integer.parseInt(txtEmail.getAttribute("minlength"));
charLength[1] = Integer.parseInt(txtEmail.getAttribute("maxlength"));
return charLength;
}
}
public class Test{
LoginPage loginPage = new LoginPage();
@Test public void verifyMinMaxCharLength() {
int[] charlength = loginPage.getEmailMinMaxLength();
int minCharLength = charlength[0];
int maxCharLength = charlength[1];
softAssert.assertEquals(minCharLength, 5);
softAssert.assertEquals(maxCharLength, 50);
softAssert.assertAll();
}
}
Upvotes: 0
Reputation: 1
To check the maximum and minimum length of any field, you can use the HTML's maxlength
and minlength
attribute and then find the length using Selenium's getAttribute("maxlength")
and getAttribute("minlength")
function.
Sample code is given below: Say we're talking about the username field.
//Getting the minimum and maximum value of the username field
int minLengthDefined = Integer.parseInt(mcp.userName().getAttribute("minlength"));
int maxLengthDefined = Integer.parseInt(mcp.userName().getAttribute("maxlength"));
//Get the typed value
String typedValue = mcp.userName().getAttribute("value");
//Get the length of the typed value
int size = typedValue.length();
if( size < minLengthDefined ){
Assert.assertEquals(size, minLengthDefined, "Username field has incorrect value.");
Sysout.out.println("It contains "+size+" characters.");
Sysout.out.println("Username should lie between "+minLengthDefined+ " and "+maxLengthDefined+ " characters." );
}
else if( size > maxLengthDefined) {
Assert.assertEquals(size, maxLengthDefined, "Username field has incorrect value.");
Sysout.out.println("It contains "+size+" characters.");
Sysout.out.println("Username should lie between "+minLengthDefined+ " and "+maxLengthDefined+ " characters." );
}
else
Sysout.out.println("unhandled issue occurred!");
}
Upvotes: 0
Reputation: 1196
There are different ways of verifying validations of fields.
If user try to enter more than max limit in field
Solution:
Using sendKeys() method send more than 20 characters to field,
Case 1: 1. Get text of the same field using getText() method, then perform String.length and perform Assert to make sure character count is 20
Case 2: Perform case 1 first point, then get locator of validation message and get text from it and verify with expected validation message.
Upvotes: 1
Reputation: 119
Enter more than 20 characters using selenium web driver in textbox and then get the text of the same textbox using selenium and calculate the length of the string it should be 20 and not more than that.
Upvotes: 0