private void WillSwitchToFrame_(IWebElement targetFrame) { ArgumentGuard.NotNull(targetFrame, nameof(targetFrame)); if (!(targetFrame is EyesRemoteWebElement eyesFrame)) { eyesFrame = new EyesRemoteWebElement(logger_, driver_, targetFrame); } Point pl = targetFrame.Location; Size ds = targetFrame.Size; SizeAndBorders sizeAndBorders = eyesFrame.SizeAndBorders; RectangularMargins borderWidths = sizeAndBorders.Borders; Size clientSize = sizeAndBorders.Size; Rectangle bounds = eyesFrame.GetClientBounds(); Point contentLocation = new Point(bounds.X + borderWidths.Left, bounds.Y + borderWidths.Top); //Point originalLocation = ScrollPositionProvider.GetCurrentPosition(jsExecutor_, driver_.FindElement(By.TagName("html"))); Point originalLocation = eyesFrame.ScrollPosition; Frame frame = new Frame(logger_, targetFrame, contentLocation, new Size(ds.Width, ds.Height), clientSize, originalLocation, bounds, borderWidths, jsExecutor_); driver_.GetFrameChain().Push(frame); }
public Bitmap CaptureWindow( IntPtr window, bool clientArea, RectangularMargins padding, bool fromScreen = true, int redrawWait = 0) { if (fromScreen) { return(popupWindow_.TakeSnapshot(window, clientArea, padding, redrawWait)); } return(CaptureWindowFromWindow_(window, clientArea)); }
/// <inheritdoc /> public void Configure(IReadAccessor <string> reader) { ArgumentGuard.NotNull(reader, nameof(reader)); string traceMsg = Tracer.FormatCall("Configure"); try { logger_.Log(traceMsg); #pragma warning disable IDE0018 // Inline variable declaration string value; #pragma warning restore IDE0018 // Inline variable declaration if (reader.TryGetValue(this, "MonitorHost", out value)) { MonitorHost = bool.Parse(value); } if (reader.TryGetValue(this, "MonitorProcessesByName", out value)) { MonitorProcessesByName = string.IsNullOrEmpty(value) ? null : new Regex(value); } if (reader.TryGetValue(this, "MonitorProcessesById", out value)) { MonitorProcessesById = value.ToInt32(); } if (reader.TryGetValue(this, "MonitorWindowsByTitle", out value)) { MonitorWindowsByTitle = string.IsNullOrEmpty(value) ? null : new Regex(value); } if (reader.TryGetValue(this, "CapturePadding", out value)) { CapturePadding = string.IsNullOrEmpty(value) ? default(RectangularMargins) : RectangularMargins.Parse(value); } logger_.Verbose("{0} succeeded", traceMsg); } catch (Exception ex) { CommonUtils.LogExceptionStackTrace(logger_, Stage.Check, StageType.CaptureScreenshot, ex); throw; } }
private Point CalcFrameLocationInScreenshot_(FrameChain frameChain, ScreenshotTypeEnum screenshotType) { EyesWebDriverTargetLocator switchTo = (EyesWebDriverTargetLocator)driver_.SwitchTo(); FrameChain currentFC = frameChain.Clone(); switchTo.DefaultContent(); Point locationInScreenshot = Point.Empty; foreach (Frame frame in currentFC) { Rectangle rect = ((EyesRemoteWebElement)frame.Reference).GetClientBounds(); SizeAndBorders sizeAndBorders = ((EyesRemoteWebElement)frame.Reference).SizeAndBorders; RectangularMargins borders = sizeAndBorders.Borders; rect.Offset(borders.Left, borders.Top); locationInScreenshot.Offset(rect.Location); switchTo.Frame(frame.Reference); } return(locationInScreenshot); }
private void WillSwitchToFrame_(IWebElement targetFrame) { ArgumentGuard.NotNull(targetFrame, nameof(targetFrame)); if (!(targetFrame is EyesRemoteWebElement eyesFrame)) { eyesFrame = new EyesRemoteWebElement(logger_, driver_, targetFrame); } Size ds = targetFrame.Size; SizeAndBorders sizeAndBorders = eyesFrame.SizeAndBorders; RectangularMargins borderWidths = sizeAndBorders.Borders; Size clientSize = sizeAndBorders.Size; Rectangle bounds = eyesFrame.GetClientBounds(); Point contentLocation = new Point(bounds.X + borderWidths.Left, bounds.Y + borderWidths.Top); Point originalLocation = eyesFrame.ScrollPosition; Frame frame = new Frame(logger_, targetFrame, contentLocation, new Size(ds.Width, ds.Height), clientSize, originalLocation, bounds, borderWidths, jsExecutor_); var activeElementBeforeSwitch = targetLocator_.ActiveElement(); targetLocator_.Frame(targetFrame); var activeElementAfterSwitch = targetLocator_.ActiveElement(); if (!activeElementAfterSwitch.Equals(activeElementBeforeSwitch)) { driver_.GetFrameChain().Push(frame); } else { throw new Exception("Failed switching to frame."); } }
public Frame(Logger logger, IWebElement reference, Point location, Size outerSize, Size innerSize, Point originalLocation, Rectangle bounds, RectangularMargins borderWidths, IEyesJsExecutor jsExecutor) { ArgumentGuard.NotNull(logger, nameof(logger)); ArgumentGuard.NotNull(reference, nameof(reference)); ArgumentGuard.NotNull(jsExecutor, nameof(jsExecutor)); logger.Verbose("Frame(logger, {0}, {1}, {2}, {3}, {4})", reference, location, outerSize, innerSize, originalLocation); logger_ = logger; Reference = reference; Location = location; OuterSize = outerSize; InnerSize = innerSize; OriginalLocation = originalLocation; Bounds = bounds; BorderWidths = borderWidths; positionMemento_ = new ScrollPositionMemento(originalLocation); jsExecutor_ = jsExecutor; }
/// <summary> /// Takes a screen snapshot of the input window. /// </summary> /// <param name="window">Window to capture</param> /// <param name="clientArea"> /// Whether to capture the client area or the full window /// </param> /// <param name="padding"> /// Inner padding defining the window region to capture or <c>null</c> to capture the /// entire window /// </param> /// <param name="redrawWait"> /// Delay in milliseconds to allow the background window to draw /// </param> public Bitmap TakeSnapshot( IntPtr window, bool clientArea, RectangularMargins padding, int redrawWait) { lock (lock_) { if (IsDisposed) { return(null); } return((Bitmap)this.Invoke( new CaptureWindowFromScreenInvoker_(CaptureWindowFromScreen_), window, clientArea, padding, redrawWait)); } }
/// <summary> /// Creates a new <see cref="GuiCapturer"/> instance. /// </summary> public GuiCapturer(Logger logger, ILowLevelCaptureApi captureApi) { ArgumentGuard.NotNull(logger, nameof(logger)); ArgumentGuard.NotNull(captureApi, nameof(captureApi)); logger_ = logger; captureApi_ = captureApi; captureApi_.EventMask = GuiEventTypes.WindowActivated | GuiEventTypes.WindowMoved | GuiEventTypes.WindowResized | GuiEventTypes.Hid; captureApi_.GuiEvent += CaptureApiGuiEventHandler_; using (var host = Process.GetCurrentProcess()) { hostId_ = host.Id; } MonitorHost = false; CapturePadding = new RectangularMargins(); MonitorProcessesById = -1; }
/// <summary> /// Captures the screen area overlapping the input window or its client area /// while placing this form as the window background which normalizes /// transparent areas. /// </summary> /// <returns>The captured window image or <c>null</c> on failure</returns> private Bitmap CaptureWindowFromScreen_( IntPtr window, bool clientArea, RectangularMargins padding, int redrawWait) { string traceMsg = Tracer.FormatCall( "CaptureWindowFromScreen_", window, clientArea, redrawWait); Bitmap result = null; try { logger_.Log(traceMsg); if (window == IntPtr.Zero) { this.Opacity = 0; return(null); } DateTime start = DateTime.Now; if (!WindowsGuiUtils.GetWindowRectangles(logger_, window, out SafeNativeMethods.RECT wr, out SafeNativeMethods.RECT cr)) { logger_.Log("{0}: GetWindowRectangles failed", traceMsg); this.Opacity = 0; return(null); } var r = clientArea ? cr : wr; r.Left += padding.Left; r.Top += padding.Top; r.Right -= padding.Right; r.Bottom -= padding.Bottom; if (r.Width <= 0 || r.Height <= 0) { logger_.Log("{0}: Target rectangle is Zero", traceMsg); this.Opacity = 0; return(null); } result = new Bitmap(r.Width, r.Height); bool hideCursor = r.Contains(Cursor.Position); if (hideCursor) { Cursor.Hide(); } if (!WindowsGuiUtils.ChangeWindowZOrder(logger_, this.Handle, window, true)) { logger_.Log("{0}: Failed to position background window", traceMsg); } this.SetBounds(r.Left, r.Top, r.Width, r.Height); this.Opacity = 100; this.Refresh(); if (redrawWait > 0) { Thread.Sleep(redrawWait); } using (Graphics g = Graphics.FromImage(result)) { try { g.CopyFromScreen( new Point(r.Left, r.Top), new Point(0, 0), new Size(r.Width, r.Height)); } catch (Exception ex) { logger_.Log("{0}: CopyFromScreen failed: {1}", traceMsg, ex); throw; } } if (hideCursor) { Cursor.Show(); } // Fail capture if window was moved while it was taken. if (WindowsGuiUtils.GetWindowRectangles(logger_, window, out SafeNativeMethods.RECT wr2, out SafeNativeMethods.RECT cr2)) { if (!wr2.Equals(wr)) { logger_.Verbose("{0}: Aborting due to window movement!", traceMsg); result.Dispose(); return(null); } } logger_.Verbose( "{0} succeeded in {1}ms", traceMsg, DateTime.Now.Subtract(start).TotalMilliseconds); return(result); } catch (Exception ex) { if (result != null) { result.Dispose(); } logger_.Log("{0} failed: {1}", traceMsg, ex); return(null); } }