How can I determine text fields and text areas?

Best practices, code snippets for common functionality, examples, and guidelines.
xeronar
Posts: 28
Joined: Mon May 22, 2017 8:19 pm

How can I determine text fields and text areas?

Post by xeronar » Wed May 24, 2017 4:33 pm

Hello everyone,

I'm kinda new to Ranorex. I've written a function that clears the whole text of a text field for a web application. Unfortunately, this does not work on text areas, so i needed another line of code, as seen below. Is there any command to ask el if it is a text field or text area? Is there a much easier way to implement this (strg + a excluded)?

Ranorex Version: 7.0.1

Code: Select all

 Namespace xy.z
	Public Module Extensions
		<Extension()> 
		Public Sub ClearText(ByVal el As Ranorex.Adapter)
			if(el is a text area)
	        	  el.PressKeys("{PageDown}{SHIFT DOWN}{PageUp}{SHIFT UP}{DELETE}")
                        else
                          ''el is a text field
	                  el.PressKeys("{END}{SHIFT DOWN}{HOME}{SHIFT UP}{DELETE}")
		End Sub         
	End Module
End Namespace
Edit: I found a solution by using GetType:

Code: Select all

Imports Ranorex

...

''Is el a text field?
If (el.[GetType]() Is GetType(InputTag))
....
''Is el a text area?
If(el.[GetType]() Is GetType(TextAreaTag))
....
So this works now, but is there a better way to implement this?

McTurtle
Posts: 297
Joined: Thu Feb 23, 2017 10:37 am
Location: Benedikt, Slovenia

Re: How can I determine text fields and text areas?

Post by McTurtle » Mon May 29, 2017 3:22 pm

Hello xeronar,

Thank you for reporting the solution.

The way that you have implemented this closely resembles user behavior. Therefore, I believe this implementation is fine.

Regards,
McTurtle

Vaughan.Douglas
Posts: 254
Joined: Tue Mar 24, 2015 5:05 pm
Location: Des Moines, Iowa, USA

Re: How can I determine text fields and text areas?

Post by Vaughan.Douglas » Tue May 30, 2017 5:23 pm

I usually just set the attribute value to "" or empty string just before doing my send keys. Unfortunately not all text inputs are created equally and you're going to run into different challenges based on different implementation.

For example, some won't "activate" until you actually click into the field. Occasionally you'll run into a situation where it will validate on some weird event, so if you PressKeys("{END}{SHIFT DOWN}{HOME}{SHIFT UP}{DELETE}") then a PressKeys("Your Text HERE") it will fail. You have to do a PressKeys("{END}{SHIFT DOWN}{HOME}{SHIFT UP}{DELETE}Your Text HERE")

TextAreas have caused me an even bigger headache. In one specific application you can't even get to the text area object until you've already clicked on the text area object. You have to know what it is before it's a text area then be able to distinguish it after you've clicked on it because it changes the DOM.

I've tried extensions, overrides and any other bit of shenanigans I can dream up. Nothing ever works every time all the time. Keep it simple and only add complexity as needed.
Doug Vaughan

xeronar
Posts: 28
Joined: Mon May 22, 2017 8:19 pm

Re: How can I determine text fields and text areas?

Post by xeronar » Mon Jun 05, 2017 8:25 pm

@McTurtle:
Hey thanks for the reply, I'm glad to hear that.

@Vaughan
Thanks for sharing your experience on text fields and text areas. I'll keep that in mind when I'll encounter such problems in my future tests.