Saturday, March 19, 2011

World of Warcraft Automation using Coded UI Tests from Visual Studio 2010

Previously I discussed automating repetitive tasks in World of Warcraft using powershell. While that works well, there are some situations where the only thing that will work is a mouse click. SendKeys makes it easy to send keyboard input to a window, but for some reason, I could not find an easy way of sending mouse input using a .Net API. "Easy" is the key word here, I could always try sending a message directly to the window through the Windows API, or involve some expensive third party automated testing tool.

I eventually stumbled upon a new feature in Visual Studio 2010: a coded UI test. The tests themselves are easy to create, the wizard in VS walks you through them excellently. There are plenty of resources on the net that describe how to use this feature in VS, so I'll spare you a redundant and feeble attempt at doing that, and show you how to integrate the result into a powershell script for easy manipulation.

The coded UI test runs very well inside of Visual Studio, but we need to run it externally. The compiled dll from the test project doesn't run by itself though, we require a utility called MSTest to run it.

The finished powershell script integrated with MSTest, looks like this:

Import-Module C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\UIAutomationClient.dll
$a  = [System.Windows.Automation.AutomationElement]::RootElement.FindAll([System.Windows.Automation.TreeScope]::Descendants, [System.Windows.Automation.Condition]::TrueCondition) 
$wow = $a | Where { $_.Current.Name -eq "World of Warcraft" } 
$pattern = $wow.GetCurrentPattern([System.Windows.Automation.WindowPattern]::Pattern)
$pattern.SetWindowVisualState([System.Windows.Automation.WindowVisualState]::Normal)
for($i = 0; $i -lt 80; $i++) { 
     $mstest = "C:\program files (x86)\Microsoft Visual Studio 10.0\common7\ide\mstest.exe"
     Push-Location
     Set-Location "C:\Users\Ogre\Desktop\SVN\Powershell Samples\"
     & $mstest "/testcontainer:testproject1.dll"
     Pop-Location
     Sleep 4
     [System.Windows.Forms.SendKeys]::SendWait("=")
}

The Visual Studio test project was compiled to testproject1.dll and the output was copied into the same directory as this powershell script.

Creating a dll for a particular UI action in World of Warcraft as shown here does take a bit of doing, it certainly isn't as easy as using SendKeys, but you can, using this technique, automate any action in the UI, even the ones that Blizzard ordinarily has blocked from macro and lua programmatic usage.

Note that this only allows for automation of mindless repetitive tasks, if you want a full fledged bot, this falls far short.

No comments:

Post a Comment