How do I setup a Silk4J project that will utilise some of the SilkTest constructs?  The project will have an object repository, user-defined basestate and application state, a TestCaseEnter and a TestCaseExit method.

Application states, TestCaseExit and TestCaseEnter are all 4Test constructs used in the 4Test language for scripting in the SilkTest IDE.  Silk4J, using the java scripting language means you are required to follow the construct of the Java programming language.  For example, applications states are not by default part of the JUnit test types in Silk4J, however, we are still able to create such a construct as used in SilkTest in Silk4J.  This article will cover steps to create a BaseState method, a test enter and test exit method (similar to the TestCaseEnter and TestCaseExit functions in SilkTest.  We will also cover steps to create a repository for the application's objects.

To create a new Silk4J project, follow the steps below:
1) Open Silk4J, go to File -> New -> Other
2) Expand Silk4J contents and select Silk4J Project and click Next.
3) Specify a project name for the project in the New Silk4J Project dialog.
4) Leave the default settings and click Finish.

We should now have a branch by the name we gave our project in the Package Explorer.
It is recommended at this stage to create a new Java package for the project.  A Java package is a mechanism for organizing Java classes into namespaces and can be used to organize classes belonging to the same category or providing similar functionality.

5) To create a Java package for the project, right click on the src folder for the project, select New -> Package.
6) In the New Java Package dialog, the source folder should be of the name project_name/src.  Specify a name for the package.
7) Now within the package, we can create our Java classes.  Right click on the package, and select New -> file.  In the New File dialog, give the file a name with a *.java extension, for example iebasestate.java.  This should open the file.  This file will contain some information about the application we are going to test as shown in the example below.

package

com.mf.iebasestate;

import

com.borland.silktest.jtf.Desktop;

public

class iebasestate extends Desktop {

public Desktop desktop = new Desktop();

public String executable = "C:/Program Files/Internet Explorer/iexplore.exe";

public String url = "http://www.google.co.uk/";

public String locator = "//BrowserApplication";

// generic sleep statement

// sleep

public void sleep (int i) {

try {

Thread.sleep(i*1000);

}

catch(Exception e) {

}

}

}

Notice the above class inherits from the Desktop class using the extends keyword.  In Java, a class declaration can use the extends keyword on another class, for example class B extends A{...}.  When a class B extends class A, B automatically has all variables and methods defined in class A (think of it as an internal copying mechanism).

Using the steps detailed above in [7], we create a second file which will serve as our object repository.  We can call this file myObjs.java, which will contain the class myObjs.  This class will inherit the Desktop class from the class iebasestate.  The object repository will contain reference to all the objects which our tests will required.  An example repository is shown below:

package

com.mf.iebasestate;

import

com.borland.silktest.jtf.xbrowser.BrowserApplication;

import

com.borland.silktest.jtf.xbrowser.BrowserWindow;

import

com.borland.silktest.jtf.xbrowser.DomButton;

import

com.borland.silktest.jtf.xbrowser.DomCheckBox;

import

com.borland.silktest.jtf.xbrowser.DomLink;

import

com.borland.silktest.jtf.xbrowser.DomTextField;

public

class myObjs extends iebasestate {

// define the variables

public BrowserApplication wBrowserApplication;

// initialize variables

public BrowserApplication wBrowserApplication() {

return desktop.find("//BrowserApplication"); // return the XPath locator string of the object

}

public BrowserWindow wBrowserWindow() {

return wBrowserApplication().find("//BrowserWindow"); // return the XPath locator string of the object

}

public DomTextField txtSearchString() {

return wBrowserWindow().find("//INPUT[@name='q']"); // return the XPath locator string of the object

}

public DomButton bGoogleSearch() {

return wBrowserWindow().find("//INPUT[@name='btng']"); // return the XPath locator string of the object

}

public DomLink aNewUser() {

return wBrowserWindow().find("//A[@textContents='Micro Focus SupportLine - New User Registration']"); // return the XPath locator string of the object

}

public DomTextField txtFirstName() {

return wBrowserWindow().find("//INPUT[@name='First_Name']"); // return the XPath locator string of the object

}

public DomTextField txtLastName() {

return wBrowserWindow().find("//INPUT[@name='Last_Name']"); // return the XPath locator string of the object

}

public DomTextField txtCompanyName() {

return wBrowserWindow().find("//INPUT[@name='Company_Name']"); // return the XPath locator string of the object

}

public DomTextField txtSerial() {

return wBrowserWindow().find("//INPUT[@name='Serial_Number']"); // return the XPath locator string of the object

}

public DomCheckBox chkNoSerial() {

return wBrowserWindow().find("//INPUT[@name='Serial_Number_Not_Known']"); // return the XPath locator string of the object

}

}

