Skip to content

Commit

Permalink
Merge pull request #3429 from Ginger-Automation/Feature/ScreenshotPer…
Browse files Browse the repository at this point in the history
…formanceFix

Screen shot Optimization
  • Loading branch information
prashelke authored Jan 9, 2024
2 parents 00456a6 + ba67a56 commit ed2fb3a
Show file tree
Hide file tree
Showing 17 changed files with 281 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1119,8 +1119,7 @@ public void ShowScreenShot()

/// UnComment later after complete Implementation of functionalities over all platforms.
//if(IsWebMobJavaPlatform)
mWindowExplorerDriver.UnHighLightElements();

mWindowExplorerDriver.UnHighLightElements();
try
{
LoadPageSourceDoc = mWindowExplorerDriver.SupportedViews().Contains(eTabView.PageSource);
Expand All @@ -1130,22 +1129,24 @@ public void ShowScreenShot()
return;
}

Bitmap ScreenShotBitmap = ((IVisualTestingDriver)((AgentOperations)mApplicationAgent.Agent.AgentOperations).Driver).GetScreenShot();
mScreenShotViewPage = new ScreenShotViewPage("", ScreenShotBitmap, (mWindowExplorerDriver as DriverBase).ScreenShotInitialZoom());
using (Bitmap ScreenShotBitmap = ((IVisualTestingDriver)((AgentOperations)mApplicationAgent.Agent.AgentOperations).Driver).GetScreenShot())
{
mScreenShotViewPage = new ScreenShotViewPage("", ScreenShotBitmap, (mWindowExplorerDriver as DriverBase).ScreenShotInitialZoom());

mScreenShotViewPage.ImageMouseCursor = Cursors.Hand;
/// UnComment to allow Element detection on hover
//if (mPlatform.PlatformType() == ePlatformType.Web)
//{
// mScreenShotViewPage.xMainImage.MouseMove += XMainImage_MouseMove;
//}
mScreenShotViewPage.ImageMouseCursor = Cursors.Hand;
///TODO UnComment to allow Element detection on hover
//if (mPlatform.PlatformType() == ePlatformType.Web)
//{
// mScreenShotViewPage.xMainImage.MouseMove += XMainImage_MouseMove;
//}

mScreenShotViewPage.xMainImage.MouseLeftButtonDown += XMainImage_MouseLeftButtonDown;
mScreenShotViewPage.xMainImage.MouseLeftButtonDown += XMainImage_MouseLeftButtonDown;

xScreenShotFrame.ClearAndSetContent(mScreenShotViewPage);
//xDeviceImage.Source = General.ToBitmapSource(ScreenShotBitmap);
xScreenShotFrame.ClearAndSetContent(mScreenShotViewPage);
//xDeviceImage.Source = General.ToBitmapSource(ScreenShotBitmap);

xScreenShotFrame.Visibility = Visibility.Visible;
xScreenShotFrame.Visibility = Visibility.Visible;
}
}
catch (Exception exc)
{
Expand Down
25 changes: 10 additions & 15 deletions Ginger/GingerCore/Actions/UIAutomation/UIAComWrapperHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4854,8 +4854,10 @@ public override void TakeScreenShot(ActScreenShot act)
{
if (CurrentWindow != null)
{
Bitmap tempBmp = GetCurrentWindowBitmap();
act.AddScreenShot(tempBmp);
using (Bitmap tempBmp = GetCurrentWindowBitmap())
{
act.AddScreenShot(tempBmp);
}
}
}
return;
Expand All @@ -4873,8 +4875,7 @@ public override void TakeScreenShot(ActScreenShot act)

public override Bitmap GetCurrentWindowBitmap()
{
Bitmap bmp = WindowToBitmap(CurrentWindow);
return bmp;
return WindowToBitmap(CurrentWindow);
}

public List<AppWindow> GetCurrentAppWindows()
Expand All @@ -4900,8 +4901,7 @@ public override Bitmap GetAppWindowAsBitmap(AppWindow aw) //******************
try
{
UIAuto.AutomationElement tempWindow = (UIAuto.AutomationElement)((UIAElementInfo)aw.RefObject).ElementObject;
Bitmap bmp = WindowToBitmap(tempWindow);
return bmp;
return WindowToBitmap(tempWindow);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -4929,27 +4929,22 @@ public override List<Bitmap> GetAppDialogAsBitmap(AppWindow aw) ///********
for (int i = 0; i < AEList.Count; i++)
{
UIAuto.AutomationElement ele = AEList[i];
Bitmap bmp = WindowToBitmap(ele);
bList.Add(bmp);
bList.Add(WindowToBitmap(ele));
}
for (int i = 0; i < AEList1.Count; i++)
{
UIAuto.AutomationElement ele = AEList1[i];
Bitmap bmp = WindowToBitmap(ele);
bList.Add(bmp);
bList.Add(WindowToBitmap(ele));

}

for (int i = 0; i < AEListWindows.Count; i++)
{
List<Bitmap> winPopup = new List<Bitmap>();
if (AEListWindows[i].Current.BoundingRectangle.X != 0 && AEListWindows[i].Current.BoundingRectangle.Y != 0)
{
Bitmap bmp = WindowToBitmap(AEListWindows[i]);
winPopup.Add(bmp); //adding screenshot of element type "window"
bList.Add(WindowToBitmap(AEListWindows[i]));//adding screenshot of element type "window"
}
bList.AddRange(winPopup);
}

return bList;
}

Expand Down
8 changes: 5 additions & 3 deletions Ginger/GingerCore/Drivers/ASCF/ASCFDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,15 @@ public override void RunAction(Act act)
{
act.AddScreenShot(screenShot);
}
screenShots.Clear();
}
else
{
Bitmap tempBmp = TakeScreenShot(actSS);
act.AddScreenShot(tempBmp);
using (Bitmap tempBmp = TakeScreenShot(actSS))
{
act.AddScreenShot(tempBmp);
}
}

