Thursday, July 1, 2010

Write QTP test case in pure Java


As you know in my previous post, we can invoke java methods from QTP, also we can invoke VBScript from Java. However, it doesn't looking good when our Java code contains VBScripts, So it's better to write our test cases it in pure Java. Like the example below.



As you can see above my test case doesn't contains VBScript anymore. For more information regarding write QTP test case in pure java please go to http://jqtp.blogspot.com


Monday, June 28, 2010

Real World Example

Below is a QTP script that test Adobe's Flex Application

We invoke above QTP test case from Java



We can move above VBScript statements from QTP into Java

What left over in QTP is a one line call into Java's run method.


Although it's possible to execute QTP VBScript statements in java. I'm recommend you to use JQTP which let you write QTP script in pure java.


Special Syntax for execute VBScript statements in Java

When we want to execute a vbscript statement from Java, we normally pass in the string that contain the vbscript to the QTP.exec function.

For example
QTP.exec("print(\"hello QTP\")");

this statement will print "Hello QTP" to QTP's console.
Notice that we need to escape all double quotes inside
this statement. This will make the vbscript very hard
to read. There's another way we can execute a vbscript
code in java without the need to escape the double quote.
So instead of write the code like above we write this
QTP.exec(/*print("hello QTP")*/);
Notice that we don't need to escape any double quote
in this VB code. Thus our VB code are more readable.


Friday, June 25, 2010

Invoke QTP from Java


Last time we learned how to invoke java method from
QTP by using the "JavaObject.invoke" function from QTP.
Today we'll learn how to invoke QTP from Java. Thus
we can achieve two ways commnunications between
Java and QTP.


By using QTPClient.exec method we can execute any
QTP statements from Java.

QTPClient.exec("Msgbox(\"hello QTP\")");
will display "hello QTP" message in QTP's message box






In this example we saw two way communication between
Java and QTP.

QTPClient.runQtpTest(true, "resources/QTP/Hello", new Object()
{
  public void greeting(String name)throws Exception
  {
    QTPClient.exec("Msgbox(\"hello QTP\")");
  }
});

When we run above code from Java, Java will run the QTP's
"Hello" test. In the "Hello" test we invoke the "greeting"
method in java. In Java's "greeting" method, we execute
a QTP stament to bring up the message box.



Thursday, June 17, 2010

Invoke Java method with String argument

Yesterday I show you how to invoke a java method which take in no arguments. Today let see how we call a java method that take in one String argument.

let modify our java method

public void greeting(String name)
{
    System.out.println("Hello " + name + " !");
}


to call this method we pass in a String to JavaObject.invoke function


JavaObject.invoke "greeting", Array("QTP")



As you can see we can easily debug with eclipse when java method get called by QTP.



Wednesday, June 16, 2010

Invoke Java methods from QTP

Last time we learned how to run QTP from Java. Today I'll show you how to invoke java methods from QTP.



We use the "JavaObject" to make a call back to Java. In this example I use the following statement

JavaObject.invoke "greeting", Array()

to call the "greeting" method in java and pass in empty Array because this method do not take in any parameters.



Last time we learn how to invoke QTP from java by using QTPUtil.callQTPTest method. The QTPUtil.callQTPTest take in an optional third parameter which take in a java object that QTP can invoke it methods.

QTPUtil.callQTPTest(true, "resources/QTP_Scripts/Hello", this);

I pass the Hello object which have the "greeting" method to QTPUtil.callQTPTest. So from QTP we can invoke any methods available in this Java "Hello" object.



When running in debug mode we can put in a break point in the greeting method and see that method invoked by QTP.



From this greeting method we can debug like any normal Java program.



Monday, June 14, 2010

Run QTP from Java

Let start with the simplest QTP script, which have only one statement that will print "Hello World" to the console



I saved this QTP Script as "Hello" script.
Then I run this QTP from Java using QTPUtil library.

QTPUtil.callQTPTest(false, "resources/QTP_Scripts/Hello");




The QTPUtil.callQTPTest method take in two parameters. The first one is the flag indicate whether we want QTP to be visible or not, the second one is the path to the QTP script. The path to QTP script can be absolute or relative. In this example I'm using the relative path.