Created
August 13, 2017 14:13
-
-
Save angelovstanton/4c69140d23dc9fe72bcb5657af967611 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void TakingHTML2CanvasFullPageScreenshot() | |
{ | |
var options = new ChromeOptions(); | |
options.AddArguments($"load-extension={GetAssemblyDirectory()}\\FullPageScreenshotsExtension-Chrome\\"); | |
var capabilities = new DesiredCapabilities(); | |
capabilities.SetCapability(ChromeOptions.Capability, options); | |
var dc = DesiredCapabilities.Chrome(); | |
dc.SetCapability(ChromeOptions.Capability, options); | |
using (var driver = new ChromeDriver(options)) | |
{ | |
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(5); | |
driver.Navigate().GoToUrl(@"https://automatetheplanet.com"); | |
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); | |
var fullPageImg = wait.Until(ExpectedConditions.ElementExists(By.Id("fullPageScreenshotId"))); | |
var pngContent = fullPageImg.GetAttribute("src"); | |
pngContent = pngContent.Replace("data:image/png;base64,", string.Empty); | |
byte[] data = Convert.FromBase64String(pngContent); | |
var tempFilePath = Path.GetTempFileName().Replace(".tmp", ".png"); | |
Image image; | |
using (var ms = new MemoryStream(data)) | |
{ | |
image = Image.FromStream(ms); | |
} | |
image.Save(tempFilePath, ImageFormat.Png); | |
} | |
} | |
private string GetAssemblyDirectory() | |
{ | |
string codeBase = Assembly.GetExecutingAssembly().CodeBase; | |
var uri = new UriBuilder(codeBase); | |
string path = Uri.UnescapeDataString(uri.Path); | |
return Path.GetDirectoryName(path); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment