How can I attach an image to TRX (Test Result Repo

2019-09-03 09:29发布

问题:

I would like to my test takes screenshot when an error occur.

I can write like this:

TestContext.WriteLine("aaaaaaaaa");

But how can I attach an image to a .TRX file?

Test Result Reported (.TRX)

回答1:

Images of controls and of the entire desktop can be taken during a Coded UI test. They are captured as normal Image objects and can then be saved or otherwise manipulated in the test. The files can also be attached to the test results. The code below gives some ideas on the code that can be used.

UITestControl ccc = this.UIMap.uiOne.uiTwo;
Image cccImage = ccc.CaptureImage();
cccImage.Save(@"C:\cccName.bmp");
TestContext.AddResultFile(@"C:\cccName.bmp");

Image desktopImage = UITestControl.Desktop.CaptureImage()
desktopImage.Save(@"C:\desktopImage.bmp");
TestContext.AddResultFile(@"C:\desktopImage.bmp");

The file name used in the ...Save call should be modified to give each saved image a different name. This is specially necessary when the tests are data driven, otherwise there will be no way of associating images with test executions.