This is guest blog by my colleague Rajeev Kumar.
The Coded UI Test does not provide the API to compare images. There are cases when user wants to verify the changes in the UI by comparing the images of two controls. This can be easily achieved using Test API with Coded UI Test. The overview of Test API is described here and Test API visual verification is described here.
[TestMethod] [DeploymentItem("Master.png")] [DeploymentItem("ToleranceMap.png")] [DeploymentItem("SampleApp.exe")] public void CompareImagesTest() { // Launch the application. using (ApplicationUnderTest app = ApplicationUnderTest.Launch( Path.Combine(Directory.GetCurrentDirectory(), "SampleApp.exe"))) { // Select the check box, so that the background will change. WpfCheckBox checkBox = new WpfCheckBox(app); checkBox.Checked = true; // Get the actual, master and toleracne snapshots. Snapshot toleranceMap = Snapshot.FromFile("ToleranceMap.png"); Snapshot master = Snapshot.FromFile("Master.png"); Snapshot actual = Snapshot.FromWindow(app.WindowHandle, WindowSnapshotMode.ExcludeWindowBorder); // Get the difference of two images. Snapshot difference = actual.CompareTo(master); // Save the file to for manual viewing. master.ToFile(@"Master-expected.png", ImageFormat.Png); actual.ToFile(@"Master-actual.png", ImageFormat.Png); difference.ToFile(@"Master-difference.png", ImageFormat.Png); // Use SnapshotToleranceMapVerifier to verify the comparison. SnapshotVerifier verifier = new SnapshotToleranceMapVerifier(toleranceMap); Assert.AreEqual(VerificationResult.Pass, verifier.Verify(difference)); } }
This was guest blog by my colleague Rajeev Kumar.