在VS Code上,我想通过获取应用程序的屏幕快照元素来设置应用程序视觉上的UI测试。但是,当例如“ button”的元素在页面上不可见时,您需要向下滚动以查看它,创建屏幕截图时,将显示“ system of memory”错误。
这是c#中的代码提取。
public void TakeScreenshotOfElementCheckbox()
{
string fileName = @"C:\Users\...\testing\Screenshots\checkbox.png";
var screenshotDriver = driver as ITakesScreenshot;
Screenshot screenshot = screenshotDriver.GetScreenshot();
var bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));
IWebElement element = driver.FindElement(By.XPath(".//*[@class='footer-convenant']"));
var cropArea = new Rectangle(element.Location, element.Size);
var bitmap = bmpScreen.Clone(cropArea, bmpScreen.PixelFormat);
bitmap.Save(fileName);
}
有关Dispose的更多详细信息:
我正在尝试,但我认为我使用了错误的方式。使用应如何使用?我可以举个例子吗?
string fileName = @"C:\Users\...\testing\Screenshots\Checkbox\checkbox.png";
using (var image = new Bitmap(fileName))
{
var screenshotDriver = driver as ITakesScreenshot;
Screenshot screenshot = screenshotDriver.GetScreenshot();
//Bitmap bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));
// 2. Get screenshot of specific element
element = driver.FindElement(By.XPath(".//*[@class='Checkbox']"));
Rectangle rect = new Rectangle(element.Location, element.Size);
var bitmap = image.Clone(rect, image.PixelFormat);
bitmap.Save(fileName);
}
我正在尝试,但我认为我并不遥远。您能告诉我它的结构吗?
string fileName = @"C:\Users\...\testing\Screenshots\Checkbox\checkbox.png";
using (var image = new Bitmap(fileName))
{
// 1. Make screenshot of all screen
var screenshotDriver = driver as ITakesScreenshot;
Screenshot screenshot = screenshotDriver.GetScreenshot();
Bitmap bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));
// 2. Get screenshot of specific element
element = driver.FindElement(By.XPath(".//*[@class='Checkbox']"));
Rectangle rect = new Rectangle(element.Location, element.Size);
using (var bitmap = image.Clone(rect, image.PixelFormat))
{
bitmap.Save(fileName);
}
}
或
string fileName = @"C:\Users\..\testing\Screenshots\Checkbox\checkbox.png";
using (var image = new Bitmap(fileName))
{
// 1. Make screenshot of all screen
var screenshotDriver = driver as ITakesScreenshot;
Screenshot screenshot = screenshotDriver.GetScreenshot();
Bitmap bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));
// 2. Get screenshot of specific element
element = driver.FindElement(By.XPath(".//*[@class='Checkbox']"));
Rectangle rect = new Rectangle(element.Location, element.Size);
var bitmap = bmpScreen.Clone(rect, bmpScreen.PixelFormat);
bitmap.Save(fileName);
bitmap.Dispose();
}
答案 0 :(得分:3)
Bitmap
类使用需要释放的带有操作系统的非托管资源。创建位图后,您需要处置。
您创建或克隆的任何位图都需要进行处理(完成它们后)。我建议查看using
statement
提供了一种方便的语法,可确保正确使用 可分配对象。