跟您在录制模式下使用对象库识别对象一样,你也可以在你的代码中使用对象库。只需添加一个新的私有成员代表您的对象库,如下所示:
C#
public class AddCredentialEntry : ITestModule
{
// Repository object to access UI Elements
MyFirstTestProjectRepository MyRepo = MyFirstTestProjectRepository.Instance;
/// Constructs a new instance.
public AddCredentialEntry()
{
// Do not delete – a parameterless constructor is required!
}
void ITestModule.Run()
{
Mouse.DefaultMoveTime = 300;
Keyboard.DefaultKeyPressTime = 100;
Delay.SpeedFactor = 1.0;
// Click ‘Add Entry’ Button MainMenu
MyRepo.MainForm.Edit.Click();
MyRepo.KeePass.AddEntry.Click();
// Set text fields
MyRepo.AddEntry.TabSheetAddEntry.Title.TextValue = “WordPressDemo”;
MyRepo.AddEntry.TabSheetAddEntry.UserName.TextValue = “admin”;
MyRepo.AddEntry.TabSheetAddEntry.Password.TextValue = “demo123”;
MyRepo.AddEntry.TabSheetAddEntry.Repeat.TextValue = “demo123”;
MyRepo.AddEntry.TabSheetAddEntry.URL.TextValue = “bitly.com/wp_demo”;
// Choose an icon
MyRepo.AddEntry.TabSheetAddEntry.MBtnIcon.Click();
MyRepo.IconPicker.LI_Icon.Click(Location.CenterLeft);
MyRepo.IconPicker.ButtonClose.Click();
// Set Expires
MyRepo.AddEntry.TabSheetAddEntry.MBtnStandardExpires.Click();
MyRepo.KeePass.MI_Expires.Click();
// Save Credential Entry
MyRepo.AddEntry.ButtonOK.Click();
}
}
VB.NET
Public Class AddCredentialEntry
Implements ITestModule
‘ Repository object to access UI Elements
Private MyRepo As MyFirstTestProjectRepository = MyFirstTestProjectRepository.Instance
”’ Constructs a new instance.
‘ Do not delete – a parameterless constructor is required!
Public Sub New()
End Sub
Private Sub ITestModule_Run() Implements ITestModule.Run
Mouse.DefaultMoveTime = 300
Keyboard.DefaultKeyPressTime = 100
Delay.SpeedFactor = 1.0
‘ Click ‘Add Entry’ Button MainMenu
MyRepo.MainForm.Edit.Click()
MyRepo.KeePass.AddEntry.Click()
‘ Set text fields
MyRepo.AddEntry.TabSheetAddEntry.Title.TextValue = “WordPressDemo”
MyRepo.AddEntry.TabSheetAddEntry.UserName.TextValue = “admin”
MyRepo.AddEntry.TabSheetAddEntry.Password.TextValue = “demo123”
MyRepo.AddEntry.TabSheetAddEntry.Repeat.TextValue = “demo123”
MyRepo.AddEntry.TabSheetAddEntry.URL.TextValue = “bitly.com/wp_demo”
‘ Choose an icon
MyRepo.AddEntry.TabSheetAddEntry.MBtnIcon.Click()
MyRepo.IconPicker.LI_Icon.Click(Location.CenterLeft)
MyRepo.IconPicker.ButtonClose.Click()
‘ Set Expires
MyRepo.AddEntry.TabSheetAddEntry.MBtnStandardExpires.Click()
MyRepo.KeePass.MI_Expires.Click()
‘ Save Credential Entry
MyRepo.AddEntry.ButtonOK.Click()
End Sub
End Class
注意:对象库的类名默认跟显示在项目视图中的对象库的文件名(*.rxrep)一样。
现在,类中将使用一个私有成员指向对象库,以便在”Run”方法中重用一些对象(例如:“标题”,“用户名”,“密码”,“重复密码’和’URL’)。
代码示例中用到的对象库
根据您的对象库的结构,在代码中访问对象库中的项可能会变得越来越复杂。为了降低复杂性 – 特别是当该UI元素使用频繁时 – 你应该使用局部变量来代替该元素在对象库中的完整路径。
C#
var ButtonOK = MyRepo.FormAdd_Entry.ButtonOK;
ButtonOK.Click();
VB.NET
Dim ButtonOK = MyRepo.FormAdd_Entry.ButtonOK
ButtonOK.Click()
要像上面的代码一样创建局部变量,直接从对象库浏览器中拖放元素到代码中即可。
注意:如果对象库本身不是类的一部分(例如新创建的代码模块),也会生成一个对象库的局部变量。