Showing posts with label World of Warcraft. Show all posts
Showing posts with label World of Warcraft. Show all posts

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.

Saturday, February 5, 2011

World of Warcraft automation using powershell

I have some crafting that I do in World of Warcraft that is repetitive, lucrative, and boring. If I sit at the keyboard doing it, I'm essentially just executing a for loop in my head. Not my idea of fun, especially when it could be an opportunity to hone my 'leet programming skills.

I used to automate this activity with AutoHotKey (AHK). It worked great, however, it was a new pseudo-programming language that I had to learn, and a very specific one at that. I've done some neat things with AutoHotKey, but I would prefer to use something like powershell instead. Powershell has the advantage that it can be used in a much broader set of domains than AutoHotKey, and therefor more likely to be useful to me in the future for some as of yet unknown task. I had toyed with the idea of embedding AHK inside of powershell and executing AHK commands within a powershell script, but I have found a better solution.

Up until .net 4.0 I had considered making AHK commands embeddable in Powershell. But! .net 4.0 brings us the System.Windows.Automation namespace.

With this small bit of knowledge, I was able to craft the following sample:


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)
[System.Windows.Forms.SendKeys]::SendWait("{Enter}Hi{Enter}")

Making my character say "Hi", isn't terribly useful, but it illustrates how I could send any sort of command at all to Wow.

This uses .Net 4.0 which screws up in most default configurations of powershell and powershell editors. In order to configure your powershell environment to run on .net 4.0 and be able to run this script, add the following configuration file to your editor of choice.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0.30319" />
</startup>
</configuration>

A more thorough discussion of .net 4.0 and powershell.

That config file wasn't working with PowerGUI though. I had to resort to the registry keys:

New-ItemProperty -Name "OnlyUseLatestCLR" -Value 1 -Path "HKLM:\software\wow6432node\microsoft\.netframework\"
New-ItemProperty -Name "OnlyUseLatestCLR" -Value 1 -Path "HKLM:\software\microsoft\.netframework\"

Friday, July 3, 2009

Readable Things

I've created my first open-source project. ReadableThings

Its a framework for generating string output for first, second, and third person in English.

For example, using World of Warcraft, I target the Sunreaver Guardian Mage and type, "/moo".

The output is:
You moo at Sunreaver Guardian Mage. Moooooooooo.

World of Warcraft has just failed a Turing Test.

The output should be:
You moo at a sunreaver guardian mage. Moooooooooo.

This framework is based on some of the earliest c# code that I ever wrote, back during the earliest beta of Visual Studio.Net. Simple logic indicating when to use the indefinite versus the definite article in English remains relevant to game programming.

I don't have an MMORPG of my own where this would be relevant, but given enough free weekends, I may come up with an alternative use for it.

There was originally a parser to go with this framework, that together created something similar to a natural language parser, alone though, this is something more along the lines of natural language output.







Reblog this post [with Zemanta]