diff --git a/Allure.Net.Commons.Tests/UserAPITests/AllureFacadeTests/AttachmentTests.cs b/Allure.Net.Commons.Tests/UserAPITests/AllureFacadeTests/AttachmentTests.cs index aad4886b..14b35e3e 100644 --- a/Allure.Net.Commons.Tests/UserAPITests/AllureFacadeTests/AttachmentTests.cs +++ b/Allure.Net.Commons.Tests/UserAPITests/AllureFacadeTests/AttachmentTests.cs @@ -87,4 +87,43 @@ public void FixtureScreenDiff() Is.Not.Empty ); } + + [Test] + public void ScreenDiffFromBytes() + { + this.lifecycle.StartTestCase(new() { uuid = "1", fullName = "n" }); + byte[] expectedExpected = [1, 2, 3]; + byte[] expectedActual = [4, 5, 6]; + byte[] expectedDiff = [7, 8, 9]; + + AllureApi.AddScreenDiff(expectedExpected, expectedActual, expectedDiff); + + var attachment = this.Context.CurrentTest.attachments.Single(); + var content = JsonConvert.DeserializeAnonymousType( + Encoding.UTF8.GetString( + this.writer.attachments.Single().Content + ), + new { expected = "", actual = "", diff = "" } + ); + var prefix = "data:image/png;base64,"; + var actualExpected = Convert.FromBase64String( + content.expected[prefix.Length..] + ); + var actualActual = Convert.FromBase64String( + content.actual[prefix.Length..] + ); + var actualDiff = Convert.FromBase64String( + content.diff[prefix.Length..] + ); + + Assert.That(attachment.name, Is.EqualTo("diff-1")); + Assert.That(attachment.type, Is.EqualTo("application/vnd.allure.image.diff")); + Assert.That(attachment.source, Does.EndWith(".json")); + Assert.That(content.expected, Does.StartWith(prefix)); + Assert.That(content.actual, Does.StartWith(prefix)); + Assert.That(content.diff, Does.StartWith(prefix)); + Assert.That(actualExpected, Is.EqualTo(expectedExpected)); + Assert.That(actualActual, Is.EqualTo(expectedActual)); + Assert.That(actualDiff, Is.EqualTo(expectedDiff)); + } } diff --git a/Allure.Net.Commons.Tests/UserAPITests/NoContextTests/AllureApiNoContextTests.cs b/Allure.Net.Commons.Tests/UserAPITests/NoContextTests/AllureApiNoContextTests.cs index 13cb0522..058afa63 100644 --- a/Allure.Net.Commons.Tests/UserAPITests/NoContextTests/AllureApiNoContextTests.cs +++ b/Allure.Net.Commons.Tests/UserAPITests/NoContextTests/AllureApiNoContextTests.cs @@ -219,6 +219,7 @@ public void AddAttachmentShouldNotThrowEvenIfNoFileExist() public void AddScreenDiffShouldNotThrow() { Assert.That(() => AllureApi.AddScreenDiff("foo", "bar", "baz"), Throws.Nothing); + Assert.That(() => AllureApi.AddScreenDiff([], [], []), Throws.Nothing); } [Test] diff --git a/Allure.Net.Commons/AllureApi.cs b/Allure.Net.Commons/AllureApi.cs index 43799119..0dc8c837 100644 --- a/Allure.Net.Commons/AllureApi.cs +++ b/Allure.Net.Commons/AllureApi.cs @@ -530,42 +530,43 @@ public static void AddAttachment( /// Attaches screen diff images to the current fixture, test, or step. /// /// If no test or fixture is running, does nothing. - /// A path to the actual screen. - /// A path to the expected screen. - /// A path to the screen diff. + /// A path to the actual screen. + /// A path to the expected screen. + /// A path to the screen diff. public static void AddScreenDiff( - string expectedPng, - string actualPng, - string diffPng + string expectedPngPath, + string actualPngPath, + string diffPngPath ) { if (HasTestOrFixture) { - AddAttachment( - string.Format( - DIFF_NAME_PATTERN, - CurrentLifecycle.Context.CurrentStepContainer.attachments.Count( - a => a.type == DIFF_MEDIA_TYPE - ) + 1 - ), - DIFF_MEDIA_TYPE, - Encoding.UTF8.GetBytes( - JsonConvert.SerializeObject(new - { - expected = ReadDiffEntry(expectedPng), - actual = ReadDiffEntry(actualPng), - diff = ReadDiffEntry(diffPng) - }) - ), - ".json" + AddScreenDiffInternal( + File.ReadAllBytes(expectedPngPath), + File.ReadAllBytes(actualPngPath), + File.ReadAllBytes(diffPngPath) ); } } - static string ReadDiffEntry(string fileName) => - DIFF_ENTRY_PREFIX + Convert.ToBase64String( - File.ReadAllBytes(fileName) - ); + /// + /// Attaches screen diff images to the current fixture, test, or step. + /// + /// If no test or fixture is running, does nothing. + /// An actual screen bytes. + /// An expected screen bytes. + /// A screen diff bytes. + public static void AddScreenDiff( + byte[] expectedPng, + byte[] actualPng, + byte[] diffPng + ) + { + if (HasTestOrFixture) + { + AddScreenDiffInternal(expectedPng, actualPng, diffPng); + } + } #endregion @@ -813,4 +814,31 @@ string fileExtension item => item.attachments.Add(attachment) ); } + + static void AddScreenDiffInternal( + byte[] expectedPng, + byte[] actualPng, + byte[] diffPng + ) => + AddAttachment( + string.Format( + DIFF_NAME_PATTERN, + CurrentLifecycle.Context.CurrentStepContainer.attachments.Count( + a => a.type == DIFF_MEDIA_TYPE + ) + 1 + ), + DIFF_MEDIA_TYPE, + Encoding.UTF8.GetBytes( + JsonConvert.SerializeObject(new + { + expected = ToDiffEntry(expectedPng), + actual = ToDiffEntry(actualPng), + diff = ToDiffEntry(diffPng) + }) + ), + ".json" + ); + + static string ToDiffEntry(byte[] data) => + DIFF_ENTRY_PREFIX + Convert.ToBase64String(data); }