Page 1 of 1

How to overwrite text in a text input field?

Posted: Wed Mar 30, 2011 5:24 pm
by jstr
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?

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

Posted: Wed Mar 30, 2011 6:11 pm
by Ciege
Try this...

Code: Select all

Keyboard.Press("{END}{SHIFT DOWN}{HOME}{SHIFT UP}{DELETE}" + strMyText);

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

Posted: Wed Mar 30, 2011 7:48 pm
by jstr
Worked like a charm. Thanks!

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

Posted: Fri Jun 24, 2011 7:51 pm
by mrusso
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.

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

Posted: Mon Jun 27, 2011 1:26 pm
by Support Team
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

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

Posted: Mon Jun 27, 2011 6:42 pm
by mrusso
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!