# How to Add Custom Keyword in Opkey for Selenium Web driver Plug-in

March 3, 2017  
Iffat Ara Khanam

As we all know that Opkey is user friendly (easy to use) and provides a unique and rich set of more than 700 keywords to perform test automation. But at times we do come across a scenario where the existing keyword set is not sufficient and need arises to create our own keyword. Users can perform such behaviour using custom keyword. Designing custom keywords is one of the high-level features of Opkey. User can easily add and use custom keyword like other Opkey keywords. Here I am giving an example of creating custom keyword for Opkey Selenium Webdriver Plugin.  
1. **_C_ ustom-keyword without WebDriver’s object**  
2. **_C_ ustom-keyword using WebDriver’s object**

**Steps to create custom keywords:-**

Inside Eclipse select the menu item **File > New > Java Project** to open the **New Java Project** wizard.

- Type "OpkeyCustomeKeywordProject" in the **Project name** field. Then click on Next and then Finish.
- Create new folder in the Package Explorer "src" folder.
-  Type "WebdriverFiles" in **Folder name**. Then click on **Finish** button.
- Get the file "opkey-pluginbase _-x.y_.jar", "webdriverplugin _-a.b_.jar", "selenium-p.q.r" placed in the plugin folder, whose default location is:

**NOTE:** ‘x.y’ or  ‘a.b’ or ‘p.q.r’ all represent respective files version. E.g. webdriverplugin-1.03.jar

- Window -x86 (Xp/7 /8 and above)

_C:Program FilesCresTechOpkeyExecution AgentAgentDataPluginsSelenium WebDriver_

- Window -x 64 (7 /8 and above)

_C:Program Files (x86)CresTechOpkeyExecution AgentAgentDataPluginsSelenium WebDriver_

## **For BPT/Team ( version 5.07 and below) and Personal ( version  2.02 and below)**

- Window 7
- BPT/Team :  _C:ProgramDataAgentDataPluginsSelenium WebDriver_
- Personal :  _C:ProgramDataCresTechOpkeyAgentAgentDataPluginsSelenium WebDriver_
- Window Xp
- BPT/Team :  _C:Documents and SettingsAll UsersApplication DataAgentDataPluginsSelenium WebDriver_
- Personal :  _C:Documents and SettingsAll UsersApplication DataCresTechOpkeyAgentAgentDataPluginsSelenium WebDriver_

- Copy **"opkey-pluginbase _-x.y_.jar", "webdriverplugin _-a.b_.jar"** and paste these files to the created **"WebdriverFiles"** folder above.
- Explore **"WebdriverFiles"** and select **"opkey-pluginbase _-x.y_.jar", "webdriverplugin _-a.b_.jar", "selenium-p.q.r"** files. Then add these files in build Path by right click on these file and select **Build Path >Add to Build Path.**
- Select the menu item **File > New > Class** to open New Java Class wizard.

Type class name "CustomeKeyword" in name **.** Click on **Finish** button. **Now you are ready to create CustomeKeyword.**

**Import** com.crestech.opkey.plugin.KeywordLibrary;

**import** com.crestech.opkey.plugin.ResultCodes;

**import** com.crestech.opkey.plugin.communication.contracts.functionresult.FunctionResult;

**import** com.crestech.opkey.plugin.communication.contracts.functionresult.Result;

**public class CustomeKeyword implements KeywordLibrary**

{

**public FunctionResult compareTwoString(String str1,String str2)**

{

**if** (str1.equalsIgnoreCase(str2))

{//Pass

**return** Result. _PASS_().setOutput( **true**).make();

} else

{

//Fail **return** Result. _FAIL_(ResultCodes.ERROR_ARGUMENT_DATA_INVALID).setMessage("Both strings are not equal").make();

}

}

str1,str2 : Input Parameter.

**/*Note:****

_* In FAIL case: mandatory_
 
                      -setResultCode(ResultCodes.ERROR_VERIFICATION_FAILLED.Code())
 
                      -setMessage("Both strings are not equal")
 
Rest all are optional in both PASS or FAIL case _/*

**Export CustomeKeyword Class** -Right click on CustomeKeyword class under  Package Explorer and select the Export option to open Export wizard  
-Explore Java folder and select JAR file then click on Next button to open JAR Export wizard.

Click on Browse button to select the export destination. Then click on **Next->Next->Finish button**. Now you are ready to add custom Keyword in Opkey.

- Under Tool menu select  Keyword Management Wizard to Open Keyword Management Wizard

- Upload the specific custom keyword jar using the browse button. Then click on the upload button to upload the file in Opkey.

Click on Add Custom Keyword to create Custom keyword.

Keyword name : Give the name of your keyword.

Associated name : Name should be same as provided on your jar file function name.

Description : Description of your keyword.

Plug-in : Select the Selenium Webdriver plugin.

Input argument details : Argument should be in same order as provided in function.

Output type : Should be same as your function.

Library : Select Library (jar file) from drop down list.

Then click on **Save** button to save the custom keyword. Custom Keyword is added in Opkey. Now we can use custom keyword.

**Use of custom Keyword in Opkey**  
Navigate to **_Test Case module_** and add new test step.

- Click on **keyword cell**.
- Click on **Selenium Webdriver tab**. From drop-down list of keyword select custom keyword.

- On the right hand side provide the input argument value.
- Navigate to Suite and add Test Case.
- Run this suite.

**Case 1:- Both string are equal.**  
In1 = abc  
In2=abc

Code: _return Result.PASS().setOutput(_ **_true_** _).make();_

_setOutput(true):_ show on result Output column.

**Case 2:- When input argument strings are not equal.**  
In1 = abc  
In2 = xzcs

Code:

**//return** Result. _FAIL_().setOutput( **false**).setMessage("Both strings are not equal").setResultCode(ResultCodes. _ERROR_VERIFICATION_FAILLED_.Code()).make();

setOutput(false):- show on result output column cwindow.

setResultCode(ResultCodes. _ERROR_VERIFICATION_FAILLED_.Code()):- show on result message column.

setMessage("String are not equal"):- Show on result Debug Information column.

_As seen this Opkey feature is quite useful and provides flexibility to its user to create their own keywords as the need arises. It’s easy and friendly GUI provides a hassle free platform to add and use the created custom keyword._

**_Example custom-keyword using WebDriver’s object:_**

**_Classical Webdriver Approach_**  
**public class Custom {**

**static WebDriver _driver_;**

**public static void main(String args){**

_driver_ = **new** FirefoxDriver();

_driver_.get("https://www.google.com");

}

**public static String customTypeText(String strLocator, String text){**

WebElement foundElement = _driver_.findElement(By. _name_(strLocator));
  foundElement.sendKeys(text);

**return** "Successfully typed Text ";
}

}**

**_Opkey Custom Keyword Approach_**  
**public class Custom implements KeywordLibrary {**

**public FunctionResult customTypeText(String strLocator, String text) throws ToolNotSetException{**

WebDriver _driver_ = (WebDriver) Context.session.getTool();
                    WebElement foundElement = _driver_.findElement(By. _name_(strLocator));
                    foundElement.sendKeys(text);

**return** Result._PASS_().setMessage("Successfully typed Text").make();

}

}

### Iffat Ara Khanam

#### Technical Content Lead

Iffat is the content lead at Opkey. She has expertise in writing technical content focused around ERP testing, Cloud apps, automation and other IT related topics. She has rich experience in writing content for marketing collateral like whitepapers, case studies, newsletters etc. which helps in funnel creation for sales.