The two classes iebasestate and myObjs now contain relevant information about the application we are going to test and the objects we need to reference.  We are now ready to create our basestate method and tests.  Firstly, we need to create a new java file using the steps in [7].  Name the file ieTest.java.  This file will contain our user-defined basestate method, our test enter and exit methods aswell as the testcase method.  Again we will be using the extends keyword to inherit the objects we will be referencing in our testcase method from the myObjs class.  By inheriting from the myObjs class, we also inherit from the iebasestate class because there is an existing inheritance between iebasestate class and myObjs class.  We now create our basestate method as follows:

public

void _Defaultbasestate() {

// creates a new browser basestate

BrowserBaseState baseState =

new BrowserBaseState(IE8.executable, IE8.url, TechDomain.WIN32, TechDomain.XBROWSER);

// attach the necessary techdomains to the application under test

// xBrowser techdomain is also attached by default when using Silk4J, but we can define as below to attach two techdomains

desktop.attach(IE8.executable, TechDomain.WIN32, TechDomain.XBROWSER);

// execute the basestate

myObj.wBrowserApplication = baseState.execute(IE8);

}

Using the JUnit notation @Before, we can specify the actions which need to be run first (similar in a way to the TestCaseEnter function in SilkTest, @After for steps that need to be run last, TestCaseExit).  The _Defaultbasestate method above always launches the application and navigates to a certain url.  However, if Internet Explorer is already open, we do not want to launch another browser.  In this instance, we can now create an application state method using the @Before statement that checks first whether an Internet Explorer process is already running, the result will determine whether we call the above _Defaultbasestate method to launch the application and perform any further steps required.  It is recommended to use a try/catch block (similar to a do-except in SilkTest) to perform this check in order allow the test to continue to completion should an exception occur during the verification of the existence of a browser.

Below is an example JUnit test which utilises all the information discussed in this article.  It contains a user-defined basestate method which is called from the result of the myAppState method where we can specify steps to drive the application to a desired state.  We use the JUnit notations @Before to specify tests that must run first, @Test to specify our testcases, and @After to specify tests that must run last.  We also covered the creation of an object repository and how we can link these to our tests using the extends keyword.

package

com.mf.iebasestate;

import

org.junit.After;

import

org.junit.Before;

import

org.junit.Test;

import

com.borland.silktest.jtf.BrowserBaseState;

import

com.borland.silktest.jtf.common.TechDomain;

public

class ieTest extends myObjs{

// create an instance of the iebasestate class

private iebasestate IE8 = new iebasestate();

private myObjs myObj = new myObjs();

// this is our basestate method

public void _Defaultbasestate() {

// creates a new browser basestate

BrowserBaseState baseState =

new BrowserBaseState(IE8.executable, IE8.url, TechDomain.WIN32, TechDomain.XBROWSER);

// attach the necessary techdomains to the application under test

// xBrowser techdomain is also attached by default when using Silk4J, but we can define as below to attach two techdomains

desktop.attach(IE8.executable, TechDomain.WIN32, TechDomain.XBROWSER);

// execute the basestate

myObj.wBrowserApplication = baseState.execute(IE8);

}

// application state

public void myAppState() {

try {

// if IE browser already exists, then navigate to url

if(myObj.wBrowserApplication().exists()) {

// do not launch IE

// navigate to url

myObj.wBrowserWindow().navigate(IE8.url);

}

}

catch (Exception e) {

// if IE browser does not exist

_Defaultbasestate();

// run the defaultbasestate method to start the application

}

// further steps can be included below

myObj.wBrowserApplication().setActive();

myObj.wBrowserApplication().maximize();

}

// using the junit notation @Before, we can specify the actions which need to be run first

@Before

public void TestEnter() {

// run myAppState

myAppState();

}

// the test

@Test

public void testcase() {

myObj.txtSearchString().setText("Micro Focus Support Line");

sleep(1);

myObj.bGoogleSearch().click();

sleep(1);

myObj.aNewUser().click();

sleep(1);

myObj.txtFirstName().setText("Technical");

myObj.txtLastName().setText("Support");

myObj.txtCompanyName().setText("Micro Focus");

myObj.txtSerial().setText("123456789");

myObj.chkNoSerial().click();

sleep(2);

}

// steps to run after the test has run

// this normally includes application cleanup, for example logout of the application

@After

public void TestExit() {

// in this case, our cleanup will be to remove the text entered in the test

myObj.txtFirstName().setText("");

myObj.txtLastName().setText("");

myObj.txtCompanyName().setText("");

myObj.txtSerial().setText("");

if(myObj.chkNoSerial().getState() == 1) {

myObj.chkNoSerial().click();

}

myAppState();

myObj.wBrowserApplication().close();

// detach the attached techdomains to the application under test

desktop.detachAll();

}

}

Incident #00251637