Screenshots - Group Test Steps - Image Comparison Questions

Ask general questions here.
kkashyap1707
Posts: 10
Joined: Tue Jul 29, 2014 1:19 pm

Coding to capture image and compare them using Android Mobil

Post by kkashyap1707 » Tue Aug 26, 2014 11:47 am

///////////////////////////////////////////////////////////////////////////////
//
// This file was automatically generated by RANOREX.
// Your custom recording code should go in this file.
// The designer will only add methods to this file, so your custom code won't be overwritten.
// http://www.ranorex.com
//
///////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using WinForms = System.Windows.Forms;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;

namespace Recording2
{
public partial class Recording1
{
/// <summary>
/// This method gets called right after the recording has been started.
/// It can be used to execute recording specific initialization code.
/// </summary>
private void Init()
{
// Your recording specific initialization code goes here.

//Open the Mobile Browser
Host.Local.RunMobileApp("S4", "ranorex.RxBrowser");
Delay.Seconds(2);

Report.Log(ReportLevel.Info, "Touch", "Touch item 'RanorexRxBrowser.TextUrl' at Center", repo.RanorexRxBrowser.TextUrlInfo, new RecordItemIndex(1));
repo.RanorexRxBrowser.TextUrl.Touch();
Delay.Milliseconds(100);

//Pass the URL
Report.Log(ReportLevel.Info, "Set Value", "Setting attribute Text to 'release.jenkins.wwe.com/shows' on item 'RanorexRxBrowser.TextUrl'.", repo.RanorexRxBrowser.TextUrlInfo, new RecordItemIndex(2));
repo.RanorexRxBrowser.TextUrl.Element.SetAttributeValue("Text", "release.jenkins.wwe.com/shows");
Delay.Milliseconds(100);

Report.Log(ReportLevel.Info, "Touch", "Touch item 'RanorexRxBrowser.ButtonGo' at Center", repo.RanorexRxBrowser.ButtonGoInfo, new RecordItemIndex(3));
repo.RanorexRxBrowser.ButtonGo.Touch();
Delay.Seconds(25);


/// /////////////////////////////////////
//
String CurrentTimestamp = System.DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss");
Console.WriteLine(CurrentTimestamp);

String name1="2a_"+CurrentTimestamp+".png";
Console.WriteLine(name1);
name1="D:/CaptureImageAndroid/"+name1;

String name2="2b_"+CurrentTimestamp+".png";
Console.WriteLine(name2);
name2="D:/CaptureImageAndroid/"+name2;

String name3="2c_"+CurrentTimestamp+".png";
Console.WriteLine(name3);
name3="D:/CaptureImageAndroid/"+name3;

String name4="2d_"+CurrentTimestamp+".png";
Console.WriteLine(name4);
name4="D:/CaptureImageAndroid/"+name4;

//Capture the Object from Test URL
Report.Log(ReportLevel.Info, "Snapshot", "Capturing Screenshot from Test URL");
Bitmap bmp1 = get_Snapshot("/mobileapp[@title='ranorex.RxBrowser']/form[@title='RxBrowserActivity']//dom[@domain='release.jenkins.wwe.com']//div[#'banner_tout_711']");
bmp1.Save(name1, System.Drawing.Imaging.ImageFormat.Png);

Report.Log(ReportLevel.Info, "Snapshot", "Capturing Screenshot from Test URL");
Bitmap bmp2 = get_Snapshot("/mobileapp[@title='ranorex.RxBrowser']/form[@title='RxBrowserActivity']//dom[@domain='release.jenkins.wwe.com']//div[#'show_quad_pager_35307']");
bmp2.Save(name2, System.Drawing.Imaging.ImageFormat.Png);


/// ////////////////////////
/// /////////////////////////////



//Stored Image Location
Bitmap bmp5 = new Bitmap("D:/CapturedStoredImageAndroid/img1.png");
Bitmap bmp6 = new Bitmap("D:/CapturedStoredImageAndroid/img2.png");



//Compare Images:-
this.compareImages(bmp1, bmp5);
this.compareImages(bmp2, bmp6);


}


//Compare Image Method
private void compareImages(Bitmap baselineImage, Bitmap testImage)
{
if (Ranorex.Imaging.Compare(testImage,baselineImage, 0.95))
Ranorex.Report.Success("Image matched with Baseline Image");
else
Ranorex.Report.Failure("Images are not Same");
}

//Get Snapshot Method
private Bitmap get_Snapshot(string xpath)
{
var elements = Ranorex.Host.Local.Find<Ranorex.Unknown>(xpath);
if(elements.Count>0)
{
return elements[0].CaptureCompressedImage();
}
else
{
return null;
}
}
}
}