Many times we have a situation when we have to test the value of a web-element is a number or not. If the web-element stores the value in integer format then we can easily check that whether the desired element is number or not but suppose if we have a string that contains some integer value and we have to assert that value that whether it's a number or not then we will have to face the issue on asserting the string with Integer will return false and our testcase will fail.
Suppose we have the HTML:
<div class="infoBox" style="margin-bottom:10px;">
<div>
4
<br/>
<span>Employees</span>
</div>
</div>
If we print the text of above web-element then the output will be "4 Employees". Now we have to test that some count of employees should be present. In first look, the solution is that we first split the string with space and then check that whether the first index element is Integer or not. Since the first index element is a number but it is stored in String form, so this will not fulfill our requirement. The other face of this problem is that first converting the zero index value to an integer and checking that the value is an integer or not is also of no use.
So, the best solution of this issue is:
company_count = @session.find(:xpath,"//*[@id='profileInformation']/div[@class='infoBox']/div[1]").text.split(' ')
is_numeric = true if Float(company_count[0]) rescue false
expect(is_numeric).to be(true)
The value of "is_numeric" will be true if the value of zero index is not Float and then assert that the value of "is_numeric" variable should be true.
0 Comment(s)