Sunday, November 27, 2011

0 Web UI Automation using PowerShell

Recently, I come across the need for simulate the user browser web behavior. I need to simulate the user behavior by going to particular URL, filling the form and submit the request and even download the file.

There defiantly will be more then one way can do it. I intend to focus on the solution which can use the language I familiar with: Perl and PowerShell.

Perl: LWP::UserAgent seems to be the way to go. This seems to be able to send the HTTP POST request. During my very short period time of testing. It does not work for me. Probably is because I do not have the proxy server set up. ( My environment has some proxy server/firewall restriction).

Also, since I may need to implement the solution on the windows. I spend more time on digging the solution for PowerShell.

DotNet  WebClient class seems can achieve similar way like LWP. I have not spend much time on it yet. I found the “InternetExplorer.Application” is very interesting and easy to use.

Here is some testing sample code

  1. $ie = new-object -com "InternetExplorer.Application"
  2.  
  3. $ie.navigate("https://docs.google.com/")
  4.  
  5. While ( $ie.busy -eq $true){
  6. [System.Threading.Thread]::Sleep(2000)
  7. }
  8.  
  9. $doc = $ie.document
  10. $tb1 = $doc.getElementByID("Email")
  11. $tb2 = $doc.getElementByID("Passwd")
  12. $btn = $doc.getElementByID("signIn")
  13.  
  14. $tb1.value = 'XXXX'
  15. $tb2.value = 'XXXX'
  16. $btn.click()
  17.  
  18. $ie.visible = $true

I use the Firefox plugin firebug to find the proper HTML tag.

image

The current issue I am having is by using this approach to download the file. When I try to download the text file, it will be blocked by the information bar. Since in the real production environment, I do not have permission to disable the information bar via HKLM register key nor adding the website to the trust zone in IE. I have stuck on this for now.

One of limitation using this is that you have to have the Internet explorer install on the same host where you run the PowerShell. Since you are running the PowerShell already. I wont think it is much of big deal. However, I do concern about the Internet Explorer stability. I am not sure what if the IE has a lot of plugin or not stable, how would it impact the PowerShell Script.

Reference

Web UI Automation with Windows PowerShell http://msdn.microsoft.com/en-us/magazine/cc337896.aspx

0 comments:

Post a Comment

 

SQL Panda Copyright © 2011 - |- Template created by O Pregador - |- Powered by Blogger Templates