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



4 comments:

stack said...

You need to provide information for downloading and installing this.

BPT4J said...

I'm planning to make this software available early next year, when the code became stable and successfully using by my company. Also there are many things need to be done because I'm building a framework around this Junit4QTP project.

BPT4J said...

I'll regularly update my progress on this blog. I'll make the software available when it became mature and usefull.

Grace said...

Hi BPT4J,
Let me first applaud you for doing this..i mean,sharing with us about the possibility of stringing together Java and QTP in such a way.I wanted to do this and didnt know where to start..I even thought it was impossible..but thanks to you,i know it isn't.
Will be visiting your blog regularly.
Thanks again!