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

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.

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

C:Program FilesCresTechOpkeyExecution AgentAgentDataPluginsSelenium WebDriver

C:Program Files (x86)CresTechOpkeyExecution AgentAgentDataPluginsSelenium WebDriver

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

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.

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.

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.