1. Home
  2. OpKey Help
  3. 28. Working with Custom Keywords in OpKey
  4. 28.1 How to create Custom keywords in OpKey?

28.1 How to create Custom keywords in OpKey?

Overview:

As we all know that OpKey is now a well know test automation tool and provides a unique and rich set of more than 700 keywords. 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 behavior using custom keyword. Designing custom keywords is one of the advanced features of OpKey. User can easily add and use custom keyword like other OpKey keywords. In this article, it is described in details how you can create your own Custom Keywords using OpKey Selenium-JS Plugin.

1. Custom-keyword without WebDriver’s object 2. Custom-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. Click on Next and then click on 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 as below

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

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

C:Program FilesCresTechOpKeyExecution AgentAgentDataPluginsSelenium WebDriver

    • Window -x64 (7 /8 and above)

C:Program Files (x86)CresTechOpKeyExecution AgentAgentDataPluginsSelenium WebDriver

Location of Selenium-JS Plugin in the system:

  • Windows 10: C:\\Program Files (x86)\\SSTS\\OpKey\\OpKey Execution Agent\\AgentData\\Plugins\\Selenium-JS
  • Windows 7: C:\\Program Files (x86)\\SSTS\\OpKey\\OpKey Execution Agent\\AgentData\\Plugins\\Selenium-JS

 

  • 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 nameClick on Finish button. Now you are ready to create CustomeKeyword.

mport 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.

  • Select Keyword Management under Tools menu to open Keyword Management Wizard.
  • Manage Libraries tab of Keyword Management window opens.

  • Upload the specific custom keyword jar using browse button. Then, click on Open button to upload the file in OpKey.

  • Navigate to the Custom Keyword tab & click on Add new keyword button.
  • Add new Custom Keyword page opens. Fill all required fields & click on Save.
  • You can add Input Arguments as per the custom keyword requirement.

 

  • Keyword name                   : Give the name of your keyword.
  • Plug-in                                : Select the Selenium-JS plugin.
  • Associated Method            : Name should be same as provided on you jar file function name.
  • Output type                        : Should be same as your function.
  • Comment                           : Enter comments as per the keyword requirement.
  • Input Argument Details      : Argument should be in same order as provided in function.
  • Associate Library               : Select Library (jar file) from the list.

Then click on Save button to create the custom keyword. Custom Keyword is added in OpKey & you can use the created custom keyword.

noteCoded Function Library is a better alternative for creating Custom Keywords in OpKey. Here, you can write your Custom Keyword specific code in Java, compile it to fix code errors, associate required libraries, import pre-built libraries and execute it. Furthermore, this code can directly be called and used in a Test Case or a Function Library and get Executed. You can refer: Coded Function Library

Was this article helpful to you? Yes No