当我使用WatiN进行测试时,我喜欢保存截图。有时我并不需要整个浏览器窗口的图片 - 我只想要一张我正在测试的元素的图片。下面WatiN NativeElement.GetElementBounds() - 什么是度量单位?
我试图保存的代码元素的图象产生了块框的图象,因为elementBounds.Top点的像素位置的方式经过屏幕的底部。 elementBounds.Width和.Height值也似乎大约是他们应该的一半。
这是一个华廷bug,或者是措施,我要转换为像素莫名其妙的不同单元,这些属性?
public static void SaveElementScreenshot
(WatiN.Core.IE ie, WatiN.Core.Element element, string screenshotPath)
{
ScrollIntoView(ie, element);
ie.BringToFront();
var ieClass = (InternetExplorerClass) ie.InternetExplorer;
Rectangle elementBounds = element.NativeElement.GetElementBounds();
int left = ieClass.Left + elementBounds.Left;
int top = ieClass.Top + elementBounds.Top;
int width = elementBounds.Width;
int height = elementBounds.Height;
using (var bitmap = new Bitmap(width, height))
{
using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.CopyFromScreen
(new Point(left, top), Point.Empty, new Size(width, height));
}
bitmap.Save(screenshotPath, ImageFormat.Jpeg);
}
}
什么是ScrollIntoView?我无法找到它.. – Marco 2013-12-15 08:26:27
马可,这篇博客文章包含了所有该类代码:http://geekswithblogs.net/brians/archive/2010/05/31/watin-screenshot-saver.aspx – 2013-12-18 18:47:22