Showing posts with label AJAX. Show all posts
Showing posts with label AJAX. Show all posts

Wednesday, June 25, 2014

Simulate WebForm submit() in VBScript

 

One of the challenges in support of Web applications is automated testing and monitoring. There are numerous approaches and technologies implementing diverse methods: there is also Visual Studio Load and Web Tests among them. But in some situations usage of external tools and technologies is very limited. Due to usual system limitations and restrictions, good integration with different products and systems and high costs efficiency scripting became the most popular technology for automated Web application tests.

VBScript - as particular scripting technology - allows simulation of user interaction with Web applications.

One of the most common automation tasks is Web form submission. This task requires implementation of HTTP POST request sending form data and usually according simulation of Web control action (submit()).

Basically, there are two ways to implement Web form submission with scripting: automation of Internet Explorer action (control via scripts) and implementation of sequential Web requests, sent directly to Web application (direct scripting of HTTP requests).

Internet Explorer implements its own object model, which can be accessed via scripting and is instrumented to perform required operations: navigate to an URL, call page scripts, submit forms, handle page events etc. Unfortunately, this technology is sometimes hard to use in environments, where Internet Explorer enhanced security is turned on or because of additional security restrictions. Additionally, usage of Internet Explorer automation means one more tier involved into automation process (Internet Explorer application itself), which may impact performance, configuration and functionality of tests.

Direct scripting of Web requests is usually implemented using built-in operating system objects or additional components from Microsoft or 3rd parties. Scripting Msxml component is very popular in this field, has good documentation, eco-system of samples and how-to's.

Msxml component enables script developers to implement HTTP requests: GET, POST.

Here is a typical approach to simulate an ASP.NET WebForm submission using scripting of Msxml component.

The recommended way to investigate and implement automation of Web application interactions includes usage of following tools/sources:

  • Fiddler: Web requests capturing and analyzing tool, (free software, actually owned by Telerik)
  • Notepad or similar editor to write and edit automation scripts
  • OS platform with VBS scripting enabled
  • Msxml component: in the most common case is installed on the platform, but can be downloaded and installed separately

Short description of the approach: implementation of a POST request with form data is not mostly enough to simulate form submission - the submit() action must be included in POST request as well to simulate default form submit control action. For ASP.NET WebForms it means sending of according VIEWSTATE and EVENTVALIDATION data with POST request.

Step 1. Implementing GET request

To obtain the VIEWSTATE and EVENTVALIDATION data for later submit() simulation the GET request must be sent

Set xmlHttp = CreateObject("Msxml2.ServerXMLHTTP.6.0")
Call xmlHttp .open("GET", UrlToWebForm, False)
xmlHttp.send

The UrlToWebForm must contain Url of the WebForm for submission automation. In doubt start Fiddler, open Internet Browser, navigate to WebForm and monitor Url listed in Fiddler session list

responseText = xmlHttp.responseText

The responseText contains the VIEWSTATE and EVENTVALIDATION data, which must be extracted to reuse in POST request

REM extract VIEWSTATE
Function GetViewState(response)

idxStart = InStr(1,response,"__VIEWSTATE")
idxStart = InStr(idxStart,response,"value=") + 7
idxEnd = InStr(idxStart + 2, response, """")
GetViewState = Mid(response,idxStart, idxEnd - idxStart)

End Function

REM extract EVENTVALIDATION
Function GetEventValidation(response)

idxStart = InStr(1,response,"__EVENTVALIDATION")
idxStart = InStr(idxStart,response,"value=") + 7
idxEnd = InStr(idxStart + 2, response, """")
GetEventValidation = Mid(response,idxStart, idxEnd - idxStart)

End Function

Step 2. Construct POST request to submit form data

The in step 1 instantiated object can be reused for form submission using POST request

Call xmlHttp .open("POST", UrlToWebForm, False)

Add request header declaring sent data as form submission

xmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

Construct form submission data

formData = "myControl1=data1&myControl2=data2"

The content of form submission data can be easily obtained using Fiddler the same way as described above

Add VIEWSTATE and EVENTVALIDATION data, be aware of adding this data urlEncoded:

formData = formData & "&__VIEWSTATE=" & urlEncode(viewState) & "&__EVENTVALIDATION=" & urlEncode(evtValidation))

URL encode function is not contained in standard scripting environment and must be implemented or imported separately. Ask for code snippet if want to use my code.

And finally send the request

xmlHttp.send(formData)

That's it! The form data is submitted and the ASP.NET WebForm gets default button clicked event to process submitted data on the server side properly.

Enjoy!

Wednesday, June 12, 2013

Search and replace using PowerShell

 

Last week we were faced to a problem: there are numerous files distributed in hierarchy of folders on the disk and we are required to replace a pattern string in each of these files to a specified replacement string. (In fact, we had to add quickly hardwired location parameters to all the AJAX ModalPopupExtender controls in an ASP.NET solution).

The challenge is: the files cannot be copied outside of the target machine, processed and copied back – all the processing MUST occur on the target box using only on-board tools. And there aren’t any built-in software in Windows (Server 2008 R2) providing such functionality like S&R, Visual Studio or other similar tools, enabling recursive search and replace operations.

Some of us started to wipe dust off their DOS command line skills and write some batch files. But the better solution is in use of built-in PowerShell.

