QTP六脉神剑之调用Java程序
版权声明:原创作品,转载请以链接方式注明出自http://www.51testing.com/?35,否则将追究法律责任。
本文出自songfun的51Testing软件测试博客:http://www.51testing.com/?35
不少网友对于QTP调用Java程序感到束手无策,实际上要调用Java程序是非常容易的。接下来songfun老师传授给大家武林绝技之六脉神剑。在演示这个例子前,请大家先造一个java程序(我以java计算器为例,源码见下),以方便观察调用结果。
第一式:少商剑。特点:剑路雄劲,石破天惊。
打开QTP,在QTP的Expert
View中输入:InvokeApplication "cmd /k cd c:\ &&
java Counter && exit"
运行QTP,看看,打开了吗?
第二式:商阳剑。特点:巧妙灵活,难以捉摸。
在C盘上新建一个bat文件,取名为runjava.bat,在文件里面输入一段文本:cmd /k
"cd c:\ && java Counter && exit",然后保存下来。
打开QTP,在QTP的Expert
View中输入:SystemUtil.Run "C:\runjava.bat"
运行QTP,看看,打开了吗?
第三式:中冲剑。特点:大开大阖,气势雄迈。
打开QTP,在QTP的Expert
View中输入:
Dim oWsh
Set Wsh =
CreateObject("WScript.Shell")
oWsh.Exec
"cmd /k cd c:\ && java Counter && exit"
Set Wsh =
Nothing
运行QTP,看看,打开了吗?
第四式:关冲剑。特点:以拙滞古朴取胜。
在C盘上新建一个qfl文件,取名为runjava.qfl,在文件里面输入一段文本:
CallJava
"Counter"
Sub CallJava(ByVal
strJavaName)
Dim oWsh
Set Wsh = CreateObject("WScript.Shell")
oWsh.Run "cmd /k cd c:\ && java " & strJavaName &
" && exit"
Set Wsh = Nothing
End Sub
打开QTP,在QTP的Expert
View中输入:ExecuteFile "C:\runjava.qfl"
运行QTP,看看,打开了吗?
第五式:少冲剑。特点:轻灵婀娜,迅雷不及掩耳。
安装QTP的Java Add-in,之后在Automation—Record
and Run Settings中出现了Java标签页。根据下图的内容设置即可(runjava.bat文件内容参照六脉神剑第二式)。
javarecord.PNG
第六式:少泽剑。特点:忽来忽去,变化精微。
采用直接调用jar包的形式。要生成jar包,首先要确保在C盘下已经放置了Counter.java源文件,然后通过命令行工具去编译它:
cd \
javac
Counter.java
jar cvf
Counter.jar *.class
执行完命令看看在C盘是否已经生成了Counter.jar文件?
接下来解压Counter.jar文件,在C:\Counter\META-INF目录下可以找到MANIFEST.MF这个文件,用Notepad打开,在第三行空行处加入入口类的代码(蓝色字体部分):
Manifest-Version: 1.0
Created-By: 1.6.0_07 (Sun Microsystems Inc.)
Main-Class:
Counter
保存完以后,回到命令行窗口处,输入命令更新jar包:
jar umf
C:\Counter\META-INF\MANIFEST.MF Counter.jar
写完测试一下:java
-jar Counter.jar
如果计算器可以打开说明前期铺垫工作已经完成,那接下来的就是在QTP的Expert View中输入:SystemUtil.Run
"C:\Counter.jar"
运行QTP,看看,打开了吗?
本文出自songfun的51Testing软件测试博客:http://www.51testing.com/?35
当然实际上要调用Java程序的方法还有很多,这里就不一一介绍了。在下次课,songfun老师将会传授QTP独门秘笈之降龙十八掌给大家。谢谢!再见!
本文出自songfun的51Testing软件测试博客:http://www.51testing.com/?35
附:Counter.java源码。
import
java.awt.*;
import
java.awt.event.*;
import
java.lang.*;
import
javax.swing.*;
public
class Counter extends Frame.
{
//声明三个面板的布局
GridLayout
gl1,gl2,gl3;
Panel
p0,p1,p2,p3;
JTextField
tf1;
TextField
tf2;
Button
b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26;
StringBuffer
str;//显示屏所显示的字符串
double
x,y;//x和y都是运算数
int
z;//Z表示单击了那一个运算符.0表示"+",1表示"-",2表示"*",3表示"/"
static
double m;//记忆的数字
public
Counter()
{
gl1=new
GridLayout(1,4,10,0);//实例化三个面板的布局
gl2=new
GridLayout(4,1,0,15);
gl3=new
GridLayout(4,5,10,15);
tf1=new
JTextField(27);//显示屏
tf1.setHorizontalAlignment(JTextField.RIGHT);
tf1.setEnabled(false);
tf1.setText("0");
tf2=new
TextField(10);//显示记忆的索引值
tf2.setEditable(false);
//实例化所有按钮、设置其前景色并注册**
b0=new
Button("Backspace");
b0.setForeground(Color.red);
b0.addActionListener(new
Bt());
b1=new
Button("CE");
b1.setForeground(Color.red);
b1.addActionListener(new
Bt());
b2=new
Button("C");
b2.setForeground(Color.red);
b2.addActionListener(new
Bt());
b3=new
Button("MC");
b3.setForeground(Color.red);
b3.addActionListener(new
Bt());
b4=new
Button("MR");
b4.setForeground(Color.red);
b4.addActionListener(new
Bt());
b5=new
Button("MS");
b5.setForeground(Color.red);
b5.addActionListener(new
Bt());
b6=new
Button("M+");
b6.setForeground(Color.red);
b6.addActionListener(new
Bt());
b7=new
Button("7");
b7.setForeground(Color.blue);
b7.addActionListener(new
Bt());
b8=new
Button("8");
b8.setForeground(Color.blue);
b8.addActionListener(new
Bt());
b9=new
Button("9");
b9.setForeground(Color.blue);
b9.addActionListener(new
Bt());
b10=new
Button("/");
b10.setForeground(Color.red);
b10.addActionListener(new
Bt());
b11=new
Button("sqrt");
b11.setForeground(Color.blue);
b11.addActionListener(new
Bt());
b12=new
Button("4");
b12.setForeground(Color.blue);
b12.addActionListener(new
Bt());
b13=new
Button("5");
b13.setForeground(Color.blue);
b13.addActionListener(new
Bt());
b14=new
Button("6");
b14.setForeground(Color.blue);