Ranorex:Image Based Automation Technique

Ranorex:Image Based Automation Technique

Image Based Automation Technique Using Ranorex to Automate a Web Page

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;
using Ranorex.Controls;

namespace superstar
{
public partial class ImageComparision
{

private void Init()
{

//Open Browser
Host.Local.OpenBrowser(“http://us.wwe.com/superstars“,”firefox.exe”);
Delay.Seconds(15);  //Static wait

//Print Current Time-stamp     
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:/CaptureImage/”+name1;
String name2=”2b_”+CurrentTimestamp+”.png”;
Console.WriteLine(name2);
name2=”D:/CaptureImage/”+name2;

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

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

String name5=”2e_”+CurrentTimestamp+”.png”;
Console.WriteLine(name5);
name5=”D:/CaptureImage/”+name5;

String name6=”2f_”+CurrentTimestamp+”.png”;
Console.WriteLine(name6);
name6=”D:/CaptureImage/”+name6;

// Ranorex RxPath of Objects Available on the page

string xpath1 = “/dom[@domain=’us.wwe.com’]//div[#’block-system-main’]/div/div[1]”;
string xpath2 = “/dom[@domain=’us.wwe.com’]//div[#’shared_widget_51497′]”;
string xpath3 = “/dom[@domain=’us.wwe.com’]//div[#’button_tout_47294′]”;
string xpath4 = “/dom[@domain=’us.wwe.com’]//div[#’shared_widget_24696′]”;
string xpath5 = “/dom[@domain=’us.wwe.com’]//div[#’appearance_tout_47278′]”;
string xpath6 = “/dom[@domain=’us.wwe.com’]//div[#’buy_tickets_47279′]”;

//Capture the Object1 from Test URL
Report.Log(ReportLevel.Info, “Snapshot”, “Capturing Screenshot from Test URL”);
Bitmap bmp1 = get_Snapshot(xpath1);
bmp1.Save(name1, System.Drawing.Imaging.ImageFormat.Png);

//Capture the Object2 from Test URL
Report.Log(ReportLevel.Info, “Snapshot”, “Capturing Screenshot from Test URL”);
Bitmap bmp2 = get_Snapshot(xpath2);
bmp2.Save(name2, System.Drawing.Imaging.ImageFormat.Png);

//Page Scroll in web browser
Keyboard.Press(“{Down 12}”);

//Capture the Object3 from Test URL
Report.Log(ReportLevel.Info, “Snapshot”, “Capturing Screenshot from Test URL”);
Bitmap bmp3 = get_Snapshot(xpath3);
bmp3.Save(name3, System.Drawing.Imaging.ImageFormat.Png);

//Capture the Object4 from Test URL
Report.Log(ReportLevel.Info, “Snapshot”, “Capturing Screenshot from Test URL”);
Bitmap bmp4 = get_Snapshot(xpath4);
bmp4.Save(name4, System.Drawing.Imaging.ImageFormat.Png);

//Capture the Object5 from Test URL
Report.Log(ReportLevel.Info, “Snapshot”, “Capturing Screenshot from test URL”);
Bitmap bmp5 = get_Snapshot(xpath5);
bmp5.Save(name5, System.Drawing.Imaging.ImageFormat.Png);

//Capture the Object6 from Test URL
Report.Log(ReportLevel.Info, “Snapshot”, “Capturing Screenshot from Test URL”);
Bitmap bmp6 = get_Snapshot(xpath6);
bmp6.Save(name6, System.Drawing.Imaging.ImageFormat.Png);

//Stored Image Location
Bitmap bmp7 = new Bitmap(“D:/CapturedStoredImage/Superstars/img1.png”);
Bitmap bmp8 = new Bitmap(“D:/CapturedStoredImage/Superstars/img2.png”);
Bitmap bmp9 = new Bitmap(“D:/CapturedStoredImage/Superstars/img3.png”);
Bitmap bmp10 = new Bitmap(“D:/CapturedStoredImage/Superstars/img4.png”);
Bitmap bmp11 = new Bitmap(“D:/CapturedStoredImage/Superstars/img5.png”);
Bitmap bmp12 = new Bitmap(“D:/CapturedStoredImage/Superstars/img6.png”);

//Compare Images :: Comparing two same images
this.compareImages(bmp1, bmp7);
this.compareImages(bmp2, bmp8);
this.compareImages(bmp3, bmp9);
this.compareImages(bmp4, bmp10);
this.compareImages(bmp5, bmp11);
this.compareImages(bmp6, bmp12);

//Negative Scenario :: Compare two different images
this.compareImages(bmp1, bmp2);
}

//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;
}
}

//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”);
}
}
}

2 thoughts on “Ranorex:Image Based Automation Technique

Leave a Reply

Your email address will not be published. Required fields are marked *