Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
82 changes: 55 additions & 27 deletions Allure.Net.Commons/AllureApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,42 +530,43 @@ public static void AddAttachment(
/// Attaches screen diff images to the current fixture, test, or step.
/// </summary>
/// <remarks>If no test or fixture is running, does nothing.</remarks>
/// <param name="expectedPng">A path to the actual screen.</param>
/// <param name="actualPng">A path to the expected screen.</param>
/// <param name="diffPng">A path to the screen diff.</param>
/// <param name="expectedPngPath">A path to the actual screen.</param>
/// <param name="actualPngPath">A path to the expected screen.</param>
/// <param name="diffPngPath">A path to the screen diff.</param>
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)
);
/// <summary>
/// Attaches screen diff images to the current fixture, test, or step.
/// </summary>
/// <remarks>If no test or fixture is running, does nothing.</remarks>
/// <param name="expectedPng">An actual screen bytes.</param>
/// <param name="actualPng">An expected screen bytes.</param>
/// <param name="diffPng">A screen diff bytes.</param>
public static void AddScreenDiff(
byte[] expectedPng,
byte[] actualPng,
byte[] diffPng
)
{
if (HasTestOrFixture)
{
AddScreenDiffInternal(expectedPng, actualPng, diffPng);
}
}

#endregion

Expand Down Expand Up @@ -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);
}
Loading