Hello Guys,
You should not be able to type alphabets or special characters in a field which supposed to accept only numbers. Manually you can type alphabets or special characters and verify. But in automation it is little tricky.
Your application developer might restrict alphabets and special characters by setting value of “type” attribute of input tag as “number”. In this case automation will be easy.
Before we move ahead, I would like to say when type attribute of an input box is set as “number”, it works as below:
As per W3:
A string is a valid floating-point number if it consists of:
- Optionally, a U+002D HYPHEN-MINUS character (-).
- One or both of the following, in the given order:
- A series of one or more ASCII digits i.e. 0-9 .
- Both of the following, in the given order:
- A single U+002E FULL STOP character (.).
- A series of one or more ASCII digits.
- Optionally:
- Either a U+0065 LATIN SMALL LETTER E character (e) or a U+0045 LATIN CAPITAL LETTER E character (E).
- Optionally, a U+002D HYPHEN-MINUS character (-) or U+002B PLUS SIGN character (+).
- A series of one or more ASCII digits.
So, alphabets ‘E’ and ‘e’ are acceptable alphabets. While ‘+’, ‘-‘ and ‘.’ are acceptable special characters. So, 1.23E-11 or 1.23E+11 or 2e5 are valid values.
In above case, we can automatically test functionality in two ways:
- Type alphabets and special characters ( except acceptable as above ) and retrieve typed text. It should be empty.
- Get the “type” attribute of input tag and assert if it is a “number”.
OnlyNumber.html:
[html]
Enter a number : <input type="number" name="quantity">
[/html]
Java Code:
package HandlingDropdown; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class TypeInNumberInputBox { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("file:///C:/Users/amodm/Downloads/OnlyNumber.html"); // Locating firstname web element WebElement numberField = driver.findElement(By.name("quantity")); /************************** Way 1 ********************************/ // Type alphabets numberField.sendKeys("ASSDuiouoi"); // Retrieve typed value String typedValue = numberField.getAttribute("value"); // Get the length of typed value int size = typedValue.length(); if (size == 0) { System.out.println("Alphabets are not allowed."); } // Type special Characters numberField.clear(); numberField.sendKeys("#%$%&"); // Retrieve typed value String typedValue1 = numberField.getAttribute("value"); // Get the length of typed value int size1 = typedValue1.length(); if (size1 == 0) { System.out.println("Special characters are not allowed."); } /************************** Way 2 ********************************/ // Get type attribute of input box String typeAttributeValue = numberField.getAttribute("type"); if (typeAttributeValue.equals("number")) { System.out.println("Only valid numbers are allowed."); } else System.out.println("There is no restriction of Only numbers."); // Closing driver driver.quit(); } }
Output:
[java]
Alphabets are not allowed.
Special characters are not allowed.
Only valid numbers are allowed.
[/java]
But if your application is developed by putting validation after typing by user, above ways will not work.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium
if the input type=”numeric” how we can verify whether input box only accept numbers not float,decimal .
How to enter only a integer into a field which takes only integer value in selenium java
why size==0 ??? if we are passing alphabets or special characters … it would have some length right ??? so i didnt understand why are comparing to 0 ???
Read the post again carefully.