break;
case "ActGetMsgboxText":
ActGetMsgboxText actmsg = (ActGetMsgboxText)act;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1201,8 +1201,10 @@ public Bitmap GetScreenShot()

internal void AddScreenShot(Act act)
{
Bitmap tempBmp = GetScreenShot();
act.AddScreenShot(tempBmp);
using (Bitmap tempBmp = GetScreenShot())
{
act.AddScreenShot(tempBmp);
}
}

internal void GotoURL(string p)
Expand Down
6 changes: 4 additions & 2 deletions Ginger/GingerCore/Drivers/JavaDriverLib/JavaDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1844,8 +1844,10 @@ public PayLoad TakeScreenShots(ActScreenShot actScreenShot)
}

TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
Bitmap btmp = (Bitmap)tc.ConvertFrom(screenShotbytes);
actScreenShot.AddScreenShot(btmp);
using (Bitmap btmp = (Bitmap)tc.ConvertFrom(screenShotbytes))
{
actScreenShot.AddScreenShot(btmp);
}
}
catch (Exception ex)
{
Expand Down
34 changes: 18 additions & 16 deletions Ginger/GingerCore/Drivers/MainFrame/MainFrameDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,24 +514,26 @@ public override bool IsRunning()

public void TakeScreenShot(Act act)
{
int width = (int)mDriverWindow.Width;
int height = (int)mDriverWindow.Height;
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(mDriverWindow);

PngBitmapEncoder pngImage = new PngBitmapEncoder();
pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
string FileName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".png";
using (Stream fileStream = File.Create(FileName))
{
pngImage.Save(fileStream);
fileStream.Close();
}
int width = (int)mDriverWindow.Width;
int height = (int)mDriverWindow.Height;
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(mDriverWindow);

PngBitmapEncoder pngImage = new PngBitmapEncoder();
pngImage.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
string FileName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".png";
using (Stream fileStream = File.Create(FileName))
{
pngImage.Save(fileStream);
fileStream.Close();
}

Bitmap bmp = (Bitmap)Image.FromFile(FileName);
//TODO: Remove the temporary File created
using (Bitmap bmp = (Bitmap)Image.FromFile(FileName))
{
//TODO: Remove the temporary File created

act.AddScreenShot(bmp);
act.AddScreenShot(bmp);
}
}

#endregion GingerFunctions
Expand Down
18 changes: 11 additions & 7 deletions Ginger/GingerCore/Drivers/PBDriver/PBDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,12 @@ public override void RunAction(Act act)
List<Bitmap> bList;
foreach (AppWindow aw in list)
{
Bitmap bmp = mUIAutomationHelper.GetAppWindowAsBitmap(aw);
if (bmp != null)
using (Bitmap bmp = mUIAutomationHelper.GetAppWindowAsBitmap(aw))
{
act.AddScreenShot(bmp);
if (bmp != null)
{
act.AddScreenShot(bmp);
}
}
bList = mUIAutomationHelper.GetAppDialogAsBitmap(aw);
foreach (Bitmap tempbmp in bList)
Expand All @@ -249,8 +251,10 @@ public override void RunAction(Act act)
{
if (mUIAutomationHelper.GetCurrentWindow() != null)
{
Bitmap bmp = mUIAutomationHelper.GetCurrentWindowBitmap();
act.AddScreenShot(bmp);
using (Bitmap bmpcurrentwin = mUIAutomationHelper.GetCurrentWindowBitmap())
{
act.AddScreenShot(bmpcurrentwin);
}
}
}
return;
Expand Down Expand Up @@ -1250,8 +1254,8 @@ public bool CanStartAnotherInstance(out string errorMessage)
/// <returns>Application Screenshot in Bitmap format</returns>
public Bitmap GetScreenShot(Tuple<int, int> setScreenSize = null, bool IsFullPageScreenshot = false)
{
Bitmap bmp = mUIAutomationHelper.GetCurrentWindowBitmap();
return bmp;
return mUIAutomationHelper.GetCurrentWindowBitmap();

}

/// <summary>
Expand Down
9 changes: 5 additions & 4 deletions Ginger/GingerCore/Drivers/WindowsLib/WindowsDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,10 @@ public override void RunAction(Act act)
//Disabling the capturing all windows for Windows driver until we fix show window issue
if (mUIAutomationHelper.GetCurrentWindow() != null)
{
Bitmap bmp = mUIAutomationHelper.GetCurrentWindowBitmap();
act.AddScreenShot(bmp);
using (Bitmap bmp = mUIAutomationHelper.GetCurrentWindowBitmap())
{
act.AddScreenShot(bmp);
}
}
//if not running well. need to add return same as PBDrive
}
Expand Down Expand Up @@ -1571,8 +1573,7 @@ private void HandleWindowsGenericWidgetControlAction(ActGenElement actWinC)

public Bitmap GetScreenShot(Tuple<int, int> setScreenSize = null, bool IsFullPageScreenshot = false)
{
Bitmap bmp = mUIAutomationHelper.GetCurrentWindowBitmap();
return bmp;
return mUIAutomationHelper.GetCurrentWindowBitmap();
}

ObservableList<ElementInfo> IWindowExplorer.GetElements(ElementLocator EL)
Expand Down
26 changes: 22 additions & 4 deletions Ginger/GingerCoreCommon/Actions/Act.cs
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,11 @@ public void AddScreenShot(Bitmap bmp, string Name = "")
{
try
{
ScreenShots.Add(SaveScreenshotToTempFile(bmp));
ScreenShotsNames.Add(Name);
using (bmp)
{
ScreenShots.Add(SaveScreenshotToTempFile(bmp));
ScreenShotsNames.Add(Name);
}
}
catch (Exception ex)
{
Expand All @@ -999,9 +1002,10 @@ public void AddScreenShot(Bitmap bmp, string Name = "")

public void AddScreenShot(string Base64String, string Name = "")
{
byte[] bytes = Convert.FromBase64String(Base64String);
try
{
byte[] bytes = Convert.FromBase64String(Base64String);

string filePath = GetScreenShotRandomFileName();
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
{
Expand All @@ -1016,9 +1020,16 @@ public void AddScreenShot(string Base64String, string Name = "")
Error = "Failed to save the screenshot bitmap to temp file. Error= " + ex.Message;
Reporter.ToLog(eLogLevel.ERROR, Error, ex);
}
finally
{
if(bytes != null)
{
Array.Clear(bytes);
}

}
}


public void AddScreenShot(byte[] bytes, string Name)
{
try
Expand All @@ -1035,6 +1046,13 @@ public void AddScreenShot(byte[] bytes, string Name)
{
Error += "Unable to add Screen shot " + ex.Message;
}
finally
{
if (bytes != null)
{
Array.Clear(bytes);
}
}
}


Expand Down
30 changes: 16 additions & 14 deletions Ginger/GingerCoreNET/ActionsLib/UI/VisualTesting/MagickAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,28 @@ void IVisualAnalyzer.SetAction(IVisualTestingDriver driver, ActVisualTesting act

void IVisualAnalyzer.Compare()
{
MagickImage magickBaseImg = new MagickImage(General.ImageToByteArray(mAct.baseImage, System.Drawing.Imaging.ImageFormat.Bmp));//Not tested after code change
MagickImage magickTargetImg = new MagickImage(General.ImageToByteArray(mAct.targetImage, System.Drawing.Imaging.ImageFormat.Bmp));//Not tested after code change
MagickImage magickBaseImg = new MagickImage(General.ImageToByteArray(mAct.baseImage, System.Drawing.Imaging.ImageFormat.Bmp));//Not tested after code change
MagickImage magickTargetImg = new MagickImage(General.ImageToByteArray(mAct.targetImage, System.Drawing.Imaging.ImageFormat.Bmp));//Not tested after code change

MagickImage diffImg = new MagickImage();
MagickImage diffImg = new MagickImage();

double percentageDifference;
double percentageDifference;

ErrorMetric eErrorMetric = ErrorMetric.Fuzz;
Enum.TryParse<ErrorMetric>(mAct.GetOrCreateInputParam(ActVisualTesting.Fields.ErrorMetric).Value, out eErrorMetric);
percentageDifference = magickBaseImg.Compare(magickTargetImg, eErrorMetric, diffImg, Channels.Red);
percentageDifference = percentageDifference * 100;
percentageDifference = Math.Round(percentageDifference, 2);
ErrorMetric eErrorMetric = ErrorMetric.Fuzz;
Enum.TryParse<ErrorMetric>(mAct.GetOrCreateInputParam(ActVisualTesting.Fields.ErrorMetric).Value, out eErrorMetric);
percentageDifference = magickBaseImg.Compare(magickTargetImg, eErrorMetric, diffImg, Channels.Red);
percentageDifference = percentageDifference * 100;
percentageDifference = Math.Round(percentageDifference, 2);

TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
Bitmap ImgToSave = (Bitmap)tc.ConvertFrom(diffImg.ToByteArray());
mAct.CompareResult = ImgToSave;//Not tested after code change
TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
using (Bitmap ImgToSave = (Bitmap)tc.ConvertFrom(diffImg.ToByteArray()))
{
mAct.CompareResult = ImgToSave;//Not tested after code change

mAct.AddOrUpdateReturnParamActual("Percentage Difference", percentageDifference + "");
mAct.AddOrUpdateReturnParamActual("Percentage Difference", percentageDifference + "");

mAct.AddScreenShot(ImgToSave, "Compare Result");
mAct.AddScreenShot(ImgToSave, "Compare Result");
}
}

public void CreateBaseline()
Expand Down
Loading

0 comments on commit ed2fb3a

Please sign in to comment.