0

Using a non-interactive script, I want to
1) Open Visual Studio
2) Load a project (an MVC4 web site)
3) Import a NuGet package via the package manager console
4) Run (F5) the site
5) Close Visual Studio

My intent is to unit test the NuGet package.

How could I achieve this?

4

1 回答 1

2

I personally would use AutoHotkey

It would look something like this:

;Win + A to run
#a::
    ;Run Visual Studio
    Run "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe"

    ;Open Project Ctrl + Shift + O
    Send ^+o

    ;Wait for it to open 5 Seconds
    Sleep 5000

    ;Import a NuGet Package
    Send [key sequence]

    ;Wait for it to import 5 Seconds
    Sleep 5000

    ;Run the site
    Send F5

    ;Wait for your validation 10 sec
    Sleep 10000

    ;Close Visual Studio Alt + F4
    Send !{F4}
Return

EDIT:

Some Tips and things I have found that helps with automation scripts:

  • Break up a large script into multiple smaller scripts. For ex. break the script into 3 parts. The first script will open Visual Studio and open the project, the second script imports the NuGet package, the third runs the site and closes Visual Studio. If you make the hot keys associated with the script something like Ctrl + 1, Ctrl + 2, Ctrl + 3, then it is super easy to go click, click, click, and run them all. It also allows you to re-run the script if something didn't run right the first time without having to restart from the beginning. I usually break up the script if there is a delay, or I have to wait for something to load because it is too easy for the script to continue and fire off too soon.
  • Use WinWait / IfWinNotActive instead of delays. This will cause the script to wait for the specific window to be activated before you continue to run the script. For Ex: .

    WinWait, Google - Windows Internet Explorer, IfWinNotActive, Google - Windows Internet Explorer, , WinActivate, Google - Windows Internet Explorer, WinWaitActive, Google - Windows Internet Explorer, ; Carry on and do stuff in Internet Explorer

This code will wait until you have activated the Internet Explorer with the Google home page on it. There are various ways to get the window text, but it also applies to dialog boxes, so you can wait for stuff to happen.

  • Use a macro recorder to help write the script and automate things. For ex. Macro Creator is a very powerful one.
于 2013-08-19T19:10:27.283 回答