How to overwrite text in a text input field?

Class library usage, coding and language questions.
jstr
Posts: 4
Joined: Wed Mar 30, 2011 3:59 pm

How to overwrite text in a text input field?

Post by jstr » Wed Mar 30, 2011 5:24 pm

I want use the Automation API to overwrite the text in a text input field on a web page. Is there a simpler way to do it than clicking in the field and pressing the backspace key repeatedly, then typing the new text?

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: How to overwrite text in a text input field?

Post by Ciege » Wed Mar 30, 2011 6:11 pm

Try this...

Code: Select all

Keyboard.Press("{END}{SHIFT DOWN}{HOME}{SHIFT UP}{DELETE}" + strMyText);
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

jstr
Posts: 4
Joined: Wed Mar 30, 2011 3:59 pm

Re: How to overwrite text in a text input field?

Post by jstr » Wed Mar 30, 2011 7:48 pm

Worked like a charm. Thanks!

mrusso
Posts: 16
Joined: Thu Feb 03, 2011 12:54 am

Re: How to overwrite text in a text input field?

Post by mrusso » Fri Jun 24, 2011 7:51 pm

This seems like a common enough usage that there should be a method within Ranorex.InputTag, or any type of adapter that deals with textboxes, to do this very thing.

In my case, I find that I'll more often want to overwrite an input box than add characters to the string that's already present.

I wonder what the best practice for adding such functionality to Ranorex adapters would be.

Extension methods? Static methods? Container classes? Inheritance?

I've currently started to add extension methods, so that I can call an input box from a repository (ex: repo.browser.inputbox), and directly use my extensions (ex: inputbox.OverwriteText("test") ), but I'm not sure about how maintainable this solution is vs. the alternatives.

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: How to overwrite text in a text input field?

Post by Support Team » Mon Jun 27, 2011 1:26 pm

Hello,

things that come up again and again for one tester is most likely different from things that reoccur for other testers. To provide API functions for all the situations would unnecessarily blow up the API. Ranorex API is built on .NET and with C# or VB it is very easy to write a function that can be reused again and again. If built against the 3.5 framework then extension classes are a good fit for such situations:
public static class adapter_extensions
	{
		public static void ClearText(this Ranorex.Adapter a)
		{
			a.PressKeys("{END}{SHIFT DOWN}{HOME}{SHIFT UP}{DELETE}");
		}
	}

        // other code here

        void ITestModule.Run()
        {
            // other code here
            Ranorex.Text t = "/form[@controlname='formVipApplication']/text/text[@accessiblename='First Name:']";
            t.ClearText();
        }
Regards,
Roland
Ranorex Support Team
.
Image

mrusso
Posts: 16
Joined: Thu Feb 03, 2011 12:54 am

Re: How to overwrite text in a text input field?

Post by mrusso » Mon Jun 27, 2011 6:42 pm

That's a great example of Extension Methods for this particular example.

I like how it integrates with code completion, acting like the method is simply another one of the (extended) object's public methods.

The only other part to worry about was, like you mentioned, that I had to set the project's "Target Framework" to ".Net Framework 3.5" and add a reference to "System.Core".

Thanks for the response!