On a project that’s well supported with tests, it is very common to have a good suite of automated tests. The two most common frameworks for test automation in the .Net stack are CodedUI and Watin. This article will cover utility code that improves recording a Web Performance test from a CodedUI test to automate the initial recording of the performance test. While it is possible to do the same with Watin, there is less control over the recording process, so I won’t cover Watin in this post.
There are two common tasks while going from a CodedUI into a Web Performance:
- Find the Browser with the recorder.
- Control the recording process. Often part of the CodedUI is getting to where we want to do the action, and this process isn’t part of the recording phase.
Finding a browser that is ready for recording
Finding a browser that is able to record is just going through the open browsers and look for the recording toolbar and the recording buttons. If we find them, then we have one and we can use it, otherwise just open a new browser and run the test normally.
Some things to note here:
- Make sure that you wrap all the code that looks for recording controls in compiler directives. If the CodedUI is looking for these controls and can’t find them, it takes a lot longer to run, doing this as part of a build process will just increase the build time by a great amount.
- While we are looking for things, keep track of the main buttons, Record and Resume, because we may want to click them later on, as part of scoping the recording process.
- The method that launches the browser takes a Boolean parameter that allows the browser recorder to be paused at the start of the CodedUI test, instead of the default recording behavior.
The code that handles this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class CodedUIExtensions | |
{ | |
#if !DO_NOT_FIND_WEBRECORD | |
private static bool _recording; | |
private static WinButton _recordButton; | |
private static WinButton _pauseButton; | |
#endif | |
public static BrowserWindow Launch(bool pauseRecording = false) | |
{ | |
return Launch("main.aspx", pauseRecording); | |
} | |
public static BrowserWindow Launch(string path, bool pauseRecording = false) | |
{ | |
#if !DO_NOT_FIND_WEBRECORD | |
// Try to find an open browser that is recording to do a web performance recording session | |
try | |
{ | |
var recordingBrowser = new BrowserWindow(); | |
recordingBrowser.SearchProperties[UITestControl.PropertyNames.Name] = "Blank Page"; | |
recordingBrowser.SearchProperties[UITestControl.PropertyNames.ClassName] = "IEFrame"; | |
recordingBrowser.Find(); | |
var recordWindow = new WinWindow(recordingBrowser); | |
recordWindow.SearchProperties[WinControl.PropertyNames.ControlName] = "toolStrip1"; | |
recordWindow.Find(); | |
var toolbar = new WinToolBar(recordWindow); | |
toolbar.SearchProperties[UITestControl.PropertyNames.Name] = "toolStrip1"; | |
toolbar.Find(); | |
_recordButton = new WinButton(toolbar); | |
_recordButton.SearchProperties[UITestControl.PropertyNames.Name] = "Record"; | |
_recordButton.Find(); | |
_pauseButton = new WinButton(toolbar); | |
_pauseButton.SearchProperties[UITestControl.PropertyNames.Name] = "Pause"; | |
_pauseButton.Find(); | |
if (pauseRecording) | |
{ | |
Mouse.Click(_pauseButton); | |
recordingBrowser.WaitForControlReady(); | |
} | |
recordingBrowser.NavigateToUrl(new Uri(path)); | |
_recording = true; | |
return recordingBrowser; | |
} | |
catch | |
{ | |
} | |
#endif | |
// A browser with a session ready to record couldn't be found, so open a new one | |
var browserWindow = BrowserWindow.Launch(path); | |
browserWindow.WaitForControlReady(); | |
return browserWindow; | |
} | |
} |
Controlling the recording process
Besides finding the browser, that are 3 common things that we want, as part of controlling the recording process:
- Be able to pause the recording process.
- Be able to resume the recording process.
- Some applications will spawn multiple windows, so at the end of the test an ALT+F4 is sent to the target app. However in the scope of recording a performance test, we want the browser to stay open, so we can do final adjustments or just stop recording and generate the test.
To accomplish this, just add 3 more methods to the utility class (also with compiler directives to improve test run speeds during builds):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void PauseRecording() | |
{ | |
#if !DO_NOT_FIND_WEBRECORD | |
if (!_recording) return; | |
Mouse.Click(_pauseButton); | |
_pauseButton.WaitForControlReady(); | |
#endif | |
} | |
public static void ResumeRecording() | |
{ | |
#if !DO_NOT_FIND_WEBRECORD | |
if (!_recording) return; | |
Mouse.Click(_recordButton); | |
_recordButton.WaitForControlReady(); | |
#endif | |
} | |
public static void CloseWindows() | |
{ | |
#if !DO_NOT_FIND_WEBRECORD | |
if (!_recording) | |
{ | |
Keyboard.SendKeys("%{F4}"); | |
} | |
#else | |
Keyboard.SendKeys("%{F4}"); | |
#endif | |
} |