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 a custom keyword. Designing custom keywords is one of the advanced features of OpKey. Users can easily add and use a custom keyword like other OpKey keywords. In this article, it is described in detail how you can create your own Custom Keywords using the 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 Finish.
- Right click on the src folder and select New and then select Package option to create a Package.
- Browse and select the Source folder path, enter the Name, and then click Finish.
- Now you need to add the opkey-pluginbase jar file that is the base file of the OpKey plugins. The location of opkey-pluginbase jar file depends on the installation directory of OpKey Agent. If your OpKey Agent is installed in C drive (Windows 10 based machine) then the location of the opkey-pluginbase may be C:\Program Files (x86)\SSTS\OpKey\OpKey Execution Agent\AgentData\Plugins\libs\PluginBase
- Select the Project Right click on it and then click on Build Path and then select Configure Build Path.
- Select Java Build Path option and then select Libraries tab and then click on the Add External JARS link.
- Browse and select the opkey-pluginbase jar file and click Open.
- opkey-pluginbase jar file has been successfully added as an external jar file.
- Click Apply and then close the window.
- Similarly, you can add other external jar files required for the Custom keyword.
- If you are creating Web Plugin based Custom Keyword then you need to add the Web Plugin jar file by following the above-described method of adding external jars of opkey-pluginbase.
- Default location of the installed OpKey plugins (like Web) is C:\Program Files (x86)\SSTS\OpKey\OpKey Execution Agent\AgentData\Plugins\Web\Web.jar
- Let us now create a new Java Class.
- Right click on the Package name, select New, and then click on the Class option.
- Enter the name of the Class and click Finish.
- Now you are ready to create your CustomeKeyword.
- Write the Java code of the action that you want to perform. (For example, here we have written Java code to add two numbers).
- Let us now export the Jar file of the written Java code for Custom Keyword.
- Right click on your Class and select the Export option.
- Click on the Browse button to select the export destination. Then click on the Finish button.
- Now come to the OpKey Web.
- Select Keyword Management under Tools menu to open the Keyword Management Wizard.
- Manage Libraries tab of Keyword Management window opens.
- Upload the specific custom keyword jar using the browse button. Then, click on the 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 the same as provided on your jar file function name.
- Output type : Should be the same as your function.
- Comment : Enter comments as per the keyword requirement.
- Input Argument Details : Argument should be in the same order as provided in function.
- Associate Library : Select Library (jar file) from the list.
Click on Save button to create the custom keyword.
- Custom Keyword has successfully been created and you can now use it in OpKey Web.
Coded 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