Here is a neat trick, that I did not invent but I will pass along anyway. Say you have a web adapter, and you would like to perform some action on a web page, after the user hits the Submit button. Like me, you might think you would trigger your automation on the ‘Clicking’ event for the Submit button. Alas, you would be wrong, as I found out for myself. I went round and round trying different things, but ultimately, no amount of rearranging logic, or introducing delays worked. By the time your OpenSpan automation responds to the submit button clicking event, the web page is long gone.
The reason we needed to modify the web page, after the Submit button was clicked, was interesting in itself. We had disabled some controls on this web page because we did not want users modifying them. (That, of course, is one of the major use cases for OpenSpan. You can give a user access to an application, but limit the amount of damage they can do, without going to the application vendor, and forking over big bucks for custom mods.) Unfortunately, when the web page was submitted with the controls (actually, one control in particular) disabled, the web server threw an error. Why should a web server throw a fit just because one control was disabled? Seems like a bug or at least sloppy coding to me. I wanted the web app’s vendor to fix this, but they would have none of that. It worked fine without OpenSpan, so the problem is OpenSpan. So we could not enable the controls before the user hit Submit and we could not leave them disabled after the user hit Submit. A classic darned if you do and darned if you don’t situation.
So how do we solve this dilemma? How do you execute an OpenSpan automation on a web page, after the user hits Submit, but before it gets posted to the server? As I learned from the OpenSpan gurus, so I will teach you. Read on.
Let’s use this web page for our target application: http://www.w3schools.com/html/html_forms.asp Among a lot of other controls, it has a Username field and a Submit button. When you click on the Submit button another page is opened and the contents of the Username field is displayed (among other text). Go ahead, give it a try, I’ll wait…
Let’s pretend we have been given a requirement to not disable the Username field, but set the Username to “OpenSpan” regardless of what is entered in that field, after the user hits Submit.
First thing to do is create a solution with an automation and a web adapter. Set the StartPage to the fore mentioned URL and interrogate the Username text field and the Submit button. And you should end up with something along these lines in the studio:
By the way, right now I am using OS version 5.3 on 64bit Windows 7. If you read the release notes you will see that the latest version of the browser it works with is 32bit IE 9. What this means, is that I had to uninstall IE 11, uninstall IE 10, and when creating the web adapter I had to set the “BrowserBitness” property to “Only32bit”.
In the Object Explorer we see that the Submit and user fields are elements of the form named “input0”. If we go to that webpage, right click, select View source, and search for “input0”, we find this snippet of code:
<form name=”input0″ target=“_blank” action=“html_form_action.asp” method=“get”>
Username: <input type=“text” name=“user” size=“20”>
<input type=“submit” value=“Submit”>
For the script itself, I needed to somehow change the type of the element, that currently is set to “submit”, to something else, so that when the user clicked on Submit, the form is not submitted. How about if we just make it a plain button? But how do we change the type of that particular element? We need to know the name of the form and the element, to modify an attribute of that element. But that element does not have a name. In that case we need to use the Elements array of the form to access the element we are targeting. After some (OK a lot!) experimentation, I came up with this bit of code:
What this script does, is set the type of element, of form input0, to ‘button’. Sure enough, when we execute the this solution, (StartMethod is set to Start, on Web Application1 so we don’t need to manually open the web site.) and click on the Submit button, nothing happens.
Now we still need eventually submit the form, after updating the Username field. In that case I’m going to assume there exists a submit function we can call, to perform the submit action. To do that we add another ExecuteScript method to the automation. This one is triggered on the Submit button Click event. Even though the Submit button on the web page is no longer wired to the submit action, the events on the control still work as far as OpenSpan is concerned.
Just for fun, instead of setting the Username in a separate parameter block on the automation, I just included it in the script method. So for the first line: For the form named “input0”, and the element named “user”, set the value to “OpenSpan”. On line two: call the submit() function for the form named “input0”. Sure enough, when we execute this solution, “OpenSpan” is written into the Username field and displayed on the form that is opened after the user clicks on Submit. Notice in this case the Username text field had a name attribute, so we could address it by name, instead of through the elements array.
In this way we take away the name of the On Click function from the button, so nothing happens when the user clicks on it. When we are ready to actually execute the submit, we simply use another ExecuteScript block to call the function that was originally specified, by the onclick parameter.