We wanted a simple way to get a new environment set up on a developer machine, but without the hassle of Virtual Machines.
Enter, Microsoft’s appcmd.
This lets you manipulate IIS7 via command line calls. There is lots of documentation on the IIS site but it takes a while to figure out which commands you need to do everything to set up a site. So I’ve put together a selection of the useful commands below in a sample site setup script.
We save these as batch files (.bat) and put them in a shared folder.
- You work on Windows using IIS7
- (if Sitecore) You have all relevant Sitecore installation zips in a shared network folder
- (if Umbraco) You use Nuget package to distribute Umbraco CMS (or you could do the zip thing)
- You run sites locally with custom host name, using a shared DB
- All team members setup sites in the same folder path e.g. c:\work
- You have admin rights on the local machine
Sample setup script:
Put this in a .bat file and save in a shared folder. Keep as many steps as required, and replace the relevant placeholders. To run, right click Run as Administrator.
ECHO *** MUST BE RUN AS ADMIN - IF NOT PLEASE CLOSE AND RUN AGAIN, OTHERWISE: ***
ECHO *** CHECKING OUT FROM SVN ***
IF not exist c:\PATH_TO_TRUNK svn checkout -q https://SVN_PATH_TO_TRUNK c:\PATH_TO_TRUNK
ECHO *** EXTRACTING SITECORE ***
"C:\Program Files\7-Zip\7z" x -y "PATH_TO_SITECORE_ZIPS\Sitecore 7.0 rev. 140120.zip" -oc:\PATH_TO_TRUNK "Sitecore 7.0 rev. 140120\Data" "Sitecore 7.0 rev. 140120\Website"
ECHO *** RENAMING SITECORE ***
rename "c:\PATH_TO_TRUNK\Sitecore 7.0 rev. 140120" www
ECHO *** COPY LICENSE ***
copy PATH_TO_LICENSE\license.xml c:\PATH_TO_TRUNK\www\Data
ECHO *** CREATING APP POOL ***
C:\Windows\System32\inetsrv\appcmd add apppool /name:"APP_POOL_NAME" -managedRuntimeVersion:v4.0
ECHO *** CREATING SITE ***
C:\Windows\System32\inetsrv\appcmd add site /name:"APP_POOL_NAME" /bindings:http://site.localhost:80 /physicalPath:"c:\PATH_TO_TRUNK\www\Website" -applicationDefaults.applicationPool:APP_POOL_NAME
ECHO *** CREATING VIRTUAL DIRECTORIES ***
C:\Windows\System32\inetsrv\appcmd add vdir /app.name:"APP_POOL_NAME/" /path:/siteAssets /physicalPath:c:\PATH_TO_TRUNK\siteAssets
ECHO *** ADDING TO HOSTS FILE ***
find /c " site.localhost" c:\windows\system32\drivers\etc\hosts
if %errorlevel% equ 1 goto notfound
echo Already there
ECHO 127.0.0.1 site.localhost >> c:\windows\system32\drivers\etc\hosts
ECHO *** BUILDING SOLUTION ***
C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe c:\PATH_TO_TRUNK\src\Site.sln /verbosity:quiet
ECHO *** OPENING SITE ***
Hope that’s helpful to someone!
Our QA team have started creating Selenium scripts to automate browser functional testing. These allow us to automatically run through a set of actions on a website and flag up any issues.
Running Scripts Manually
You will need:
To create/open/run a script:
- Open Firefox
- Open Selenium (should be button on toolbar otherwise ctrl+alt+s or alt, Tools, Selenium IDE)
- Follow selenium documentation to record/playback scripts
Automating Scripts via Unit Tests
We can convert the scripts into C# unit tests and run them via TeamCity. This explains how to set this up. You will need:
First prepare the solution for selenium unit tests:
- Add a new project (C# Class Library) suggest something like SiteName.SeleniumTests
Right click the new project, Manage Nuget Packages, install the following:
- Selenium Remote Control (RC)
- Create a folder in which to place the unit tests (e.g. grouped like NewUserTests or just Tests)
- In Selenium IDE, choose File, Batch convert test cases, C#/Nunit/Remote Control
- Select your Tests folder as the destination folder
- Select all the test cases you wish to convert (html files) and they should be saved as C# files in the Tests folder
- In Visual Studio, show existing files, and Include in project all the generated .cs files
- Build (fix any compilation errors)
- Open Test > Windows > Test Explorer, this should show your tests (if not, rebuild)
- Close all Firefox windows (they can interfere with Selenium Server)
- Click Run All. Selenium Server should log the commands and launch Firefox to run the test.
To capture screenshots at the end of each test:
- Create a Screenshots folder in your unit test project
In each .cs file, in Teardown method add the following line of code before selenium.Stop():
- selenium.CaptureEntirePageScreenshot(System.IO.Path.Combine(System.Environment.CurrentDirectory, @”..\..\Screenshots\” + this.GetType().Name + “.png”), “”);
- Run the test, screenshot should be saved into the Screenshots folder with same name as the test class.
Integration with TeamCity
To integrate into TeamCity, set up a build configuration to run the NUnit test with the following build steps:
- Compile selenium tests – Msbuild runner, point at Selenium.csproj file
- Run selenium tests – Nunit runner, run tests from (path to compiled DLL)
Set the Artifact paths setting on the build settings to point to the Screenshots folder to save the screenshots after each run.
To run via TeamCity the Selenium server must be running. You could start this manually same as above, hopefully this can be made to run automatically as a service but I’m not sure how.
Each test must be independent of any other as they may be run in any order, or perhaps only failed tests might be run. Also the server will restart the browser between each test so they can’t depend on the final state of the previous script (like they can in the IDE).
We’re going to try using New Relic Synthetics which has a feature for test automation – apparently you can upload a Selenium script and it can automatically run as and when, we have yet to try this out though!