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!

Tuesday, March 04, 2014

“The World Wide Web Publishing Service (WWW Service) did not register the URL prefix http://*:443/ for site x. The site has been disabled. The data field contains the error number.” when you try to start an IIS WebSite on Windows 8/8.1

 

Starting a custom WebSite with https (Port 443) binding you may encounter following problem:

image

Event viewer says

The World Wide Web Publishing Service (WWW Service) did not register the URL prefix https://localhost:443/ for site 2. The site has been disabled. The data field contains the error number.

The problem is: one of currently running programs uses the port and doesn’t let IIS to start the WebSite. Which program does it .- displays TcpView tool from Sysinternals:

image

In this case it is Skype: quit it – not just log out etc., you have to quit the Skype process to release the port. Then restart WebSite – and enjoy!

BTW: if you start Skype after the WebSite started – it starts as well and finds another port to communicate: you do not have to restrict you Windows comfort working without Skype while using local IIS WebSite with https binding