Thursday, July 15, 2010

Run QTP testcase with multiple set of data from JUnit

Last time I show you how to run a QTP script with one set of data from JUnit. Today let see how can we run multiple iteration of QTP script with multiple set of data from JUnit.

Thursday, July 8, 2010

Passing data from JUnit to QTP test case

Last time I show you how to invoke a QTP test case from JUnit. Today I want to show you how to pass dynamic data to our QTP test from JUnit so that we can run the same test case with multiple set of data without the need to modify our QTP script. Because JUnit4QTP built on top of Java QTP Communication Channel so from our QTP script we can call back to our java by using JavaObject.invoke function. In Junit we'll implement a getParameter method that will pass the data to our QTP script.

package junit4qtp;

import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;
import qtpcommandserver.client.QTP;

public class FlexStoreTest_DynamicData extends TestCase
{
Map dataMap = new HashMap();

public FlexStoreTest_DynamicData()
{
dataMap.put("phonePart1", "917");
dataMap.put("phonePart2", "123");
dataMap.put("phonePart3", "4567");
dataMap.put("password", "secretPass");
}

public String getParameter(String name)
{
return dataMap.get(name);
}

public void testHome() throws Exception
{
QTP.runTest(true, "resources/QTP/FlexStoreHomeTest-DynamicData", this);
}
}



From QTP script we calling back into our JUnit to get the data.



Function getParameter(name)
getParameter = JavaObject.invoke("getParameter", Array(name))
End Function

SystemUtil.Run "C:/Program Files/Internet Explorer/IExplore.exe", "http://examples.adobe.com/flex2/inproduct/lcds/flexstore/flexstore.html"

Browser("FlexStore").Sync

Window("hWnd:=" & Browser("FlexStore").Object.hWnd).Activate
Window("hWnd:=" & Browser("FlexStore").Object.hWnd).Maximize
Window("hWnd:=" & Browser("FlexStore").Object.hWnd).Resize 715, 720

Browser("FlexStore").FlexApplication("flexstore").FlexCanvas("Home").FlexTextArea("index:31").Input(getParameter("phonePart1"))
Browser("FlexStore").FlexApplication("flexstore").FlexCanvas("Home").FlexTextArea("index:33").Input(getParameter("phonePart2"))
Browser("FlexStore").FlexApplication("flexstore").FlexCanvas("Home").FlexTextArea("index:35").Input(getParameter("phonePart3"))
Browser("FlexStore").FlexApplication("flexstore").FlexCanvas("Home").FlexTextArea("index:37").Input(getParameter("password"))

if(Browser("FlexStore").FlexApplication("id:=flexstore").FlexCanvas("automationname:=Home").FlexCheckBox("automationname:=Remember my phone number").GetRoProperty("selected") = "True") Then
Browser("FlexStore").FlexApplication("flexstore").FlexCanvas("Home").FlexCheckBox("Remember my phone number").Click
end if

Browser("FlexStore").FlexApplication("flexstore").FlexCanvas("Home").FlexButton("Login").Click
Browser("FlexStore").FlexApplication("flexstore").FlexAlert("Login").FlexButton("OK").Click
Browser("FlexStore").FlexApplication("flexstore").FlexCanvas("Home").FlexButton("Login").Click

if("This feature is not implemented in this sample" <> Browser("FlexStore").FlexApplication("id:=flexstore").FlexAlert("automationname:=Locate").GetRoProperty("text")) then
msgbox("error")
end if

Browser("FlexStore").FlexApplication("flexstore").FlexAlert("Login").FlexButton("OK").Click

Window("hWnd:=" & Browser("FlexStore").Object.hWnd).Close



Run QTP from JUnit

Run QTP from JUnit is very straight forward. We just need to pass the path to the QTP script to the QTP.run method, and indicate whether we want QTP to be visible or not.

For example
QTP.runTest(false, "resources/QTP/FlexStoreHomeTest");

This statement will run QTP test case from "resources/QTP/FlexStoreHomeTest" with QTP not visible.




Below is the screenshot of my QTP test case