How to get the maximum allowed character in any text box eg.user name, email address, in selenium webdriver ?
3 Answers
You can find the field and get the maxlength attribute
max_length=find('input#id').getAttribute('maxlength')
In the IDE I think it would be something like this to store it in 'x':
<td>storeAttribute</td>
<td>//input[@id='identifier']@maxlength</td>
<td>x</td>
although I would use css for the identifier, more like:
input#identifier@maxlength
Note: in terms of how many characters the user is putting in the field - https://stackoverflow.com/a/3166107/631619 shows over a million allowed so finding out by by using a loop might take a long while (due to the need to actually interact with the browser) depending on whether there is a server side limit being enforced - that will require a POST or PUT so you'll need thousands of them
-
@FDM I think you mean "which is not always present". I just checked this site, slack, my bank and they ALL have the maxlength attribute on input fields :). Google did not however. So not dependable on many sites but if it is used on the site in question it will work. so depends on that. not really 99% 'not' though for major sites unless you have more stats on that Commented Jul 15, 2017 at 11:34
-
Yeah sorry, not 99 out of 100 but in my experience, with in-house company apps I have never seen it used.– FDMCommented Jul 15, 2017 at 15:52
Different scenarios:
1) maxlength attribute
In HTML you might have a maxlength
attribute on your input tag. In this case you can extract the value with Selenium's GetAttribute("maxlength")
or similar.
2) No maxlength attribute
In this case, the validation (if any) is either server-side or client-side (Javascript). Server-side code is impossible to see (ask the developers), client-side Javascript can be analysed but that will be a manual process rather than a Selenium script.
However, if you still want to use Selenium to figure it out, here's the pseudo-code logic on how to do it:
- Enter a character N times in the input field (use a loop).
- Get the input's value and see if it is N characters long.
- If it is, start at 1. again with N = N+1.
- If it's not, the maximum length is N-1
Edit: it's possible that you could enter 500 characters but the server-side validation is at 100, for example. To test this, you'll need to perform another action (like saving) to see if any validation gets triggered.
To check the maximum and minimum length of any field, you can use the HTML's maxlenght and minlength attribute and then find the lenght 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 lenght 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!");
}