From 59815bee1db70dbfbe0c97739bf2683318174cfd Mon Sep 17 00:00:00 2001 From: lefthandedgoat Date: Fri, 28 Mar 2014 13:31:22 -0500 Subject: [PATCH] Fix #135 Unhandeled alert will even blow up the runner --- canopy/canopy.fs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/canopy/canopy.fs b/canopy/canopy.fs index c2458a6f..d0916748 100644 --- a/canopy/canopy.fs +++ b/canopy/canopy.fs @@ -14,6 +14,8 @@ open levenshtein open reporters open types open finders +open System.Drawing +open System.Drawing.Imaging let mutable (browser : IWebDriver) = null let mutable (failureMessage : string) = null @@ -31,12 +33,33 @@ let mutable browsers = [] //misc let failsWith message = failureMessage <- message -let private takeScreenshot directory filename = - let pic = (browser :?> ITakesScreenshot).GetScreenshot().AsByteArray +let private saveScreenshot directory filename pic = if not <| Directory.Exists(directory) then Directory.CreateDirectory(directory) |> ignore IO.File.WriteAllBytes(Path.Combine(directory,filename + ".png"), pic) - pic + +let private takeScreenShotIfAlertUp () = + let bitmap = new Bitmap(width= System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, height= System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height, format=PixelFormat.Format32bppArgb); + use graphics = Graphics.FromImage(bitmap) + graphics.CopyFromScreen(System.Windows.Forms.Screen.PrimaryScreen.Bounds.X, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Y, 0, 0, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); + use stream = new MemoryStream() + bitmap.Save(stream, ImageFormat.Png) + stream.Close() + stream.ToArray() + + +let private takeScreenshot directory filename = + try + let pic = (browser :?> ITakesScreenshot).GetScreenshot().AsByteArray + saveScreenshot directory filename pic + pic + with + | :? OpenQA.Selenium.UnhandledAlertException as ex-> + let pic = takeScreenShotIfAlertUp() + saveScreenshot directory filename pic + let alert = browser.SwitchTo().Alert() + alert.Accept() + pic let screenshot directory filename = match box browser with