Created
September 19, 2018 16:49
-
-
Save safestudent/0cbcaf87e8aa62f7570e4d7859c4a3b6 to your computer and use it in GitHub Desktop.
Screenshots
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
public static string GetPathToMyDocuments() | |
{ | |
// return the absolute path to the MyDocuments folder on your computer | |
return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); | |
} | |
// you need to pass through the filename you want to call your screenshot | |
public static void FullScreenScreenshot(string fileName) | |
{ | |
// take the screenshot | |
Screenshot screenshot = ((ITakesScreenshot)Browser).GetScreenshot(); | |
// create the filename and the path | |
string path = GetPathToMyDocuments() + @"\" + fileName + ".png"; | |
// save the screenshot as a png | |
screenshot.SaveAsFile(path, ScreenshotImageFormat.Png); | |
} | |
// you need to pass through what you want to call your screenshot and the locator for the element | |
public static void ElementScreenShot(By locator, string fileName) | |
{ | |
// find the element | |
IWebElement element = WaitForElement(locator); | |
// take the screenshot | |
Screenshot screenshot = ((ITakesScreenshot)Browser).GetScreenshot(); | |
// convert it to bitmap and put it in memory | |
var img = Image.FromStream(new MemoryStream(screenshot.AsByteArray)) as Bitmap; | |
// crop the image to the size and location of the element | |
var crop = img.Clone(new Rectangle(element.Location, element.Size), img.PixelFormat); | |
// create the path and name of the screenshot | |
string path = GetPathToMyDocuments() + @"\" + fileName + ".bmp"; | |
// save the cropped image in the path with the name we chose | |
crop.Save(path); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment