Mouse interactions not showing in webdriver screenshots - c#

I'm currently using Selenium WebDriver 2.35 and hit a roadblock when it comes to taking a screenshot. I wrote up a small function that takes in an IWebElement and will return a screenshot of the specific element. The element I am trying to screenshot is actually an image pulled from a sprite. This element is tricky though, because upon mouseover/hover the image changes from gray to its true color(by moving to a different part of sprite). I am able to get a proper screenshot of the image via this function, but cannot get it to recognize the mouse interactions with ITakesScreenshot. I can visually see in the browser that the image is hovered over, but the screenshot cannot. Any thoughts?
public static Bitmap GetImage(IWebElement element)
{
RemoteWebDriver driver = BrowserManager.GetInstance().GetDriver();
Actions action = new Actions(driver);
//take screenshot of page
action.MoveToElement(element).Build().Perform();
Byte[] ba= ((ITakesScreenshot)driver).GetScreenshot().AsByteArray;
Bitmap ss = new Bitmap(new MemoryStream(ba));
//ss.Save("c:\\tmp\\ss.png", ImageFormat.Png);
Rectangle crop = new Rectangle(element.Location.X, element.Location.Y, element.Size.Width, element.Size.Height);
//create a new image by cropping the original screenshot
Bitmap image = ss.Clone(crop, ss.PixelFormat);
return image;
}

In my experience, with automations that route through Selenium Grid, no mouse is seen. Perhaps this is because the "mouse" is actually a virtual Selenium mouse and unconnected to the systems native mouse driver.

It seems as though the newest selenium (2.39) has addressed this issue and I am able to see mouse hover in this screenshot method. Thanks everyone for your help!

Related

Drawing a Panels Handle to a Bitmap

I have a WPF-Application which hosts a WinformsPanel
<WindowsFormsHost>
<windowsForms:Panel
x:Name="PlayerHost">
</windowsForms:Panel>
<WindowsFormsHost>
I then use this Panel to display a video. I'm using Mpv.NET lib to do this.
The video player is initialized properly:
//panel.Handle is the windowsForms:Panel named PlayerHost
player = new MpvPlayer(panel.Handle, Common.IO.FindLib.FindMpvLib(binaryPath));
player.Load(videoFilePath);
Now, if I try to draw the panels content, the resulting image remains blank. The code to draw the image is as follows:
using (var bmp = new Bitmap(panel.ClientSize.Width, panel.ClientSize.Height))
{
panel.DrawToBitmap(bmp, panel.ClientRectangle);
bmp.Save(#"Some:\path.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
To make it clear: The video gets displayed. I can see the video content. And I start drawing to an image when the media is properly loaded. I even offloaded the drawing command to a button click. So while the video was running I tried to take a "frame capture" so to say. But the image remains blank.
How can I capture the panels content? Has it something to do with the native Handle being provided to the video player? Thanks in advance.
You can save the panels content to a bitmap with the following code:
using (Bitmap b = new Bitmap(panel.Width, panel.Height))
{
panel.DrawToBitmap(b, new Rectangle(0, 0, b.Width, b.Height));
b.Save("test.png");
}

Top and bottom of Photo captured using CameraCaptureUI is getting stretched

CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.MaxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.HighestAvailable;
StorageFile file = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
This shows up the camera, and I am able to preview and click to capture the image. The captured image is shown in another window over the original camera image windows. I am able to crop and click to save this captured image.
However, right after I click save, the image is getting stretched, and after it is saved, it remains stretched as well. In essence, the image I capture and save is being stretched,
I tried removing the crop and it works fine, but I need the crop feature as well.
What am I doing wrong? Should I be using any other values for these settings?
This was working fine till December if I remember correctly.
Any help here is appreciated. Screenshots added below
, ,

Photo captured using CameraCaptureUI is getting stretched when saving

This is for developing a Windows 10 application.
I am trying to capture an image using the camera, edit the captured image,
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.AllowCropping = true;
captureUI.PhotoSettings.CroppedAspectRatio = new Windows.Foundation.Size(4, 3);
//captureUI.PhotoSettings.CroppedSizeInPixels = new Windows.Foundation.Size(0,0);
captureUI.PhotoSettings.MaxResolution = Windows.Media.Capture.CameraCaptureUIMaxPhotoResolution.HighestAvailable;
StorageFile file = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
This shows up the camera, and I am able to preview and click to capture the image. The captured image is shown in another window over the original camera image windows. I am able to crop and click to save this captured image.
However, right after I click save, the image is getting stretched, and after it is saved, it remains stretched as well. In essence, the image I capture and save is being stretched,
I tried changing the CroppedSizeInPixels with size from (0,0) to (200,200). And also various CroppedAspectRatios.
What am I doing wrong? Should I be using any other values for these settings?
Any help here is appreciated.

C# selenium webdriver - How to save a specific image regardless of location

I've seen a ton of examples how to save an image based on saving a screenshot.
This has a fundamental flaw. The take screenshot only takes whats visable on the page at the time. So if I have an image at the bottom of the page, and I want to save it based on the location of the elements that I found, either one or two problems occur.
If I save the screenshot and then try to save by the location, the screen shot ends at point 1200 but the image is located at 3000. If i focus on the image and then take a screen shot, the image is is now on the screen shot, however, the location doesn't work. It doesn't work because I still have a 1200px height image with a location of 3000.
How can I simply say, I have an image at 3000x 3014y and I just want to save it?
You can basically get base64 string of the image and save it to file.
var base64string = driver.ExecuteScript(#"
var c = document.createElement('canvas');
var ctx = c.getContext('2d');
var img = document.getElementsByTagName('img')[0];
c.height=img.height;
c.width=img.width;
ctx.drawImage(img, 0, 0,img.width, img.height);
var base64String = c.toDataURL();
return base64String;
") as string;
var base64 = base64string.Split(',').Last();
using (var stream = new MemoryStream(Convert.FromBase64String(base64)))
{
using (var bitmap = new Bitmap(stream))
{
var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{Guid.NewGuid()}.jpg");
bitmap.Save(filepath, ImageFormat.Jpeg);
}
}
I've resolved it. Chrome vs IE vs Firefox take screen shots in different manners. Firefox will stitch the screenshot together, chrome only takes whats visible and IE will shrink everything to try to fit on one page.
If you need to do a screenshot of something that is not on the base page i'd suggest firefox

Take screenshot of wpf popup window

I try to take a screenshot of an application writen in WPF and the application is not captured, must I use a special tool to take the screenshot?
You can use RenderTargetBitmap to generate an image from your WPF control.
public const int IMAGE_DPI = 96;
public Image GenerateImage(T control)
where T : Control, new()
{
Size size = RetrieveDesiredSize(control);
Rect rect = new Rect(0, 0, size.Width, size.Height);
RenderTargetBitmap rtb = new RenderTargetBitmap((int)size.Width, (int)size.Height, IMAGE_DPI, IMAGE_DPI, PixelFormats.Pbgra32);
control.Arrange(rect); //Let the control arrange itself inside your Rectangle
rtb.Render(control); //Render the control on the RenderTargetBitmap
//Now encode and convert to a gdi+ Image object
PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
using (MemoryStream stream = new MemoryStream())
{
png.Save(stream);
return Image.FromStream(stream);
}
}
private Size RetrieveDesiredSize(T control)
{
if (Equals(control.Width, double.NaN) || Equals(control.Height, double.NaN))
{
//Make sure the control has measured first:
control.Measure(new Size(double.MaxValue, double.MaxValue));
return control.DesiredSize;
}
return new Size(control.Width, control.Height);
}
Note that this will generate a PNG image ;) If you wish to store it as a JPEG, I suggest you use another encoder :)
Image image = GenerateImage(gridControl);
image.Save("mygrid.png");
You can simply press PrtScr button (windows will copy whole descktop image to buffer), then paste it in Power Point and crop if you like.
I'm having the same issue, I need to take screenshots to document my tests but can't seem to get there.
The Window in question is a Borderless modal Window with rounded corners / transparency allowed. Here's my report:
HP Quality Center's Screenshot tool doesn't recognize it as a window and thus takes its screenshots as if the window weren't there.
SnagIt utilizes a keycombo to enter the capture mode. Once stroke is hit, the popup vanishes. It reappears after the capture has ended.
Standard Windows capture works OK (Alt + Prt Scr) and captures the window as it was intended to be captured.
Next thing I tried was capturing the window with an opened dropdownlist. None of the approaches mentioned above seems to work (the last approach captures the Window the same way it did before, without the open dropdown).
As far as I have understood all the talk correctly, the only thing you can do is implement this into the applications...

Categories

Resources