如何做基于图像的自动化
如果Ranorex不能明确地识别某些你的GUI元素,那么使用基于图像的机制来自动化它们将会大有帮助。
C#
// Create bitmap to search for
// within application form and
// click it
Bitmap bmp = Ranorex.Imaging.Load(
@”..\..\Green Sea Turtle Small.bmp”);
// Performs a right click on the image found
// within the application window based on
// the image location
myRepo.WinFormsApp.Self.Click(MouseButtons.Right,bmp);
// You can also search for images that are slightly different to the
// loaded image by specifying the minimum Similarity for a match (95% = 0.95).
myRepo.WinFormsApp.Self.Click(new Location(bmp, new Imaging.FindOptions(0.95)));
// OR Set the default Similarity value for all following image operations
Imaging.FindOptions.Default.Similarity = 0.95;
myRepo.WinFormsApp.Self.Click(bmp);
Report.Success(“Image found and clicked successfully”);
VB.NET
‘ Create bitmap to search for
‘ within application form and
‘ click it
Dim bmp As Bitmap = Ranorex.Imaging.Load(“..\..\Green Sea Turtle Small.bmp”)
‘ Performs a right click on the image found
‘ within the application window based
‘ on the image location
myRepo.WinFormsApp.Self.Click(MouseButtons.Right, bmp)
‘ You can also search for images that are slightly different to the
‘ loaded image by specifying the minimum Similarity for a match (95% = 0.95).
myRepo.WinFormsApp.Self.Click(new Location(bmp, new Imaging.FindOptions(0.95)))
‘ OR Set the default Similarity value for all following image operations
Imaging.FindOptions.Default.Similarity = 0.95
myRepo.WinFormsApp.Self.Click(bmp)
Report.Success(“Image displayed successfully”)
如何查找和比较图像
在Ranorex里面使用图像比较来查找对象,可以使用contains方法
C#
// Create bitmap
Bitmap bmp = Ranorex.Imaging.Load(
@”..\..\Green Sea Turtle Small.bmp”);
// Search for it within the application window
if (Ranorex.Imaging.Contains(myRepo.WinFormsApp.Self,bmp) == true)
{
Report.Success(“Image found within WinForms application”);
}
VB.NET
‘ Create bitmap
Dim bmp As Bitmap = Ranorex.Imaging.Load(“..\..\Green Sea Turtle Small.bmp”)
‘ Search for it within the application window
If Imaging.Contains(myRepo.WinFormsApp.Self,bmp Then
Report.Success(“Image found within WinForms application”)
End If
注意:两个例子都是加载为压缩的图像(BMP, PNG格式)来进行一对一的比较。使用FindOption类可以来定义相似性等其他属性。
处理意外出现的对话框
UI测试中常出现的一个问题是意外出现的对话框,例如KeePass里面的Update-Check对话框。
为了克服这个问题,你可以使用PopupWatcher类。
使用这个类,你可以为每一个可能在测试执行过程中弹出的对话框添加watch
在这些watch里面,你可以指定PopupWatcher需要关注的RanoreXPath或者是对象库里的项。一旦出现,那么将会触发一个处理方法,或者对象库的项会被点击。
C#
void ITestModule.Run()
{
// Create PopupWatcher
PopupWatcher myPopupWatcher = new PopupWatcher();
// Add a Watch using a RanoreXPath and triggering the Method CloseUpdateCheckDialog
myPopupWatcher.Watch(“/form[@controlname=’UpdateCheckForm’]/button[@controlname=’m_btnClose’]”, CloseUpdateCheckDialog);
// Add a Watch using the info object of a button and triggering the Method CloseUpdateCheckDialog
// myPopupWatcher.Watch(repo.UpdateCheckDialog.btCloseInfo, CloseUpdateCheckDialog);
// Add a Watch using the info object of the dialog and the info object of the button to click
// myPopupWatcher.WatchAndClick(repo.UpdateCheckDialog.SelfInfo, repo.UpdateCheckDialog.btCloseInfo);
// Add a Watch using a repository folder object and the info object of the button to click
// myPopupWatcher.WatchAndClick(repo.UpdateCheckDialog, repo.UpdateCheckDialog.btCloseInfo);
// Start PopupWatcher
myPopupWatcher.Start();
}
public static void CloseUpdateCheckDialog(Ranorex.Core.Repository.RepoItemInfo myInfo, Ranorex.Core.Element myElement)
{
myElement.As<ranorex.button>().Click();
}
public static void CloseUpdateCheckDialog(Ranorex.Core.RxPath myPath, Ranorex.Core.Element myElement)
{
myElement.As<ranorex.button>().Click();
}
VB.NET
Private Sub ITestModule_Run() Implements ITestModule.Run
‘ Create PopupWatcher
Dim myPopupWatcher As New PopupWatcher()
‘ Add a Watch using a RanoreXPath and triggering the Method CloseUpdateCheckDialog
myPopupWatcher.Watch(“/form[@controlname=’UpdateCheckForm’]/button[@controlname=’m_btnClose’]”, AddressOf CloseUpdateCheckDialog)
‘ Add a Watch using the info object of a button and triggering the Method CloseUpdateCheckDialog
‘ myPopupWatcher.Watch(repo.UpdateCheckDialog.btCloseInfo, CloseUpdateCheckDialog);
‘ Add a Watch using the info object of the dialog and the info object of the button to click
‘ myPopupWatcher.WatchAndClick(repo.UpdateCheckDialog.SelfInfo, repo.UpdateCheckDialog.btCloseInfo);
‘ Add a Watch using a repository folder object and the info object of the button to click
‘ myPopupWatcher.WatchAndClick(repo.UpdateCheckDialog, repo.UpdateCheckDialog.btCloseInfo);
‘ Start PopupWatcher
myPopupWatcher.Start()
End Sub