LDTP doc:
https://ldtp.freedesktop.org/user-doc/
对于 web 自动化,我们用 Selenium, 但是对于 windows 控件,我们可以使用 LDTP。
首先,需要引用的 jar 包。
CobraWinLDTP-3.0.0.msi (这个是需要安装的,安装到 C:Program Files (x86)VMwareCobraWinLDTP, 而核心的文件就是
commons-logging-1.1.2.jar
LDTP.jar
ws-commons-util-1.0.2.jar
xmlrpc-client-3.1.3.jar
xmlrpc-common-3.1.3.jar
CobraWinLDTP.exe。也就是它真正操作了 windows 控件。)
commons-codec-1.6.jarcommons-logging-1.1.2.jar
LDTP.jar
ws-commons-util-1.0.2.jar
xmlrpc-client-3.1.3.jar
xmlrpc-common-3.1.3.jar
上面第一个是安装 vmware 的 LDTP。
Ldtp不需要安装 python,也不需要安装 jep,它只需要外部有 CobraWinLDTP.exe,并按照 client-server 的方式工作。ldtp.jar是client,先通过启动进程的方式运行CobraWinLDTP.exe,然后通过xmlrpc协议,把java的命令变成调用CobraWinLDTP.exe的命令,从而可以识别控件、操纵控件
(对,java调用pywinauto时,需要依赖jep这个桥梁来把java的命令变成python端的,jep就是负责传递命令的)
由于 CobraWinLDTP.exe 是 server 端,所以先需要在命令行中启动 CobraWinLDTP.exe。
然后看代码:
package ldtp;
import com.cobra.ldtp.Ldtp;
public class TestLdtp {
public static void main(String[] args) {
Ldtp ldtp = new Ldtp("abc");
String[] wins = ldtp.getWindowList();
for (String win : wins)
{
System.out.println(win);
}
}
}
====打印结果====import com.cobra.ldtp.Ldtp;
public class TestLdtp {
public static void main(String[] args) {
Ldtp ldtp = new Ldtp("abc");
String[] wins = ldtp.getWindowList();
for (String win : wins)
{
System.out.println(win);
}
}
}
paneTaskbar
frmEclipse SDK
frm?????? - ??? - Mozilla Firefox
frmNew Tab
frmWeChat
frmD:EclipseSpaceNewLdtpTestNewlib
frmC:Program Files (x86)VMwareCobraWinLDTP
frmAdministrator: Command Prompt - CobraWinLDTP.exe
frmUntitled - Paint
lst0
paneProgram Manager
frmEclipse SDK
frm?????? - ??? - Mozilla Firefox
frmNew Tab
frmWeChat
frmD:EclipseSpaceNewLdtpTestNewlib
frmC:Program Files (x86)VMwareCobraWinLDTP
frmAdministrator: Command Prompt - CobraWinLDTP.exe
frmUntitled - Paint
lst0
paneProgram Manager
上面代码中的 "abc" 是随便一个名字。这段代码可以打印出所有的 frame。然后你找到你需要的,就可以继续操作了。
下面是打开 notepad 后一些操作的代码
package ldtp;
import com.cobra.ldtp.Ldtp;
public class TestLdtp {
public static void main(String[] args) {
String winStr = "frmUntitled - Notepad"; //Open Notepad first please
Ldtp ldtp = new Ldtp(winStr);
String[] wins = ldtp.getWindowList(); //Print all windows
for (String win : wins)
{
System.out.println(win);
}
ldtp.activateWindow(winStr);
System.out.println("=================");
String[] objects = ldtp.getObjectList(); //Print all objects
for (String ob : objects)
{
System.out.println(ob);
}
// ldtp.click("mnuView"); //This can click the View menu
ldtp.selectMenuItem("View;Status Bar"); //Click View > Status Bar
ldtp.appendText("txtTextEditor", "hello allen");
}
}
import com.cobra.ldtp.Ldtp;
public class TestLdtp {
public static void main(String[] args) {
String winStr = "frmUntitled - Notepad"; //Open Notepad first please
Ldtp ldtp = new Ldtp(winStr);
String[] wins = ldtp.getWindowList(); //Print all windows
for (String win : wins)
{
System.out.println(win);
}
ldtp.activateWindow(winStr);
System.out.println("=================");
String[] objects = ldtp.getObjectList(); //Print all objects
for (String ob : objects)
{
System.out.println(ob);
}
// ldtp.click("mnuView"); //This can click the View menu
ldtp.selectMenuItem("View;Status Bar"); //Click View > Status Bar
ldtp.appendText("txtTextEditor", "hello allen");
}
}
====打印结果====
paneTaskbar
frmEclipse SDK
frmUntitled - Notepad
frm?????? - ??? - Mozilla Firefox
frmNew Tab
frmslc12dkq-16-2012-OBIEEServer - Remote Desktop Connection Manager v2.2
frmD:swUIAVerify2.0_x86
frmNow Playing
frmAdministrator: Command Prompt - CobraWinLDTP.exe
frmD: 0????
pane1
pane2
paneChatContactMenu
lst0
paneProgram Manager
=================
txtTextEditor
uknVertical
btnLineup
btnLinedown
uknHorizontal
btnColumnleft
btnColumnright
ukn2
ukn3
lbl0
lblLn1,Col1
ukn4
mnuSystem
mnuSystem1
btnMinimize
btnMaximize
btnClose
mnuApplication
mnuFile
mnuEdit
mnuFormat
mnuView
mnuHelp
frmEclipse SDK
frmUntitled - Notepad
frm?????? - ??? - Mozilla Firefox
frmNew Tab
frmslc12dkq-16-2012-OBIEEServer - Remote Desktop Connection Manager v2.2
frmD:swUIAVerify2.0_x86
frmNow Playing
frmAdministrator: Command Prompt - CobraWinLDTP.exe
frmD: 0????
pane1
pane2
paneChatContactMenu
lst0
paneProgram Manager
=================
txtTextEditor
uknVertical
btnLineup
btnLinedown
uknHorizontal
btnColumnleft
btnColumnright
ukn2
ukn3
lbl0
lblLn1,Col1
ukn4
mnuSystem
mnuSystem1
btnMinimize
btnMaximize
btnClose
mnuApplication
mnuFile
mnuEdit
mnuFormat
mnuView
mnuHelp