Start PowerShell and launch following command:

dir –r {fileseach pattern}| %{ $x = get-content $_ ; $x = $x -replace ({search pattern}, {replacement pattern}) ; set-content $_ $x }

For example:

dir -r *.aspx| %{ $x = get-content $_ ; $x = $x -replace ("ModalPopupExtender ID", "ModalPopupExtender X=""100"" Y=""100"" ID") ; set-content $_ $x }

replaces string

ModalPopupExtender ID

with

ModalPopupExtender X="100" Y="100" ID

in all .aspx files in current folder and below

This makes from

<ajax:ModalPopupExtender ID=”mpe1” …/>

the string

<ajax:ModalPopupExtender X="100" Y="100" ID=”mpe1” …/>

So all the processing occurred on the target box without any need to copy the files to equipped machine, process them and copy back. Also without any need to install/deploy additional software to the target machine specially to perform desired processing.

Next time you need to replace some string with another one in a number of files distributed in recursive subfolders using only built-in software of Windows OS – do not panic: use pattern PowerShell command from above and enjoy!

Thursday, March 03, 2011

ASP.NET & AJAX: “Object required” Script error while using ModalPopupExtender

Loading an ASP.NET Webpage containing ModalPopupExtender you may encounter following error:

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729)
Timestamp: Thu, 3 Mar 2011 07:21:58 UTC


Message: Object required
Line: 106
Char: 13
Code: 0
URI:
http://localhost:49461/ScriptResource.axd?d=dVTmaeP-mJEQBF1x5WPACI57lI3BdxWWqqcjOSvk5IFYlzy3XmoJ3-mETMUX_E93dUXCirqabEt7Y5F8nk9OJF0v1sSq6MFJL4n1ystjZ74KXzsw7JAw7fRseSEZ165rrnTm4dOfgdUE7UZpQUpfBqdlctB5nzYxeqQR46Jg_TR_hyK50&t=ffffffffa4fab0f1

image

Start debugger, and you will learn more:

image

this._popupElement = $get(this._PopupControlID);
if (this._DropShadow) {
this._foregroundElement = document.createElement('div');
this._foregroundElement.id = this.get_id() + '_foregroundElement';
this._popupElement.parentNode.appendChild(this._foregroundElement);
this._foregroundElement.appendChild(this._popupElement);
}
else {
this._foregroundElement = this._popupElement;
}


The cause of this error is most likely the Panel used as TargetControl for ModulPopupExtender. Check if this Panel not set accidentally to



Visible="false"


In this case ASP.NET doesn’t render this panel at all, and the _PopupControlID property of the popup extender on the client side points to non existent object.


Check the Visible attribute of the popup panel, reset it to visible and enjoy!

Wednesday, March 02, 2011

Internet Explorer reports error: “The specified module could not be found”

Browsing (your customized?) ASP.NET application with IE7/8/9 you may encounter following error:

clip_image001

The error message says: “The specified module could not be found”. Most frequently you will encounter this error while displaying the pages containing Accordion AJAX control.

Surprisingly, other browsers like Firefox or Chrome do not cause this error and still display the Web application pages and all the controls correctly.

There are numerous methods to debug this error, we selected the local instance of Visual Studio:

clip_image001[4]

The error occurs exactly in getOpacity() function of a local JScript:

clip_image001[6]

getElementOpacity : function(element) {
if (!element) {
throw Error.argumentNull('element');}
var hasOpacity = false;var opacity;if (element.filters) {
var filters = element.filters;if (filters.length !== 0) {
var alphaFilter = filters['DXImageTransform.Microsoft.Alpha'];if (alphaFilter) {
opacity = alphaFilter.opacity / 100.0;hasOpacity = true;}
}
}

Quick watch on the filters says exactly the error we saw above:

clip_image001[8]

The Interface IHTMLFilterCollection is implemented in oleaut32.dll:

clip_image002

clip_image002[4]

clip_image002[6]

clip_image002[8]

There’s a Microsoft KB article describing a similar problem: http://support.microsoft.com/kb/281679/de.

According this article just try to re-register the oleat32.dll and actxprxy.dll:

clip_image002[10]

clip_image001[10]

clip_image001[12]

On some computers it helped.

On others we were obliged to re-run dxdiag: to detect any DirectX errors on the workstations. Some of them were cured just after installing of most recent DirectX graphics drivers

The rest of boxes displayed the same error: “DCIMAN32.DLL is missing”.

This is a symptom, some of latest software updates or new installations corrupted the system and removed the DCIMAN32.DLL from the %SYSTEM% folder. Short description was presented here: http://answers.microsoft.com/en-us/windows/forum/windows_7-system/dciman32dll/b38a81fa-89f2-4550-8749-89e91a6f0a40

But one more tasks of this library is to provide Internet Explorer with additional graphics related functions, that rely on the interfaces of this library and underlying modules. Fortunately, other browsers do not rely on this operating system dependent functionalities, so they can display the pages correctly.

What we’ve done was the fresh copy of DCIMAN32.DLL form an healthy reference system directly into %SYSTEM% folder of the victim machine – you do not event need to register this library with regsvr32 or reboot the computer.

It just worked after this – try to follow the steps and enjoy!