Skip to content

Instantly share code, notes, and snippets.

@benbrandt22
Created March 16, 2023 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benbrandt22/abe7b26eaf03bd9e30113fda9aeb77d2 to your computer and use it in GitHub Desktop.
Save benbrandt22/abe7b26eaf03bd9e30113fda9aeb77d2 to your computer and use it in GitHub Desktop.
Test Helper - Send HTML string to display in a browser
using System.Diagnostics;
public static class TestingHelpers
{
/// <summary>
/// Opens a browser and displays the provided HTML, without needing to save it to disk
/// </summary>
public static void DisplayHtmlInBrowser(string html, string browserExe = "chrome.exe")
{
var toEncodeAsBytes = System.Text.Encoding.ASCII.GetBytes(html);
var base64EncodedHtml = System.Convert.ToBase64String(toEncodeAsBytes);
var dataUri = $"data:text/html;base64,{base64EncodedHtml}";
Process.Start(new ProcessStartInfo { FileName = browserExe, Arguments = dataUri, UseShellExecute = true });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment