OpKey Blog

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

Radhe Shyam Prajapati 03.03.2017

As we all know that OpKey is user friendly (easy to use) and provides a unique and rich set of more than 400 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 behavior 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. 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.

Pic1

  • 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 as below

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

For BPT/Team ( version 5.08 and above ) and Personal (version 2.03 and above )

    • 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

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.

pic3 Type class name “CustomeKeyword” in name. Click on Finish button . Now you are ready to create CustomeKeyword. pic4 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 pic7 -Explore Java folder and select JAR file then click on Next button to open JAR Export wizard. pic19 -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

pic16

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

pic17 Click on Add Custom Keyword to create Custom keyword.

pic18

 Keyword name                  : Give the name of your keyword.

Associated name               : Name should be same as provided on you 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

pic15

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

Inp1 = abc

Inp2=abc

pic13

Code: return Result.PASS().setOutput(true).make();

setOutput(true) :-  show on result Output column.

 Case 2:- When input argument strings are not equal.

Inp1 = abc

Inp2 = xzcs

pic15

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("http://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();

}

}

Recent Posts

Free Trial

This site use Cookies to deliver our Service. By using this site you acknowledge that you have read and accepted Our Privacy and Cookie Policy. Your interest in OpKey or use of its free trial or purchase of a subscription package is subject to these Terms and Policies